]> gitweb @ CieloNegro.org - Rakka.git/blob - js/systemConfig.js
Rakka.getSystemConfig()
[Rakka.git] / js / systemConfig.js
1 (function () {
2
3     var identityDecoder = function (src) {
4         return src;
5     };
6
7     var mapDecoder = function (src) {
8         var map   = {};
9         var lines = src.split(/\n/);
10         
11         $.each(lines, function () {
12             var m = this.match(/^(\S+)\s+(\S+)$/);
13
14             map[ m[1] ] = m[2];
15         });
16
17         return map;
18     };
19
20     var boolDecoder = function (src) {
21         return src == "*";
22     };
23
24     var decoder_of = {
25         siteName   : identityDecoder,
26         baseURI    : identityDecoder,
27         defaultPage: identityDecoder,
28         styleSheet : identityDecoder,
29         languages  : mapDecoder,
30         globalLock : boolDecoder
31     };
32
33     Rakka.getSystemConfig = function () {
34         var conf = {};
35         
36         $.ajax({
37             type   : "GET",
38             url    : Rakka.baseURI + "systemConfig",
39             async  : false,
40             success: function (xml) {
41                 $(xml).find("value").each(function () {
42                     var path    = this.getAttribute("path");
43                     var decoder = decoder_of[path];
44
45                     if (decoder == null) {
46                         throw new Error("unknown config path: " + path);
47                     }
48                     else {
49                         conf[path] = decoder($(this).text());
50                     }
51                 });
52             },
53             error  : function (req) {
54                 throw new Error(req.status + " " + req.statusText);
55             }
56         });
57
58         return conf;
59     };
60
61 })();