forked from xeolabs/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
95 lines (86 loc) · 2.39 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Backend that manages configurations.
*
* @class SceneJS_configsModule
* @private
*/
var SceneJS_configsModule = new (function () {
this.configs = {};
var subs = {};
/**
* Set a config
* @param path
* @param data
*/
this.setConfigs = function (path, data) {
// Update configs
if (!path) {
this.configs = data;
} else {
var parts = path.split(".");
var cfg = this.configs;
var subCfg;
var name;
for (var i = 0; i < parts.length - 1; i++) {
name = parts[i];
subCfg = cfg[name];
if (!subCfg) {
subCfg = cfg[name] = {};
}
cfg = subCfg;
}
cfg[parts.length - 1] = data;
}
// Notify subscribers
var list = subs[path || "_all"];
if (list) {
for (var i = 0, len = list.length; i < len; i++) {
list[i](cfg);
}
}
SceneJS.publish("configs", this.configs);
};
/**
* Get a config
* @param path
* @return {*}
*/
this.getConfigs = function (path) {
if (!path) {
return this.configs;
} else {
var cfg = this.configs;
var parts = path.split(".");
for (var i = 0; cfg && i < parts.length; i++) {
cfg = cfg[parts[i]];
}
return (cfg != undefined) ? cfg : {};
}
};
/**
* Subscribe to updates to a config
* @param path
* @param ok
*/
this.on = function (path, ok) {
path = path || "_all";
(subs[path] || (subs[path] = [])).push(ok);
ok(this.getConfigs(path));
};
})();
/** Sets configurations.
*/
SceneJS.configure = SceneJS.setConfigs = SceneJS.setDebugConfigs = function () {
if (arguments.length == 1) {
SceneJS_configsModule.setConfigs(null, arguments[0]);
} else if (arguments.length == 2) {
SceneJS_configsModule.setConfigs(arguments[0], arguments[1]);
} else {
throw SceneJS_error.fatalError("Illegal arguments given to SceneJS.setDebugs - should be either ({String}:name, {Object}:cfg) or ({Object}:cfg)");
}
};
/** Gets configurations
*/
SceneJS.getConfigs = SceneJS.getDebugConfigs = function (path) {
return SceneJS_configsModule.getConfigs(path);
};