-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpega_yui_attributeprovider.js
241 lines (213 loc) · 8.02 KB
/
pega_yui_attributeprovider.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
(function() {
var Lang = pega.util.Lang;
/*
Copyright (c) 2006, pega! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.pega.net/yui/license.txt
*/
/**
* Provides and manages pega.util.Attribute instances
* @namespace pega.util
* @class AttributeProvider
* @uses pega.util.EventProvider
*/
pega.util.AttributeProvider = function() {};
pega.util.AttributeProvider.prototype = {
/**
* A key-value map of Attribute configurations
* @property _configs
* @protected (may be used by subclasses and augmentors)
* @private
* @type {Object}
*/
_configs: null,
/**
* Returns the current value of the attribute.
* @method get
* @param {String} key The attribute whose value will be returned.
*/
get: function(key){
var configs = this._configs || {};
var config = configs[key];
if (!config) {
pega.log(key + ' not found', 'error', 'AttributeProvider');
return undefined;
}
return config.value;
},
/**
* Sets the value of a config.
* @method set
* @param {String} key The name of the attribute
* @param {Any} value The value to apply to the attribute
* @param {Boolean} silent Whether or not to suppress change events
* @return {Boolean} Whether or not the value was set.
*/
set: function(key, value, silent){
var configs = this._configs || {};
var config = configs[key];
if (!config) {
pega.log('set failed: ' + key + ' not found',
'error', 'AttributeProvider');
return false;
}
return config.setValue(value, silent);
},
/**
* Returns an array of attribute names.
* @method getAttributeKeys
* @return {Array} An array of attribute names.
*/
getAttributeKeys: function(){
var configs = this._configs;
var keys = [];
var config;
for (var key in configs) {
config = configs[key];
if ( Lang.hasOwnProperty(configs, key) &&
!Lang.isUndefined(config) ) {
keys[keys.length] = key;
}
}
return keys;
},
/**
* Sets multiple attribute values.
* @method setAttributes
* @param {Object} map A key-value map of attributes
* @param {Boolean} silent Whether or not to suppress change events
*/
setAttributes: function(map, silent){
for (var key in map) {
if ( Lang.hasOwnProperty(map, key) ) {
this.set(key, map[key], silent);
}
}
},
/**
* Resets the specified attribute's value to its initial value.
* @method resetValue
* @param {String} key The name of the attribute
* @param {Boolean} silent Whether or not to suppress change events
* @return {Boolean} Whether or not the value was set
*/
resetValue: function(key, silent){
var configs = this._configs || {};
if (configs[key]) {
this.set(key, configs[key]._initialConfig.value, silent);
return true;
}
return false;
},
/**
* Sets the attribute's value to its current value.
* @method refresh
* @param {String | Array} key The attribute(s) to refresh
* @param {Boolean} silent Whether or not to suppress change events
*/
refresh: function(key, silent){
var configs = this._configs;
key = ( ( Lang.isString(key) ) ? [key] : key ) ||
this.getAttributeKeys();
for (var i = 0, len = key.length; i < len; ++i) {
if ( // only set if there is a value and not null
configs[key[i]] &&
! Lang.isUndefined(configs[key[i]].value) &&
! Lang.isNull(configs[key[i]].value) ) {
configs[key[i]].refresh(silent);
}
}
},
/**
* Adds an Attribute to the AttributeProvider instance.
* @method register
* @param {String} key The attribute's name
* @param {Object} map A key-value map containing the
* attribute's properties.
* @deprecated Use setAttributeConfig
*/
register: function(key, map) {
this.setAttributeConfig(key, map);
},
/**
* Returns the attribute's properties.
* @method getAttributeConfig
* @param {String} key The attribute's name
* @private
* @return {object} A key-value map containing all of the
* attribute's properties.
*/
getAttributeConfig: function(key) {
var configs = this._configs || {};
var config = configs[key] || {};
var map = {}; // returning a copy to prevent overrides
for (key in config) {
if ( Lang.hasOwnProperty(config, key) ) {
map[key] = config[key];
}
}
return map;
},
/**
* Sets or updates an Attribute instance's properties.
* @method setAttributeConfig
* @param {String} key The attribute's name.
* @param {Object} map A key-value map of attribute properties
* @param {Boolean} init Whether or not this should become the intial config.
*/
setAttributeConfig: function(key, map, init) {
var configs = this._configs || {};
map = map || {};
if (!configs[key]) {
map.name = key;
configs[key] = new pega.util.Attribute(map, this);
} else {
configs[key].configure(map, init);
}
},
/**
* Sets or updates an Attribute instance's properties.
* @method configureAttribute
* @param {String} key The attribute's name.
* @param {Object} map A key-value map of attribute properties
* @param {Boolean} init Whether or not this should become the intial config.
* @deprecated Use setAttributeConfig
*/
configureAttribute: function(key, map, init) {
this.setAttributeConfig(key, map, init);
},
/**
* Resets an attribute to its intial configuration.
* @method resetAttributeConfig
* @param {String} key The attribute's name.
* @private
*/
resetAttributeConfig: function(key){
var configs = this._configs || {};
configs[key].resetConfig();
},
/**
* Fires the attribute's beforeChange event.
* @method fireBeforeChangeEvent
* @param {String} key The attribute's name.
* @param {Obj} e The event object to pass to handlers.
*/
fireBeforeChangeEvent: function(e) {
var type = 'before';
type += e.type.charAt(0).toUpperCase() + e.type.substr(1) + 'Change';
e.type = type;
return this.fireEvent(e.type, e);
},
/**
* Fires the attribute's change event.
* @method fireChangeEvent
* @param {String} key The attribute's name.
* @param {Obj} e The event object to pass to the handlers.
*/
fireChangeEvent: function(e) {
e.type += 'Change';
return this.fireEvent(e.type, e);
}
};
pega.augment(pega.util.AttributeProvider, pega.util.EventProvider);
})();