-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpega_yui_attribute.js
193 lines (166 loc) · 4.69 KB
/
pega_yui_attribute.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
/**
* Provides Attribute configurations.
* @namespace pega.util
* @class Attribute
* @constructor
* @param hash {Object} The intial Attribute.
* @param {pega.util.AttributeProvider} The owner of the Attribute instance.
*/
pega.util.Attribute = function(hash, owner) {
if (owner) {
this.owner = owner;
this.configure(hash, true);
}
};
pega.util.Attribute.prototype = {
/**
* The name of the attribute.
* @property name
* @type String
*/
name: undefined,
/**
* The value of the attribute.
* @property value
* @type String
*/
value: null,
/**
* The owner of the attribute.
* @property owner
* @type pega.util.AttributeProvider
*/
owner: null,
/**
* Whether or not the attribute is read only.
* @property readOnly
* @type Boolean
*/
readOnly: false,
/**
* Whether or not the attribute can only be written once.
* @property writeOnce
* @type Boolean
*/
writeOnce: false,
/**
* The attribute's initial configuration.
* @private
* @property _initialConfig
* @type Object
*/
_initialConfig: null,
/**
* Whether or not the attribute's value has been set.
* @private
* @property _written
* @type Boolean
*/
_written: false,
/**
* The method to use when setting the attribute's value.
* The method recieves the new value as the only argument.
* @property method
* @type Function
*/
method: null,
/**
* The validator to use when setting the attribute's value.
* @property validator
* @type Function
* @return Boolean
*/
validator: null,
/**
* Retrieves the current value of the attribute.
* @method getValue
* @return {any} The current value of the attribute.
*/
getValue: function() {
return this.value;
},
/**
* Sets the value of the attribute and fires beforeChange and change events.
* @method setValue
* @param {Any} value The value to apply to the attribute.
* @param {Boolean} silent If true the change events will not be fired.
* @return {Boolean} Whether or not the value was set.
*/
setValue: function(value, silent) {
var beforeRetVal;
var owner = this.owner;
var name = this.name;
var event = {
type: name,
prevValue: this.getValue(),
newValue: value
};
if (this.readOnly || ( this.writeOnce && this._written) ) {
return false; // write not allowed
}
if (this.validator && !this.validator.call(owner, value) ) {
return false; // invalid value
}
if (!silent) {
beforeRetVal = owner.fireBeforeChangeEvent(event);
if (beforeRetVal === false) {
pega.log('setValue ' + name +
'cancelled by beforeChange event', 'info', 'Attribute');
return false;
}
}
if (this.method) {
this.method.call(owner, value);
}
this.value = value;
this._written = true;
event.type = name;
if (!silent) {
this.owner.fireChangeEvent(event);
}
return true;
},
/**
* Allows for configuring the Attribute's properties.
* @method configure
* @param {Object} map A key-value map of Attribute properties.
* @param {Boolean} init Whether or not this should become the initial config.
*/
configure: function(map, init) {
map = map || {};
this._written = false; // reset writeOnce
this._initialConfig = this._initialConfig || {};
for (var key in map) {
if ( key && pega.lang.hasOwnProperty(map, key) ) {
this[key] = map[key];
if (init) {
this._initialConfig[key] = map[key];
}
}
}
},
/**
* Resets the value to the initial config value.
* @method resetValue
* @return {Boolean} Whether or not the value was set.
*/
resetValue: function() {
return this.setValue(this._initialConfig.value);
},
/**
* Resets the attribute config to the initial config state.
* @method resetConfig
*/
resetConfig: function() {
this.configure(this._initialConfig);
},
/**
* Resets the value to the current value.
* Useful when values may have gotten out of sync with actual properties.
* @method refresh
* @return {Boolean} Whether or not the value was set.
*/
refresh: function(silent) {
this.setValue(this.value, silent);
}
};