-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathglobal.js
385 lines (336 loc) · 9.33 KB
/
global.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
var util = require('util'),
event = require('event');
/**
* @ignore
*/
function getAccessors(obj) {
if (!obj.js_accessors_) {
obj.js_accessors_ = {};
}
return obj.js_accessors_;
}
/**
* @ignore
*/
function getBindings(obj) {
if (!obj.js_bindings_) {
obj.js_bindings_ = {};
}
return obj.js_bindings_;
}
/**
* @ignore
*/
function addAccessor(obj, key, target, targetKey, noNotify) {
getAccessors(obj)[key] = {
key: targetKey,
target: target
};
if (!noNotify) {
obj.triggerChanged(key);
}
}
/**
* @ignore
*/
var objectID = 0;
/**
* @class
* A bindable object. Allows observing and binding to its properties.
*/
var BObject = function () {};
BObject.prototype = util.extend(BObject.prototype, /** @lends BObject# */{
/**
* Unique ID
* @type Integer
*/
_id: 0,
/**
* The constructor for subclasses. Overwrite this for any initalisation you
* need to do.
* @ignore
*/
init: function () {},
/**
* Get a property from the object. Always use this instead of trying to
* access the property directly. This will ensure all bindings, setters and
* getters work correctly.
*
* @param {String} key Name of property to get
* @returns {*} Value of the property
*/
get: function (key) {
var accessor = getAccessors(this)[key];
if (accessor) {
return accessor.target.get(accessor.key);
} else {
// Call getting function
if (this['get_' + key]) {
return this['get_' + key]();
}
return this[key];
}
},
/**
* Set a property on the object. Always use this instead of trying to
* access the property directly. This will ensure all bindings, setters and
* getters work correctly.
*
* @param {String} key Name of property to get
* @param {*} value New value for the property
*/
set: function (key, value) {
var accessor = getAccessors(this)[key],
oldVal = this.get(key);
if (accessor) {
accessor.target.set(accessor.key, value);
} else {
if (this['set_' + key]) {
this['set_' + key](value);
} else {
this[key] = value;
}
}
this.triggerChanged(key, oldVal);
},
/**
* Set multiple propertys in one go
*
* @param {Object} kvp An Object where the key is a property name and the value is the value to assign to the property
*
* @example
* var props = {
* monkey: 'ook',
* cat: 'meow',
* dog: 'woof'
* };
* foo.setValues(props);
* console.log(foo.get('cat')); // Logs 'meow'
*/
setValues: function (kvp) {
for (var x in kvp) {
if (kvp.hasOwnProperty(x)) {
this.set(x, kvp[x]);
}
}
},
changed: function (key) {
},
/**
* @private
*/
notify: function (key, oldVal) {
var accessor = getAccessors(this)[key];
if (accessor) {
accessor.target.notify(accessor.key, oldVal);
}
},
/**
* @private
*/
triggerChanged: function(key, oldVal) {
event.trigger(this, key.toLowerCase() + '_changed', oldVal);
},
/**
* Bind the value of a property on this object to that of another object so
* they always have the same value. Setting the value on either object will update
* the other too.
*
* @param {String} key Name of the property on this object that should be bound
* @param {BOject} target Object to bind to
* @param {String} [targetKey=key] Key on the target object to bind to
* @param {Boolean} [noNotify=false] Set to true to prevent this object's property triggering a 'changed' event when adding the binding
*/
bindTo: function (key, target, targetKey, noNotify) {
targetKey = targetKey || key;
var self = this;
this.unbind(key);
var oldVal = this.get(key);
// When bound property changes, trigger a 'changed' event on this one too
getBindings(this)[key] = event.addListener(target, targetKey.toLowerCase() + '_changed', function (oldVal) {
self.triggerChanged(key, oldVal);
});
addAccessor(this, key, target, targetKey, noNotify);
},
/**
* Remove binding from a property which set setup using BObject#bindTo.
*
* @param {String} key Name of the property on this object to unbind
*/
unbind: function (key) {
var binding = getBindings(this)[key];
if (!binding) {
return;
}
delete getBindings(this)[key];
event.removeListener(binding);
// Grab current value from bound property
var val = this.get(key);
delete getAccessors(this)[key];
// Set bound value
this[key] = val;
},
/**
* Remove all bindings on this object
*/
unbindAll: function () {
var keys = [],
bindings = getBindings(this);
for (var k in bindings) {
if (bindings.hasOwnProperty(k)) {
this.unbind(k);
}
}
},
/**
* Unique ID for this object
* @getter id
*/
get_id: function() {
if (!this._id) {
this._id = ++objectID;
}
return this._id;
}
});
/**
* Create a new instance of this object
* @returns {BObject} New instance of this object
*/
BObject.create = function() {
var ret = new this();
ret.init.apply(ret, arguments);
return ret;
};
/**
* Create a new subclass by extending this one
* @returns {Object} A new subclass of this object
*/
BObject.extend = function() {
var newObj = function() {},
args = [],
i;
// Copy 'class' methods
for (x in this) {
// Don't copy built-ins
if (!this.hasOwnProperty(x)) {
continue;
}
newObj[x] = this[x];
}
// Add given properties to the prototype
newObj.prototype = util.beget(this.prototype)
args.push(newObj.prototype);
for (i = 0; i<arguments.length; i++) {
args.push(arguments[i])
}
util.extend.apply(null, args);
newObj.superclass = this;
// Create new instance
return newObj;
};
/**
* Get a property from the class. Always use this instead of trying to
* access the property directly. This will ensure all bindings, setters and
* getters work correctly.
*
* @function
* @param {String} key Name of property to get
* @returns {*} Value of the property
*/
BObject.get = BObject.prototype.get;
/**
* Set a property on the class. Always use this instead of trying to
* access the property directly. This will ensure all bindings, setters and
* getters work correctly.
*
* @function
* @param {String} key Name of property to get
* @param {*} value New value for the property
*/
BObject.set = BObject.prototype.set;
var BArray = BObject.extend(/** @lends BArray# */{
/**
* @constructs
* A bindable array. Allows observing for changes made to its contents
*
* @extends BObject
* @param {Array} [array=[]] A normal JS array to use for data
*/
init: function (array) {
this.array = arrary || [];
this.set('length', this.array.length);
},
/**
* Get an item
*
* @param {Integer} i Index to get item from
* @returns {*} Value stored in the array at index 'i'
*/
getAt: function (i) {
return this.array[i];
},
/**
* Set an item -- Overwrites any existing item at index
*
* @param {Integer} i Index to set item to
* @param {*} value Value to assign to index
*/
setAt: function (i, value) {
var oldVal = this.array[i];
this.array[i] = value;
event.trigger(this, 'set_at', i, oldVal);
},
/**
* Insert a new item into the array without overwriting anything
*
* @param {Integer} i Index to insert item at
* @param {*} value Value to insert
*/
insertAt: function (i, value) {
this.array.splice(i, 0, value);
this.set('length', this.array.length);
event.trigger(this, 'insert_at', i);
},
/**
* Remove item from the array and return it
*
* @param {Integer} i Index to remove
* @returns {*} Value that was removed
*/
removeAt: function (i) {
var oldVal = this.array[i];
this.array.splice(i, 1);
this.set('length', this.array.length);
event.trigger(this, 'remove_at', i, oldVal);
return oldVal;
},
/**
* Get the internal Javascript Array instance
*
* @returns {Array} Internal Javascript Array
*/
getArray: function () {
return this.array;
},
/**
* Append a value to the end of the array and return its new length
*
* @param {*} value Value to append to the array
* @returns {Integer} New length of the array
*/
push: function (value) {
this.insertAt(this.array.length, value);
return this.array.length;
},
/**
* Remove value from the end of the array and return it
*
* @returns {*} Value that was removed
*/
pop: function () {
return this.removeAt(this.array.length - 1);
}
});
exports.BObject = BObject;
exports.BArray = BArray;