-
Notifications
You must be signed in to change notification settings - Fork 0
/
pega_yui_tab.js
507 lines (449 loc) · 16 KB
/
pega_yui_tab.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
;(function() {
var Dom = pega.util.Dom,
Event = pega.util.Event;
/**
* A representation of a Tab's label and content.
* @namespace pega.widget
* @class Tab
* @extends pega.util.Element
* @constructor
* @param element {HTMLElement | String} (optional) The html element that
* represents the TabView. An element will be created if none provided.
* @param {Object} properties A key map of initial properties
*/
var Tab = function(el, attr) {
attr = attr || {};
if (arguments.length == 1 && !pega.lang.isString(el) && !el.nodeName) {
attr = el;
el = attr.element;
}
if (!el && !attr.element) {
el = _createTabElement.call(this, attr);
}
this.loadHandler = {
success: function(o) {
this.set('content', o.responseText);
},
failure: function(o) {
pega.log('loading failed: ' + o.statusText,
'error', 'Tab');
}
};
Tab.superclass.constructor.call(this, el, attr);
this.DOM_EVENTS = {}; // delegating to tabView
};
pega.extend(Tab, pega.util.Element);
var proto = Tab.prototype;
/**
* The default tag name for a Tab's inner element.
* @property LABEL_INNER_TAGNAME
* @type String
* @default "em"
*/
proto.LABEL_TAGNAME = 'em';
/**
* The class name applied to active tabs.
* @property ACTIVE_CLASSNAME
* @type String
* @default "on"
*/
proto.ACTIVE_CLASSNAME = 'selected';
/**
* The class name applied to disabled tabs.
* @property DISABLED_CLASSNAME
* @type String
* @default "disabled"
*/
proto.DISABLED_CLASSNAME = 'disabled';
/**
* The class name applied to dynamic tabs while loading.
* @property LOADING_CLASSNAME
* @type String
* @default "disabled"
*/
proto.LOADING_CLASSNAME = 'loading';
/**
* Provides a reference to the connection request object when data is
* loaded dynamically.
* @property dataConnection
* @type Object
*/
proto.dataConnection = null;
/**
* Object containing success and failure callbacks for loading data.
* @property loadHandler
* @type object
*/
proto.loadHandler = null;
/**
* Provides a readable name for the tab.
* @method toString
* @return String
*/
proto.toString = function() {
var el = this.get('element');
var id = el.id || el.tagName;
return "Tab " + id;
};
/**
* setAttributeConfigs TabView specific properties.
* @method initAttributes
* @param {Object} attr Hash of initial attributes
*/
proto.initAttributes = function(attr) {
attr = attr || {};
Tab.superclass.initAttributes.call(this, attr);
var el = this.get('element');
/**
* The event that triggers the tab's activation.
* @config activationEvent
* @type String
*/
this.setAttributeConfig('activationEvent', {
value: attr.activationEvent || 'click'
});
/**
* The element that contains the tab's label.
* @config labelEl
* @type HTMLElement
*/
this.setAttributeConfig('labelEl', {
value: attr.labelEl || _getlabelEl.call(this),
method: function(value) {
var current = this.get('labelEl');
if (current) {
if (current == value) {
return false; // already set
}
this.replaceChild(value, current);
} else if (el.firstChild) { // ensure label is firstChild by default
this.insertBefore(value, el.firstChild);
} else {
this.appendChild(value);
}
}
});
/**
* The tab's label text (or innerHTML).
* @config label
* @type String
*/
this.setAttributeConfig('label', {
value: attr.label || _getLabel.call(this),
method: function(value) {
var labelEl = this.get('labelEl');
if (!labelEl) { // create if needed
this.set('labelEl', _createlabelEl.call(this));
}
_setLabel.call(this, value);
}
});
/**
* The HTMLElement that contains the tab's content.
* @config contentEl
* @type HTMLElement
*/
this.setAttributeConfig('contentEl', {
value: attr.contentEl || document.createElement('div'),
method: function(value) {
var current = this.get('contentEl');
if (current) {
if (current == value) {
return false; // already set
}
this.replaceChild(value, current);
}
}
});
/**
* The tab's content.
* @config content
* @type String
*/
this.setAttributeConfig('content', {
value: attr.content,
method: function(value) {
this.get('contentEl').innerHTML = value;
}
});
var _dataLoaded = false;
/**
* The tab's data source, used for loading content dynamically.
* @config dataSrc
* @type String
*/
this.setAttributeConfig('dataSrc', {
value: attr.dataSrc
});
/**
* Whether or not content should be reloaded for every view.
* @config cacheData
* @type Boolean
* @default false
*/
this.setAttributeConfig('cacheData', {
value: attr.cacheData || false,
validator: pega.lang.isBoolean
});
/**
* The method to use for the data request.
* @config loadMethod
* @type String
* @default "GET"
*/
this.setAttributeConfig('loadMethod', {
value: attr.loadMethod || 'GET',
validator: pega.lang.isString
});
/**
* Whether or not any data has been loaded from the server.
* @config dataLoaded
* @type Boolean
*/
this.setAttributeConfig('dataLoaded', {
value: false,
validator: pega.lang.isBoolean,
writeOnce: true
});
/**
* Number if milliseconds before aborting and calling failure handler.
* @config dataTimeout
* @type Number
* @default null
*/
this.setAttributeConfig('dataTimeout', {
value: attr.dataTimeout || null,
validator: pega.lang.isNumber
});
/**
* Whether or not the tab is currently active.
* If a dataSrc is set for the tab, the content will be loaded from
* the given source.
* @config active
* @type Boolean
*/
this.setAttributeConfig('active', {
value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
method: function(value) {
if (value === true) {
this.addClass(this.ACTIVE_CLASSNAME);
this.set('title', 'active');
} else {
this.removeClass(this.ACTIVE_CLASSNAME);
this.set('title', '');
}
},
validator: function(value) {
return pega.lang.isBoolean(value) && !this.get('disabled') ;
}
});
/**
* Whether or not the tab is disabled.
* @config disabled
* @type Boolean
*/
this.setAttributeConfig('disabled', {
value: attr.disabled || this.hasClass(this.DISABLED_CLASSNAME),
method: function(value) {
if (value === true) {
Dom.addClass(this.get('element'), this.DISABLED_CLASSNAME);
} else {
Dom.removeClass(this.get('element'), this.DISABLED_CLASSNAME);
}
},
validator: pega.lang.isBoolean
});
/**
* The href of the tab's anchor element.
* @config href
* @type String
* @default '#'
*/
this.setAttributeConfig('href', {
value: attr.href || '#',
method: function(value) {
this.getElementsByTagName('a')[0].href = value;
},
validator: pega.lang.isString
});
/**
* The Whether or not the tab's content is visible.
* @config contentVisible
* @type Boolean
* @default false
*/
this.setAttributeConfig('contentVisible', {
value: attr.contentVisible,
method: function(value) {
if (value) {
this.get('contentEl').style.display = 'block';
if ( this.get('dataSrc') ) {
// load dynamic content unless already loaded and caching
if ( !this.get('dataLoaded') || !this.get('cacheData') ) {
_dataConnect.call(this);
}
}
} else {
this.get('contentEl').style.display = 'none';
}
},
validator: pega.lang.isBoolean
});
};
var _createTabElement = function(attr) {
var el = document.createElement('li');
var a = document.createElement('a');
a.href = attr.href || '#';
el.appendChild(a);
var label = attr.label || null;
var labelEl = attr.labelEl || null;
if (labelEl) { // user supplied labelEl
if (!label) { // user supplied label
label = _getLabel.call(this, labelEl);
}
} else {
labelEl = _createlabelEl.call(this);
}
a.appendChild(labelEl);
return el;
};
var _getlabelEl = function() {
return this.getElementsByTagName(this.LABEL_TAGNAME)[0];
};
var _createlabelEl = function() {
var el = document.createElement(this.LABEL_TAGNAME);
return el;
};
var _setLabel = function(label) {
var el = this.get('labelEl');
el.innerHTML = label;
};
var _getLabel = function() {
var label,
el = this.get('labelEl');
if (!el) {
return undefined;
}
return el.innerHTML;
};
var _dataConnect = function() {
if (!pega.util.Connect) {
pega.log('pega.util.Connect dependency not met',
'error', 'Tab');
return false;
}
Dom.addClass(this.get('contentEl').parentNode, this.LOADING_CLASSNAME);
this.dataConnection = pega.util.Connect.asyncRequest(
this.get('loadMethod'),
this.get('dataSrc'),
{
success: function(o) {
this.loadHandler.success.call(this, o);
this.set('dataLoaded', true);
this.dataConnection = null;
Dom.removeClass(this.get('contentEl').parentNode,
this.LOADING_CLASSNAME);
},
failure: function(o) {
this.loadHandler.failure.call(this, o);
this.dataConnection = null;
Dom.removeClass(this.get('contentEl').parentNode,
this.LOADING_CLASSNAME);
},
scope: this,
timeout: this.get('dataTimeout')
}
);
};
pega.ui.Tab = Tab;
/**
* Fires before the active state is changed.
* <p>See: <a href="pega.util.Element.html#addListener">Element.addListener</a></p>
* <p>If handler returns false, the change will be cancelled, and the value will not
* be set.</p>
* <p><strong>Event fields:</strong><br>
* <code><String> type</code> beforeActiveChange<br>
* <code><Boolean>
* prevValue</code> the current value<br>
* <code><Boolean>
* newValue</code> the new value</p>
* <p><strong>Usage:</strong><br>
* <code>var handler = function(e) {var previous = e.prevValue};<br>
* myTabs.addListener('beforeActiveChange', handler);</code></p>
* @event beforeActiveChange
*/
/**
* Fires after the active state is changed.
* <p>See: <a href="pega.util.Element.html#addListener">Element.addListener</a></p>
* <p><strong>Event fields:</strong><br>
* <code><String> type</code> activeChange<br>
* <code><Boolean>
* prevValue</code> the previous value<br>
* <code><Boolean>
* newValue</code> the updated value</p>
* <p><strong>Usage:</strong><br>
* <code>var handler = function(e) {var previous = e.prevValue};<br>
* myTabs.addListener('activeChange', handler);</code></p>
* @event activeChange
*/
/**
* Fires before the tab label is changed.
* <p>See: <a href="pega.util.Element.html#addListener">Element.addListener</a></p>
* <p>If handler returns false, the change will be cancelled, and the value will not
* be set.</p>
* <p><strong>Event fields:</strong><br>
* <code><String> type</code> beforeLabelChange<br>
* <code><String>
* prevValue</code> the current value<br>
* <code><String>
* newValue</code> the new value</p>
* <p><strong>Usage:</strong><br>
* <code>var handler = function(e) {var previous = e.prevValue};<br>
* myTabs.addListener('beforeLabelChange', handler);</code></p>
* @event beforeLabelChange
*/
/**
* Fires after the tab label is changed.
* <p>See: <a href="pega.util.Element.html#addListener">Element.addListener</a></p>
* <p><strong>Event fields:</strong><br>
* <code><String> type</code> labelChange<br>
* <code><String>
* prevValue</code> the previous value<br>
* <code><String>
* newValue</code> the updated value</p>
* <p><strong>Usage:</strong><br>
* <code>var handler = function(e) {var previous = e.prevValue};<br>
* myTabs.addListener('labelChange', handler);</code></p>
* @event labelChange
*/
/**
* Fires before the tab content is changed.
* <p>See: <a href="pega.util.Element.html#addListener">Element.addListener</a></p>
* <p>If handler returns false, the change will be cancelled, and the value will not
* be set.</p>
* <p><strong>Event fields:</strong><br>
* <code><String> type</code> beforeContentChange<br>
* <code><String>
* prevValue</code> the current value<br>
* <code><String>
* newValue</code> the new value</p>
* <p><strong>Usage:</strong><br>
* <code>var handler = function(e) {var previous = e.prevValue};<br>
* myTabs.addListener('beforeContentChange', handler);</code></p>
* @event beforeContentChange
*/
/**
* Fires after the tab content is changed.
* <p>See: <a href="pega.util.Element.html#addListener">Element.addListener</a></p>
* <p><strong>Event fields:</strong><br>
* <code><String> type</code> contentChange<br>
* <code><String>
* prevValue</code> the previous value<br>
* <code><Boolean>
* newValue</code> the updated value</p>
* <p><strong>Usage:</strong><br>
* <code>var handler = function(e) {var previous = e.prevValue};<br>
* myTabs.addListener('contentChange', handler);</code></p>
* @event contentChange
*/
})();