-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathoptions.js
More file actions
105 lines (97 loc) · 3.58 KB
/
Copy pathoptions.js
File metadata and controls
105 lines (97 loc) · 3.58 KB
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
95
96
97
98
99
100
101
102
103
104
105
(function (ns) {
/**
* Class to handle options. Currently only used for global options, could
* be extended to work hierarchically to configure other elements.
* @constructor
*/
var Options = function () {
this._options = {};
this._listeners = { "*": [] };
};
Options.prototype = {
register: function (key, defaultValue) {
if (this._options.hasOwnProperty(key))
throw new Error("Option already registered '" + key + "'");
this._options[key] = {
currentValue: defaultValue,
defaultValue: defaultValue
};
},
resetValue: function (key) {
if (!this._options.hasOwnProperty(key))
throw new Error("Invalid configuration key '" + key + "'");
this._options[key].currentValue = this._options[key].defaultValue;
this.notifyObservers(key, this._options[key].currentValue);
},
setValue: function (key, value) {
if (!this._options.hasOwnProperty(key))
throw new Error("Invalid configuration key '" + key + "'");
this._options[key].currentValue = value;
this.notifyObservers(key, value);
},
getValue: function (key) {
if (!this._options.hasOwnProperty(key)) {
throw new Error("Invalid configuration key '" + key + "'");
}
return this._options[key].currentValue;
},
getKeys: function () {
return Object.keys(this._options);
},
notifyObservers: function (key, value) {
// Notify specific observers
if(this._listeners.hasOwnProperty(key)) {
this._listeners[key].forEach(function(l) {
l(key, value);
});
}
// Notify generic observers
this._listeners["*"].forEach(function(l) {
l(key, value);
});
},
addObserver: function (key, observer) {
if(typeof key == 'function') {
observer = key;
key = "*"
}
if(!this._options.hasOwnProperty(key) && key !== "*") {
throw new Error("Can't register to unknown option '" + key + "'");
}
if(!this._listeners.hasOwnProperty(key)) {
this._listeners[key] = [];
}
this._listeners[key].push(observer);
},
removeObserver: function (observer) {
for(var filter in this._listeners) {
var listeners = this._listeners[filter];
var idx = listeners.indexOf(observer);
if (idx != -1)
listeners.splice(idx, 1);
}
}
};
var GlobalOptions = new Options();
GlobalOptions.setOptionsFromQuery = function () {
var p = window.location.search.substr(1).split('&');
p.forEach(function (e) {
var keyVal = e.split('=');
try {
var key = keyVal[0].toLowerCase();
if (key.indexOf("xml3d-") === 0) {
var value = decodeURIComponent(keyVal[1]);
try {
value = JSON.parse(value);
} catch (e) {
// Do nothing
}
XML3D.options.setValue(key.substr(6), value);
}
} catch (e) {
XML3D.debug && XML3D.debug.logError(e);
}
});
};
ns.exports = GlobalOptions;
}(module));