| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /*
- Copyright (c) 2010, Yahoo! Inc. All rights reserved.
- Code licensed under the BSD License:
- http://developer.yahoo.com/yui/license.html
- version: 3.4.0
- build: nightly
- */
- YUI.add('widget-child', function(Y) {
-
- /**
- * Extension enabling a Widget to be a child of another Widget.
- *
- * @module widget-child
- */
-
- var Lang = Y.Lang;
-
- /**
- * Widget extension providing functionality enabling a Widget to be a
- * child of another Widget.
- *
- * @class WidgetChild
- * @param {Object} config User configuration object.
- */
- function Child() {
-
- // Widget method overlap
- Y.after(this._syncUIChild, this, "syncUI");
- Y.after(this._bindUIChild, this, "bindUI");
-
- }
-
- Child.ATTRS = {
-
- /**
- * @attribute selected
- * @type Number
- * @default 0
- *
- * @description Number indicating if the Widget is selected. Possible
- * values are:
- * <dl>
- * <dt>0</dt> <dd>(Default) Not selected</dd>
- * <dt>1</dt> <dd>Fully selected</dd>
- * <dt>2</dt> <dd>Partially selected</dd>
- * </dl>
- */
- selected: {
- value: 0,
- validator: Lang.isNumber
- },
-
-
- /**
- * @attribute index
- * @type Number
- * @readOnly
- *
- * @description Number representing the Widget's ordinal position in its
- * parent Widget.
- */
- index: {
- readOnly: true,
- getter: function () {
-
- var parent = this.get("parent"),
- index = -1;
-
- if (parent) {
- index = parent.indexOf(this);
- }
-
- return index;
-
- }
- },
-
-
- /**
- * @attribute parent
- * @type Widget
- * @readOnly
- *
- * @description Retrieves the parent of the Widget in the object hierarchy.
- */
- parent: {
- readOnly: true
- },
-
-
- /**
- * @attribute depth
- * @type Number
- * @default -1
- * @readOnly
- *
- * @description Number representing the depth of this Widget relative to
- * the root Widget in the object heirarchy.
- */
- depth: {
- readOnly: true,
- getter: function () {
-
- var parent = this.get("parent"),
- root = this.get("root"),
- depth = -1;
-
- while (parent) {
-
- depth = (depth + 1);
-
- if (parent == root) {
- break;
- }
-
- parent = parent.get("parent");
-
- }
-
- return depth;
-
- }
- },
-
- /**
- * @attribute root
- * @type Widget
- * @readOnly
- *
- * @description Returns the root Widget in the object hierarchy. If the
- * ROOT_TYPE property is set, the search for the root Widget will be
- * constrained to parent Widgets of the specified type.
- */
- root: {
- readOnly: true,
- getter: function () {
-
- var getParent = function (child) {
-
- var parent = child.get("parent"),
- FnRootType = child.ROOT_TYPE,
- criteria = parent;
-
- if (FnRootType) {
- criteria = (parent && Y.instanceOf(parent, FnRootType));
- }
-
- return (criteria ? getParent(parent) : child);
-
- };
-
- return getParent(this);
-
- }
- }
-
- };
-
- Child.prototype = {
-
- /**
- * Constructor reference used to determine the root of a Widget-based
- * object tree.
- * <p>
- * Currently used to control the behavior of the <code>root</code>
- * attribute so that recursing up the object heirarchy can be constrained
- * to a specific type of Widget. Widget authors should set this property
- * to the constructor function for a given Widget implementation.
- * </p>
- *
- * @property ROOT_TYPE
- * @type Object
- */
- ROOT_TYPE: null,
-
- /**
- * Returns the node on which to bind delegate listeners.
- *
- * Override of Widget's implementation of _getUIEventNode() to ensure that
- * all event listeners are bound to the Widget's topmost DOM element.
- * This ensures that the firing of each type of Widget UI event (click,
- * mousedown, etc.) is facilitated by a single, top-level, delegated DOM
- * event listener.
- *
- * @method _getUIEventNode
- * @for Widget
- * @protected
- */
- _getUIEventNode: function () {
- var root = this.get("root"),
- returnVal;
-
- if (root) {
- returnVal = root.get("boundingBox");
- }
-
- return returnVal;
- },
-
- /**
- * @method next
- * @description Returns the Widget's next sibling.
- * @param {Boolean} circular Boolean indicating if the parent's first child
- * should be returned if the child has no next sibling.
- * @return {Widget} Widget instance.
- */
- next: function (circular) {
-
- var parent = this.get("parent"),
- sibling;
-
- if (parent) {
- sibling = parent.item((this.get("index")+1));
- }
-
- if (!sibling && circular) {
- sibling = parent.item(0);
- }
-
- return sibling;
-
- },
-
-
- /**
- * @method previous
- * @description Returns the Widget's previous sibling.
- * @param {Boolean} circular Boolean indicating if the parent's last child
- * should be returned if the child has no previous sibling.
- * @return {Widget} Widget instance.
- */
- previous: function (circular) {
-
- var parent = this.get("parent"),
- index = this.get("index"),
- sibling;
-
- if (parent && index > 0) {
- sibling = parent.item([(index-1)]);
- }
-
- if (!sibling && circular) {
- sibling = parent.item((parent.size() - 1));
- }
-
- return sibling;
-
- },
-
-
- // Override of Y.WidgetParent.remove()
- // Sugar implementation allowing a child to remove itself from its parent.
- remove: function (index) {
-
- var parent,
- removed;
-
- if (Lang.isNumber(index)) {
- removed = Y.WidgetParent.prototype.remove.apply(this, arguments);
- }
- else {
-
- parent = this.get("parent");
-
- if (parent) {
- removed = parent.remove(this.get("index"));
- }
-
- }
-
- return removed;
-
- },
-
-
- /**
- * @method isRoot
- * @description Determines if the Widget is the root Widget in the
- * object hierarchy.
- * @return {Boolean} Boolean indicating if Widget is the root Widget in the
- * object hierarchy.
- */
- isRoot: function () {
- return (this == this.get("root"));
- },
-
-
- /**
- * @method ancestor
- * @description Returns the Widget instance at the specified depth.
- * @param {number} depth Number representing the depth of the ancestor.
- * @return {Widget} Widget instance.
- */
- ancestor: function (depth) {
-
- var root = this.get("root"),
- parent;
-
- if (this.get("depth") > depth) {
-
- parent = this.get("parent");
-
- while (parent != root && parent.get("depth") > depth) {
- parent = parent.get("parent");
- }
-
- }
-
- return parent;
-
- },
-
-
- /**
- * Updates the UI to reflect the <code>selected</code> attribute value.
- *
- * @method _uiSetChildSelected
- * @protected
- * @param {number} selected The selected value to be reflected in the UI.
- */
- _uiSetChildSelected: function (selected) {
-
- var box = this.get("boundingBox"),
- sClassName = this.getClassName("selected");
-
- if (selected === 0) {
- box.removeClass(sClassName);
- }
- else {
- box.addClass(sClassName);
- }
-
- },
-
-
- /**
- * Default attribute change listener for the <code>selected</code>
- * attribute, responsible for updating the UI, in response to
- * attribute changes.
- *
- * @method _afterChildSelectedChange
- * @protected
- * @param {EventFacade} event The event facade for the attribute change.
- */
- _afterChildSelectedChange: function (event) {
- this._uiSetChildSelected(event.newVal);
- },
-
-
- /**
- * Synchronizes the UI to match the WidgetChild state.
- * <p>
- * This method is invoked after bindUI is invoked for the Widget class
- * using YUI's aop infrastructure.
- * </p>
- *
- * @method _syncUIChild
- * @protected
- */
- _syncUIChild: function () {
- this._uiSetChildSelected(this.get("selected"));
- },
-
-
- /**
- * Binds event listeners responsible for updating the UI state in response
- * to WidgetChild related state changes.
- * <p>
- * This method is invoked after bindUI is invoked for the Widget class
- * using YUI's aop infrastructure.
- * </p>
- * @method _bindUIChild
- * @protected
- */
- _bindUIChild: function () {
- this.after("selectedChange", this._afterChildSelectedChange);
- }
-
- };
-
- Y.WidgetChild = Child;
-
-
- }, '3.4.0' ,{requires:['base-build', 'widget']});
|