2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('tree-openable', function (Y, NAME) {
10 /*jshint expr:true, onevar:false */
13 Extension for `Tree` that adds the concept of open/closed state for nodes.
16 @submodule tree-openable
21 Extension for `Tree` that adds the concept of open/closed state for nodes.
29 Fired when a node is closed.
32 @param {Tree.Node} node Node being closed.
33 @param {String} src Source of the event.
34 @preventable _defCloseFn
36 var EVT_CLOSE = 'close';
39 Fired when a node is opened.
42 @param {Tree.Node} node Node being opened.
43 @param {String} src Source of the event.
44 @preventable _defOpenFn
46 var EVT_OPEN = 'open';
48 function Openable() {}
50 Openable.prototype = {
51 // -- Lifecycle ------------------------------------------------------------
52 initializer: function () {
53 this.nodeExtensions = this.nodeExtensions.concat(Y.Tree.Node.Openable);
56 // -- Public Methods -------------------------------------------------------
59 Closes the specified node if it isn't already closed.
62 @param {Tree.Node} node Node to close.
63 @param {Object} [options] Options.
64 @param {Boolean} [options.silent=false] If `true`, the `close` event
66 @param {String} [options.src] Source of the change, to be passed along
67 to the event facade of the resulting event. This can be used to
68 distinguish between changes triggered by a user and changes
69 triggered programmatically, for example.
72 closeNode: function (node, options) {
73 if (node.canHaveChildren && node.isOpen()) {
74 this._fireTreeEvent(EVT_CLOSE, {
76 src : options && options.src
78 defaultFn: this._defCloseFn,
79 silent : options && options.silent
87 Opens the specified node if it isn't already open.
90 @param {Tree.Node} node Node to open.
91 @param {Object} [options] Options.
92 @param {Boolean} [options.silent=false] If `true`, the `open` event
94 @param {String} [options.src] Source of the change, to be passed along
95 to the event facade of the resulting event. This can be used to
96 distinguish between changes triggered by a user and changes
97 triggered programmatically, for example.
100 openNode: function (node, options) {
101 if (node.canHaveChildren && !node.isOpen()) {
102 this._fireTreeEvent(EVT_OPEN, {
104 src : options && options.src
106 defaultFn: this._defOpenFn,
107 silent : options && options.silent
115 Toggles the open/closed state of the specified node, closing it if it's
116 currently open or opening it if it's currently closed.
118 @method toggleOpenNode
119 @param {Tree.Node} node Node to toggle.
120 @param {Object} [options] Options.
121 @param {Boolean} [options.silent=false] If `true`, events will be
123 @param {String} [options.src] Source of the change, to be passed along
124 to the event facade of the resulting event. This can be used to
125 distinguish between changes triggered by a user and changes
126 triggered programmatically, for example.
129 toggleOpenNode: function (node, options) {
130 return node.isOpen() ? this.closeNode(node, options) :
131 this.openNode(node, options);
134 // -- Default Event Handlers -----------------------------------------------
137 Default handler for the `close` event.
140 @param {EventFacade} e
143 _defCloseFn: function (e) {
144 delete e.node.state.open;
148 Default handler for the `open` event.
151 @param {EventFacade} e
154 _defOpenFn: function (e) {
155 e.node.state.open = true;
159 Y.Tree.Openable = Openable;
162 @submodule tree-openable
166 `Tree.Node` extension that adds methods useful for nodes in trees that use the
167 `Tree.Openable` extension.
169 @class Tree.Node.Openable
171 @extensionfor Tree.Node
174 function NodeOpenable() {}
176 NodeOpenable.prototype = {
178 Closes this node if it's currently open.
181 @param {Object} [options] Options.
182 @param {Boolean} [options.silent=false] If `true`, the `close` event
184 @param {String} [options.src] Source of the change, to be passed along
185 to the event facade of the resulting event. This can be used to
186 distinguish between changes triggered by a user and changes
187 triggered programmatically, for example.
190 close: function (options) {
191 this.tree.closeNode(this, options);
196 Returns `true` if this node is currently open.
198 Note: the root node of a tree is always considered to be open.
201 @return {Boolean} `true` if this node is currently open, `false` otherwise.
203 isOpen: function () {
204 return !!this.state.open || this.isRoot();
208 Opens this node if it's currently closed.
211 @param {Object} [options] Options.
212 @param {Boolean} [options.silent=false] If `true`, the `open` event
214 @param {String} [options.src] Source of the change, to be passed along
215 to the event facade of the resulting event. This can be used to
216 distinguish between changes triggered by a user and changes
217 triggered programmatically, for example.
220 open: function (options) {
221 this.tree.openNode(this, options);
226 Toggles the open/closed state of this node, closing it if it's currently
227 open or opening it if it's currently closed.
230 @param {Object} [options] Options.
231 @param {Boolean} [options.silent=false] If `true`, events will be
233 @param {String} [options.src] Source of the change, to be passed along
234 to the event facade of the resulting event. This can be used to
235 distinguish between changes triggered by a user and changes
236 triggered programmatically, for example.
239 toggleOpen: function (options) {
240 this.tree.toggleOpenNode(this, options);
245 Y.Tree.Node.Openable = NodeOpenable;
248 }, '3.13.0', {"requires": ["tree"]});