forked from thinkpixellab/pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.js
106 lines (94 loc) · 2.24 KB
/
property.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
goog.provide('pl.Property');
goog.require('goog.array');
goog.require('goog.math.Coordinate');
goog.require('goog.math.Size');
/**
* @constructor
* @param {!string} name
* @param {*=} opt_defaultValue
*/
pl.Property = function(name, opt_defaultValue) {
this._name = name;
this._defaultValue = opt_defaultValue;
this._id = pl.Property._propertyId++;
};
/**
* @param {!Object} object
*/
pl.Property.prototype.clear = function(object) {
if (object[pl.Property._PROP_ID_PROPERTY]) {
var hash = object[pl.Property._PROP_ID_PROPERTY];
delete hash[this._id];
this._notify(object);
}
};
/**
* @param {!Object} object
*/
pl.Property.prototype.get = function(object) {
var coreVal = this.getCore(object);
if (coreVal) {
return coreVal[0];
}
else {
return this._defaultValue;
}
};
/**
* @param {!Object} object
*/
pl.Property.prototype.set = function(object, value) {
var hash = object[pl.Property._PROP_ID_PROPERTY] || (object[pl.Property._PROP_ID_PROPERTY] = {});
hash[this._id] = value;
this._notify(object);
};
/**
* @param {!Object} object
* @return {boolean}
*/
pl.Property.prototype.isSet = function(object) {
return object[pl.Property._PROP_ID_PROPERTY] && this._id in object[pl.Property._PROP_ID_PROPERTY];
};
pl.Property.prototype._notify = function(object) {
var listener = pl.Property._callbackProperty.get(object);
if (listener) {
listener(this);
}
};
/**
* @param {!Object} object
* @return {Array|undefined}
*/
pl.Property.prototype.getCore = function(object) {
if (object[pl.Property._PROP_ID_PROPERTY]) {
var hash = object[pl.Property._PROP_ID_PROPERTY];
if (this._id in hash) {
return [hash[this._id]];
}
else {
return null;
}
}
};
/**
* @param {!Object} object
* @param {!Function} listener.
*/
pl.Property.watchChanges = function(object, listener) {
if (pl.Property._callbackProperty.get(object)) {
throw Error('already set');
}
pl.Property._callbackProperty.set(object, listener);
};
/**
* @type {string}
* @private
*/
pl.Property._PROP_ID_PROPERTY = 'prop_uid_' +
Math.floor(Math.random() * 2147483648).toString(36);
/**
* @type {number}
* @private
*/
pl.Property._propertyId = 0;
pl.Property._callbackProperty = new pl.Property('Callback');