|
602
|
1 |
YUI.add('widget-parent', function (Y, NAME) { |
|
|
2 |
|
|
|
3 |
/** |
|
|
4 |
* Extension enabling a Widget to be a parent of another Widget. |
|
|
5 |
* |
|
|
6 |
* @module widget-parent |
|
|
7 |
*/ |
|
|
8 |
|
|
|
9 |
var Lang = Y.Lang, |
|
|
10 |
RENDERED = "rendered", |
|
|
11 |
BOUNDING_BOX = "boundingBox"; |
|
|
12 |
|
|
|
13 |
/** |
|
|
14 |
* Widget extension providing functionality enabling a Widget to be a |
|
|
15 |
* parent of another Widget. |
|
|
16 |
* |
|
|
17 |
* <p>In addition to the set of attributes supported by WidgetParent, the constructor |
|
|
18 |
* configuration object can also contain a <code>children</code> which can be used |
|
|
19 |
* to add child widgets to the parent during construction. The <code>children</code> |
|
|
20 |
* property is an array of either child widget instances or child widget configuration |
|
|
21 |
* objects, and is sugar for the <a href="#method_add">add</a> method. See the |
|
|
22 |
* <a href="#method_add">add</a> for details on the structure of the child widget |
|
|
23 |
* configuration object. |
|
|
24 |
* @class WidgetParent |
|
|
25 |
* @constructor |
|
|
26 |
* @uses ArrayList |
|
|
27 |
* @param {Object} config User configuration object. |
|
|
28 |
*/ |
|
|
29 |
function Parent(config) { |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* Fires when a Widget is add as a child. The event object will have a |
|
|
33 |
* 'child' property that returns a reference to the child Widget, as well |
|
|
34 |
* as an 'index' property that returns a reference to the index specified |
|
|
35 |
* when the add() method was called. |
|
|
36 |
* <p> |
|
|
37 |
* Subscribers to the "on" moment of this event, will be notified |
|
|
38 |
* before a child is added. |
|
|
39 |
* </p> |
|
|
40 |
* <p> |
|
|
41 |
* Subscribers to the "after" moment of this event, will be notified |
|
|
42 |
* after a child is added. |
|
|
43 |
* </p> |
|
|
44 |
* |
|
|
45 |
* @event addChild |
|
|
46 |
* @preventable _defAddChildFn |
|
|
47 |
* @param {EventFacade} e The Event Facade |
|
|
48 |
*/ |
|
|
49 |
this.publish("addChild", { |
|
|
50 |
defaultTargetOnly: true, |
|
|
51 |
defaultFn: this._defAddChildFn |
|
|
52 |
}); |
|
|
53 |
|
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Fires when a child Widget is removed. The event object will have a |
|
|
57 |
* 'child' property that returns a reference to the child Widget, as well |
|
|
58 |
* as an 'index' property that returns a reference child's ordinal position. |
|
|
59 |
* <p> |
|
|
60 |
* Subscribers to the "on" moment of this event, will be notified |
|
|
61 |
* before a child is removed. |
|
|
62 |
* </p> |
|
|
63 |
* <p> |
|
|
64 |
* Subscribers to the "after" moment of this event, will be notified |
|
|
65 |
* after a child is removed. |
|
|
66 |
* </p> |
|
|
67 |
* |
|
|
68 |
* @event removeChild |
|
|
69 |
* @preventable _defRemoveChildFn |
|
|
70 |
* @param {EventFacade} e The Event Facade |
|
|
71 |
*/ |
|
|
72 |
this.publish("removeChild", { |
|
|
73 |
defaultTargetOnly: true, |
|
|
74 |
defaultFn: this._defRemoveChildFn |
|
|
75 |
}); |
|
|
76 |
|
|
|
77 |
this._items = []; |
|
|
78 |
|
|
|
79 |
var children, |
|
|
80 |
handle; |
|
|
81 |
|
|
|
82 |
if (config && config.children) { |
|
|
83 |
|
|
|
84 |
children = config.children; |
|
|
85 |
|
|
|
86 |
handle = this.after("initializedChange", function (e) { |
|
|
87 |
this._add(children); |
|
|
88 |
handle.detach(); |
|
|
89 |
}); |
|
|
90 |
|
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
// Widget method overlap |
|
|
94 |
Y.after(this._renderChildren, this, "renderUI"); |
|
|
95 |
Y.after(this._bindUIParent, this, "bindUI"); |
|
|
96 |
|
|
|
97 |
this.after("selectionChange", this._afterSelectionChange); |
|
|
98 |
this.after("selectedChange", this._afterParentSelectedChange); |
|
|
99 |
this.after("activeDescendantChange", this._afterActiveDescendantChange); |
|
|
100 |
|
|
|
101 |
this._hDestroyChild = this.after("*:destroy", this._afterDestroyChild); |
|
|
102 |
this.after("*:focusedChange", this._updateActiveDescendant); |
|
|
103 |
|
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
Parent.ATTRS = { |
|
|
107 |
|
|
|
108 |
/** |
|
|
109 |
* @attribute defaultChildType |
|
|
110 |
* @type {String|Object} |
|
|
111 |
* |
|
|
112 |
* @description String representing the default type of the children |
|
|
113 |
* managed by this Widget. Can also supply default type as a constructor |
|
|
114 |
* reference. |
|
|
115 |
*/ |
|
|
116 |
defaultChildType: { |
|
|
117 |
setter: function (val) { |
|
|
118 |
|
|
|
119 |
var returnVal = Y.Attribute.INVALID_VALUE, |
|
|
120 |
FnConstructor = Lang.isString(val) ? Y[val] : val; |
|
|
121 |
|
|
|
122 |
if (Lang.isFunction(FnConstructor)) { |
|
|
123 |
returnVal = FnConstructor; |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
return returnVal; |
|
|
127 |
} |
|
|
128 |
}, |
|
|
129 |
|
|
|
130 |
/** |
|
|
131 |
* @attribute activeDescendant |
|
|
132 |
* @type Widget |
|
|
133 |
* @readOnly |
|
|
134 |
* |
|
|
135 |
* @description Returns the Widget's currently focused descendant Widget. |
|
|
136 |
*/ |
|
|
137 |
activeDescendant: { |
|
|
138 |
readOnly: true |
|
|
139 |
}, |
|
|
140 |
|
|
|
141 |
/** |
|
|
142 |
* @attribute multiple |
|
|
143 |
* @type Boolean |
|
|
144 |
* @default false |
|
|
145 |
* @writeOnce |
|
|
146 |
* |
|
|
147 |
* @description Boolean indicating if multiple children can be selected at |
|
|
148 |
* once. Whether or not multiple selection is enabled is always delegated |
|
|
149 |
* to the value of the <code>multiple</code> attribute of the root widget |
|
|
150 |
* in the object hierarchy. |
|
|
151 |
*/ |
|
|
152 |
multiple: { |
|
|
153 |
value: false, |
|
|
154 |
validator: Lang.isBoolean, |
|
|
155 |
writeOnce: true, |
|
|
156 |
getter: function (value) { |
|
|
157 |
var root = this.get("root"); |
|
|
158 |
return (root && root != this) ? root.get("multiple") : value; |
|
|
159 |
} |
|
|
160 |
}, |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
/** |
|
|
164 |
* @attribute selection |
|
|
165 |
* @type {ArrayList|Widget} |
|
|
166 |
* @readOnly |
|
|
167 |
* |
|
|
168 |
* @description Returns the currently selected child Widget. If the |
|
|
169 |
* <code>mulitple</code> attribte is set to <code>true</code> will |
|
|
170 |
* return an Y.ArrayList instance containing the currently selected |
|
|
171 |
* children. If no children are selected, will return null. |
|
|
172 |
*/ |
|
|
173 |
selection: { |
|
|
174 |
readOnly: true, |
|
|
175 |
setter: "_setSelection", |
|
|
176 |
getter: function (value) { |
|
|
177 |
var selection = Lang.isArray(value) ? |
|
|
178 |
(new Y.ArrayList(value)) : value; |
|
|
179 |
return selection; |
|
|
180 |
} |
|
|
181 |
}, |
|
|
182 |
|
|
|
183 |
selected: { |
|
|
184 |
setter: function (value) { |
|
|
185 |
|
|
|
186 |
// Enforces selection behavior on for parent Widgets. Parent's |
|
|
187 |
// selected attribute can be set to the following: |
|
|
188 |
// 0 - Not selected |
|
|
189 |
// 1 - Fully selected (all children are selected). In order for |
|
|
190 |
// all children to be selected, multiple selection must be |
|
|
191 |
// enabled. Therefore, you cannot set the "selected" attribute |
|
|
192 |
// on a parent Widget to 1 unless multiple selection is enabled. |
|
|
193 |
// 2 - Partially selected, meaning one ore more (but not all) |
|
|
194 |
// children are selected. |
|
|
195 |
|
|
|
196 |
var returnVal = value; |
|
|
197 |
|
|
|
198 |
if (value === 1 && !this.get("multiple")) { |
|
|
199 |
returnVal = Y.Attribute.INVALID_VALUE; |
|
|
200 |
} |
|
|
201 |
|
|
|
202 |
return returnVal; |
|
|
203 |
} |
|
|
204 |
} |
|
|
205 |
|
|
|
206 |
}; |
|
|
207 |
|
|
|
208 |
Parent.prototype = { |
|
|
209 |
|
|
|
210 |
/** |
|
|
211 |
* The destructor implementation for Parent widgets. Destroys all children. |
|
|
212 |
* @method destructor |
|
|
213 |
*/ |
|
|
214 |
destructor: function() { |
|
|
215 |
this._destroyChildren(); |
|
|
216 |
}, |
|
|
217 |
|
|
|
218 |
/** |
|
|
219 |
* Destroy event listener for each child Widget, responsible for removing |
|
|
220 |
* the destroyed child Widget from the parent's internal array of children |
|
|
221 |
* (_items property). |
|
|
222 |
* |
|
|
223 |
* @method _afterDestroyChild |
|
|
224 |
* @protected |
|
|
225 |
* @param {EventFacade} event The event facade for the attribute change. |
|
|
226 |
*/ |
|
|
227 |
_afterDestroyChild: function (event) { |
|
|
228 |
var child = event.target; |
|
|
229 |
|
|
|
230 |
if (child.get("parent") == this) { |
|
|
231 |
child.remove(); |
|
|
232 |
} |
|
|
233 |
}, |
|
|
234 |
|
|
|
235 |
/** |
|
|
236 |
* Attribute change listener for the <code>selection</code> |
|
|
237 |
* attribute, responsible for setting the value of the |
|
|
238 |
* parent's <code>selected</code> attribute. |
|
|
239 |
* |
|
|
240 |
* @method _afterSelectionChange |
|
|
241 |
* @protected |
|
|
242 |
* @param {EventFacade} event The event facade for the attribute change. |
|
|
243 |
*/ |
|
|
244 |
_afterSelectionChange: function (event) { |
|
|
245 |
|
|
|
246 |
if (event.target == this && event.src != this) { |
|
|
247 |
|
|
|
248 |
var selection = event.newVal, |
|
|
249 |
selectedVal = 0; // Not selected |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
if (selection) { |
|
|
253 |
|
|
|
254 |
selectedVal = 2; // Assume partially selected, confirm otherwise |
|
|
255 |
|
|
|
256 |
|
|
|
257 |
if (Y.instanceOf(selection, Y.ArrayList) && |
|
|
258 |
(selection.size() === this.size())) { |
|
|
259 |
|
|
|
260 |
selectedVal = 1; // Fully selected |
|
|
261 |
|
|
|
262 |
} |
|
|
263 |
|
|
|
264 |
} |
|
|
265 |
|
|
|
266 |
this.set("selected", selectedVal, { src: this }); |
|
|
267 |
|
|
|
268 |
} |
|
|
269 |
}, |
|
|
270 |
|
|
|
271 |
|
|
|
272 |
/** |
|
|
273 |
* Attribute change listener for the <code>activeDescendant</code> |
|
|
274 |
* attribute, responsible for setting the value of the |
|
|
275 |
* parent's <code>activeDescendant</code> attribute. |
|
|
276 |
* |
|
|
277 |
* @method _afterActiveDescendantChange |
|
|
278 |
* @protected |
|
|
279 |
* @param {EventFacade} event The event facade for the attribute change. |
|
|
280 |
*/ |
|
|
281 |
_afterActiveDescendantChange: function (event) { |
|
|
282 |
var parent = this.get("parent"); |
|
|
283 |
|
|
|
284 |
if (parent) { |
|
|
285 |
parent._set("activeDescendant", event.newVal); |
|
|
286 |
} |
|
|
287 |
}, |
|
|
288 |
|
|
|
289 |
/** |
|
|
290 |
* Attribute change listener for the <code>selected</code> |
|
|
291 |
* attribute, responsible for syncing the selected state of all children to |
|
|
292 |
* match that of their parent Widget. |
|
|
293 |
* |
|
|
294 |
* |
|
|
295 |
* @method _afterParentSelectedChange |
|
|
296 |
* @protected |
|
|
297 |
* @param {EventFacade} event The event facade for the attribute change. |
|
|
298 |
*/ |
|
|
299 |
_afterParentSelectedChange: function (event) { |
|
|
300 |
|
|
|
301 |
var value = event.newVal; |
|
|
302 |
|
|
|
303 |
if (this == event.target && event.src != this && |
|
|
304 |
(value === 0 || value === 1)) { |
|
|
305 |
|
|
|
306 |
this.each(function (child) { |
|
|
307 |
|
|
|
308 |
// Specify the source of this change as the parent so that |
|
|
309 |
// value of the parent's "selection" attribute isn't |
|
|
310 |
// recalculated |
|
|
311 |
|
|
|
312 |
child.set("selected", value, { src: this }); |
|
|
313 |
|
|
|
314 |
}, this); |
|
|
315 |
|
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
}, |
|
|
319 |
|
|
|
320 |
|
|
|
321 |
/** |
|
|
322 |
* Default setter for <code>selection</code> attribute changes. |
|
|
323 |
* |
|
|
324 |
* @method _setSelection |
|
|
325 |
* @protected |
|
|
326 |
* @param child {Widget|Array} Widget or Array of Widget instances. |
|
|
327 |
* @return {Widget|Array} Widget or Array of Widget instances. |
|
|
328 |
*/ |
|
|
329 |
_setSelection: function (child) { |
|
|
330 |
|
|
|
331 |
var selection = null, |
|
|
332 |
selected; |
|
|
333 |
|
|
|
334 |
if (this.get("multiple") && !this.isEmpty()) { |
|
|
335 |
|
|
|
336 |
selected = []; |
|
|
337 |
|
|
|
338 |
this.each(function (v) { |
|
|
339 |
|
|
|
340 |
if (v.get("selected") > 0) { |
|
|
341 |
selected.push(v); |
|
|
342 |
} |
|
|
343 |
|
|
|
344 |
}); |
|
|
345 |
|
|
|
346 |
if (selected.length > 0) { |
|
|
347 |
selection = selected; |
|
|
348 |
} |
|
|
349 |
|
|
|
350 |
} |
|
|
351 |
else { |
|
|
352 |
|
|
|
353 |
if (child.get("selected") > 0) { |
|
|
354 |
selection = child; |
|
|
355 |
} |
|
|
356 |
|
|
|
357 |
} |
|
|
358 |
|
|
|
359 |
return selection; |
|
|
360 |
|
|
|
361 |
}, |
|
|
362 |
|
|
|
363 |
|
|
|
364 |
/** |
|
|
365 |
* Attribute change listener for the <code>selected</code> |
|
|
366 |
* attribute of child Widgets, responsible for setting the value of the |
|
|
367 |
* parent's <code>selection</code> attribute. |
|
|
368 |
* |
|
|
369 |
* @method _updateSelection |
|
|
370 |
* @protected |
|
|
371 |
* @param {EventFacade} event The event facade for the attribute change. |
|
|
372 |
*/ |
|
|
373 |
_updateSelection: function (event) { |
|
|
374 |
|
|
|
375 |
var child = event.target, |
|
|
376 |
selection; |
|
|
377 |
|
|
|
378 |
if (child.get("parent") == this) { |
|
|
379 |
|
|
|
380 |
if (event.src != "_updateSelection") { |
|
|
381 |
|
|
|
382 |
selection = this.get("selection"); |
|
|
383 |
|
|
|
384 |
if (!this.get("multiple") && selection && event.newVal > 0) { |
|
|
385 |
|
|
|
386 |
// Deselect the previously selected child. |
|
|
387 |
// Set src equal to the current context to prevent |
|
|
388 |
// unnecessary re-calculation of the selection. |
|
|
389 |
|
|
|
390 |
selection.set("selected", 0, { src: "_updateSelection" }); |
|
|
391 |
|
|
|
392 |
} |
|
|
393 |
|
|
|
394 |
this._set("selection", child); |
|
|
395 |
|
|
|
396 |
} |
|
|
397 |
|
|
|
398 |
if (event.src == this) { |
|
|
399 |
this._set("selection", child, { src: this }); |
|
|
400 |
} |
|
|
401 |
|
|
|
402 |
} |
|
|
403 |
|
|
|
404 |
}, |
|
|
405 |
|
|
|
406 |
/** |
|
|
407 |
* Attribute change listener for the <code>focused</code> |
|
|
408 |
* attribute of child Widgets, responsible for setting the value of the |
|
|
409 |
* parent's <code>activeDescendant</code> attribute. |
|
|
410 |
* |
|
|
411 |
* @method _updateActiveDescendant |
|
|
412 |
* @protected |
|
|
413 |
* @param {EventFacade} event The event facade for the attribute change. |
|
|
414 |
*/ |
|
|
415 |
_updateActiveDescendant: function (event) { |
|
|
416 |
var activeDescendant = (event.newVal === true) ? event.target : null; |
|
|
417 |
this._set("activeDescendant", activeDescendant); |
|
|
418 |
}, |
|
|
419 |
|
|
|
420 |
/** |
|
|
421 |
* Creates an instance of a child Widget using the specified configuration. |
|
|
422 |
* By default Widget instances will be created of the type specified |
|
|
423 |
* by the <code>defaultChildType</code> attribute. Types can be explicitly |
|
|
424 |
* defined via the <code>childType</code> property of the configuration object |
|
|
425 |
* literal. The use of the <code>type</code> property has been deprecated, but |
|
|
426 |
* will still be used as a fallback, if <code>childType</code> is not defined, |
|
|
427 |
* for backwards compatibility. |
|
|
428 |
* |
|
|
429 |
* @method _createChild |
|
|
430 |
* @protected |
|
|
431 |
* @param config {Object} Object literal representing the configuration |
|
|
432 |
* used to create an instance of a Widget. |
|
|
433 |
*/ |
|
|
434 |
_createChild: function (config) { |
|
|
435 |
|
|
|
436 |
var defaultType = this.get("defaultChildType"), |
|
|
437 |
altType = config.childType || config.type, |
|
|
438 |
child, |
|
|
439 |
Fn, |
|
|
440 |
FnConstructor; |
|
|
441 |
|
|
|
442 |
if (altType) { |
|
|
443 |
Fn = Lang.isString(altType) ? Y[altType] : altType; |
|
|
444 |
} |
|
|
445 |
|
|
|
446 |
if (Lang.isFunction(Fn)) { |
|
|
447 |
FnConstructor = Fn; |
|
|
448 |
} else if (defaultType) { |
|
|
449 |
// defaultType is normalized to a function in it's setter |
|
|
450 |
FnConstructor = defaultType; |
|
|
451 |
} |
|
|
452 |
|
|
|
453 |
if (FnConstructor) { |
|
|
454 |
child = new FnConstructor(config); |
|
|
455 |
} else { |
|
|
456 |
Y.error("Could not create a child instance because its constructor is either undefined or invalid."); |
|
|
457 |
} |
|
|
458 |
|
|
|
459 |
return child; |
|
|
460 |
|
|
|
461 |
}, |
|
|
462 |
|
|
|
463 |
/** |
|
|
464 |
* Default addChild handler |
|
|
465 |
* |
|
|
466 |
* @method _defAddChildFn |
|
|
467 |
* @protected |
|
|
468 |
* @param event {EventFacade} The Event object |
|
|
469 |
* @param child {Widget} The Widget instance, or configuration |
|
|
470 |
* object for the Widget to be added as a child. |
|
|
471 |
* @param index {Number} Number representing the position at |
|
|
472 |
* which the child will be inserted. |
|
|
473 |
*/ |
|
|
474 |
_defAddChildFn: function (event) { |
|
|
475 |
|
|
|
476 |
var child = event.child, |
|
|
477 |
index = event.index, |
|
|
478 |
children = this._items; |
|
|
479 |
|
|
|
480 |
if (child.get("parent")) { |
|
|
481 |
child.remove(); |
|
|
482 |
} |
|
|
483 |
|
|
|
484 |
if (Lang.isNumber(index)) { |
|
|
485 |
children.splice(index, 0, child); |
|
|
486 |
} |
|
|
487 |
else { |
|
|
488 |
children.push(child); |
|
|
489 |
} |
|
|
490 |
|
|
|
491 |
child._set("parent", this); |
|
|
492 |
child.addTarget(this); |
|
|
493 |
|
|
|
494 |
// Update index in case it got normalized after addition |
|
|
495 |
// (e.g. user passed in 10, and there are only 3 items, the actual index would be 3. We don't want to pass 10 around in the event facade). |
|
|
496 |
event.index = child.get("index"); |
|
|
497 |
|
|
|
498 |
// TO DO: Remove in favor of using event bubbling |
|
|
499 |
child.after("selectedChange", Y.bind(this._updateSelection, this)); |
|
|
500 |
}, |
|
|
501 |
|
|
|
502 |
|
|
|
503 |
/** |
|
|
504 |
* Default removeChild handler |
|
|
505 |
* |
|
|
506 |
* @method _defRemoveChildFn |
|
|
507 |
* @protected |
|
|
508 |
* @param event {EventFacade} The Event object |
|
|
509 |
* @param child {Widget} The Widget instance to be removed. |
|
|
510 |
* @param index {Number} Number representing the index of the Widget to |
|
|
511 |
* be removed. |
|
|
512 |
*/ |
|
|
513 |
_defRemoveChildFn: function (event) { |
|
|
514 |
|
|
|
515 |
var child = event.child, |
|
|
516 |
index = event.index, |
|
|
517 |
children = this._items; |
|
|
518 |
|
|
|
519 |
if (child.get("focused")) { |
|
|
520 |
child.blur(); // focused is readOnly, so use the public i/f to unset it |
|
|
521 |
} |
|
|
522 |
|
|
|
523 |
if (child.get("selected")) { |
|
|
524 |
child.set("selected", 0); |
|
|
525 |
} |
|
|
526 |
|
|
|
527 |
children.splice(index, 1); |
|
|
528 |
|
|
|
529 |
child.removeTarget(this); |
|
|
530 |
child._oldParent = child.get("parent"); |
|
|
531 |
child._set("parent", null); |
|
|
532 |
}, |
|
|
533 |
|
|
|
534 |
/** |
|
|
535 |
* @method _add |
|
|
536 |
* @protected |
|
|
537 |
* @param child {Widget|Object} The Widget instance, or configuration |
|
|
538 |
* object for the Widget to be added as a child. |
|
|
539 |
* @param child {Array} Array of Widget instances, or configuration |
|
|
540 |
* objects for the Widgets to be added as a children. |
|
|
541 |
* @param index {Number} (Optional.) Number representing the position at |
|
|
542 |
* which the child should be inserted. |
|
|
543 |
* @description Adds a Widget as a child. If the specified Widget already |
|
|
544 |
* has a parent it will be removed from its current parent before |
|
|
545 |
* being added as a child. |
|
|
546 |
* @return {Widget|Array} Successfully added Widget or Array containing the |
|
|
547 |
* successfully added Widget instance(s). If no children where added, will |
|
|
548 |
* will return undefined. |
|
|
549 |
*/ |
|
|
550 |
_add: function (child, index) { |
|
|
551 |
|
|
|
552 |
var children, |
|
|
553 |
oChild, |
|
|
554 |
returnVal; |
|
|
555 |
|
|
|
556 |
|
|
|
557 |
if (Lang.isArray(child)) { |
|
|
558 |
|
|
|
559 |
children = []; |
|
|
560 |
|
|
|
561 |
Y.each(child, function (v, k) { |
|
|
562 |
|
|
|
563 |
oChild = this._add(v, (index + k)); |
|
|
564 |
|
|
|
565 |
if (oChild) { |
|
|
566 |
children.push(oChild); |
|
|
567 |
} |
|
|
568 |
|
|
|
569 |
}, this); |
|
|
570 |
|
|
|
571 |
|
|
|
572 |
if (children.length > 0) { |
|
|
573 |
returnVal = children; |
|
|
574 |
} |
|
|
575 |
|
|
|
576 |
} |
|
|
577 |
else { |
|
|
578 |
|
|
|
579 |
if (Y.instanceOf(child, Y.Widget)) { |
|
|
580 |
oChild = child; |
|
|
581 |
} |
|
|
582 |
else { |
|
|
583 |
oChild = this._createChild(child); |
|
|
584 |
} |
|
|
585 |
|
|
|
586 |
if (oChild && this.fire("addChild", { child: oChild, index: index })) { |
|
|
587 |
returnVal = oChild; |
|
|
588 |
} |
|
|
589 |
|
|
|
590 |
} |
|
|
591 |
|
|
|
592 |
return returnVal; |
|
|
593 |
|
|
|
594 |
}, |
|
|
595 |
|
|
|
596 |
|
|
|
597 |
/** |
|
|
598 |
* @method add |
|
|
599 |
* @param child {Widget|Object} The Widget instance, or configuration |
|
|
600 |
* object for the Widget to be added as a child. The configuration object |
|
|
601 |
* for the child can include a <code>childType</code> property, which is either |
|
|
602 |
* a constructor function or a string which names a constructor function on the |
|
|
603 |
* Y instance (e.g. "Tab" would refer to Y.Tab) (<code>childType</code> used to be |
|
|
604 |
* named <code>type</code>, support for which has been deprecated, but is still |
|
|
605 |
* maintained for backward compatibility. <code>childType</code> takes precedence |
|
|
606 |
* over <code>type</code> if both are defined. |
|
|
607 |
* @param child {Array} Array of Widget instances, or configuration |
|
|
608 |
* objects for the Widgets to be added as a children. |
|
|
609 |
* @param index {Number} (Optional.) Number representing the position at |
|
|
610 |
* which the child should be inserted. |
|
|
611 |
* @description Adds a Widget as a child. If the specified Widget already |
|
|
612 |
* has a parent it will be removed from its current parent before |
|
|
613 |
* being added as a child. |
|
|
614 |
* @return {ArrayList} Y.ArrayList containing the successfully added |
|
|
615 |
* Widget instance(s). If no children where added, will return an empty |
|
|
616 |
* Y.ArrayList instance. |
|
|
617 |
*/ |
|
|
618 |
add: function () { |
|
|
619 |
|
|
|
620 |
var added = this._add.apply(this, arguments), |
|
|
621 |
children = added ? (Lang.isArray(added) ? added : [added]) : []; |
|
|
622 |
|
|
|
623 |
return (new Y.ArrayList(children)); |
|
|
624 |
|
|
|
625 |
}, |
|
|
626 |
|
|
|
627 |
|
|
|
628 |
/** |
|
|
629 |
* @method remove |
|
|
630 |
* @param index {Number} (Optional.) Number representing the index of the |
|
|
631 |
* child to be removed. |
|
|
632 |
* @description Removes the Widget from its parent. Optionally, can remove |
|
|
633 |
* a child by specifying its index. |
|
|
634 |
* @return {Widget} Widget instance that was successfully removed, otherwise |
|
|
635 |
* undefined. |
|
|
636 |
*/ |
|
|
637 |
remove: function (index) { |
|
|
638 |
|
|
|
639 |
var child = this._items[index], |
|
|
640 |
returnVal; |
|
|
641 |
|
|
|
642 |
if (child && this.fire("removeChild", { child: child, index: index })) { |
|
|
643 |
returnVal = child; |
|
|
644 |
} |
|
|
645 |
|
|
|
646 |
return returnVal; |
|
|
647 |
|
|
|
648 |
}, |
|
|
649 |
|
|
|
650 |
|
|
|
651 |
/** |
|
|
652 |
* @method removeAll |
|
|
653 |
* @description Removes all of the children from the Widget. |
|
|
654 |
* @return {ArrayList} Y.ArrayList instance containing Widgets that were |
|
|
655 |
* successfully removed. If no children where removed, will return an empty |
|
|
656 |
* Y.ArrayList instance. |
|
|
657 |
*/ |
|
|
658 |
removeAll: function () { |
|
|
659 |
|
|
|
660 |
var removed = [], |
|
|
661 |
child; |
|
|
662 |
|
|
|
663 |
Y.each(this._items.concat(), function () { |
|
|
664 |
|
|
|
665 |
child = this.remove(0); |
|
|
666 |
|
|
|
667 |
if (child) { |
|
|
668 |
removed.push(child); |
|
|
669 |
} |
|
|
670 |
|
|
|
671 |
}, this); |
|
|
672 |
|
|
|
673 |
return (new Y.ArrayList(removed)); |
|
|
674 |
|
|
|
675 |
}, |
|
|
676 |
|
|
|
677 |
/** |
|
|
678 |
* Selects the child at the given index (zero-based). |
|
|
679 |
* |
|
|
680 |
* @method selectChild |
|
|
681 |
* @param {Number} i the index of the child to be selected |
|
|
682 |
*/ |
|
|
683 |
selectChild: function(i) { |
|
|
684 |
this.item(i).set('selected', 1); |
|
|
685 |
}, |
|
|
686 |
|
|
|
687 |
/** |
|
|
688 |
* Selects all children. |
|
|
689 |
* |
|
|
690 |
* @method selectAll |
|
|
691 |
*/ |
|
|
692 |
selectAll: function () { |
|
|
693 |
this.set("selected", 1); |
|
|
694 |
}, |
|
|
695 |
|
|
|
696 |
/** |
|
|
697 |
* Deselects all children. |
|
|
698 |
* |
|
|
699 |
* @method deselectAll |
|
|
700 |
*/ |
|
|
701 |
deselectAll: function () { |
|
|
702 |
this.set("selected", 0); |
|
|
703 |
}, |
|
|
704 |
|
|
|
705 |
/** |
|
|
706 |
* Updates the UI in response to a child being added. |
|
|
707 |
* |
|
|
708 |
* @method _uiAddChild |
|
|
709 |
* @protected |
|
|
710 |
* @param child {Widget} The child Widget instance to render. |
|
|
711 |
* @param parentNode {Object} The Node under which the |
|
|
712 |
* child Widget is to be rendered. |
|
|
713 |
*/ |
|
|
714 |
_uiAddChild: function (child, parentNode) { |
|
|
715 |
|
|
|
716 |
child.render(parentNode); |
|
|
717 |
|
|
|
718 |
// TODO: Ideally this should be in Child's render UI. |
|
|
719 |
|
|
|
720 |
var childBB = child.get("boundingBox"), |
|
|
721 |
siblingBB, |
|
|
722 |
nextSibling = child.next(false), |
|
|
723 |
prevSibling; |
|
|
724 |
|
|
|
725 |
// Insert or Append to last child. |
|
|
726 |
|
|
|
727 |
// Avoiding index, and using the current sibling |
|
|
728 |
// state (which should be accurate), means we don't have |
|
|
729 |
// to worry about decorator elements which may be added |
|
|
730 |
// to the _childContainer node. |
|
|
731 |
|
|
|
732 |
if (nextSibling && nextSibling.get(RENDERED)) { |
|
|
733 |
|
|
|
734 |
siblingBB = nextSibling.get(BOUNDING_BOX); |
|
|
735 |
siblingBB.insert(childBB, "before"); |
|
|
736 |
|
|
|
737 |
} else { |
|
|
738 |
|
|
|
739 |
prevSibling = child.previous(false); |
|
|
740 |
|
|
|
741 |
if (prevSibling && prevSibling.get(RENDERED)) { |
|
|
742 |
|
|
|
743 |
siblingBB = prevSibling.get(BOUNDING_BOX); |
|
|
744 |
siblingBB.insert(childBB, "after"); |
|
|
745 |
|
|
|
746 |
} else if (!parentNode.contains(childBB)) { |
|
|
747 |
|
|
|
748 |
// Based on pull request from andreas-karlsson |
|
|
749 |
// https://github.com/yui/yui3/pull/25#issuecomment-2103536 |
|
|
750 |
|
|
|
751 |
// Account for case where a child was rendered independently of the |
|
|
752 |
// parent-child framework, to a node outside of the parentNode, |
|
|
753 |
// and there are no siblings. |
|
|
754 |
|
|
|
755 |
parentNode.appendChild(childBB); |
|
|
756 |
} |
|
|
757 |
} |
|
|
758 |
|
|
|
759 |
}, |
|
|
760 |
|
|
|
761 |
/** |
|
|
762 |
* Updates the UI in response to a child being removed. |
|
|
763 |
* |
|
|
764 |
* @method _uiRemoveChild |
|
|
765 |
* @protected |
|
|
766 |
* @param child {Widget} The child Widget instance to render. |
|
|
767 |
*/ |
|
|
768 |
_uiRemoveChild: function (child) { |
|
|
769 |
child.get("boundingBox").remove(); |
|
|
770 |
}, |
|
|
771 |
|
|
|
772 |
_afterAddChild: function (event) { |
|
|
773 |
var child = event.child; |
|
|
774 |
|
|
|
775 |
if (child.get("parent") == this) { |
|
|
776 |
this._uiAddChild(child, this._childrenContainer); |
|
|
777 |
} |
|
|
778 |
}, |
|
|
779 |
|
|
|
780 |
_afterRemoveChild: function (event) { |
|
|
781 |
var child = event.child; |
|
|
782 |
|
|
|
783 |
if (child._oldParent == this) { |
|
|
784 |
this._uiRemoveChild(child); |
|
|
785 |
} |
|
|
786 |
}, |
|
|
787 |
|
|
|
788 |
/** |
|
|
789 |
* Sets up DOM and CustomEvent listeners for the parent widget. |
|
|
790 |
* <p> |
|
|
791 |
* This method in invoked after bindUI is invoked for the Widget class |
|
|
792 |
* using YUI's aop infrastructure. |
|
|
793 |
* </p> |
|
|
794 |
* |
|
|
795 |
* @method _bindUIParent |
|
|
796 |
* @protected |
|
|
797 |
*/ |
|
|
798 |
_bindUIParent: function () { |
|
|
799 |
this.after("addChild", this._afterAddChild); |
|
|
800 |
this.after("removeChild", this._afterRemoveChild); |
|
|
801 |
}, |
|
|
802 |
|
|
|
803 |
/** |
|
|
804 |
* Renders all child Widgets for the parent. |
|
|
805 |
* <p> |
|
|
806 |
* This method in invoked after renderUI is invoked for the Widget class |
|
|
807 |
* using YUI's aop infrastructure. |
|
|
808 |
* </p> |
|
|
809 |
* @method _renderChildren |
|
|
810 |
* @protected |
|
|
811 |
*/ |
|
|
812 |
_renderChildren: function () { |
|
|
813 |
|
|
|
814 |
/** |
|
|
815 |
* <p>By default WidgetParent will render it's children to the parent's content box.</p> |
|
|
816 |
* |
|
|
817 |
* <p>If the children need to be rendered somewhere else, the _childrenContainer property |
|
|
818 |
* can be set to the Node which the children should be rendered to. This property should be |
|
|
819 |
* set before the _renderChildren method is invoked, ideally in your renderUI method, |
|
|
820 |
* as soon as you create the element to be rendered to.</p> |
|
|
821 |
* |
|
|
822 |
* @protected |
|
|
823 |
* @property _childrenContainer |
|
|
824 |
* @value The content box |
|
|
825 |
* @type Node |
|
|
826 |
*/ |
|
|
827 |
var renderTo = this._childrenContainer || this.get("contentBox"); |
|
|
828 |
|
|
|
829 |
this._childrenContainer = renderTo; |
|
|
830 |
|
|
|
831 |
this.each(function (child) { |
|
|
832 |
child.render(renderTo); |
|
|
833 |
}); |
|
|
834 |
}, |
|
|
835 |
|
|
|
836 |
/** |
|
|
837 |
* Destroys all child Widgets for the parent. |
|
|
838 |
* <p> |
|
|
839 |
* This method is invoked before the destructor is invoked for the Widget |
|
|
840 |
* class using YUI's aop infrastructure. |
|
|
841 |
* </p> |
|
|
842 |
* @method _destroyChildren |
|
|
843 |
* @protected |
|
|
844 |
*/ |
|
|
845 |
_destroyChildren: function () { |
|
|
846 |
|
|
|
847 |
// Detach the handler responsible for removing children in |
|
|
848 |
// response to destroying them since: |
|
|
849 |
// 1) It is unnecessary/inefficient at this point since we are doing |
|
|
850 |
// a batch destroy of all children. |
|
|
851 |
// 2) Removing each child will affect our ability to iterate the |
|
|
852 |
// children since the size of _items will be changing as we |
|
|
853 |
// iterate. |
|
|
854 |
this._hDestroyChild.detach(); |
|
|
855 |
|
|
|
856 |
// Need to clone the _items array since |
|
|
857 |
this.each(function (child) { |
|
|
858 |
child.destroy(); |
|
|
859 |
}); |
|
|
860 |
} |
|
|
861 |
|
|
|
862 |
}; |
|
|
863 |
|
|
|
864 |
Y.augment(Parent, Y.ArrayList); |
|
|
865 |
|
|
|
866 |
Y.WidgetParent = Parent; |
|
|
867 |
|
|
|
868 |
|
|
|
869 |
}, '@VERSION@', {"requires": ["arraylist", "base-build", "widget"]}); |