1 | define(["require", "exports", "./dom/getWindow"], function (require, exports, getWindow_1) {
|
2 | "use strict";
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var GLOBAL_SETTINGS_PROP_NAME = '__globalSettings__';
|
13 | var CALLBACK_STATE_PROP_NAME = '__callbacks__';
|
14 | var _counter = 0;
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var GlobalSettings = (function () {
|
24 | function GlobalSettings() {
|
25 | }
|
26 | GlobalSettings.getValue = function (key, defaultValue) {
|
27 | var globalSettings = _getGlobalSettings();
|
28 | if (globalSettings[key] === undefined) {
|
29 | globalSettings[key] = typeof defaultValue === 'function' ? defaultValue() : defaultValue;
|
30 | }
|
31 | return globalSettings[key];
|
32 | };
|
33 | GlobalSettings.setValue = function (key, value) {
|
34 | var globalSettings = _getGlobalSettings();
|
35 | var callbacks = globalSettings[CALLBACK_STATE_PROP_NAME];
|
36 | var oldValue = globalSettings[key];
|
37 | if (value !== oldValue) {
|
38 | globalSettings[key] = value;
|
39 | var changeDescription = {
|
40 | oldValue: oldValue,
|
41 | value: value,
|
42 | key: key,
|
43 | };
|
44 | for (var id in callbacks) {
|
45 | if (callbacks.hasOwnProperty(id)) {
|
46 | callbacks[id](changeDescription);
|
47 | }
|
48 | }
|
49 | }
|
50 | return value;
|
51 | };
|
52 | GlobalSettings.addChangeListener = function (cb) {
|
53 |
|
54 |
|
55 | var id = cb.__id__;
|
56 | var callbacks = _getCallbacks();
|
57 | if (!id) {
|
58 | id = cb.__id__ = String(_counter++);
|
59 | }
|
60 | callbacks[id] = cb;
|
61 | };
|
62 | GlobalSettings.removeChangeListener = function (cb) {
|
63 | var callbacks = _getCallbacks();
|
64 | delete callbacks[cb.__id__];
|
65 | };
|
66 | return GlobalSettings;
|
67 | }());
|
68 | exports.GlobalSettings = GlobalSettings;
|
69 |
|
70 | function _getGlobalSettings() {
|
71 | var _a;
|
72 | var win = getWindow_1.getWindow();
|
73 |
|
74 | var globalObj = win || {};
|
75 | if (!globalObj[GLOBAL_SETTINGS_PROP_NAME]) {
|
76 | globalObj[GLOBAL_SETTINGS_PROP_NAME] = (_a = {},
|
77 | _a[CALLBACK_STATE_PROP_NAME] = {},
|
78 | _a);
|
79 | }
|
80 | return globalObj[GLOBAL_SETTINGS_PROP_NAME];
|
81 | }
|
82 | function _getCallbacks() {
|
83 | var globalSettings = _getGlobalSettings();
|
84 | return globalSettings[CALLBACK_STATE_PROP_NAME];
|
85 | }
|
86 | });
|
87 |
|
\ | No newline at end of file |