-
Notifications
You must be signed in to change notification settings - Fork 2
/
brum-global-variable.js
116 lines (114 loc) · 3.41 KB
/
brum-global-variable.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import '@polymer/polymer/polymer-legacy.js';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
/**
* `brum-global-variable`
* A component to easily define, edit and subscribe to global variables
*
* @customElement
* @polymer
* @demo demo/index.html
*/
// Defining global variable object
var brumGlobalVariable = brumGlobalVariable || {};
/**
* WARNING DO NOT STORE ANY SENSITIVE DATA HERE
* All data CAN be editted within any browser console
*/
Polymer({
is: 'brum-global-variable',
properties: {
/**
* key that is connected to value
*/
key: String,
/**
* Value bound to key property
*/
value: {
type: Object,
notify: true,
observer: '_valueChanged'
},
/**
* add readonly attribute to disabled editting, like so:
* <brum-global-variable key="foo" value="{{bar}}" readonly></brum-global-variable>
* This DOES NOT make the value readonly, but makes sure THIS element cant edit the value
* So if another component has <brum-global-variable key="foo" value="{{bar}}"></brum-global-variable>
* that component CAN edit the value
*/
readonly: {
type: Boolean,
value: false,
}
},
/**
* Once component is attached, check if key is present
* check if key exists: yes --> reflect to component. no --> build global variable
* Set observers
*/
attached: function () {
if (this.key) {
if (brumGlobalVariable[this.key]) {
if (this.value != brumGlobalVariable[this.key].value) {
this.value = brumGlobalVariable[this.key].value;
}
} else {
brumGlobalVariable[this.key] = {
value: this.value,
observers: [this],
isChanging: false,
};
}
this._setObserver(this.key);
} else {
throw new Error('Invalid Argument: The `key` property must be defined.');
}
},
/**
* Remove observers when component is detached from DOM
*/
detached: function () {
var index = brumGlobalVariable[this.key].observers.indexOf(this);
if (index !== -1) {
brumGlobalVariable[this.key].observers.splice(index, 1);
}
},
/**
* Set current element as observer.
*/
_setObserver: function (key) {
var index = brumGlobalVariable[key].observers.indexOf(this);
if (index === -1) {
brumGlobalVariable[key].observers.push(this);
}
},
/**
* Check if not readonly and if value is actually a new value
* Check if value doesnt already exist in global variable, and if variable isn't currently in changing state
* Check if key exists
*/
_valueChanged: function (value, oldValue) {
if (!this.readonly && value != oldValue &&
brumGlobalVariable[this.key] &&
value != brumGlobalVariable[this.key].value &&
!brumGlobalVariable[this.key].isChanging) {
this._setValue(this.key, value);
}
},
/**
* Set variable to changing state (so it cant be overwritten in this process)
* Overwrite old value with new value
* Notify all observers (components)
* Change changing state to false
*/
_setValue: function (key, value) {
brumGlobalVariable[key].isChanging = true;
brumGlobalVariable[key].value = value;
for (var i in brumGlobalVariable[key].observers) {
if (brumGlobalVariable[key].observers[i] != this) {
brumGlobalVariable[key].observers[i].set('value', value);
}
}
brumGlobalVariable[key].isChanging = false;
}
});