3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('widget-child', function(Y) {
10
* Extension enabling a Widget to be a child of another Widget.
12
* @module widget-child
18
* Widget extension providing functionality enabling a Widget to be a
19
* child of another Widget.
22
* @param {Object} config User configuration object.
26
// Widget method overlap
27
Y.after(this._syncUIChild, this, "syncUI");
28
Y.after(this._bindUIChild, this, "bindUI");
39
* @description Number indicating if the Widget is selected. Possible
42
* <dt>0</dt> <dd>(Default) Not selected</dd>
43
* <dt>1</dt> <dd>Fully selected</dd>
44
* <dt>2</dt> <dd>Partially selected</dd>
49
validator: Lang.isNumber
58
* @description Number representing the Widget's ordinal position in its
65
var parent = this.get("parent"),
69
index = parent.indexOf(this);
83
* @description Retrieves the parent of the Widget in the object hierarchy.
96
* @description Number representing the depth of this Widget relative to
97
* the root Widget in the object heirarchy.
101
getter: function () {
103
var parent = this.get("parent"),
104
root = this.get("root"),
111
if (parent == root) {
115
parent = parent.get("parent");
129
* @description Returns the root Widget in the object hierarchy. If the
130
* ROOT_TYPE property is set, the search for the root Widget will be
131
* constrained to parent Widgets of the specified type.
135
getter: function () {
137
var getParent = function (child) {
139
var parent = child.get("parent"),
140
FnRootType = child.ROOT_TYPE,
144
criteria = (parent && Y.instanceOf(parent, FnRootType));
147
return (criteria ? getParent(parent) : child);
151
return getParent(this);
161
* Constructor reference used to determine the root of a Widget-based
164
* Currently used to control the behavior of the <code>root</code>
165
* attribute so that recursing up the object heirarchy can be constrained
166
* to a specific type of Widget. Widget authors should set this property
167
* to the constructor function for a given Widget implementation.
170
* @property ROOT_TYPE
176
* Returns the node on which to bind delegate listeners.
178
* Override of Widget's implementation of _getUIEventNode() to ensure that
179
* all event listeners are bound to the Widget's topmost DOM element.
180
* This ensures that the firing of each type of Widget UI event (click,
181
* mousedown, etc.) is facilitated by a single, top-level, delegated DOM
184
* @method _getUIEventNode
188
_getUIEventNode: function () {
189
var root = this.get("root"),
193
returnVal = root.get("boundingBox");
201
* @description Returns the Widget's next sibling.
202
* @param {Boolean} circular Boolean indicating if the parent's first child
203
* should be returned if the child has no next sibling.
204
* @return {Widget} Widget instance.
206
next: function (circular) {
208
var parent = this.get("parent"),
212
sibling = parent.item((this.get("index")+1));
215
if (!sibling && circular) {
216
sibling = parent.item(0);
226
* @description Returns the Widget's previous sibling.
227
* @param {Boolean} circular Boolean indicating if the parent's last child
228
* should be returned if the child has no previous sibling.
229
* @return {Widget} Widget instance.
231
previous: function (circular) {
233
var parent = this.get("parent"),
234
index = this.get("index"),
237
if (parent && index > 0) {
238
sibling = parent.item([(index-1)]);
241
if (!sibling && circular) {
242
sibling = parent.item((parent.size() - 1));
250
// Override of Y.WidgetParent.remove()
251
// Sugar implementation allowing a child to remove itself from its parent.
252
remove: function (index) {
257
if (Lang.isNumber(index)) {
258
removed = Y.WidgetParent.prototype.remove.apply(this, arguments);
262
parent = this.get("parent");
265
removed = parent.remove(this.get("index"));
277
* @description Determines if the Widget is the root Widget in the
279
* @return {Boolean} Boolean indicating if Widget is the root Widget in the
282
isRoot: function () {
283
return (this == this.get("root"));
289
* @description Returns the Widget instance at the specified depth.
290
* @param {number} depth Number representing the depth of the ancestor.
291
* @return {Widget} Widget instance.
293
ancestor: function (depth) {
295
var root = this.get("root"),
298
if (this.get("depth") > depth) {
300
parent = this.get("parent");
302
while (parent != root && parent.get("depth") > depth) {
303
parent = parent.get("parent");
314
* Updates the UI to reflect the <code>selected</code> attribute value.
316
* @method _uiSetChildSelected
318
* @param {number} selected The selected value to be reflected in the UI.
320
_uiSetChildSelected: function (selected) {
322
var box = this.get("boundingBox"),
323
sClassName = this.getClassName("selected");
325
if (selected === 0) {
326
box.removeClass(sClassName);
329
box.addClass(sClassName);
336
* Default attribute change listener for the <code>selected</code>
337
* attribute, responsible for updating the UI, in response to
340
* @method _afterChildSelectedChange
342
* @param {EventFacade} event The event facade for the attribute change.
344
_afterChildSelectedChange: function (event) {
345
this._uiSetChildSelected(event.newVal);
350
* Synchronizes the UI to match the WidgetChild state.
352
* This method is invoked after bindUI is invoked for the Widget class
353
* using YUI's aop infrastructure.
356
* @method _syncUIChild
359
_syncUIChild: function () {
360
this._uiSetChildSelected(this.get("selected"));
365
* Binds event listeners responsible for updating the UI state in response
366
* to WidgetChild related state changes.
368
* This method is invoked after bindUI is invoked for the Widget class
369
* using YUI's aop infrastructure.
371
* @method _bindUIChild
374
_bindUIChild: function () {
375
this.after("selectedChange", this._afterChildSelectedChange);
380
Y.WidgetChild = Child;
383
}, '3.4.1' ,{requires:['base-build', 'widget']});