|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('tree-openable', function (Y, NAME) { |
|
9 |
|
10 /*jshint expr:true, onevar:false */ |
|
11 |
|
12 /** |
|
13 Extension for `Tree` that adds the concept of open/closed state for nodes. |
|
14 |
|
15 @module tree |
|
16 @submodule tree-openable |
|
17 @main tree-openable |
|
18 **/ |
|
19 |
|
20 /** |
|
21 Extension for `Tree` that adds the concept of open/closed state for nodes. |
|
22 |
|
23 @class Tree.Openable |
|
24 @constructor |
|
25 @extensionfor Tree |
|
26 **/ |
|
27 |
|
28 /** |
|
29 Fired when a node is closed. |
|
30 |
|
31 @event close |
|
32 @param {Tree.Node} node Node being closed. |
|
33 @param {String} src Source of the event. |
|
34 @preventable _defCloseFn |
|
35 **/ |
|
36 var EVT_CLOSE = 'close'; |
|
37 |
|
38 /** |
|
39 Fired when a node is opened. |
|
40 |
|
41 @event open |
|
42 @param {Tree.Node} node Node being opened. |
|
43 @param {String} src Source of the event. |
|
44 @preventable _defOpenFn |
|
45 **/ |
|
46 var EVT_OPEN = 'open'; |
|
47 |
|
48 function Openable() {} |
|
49 |
|
50 Openable.prototype = { |
|
51 // -- Lifecycle ------------------------------------------------------------ |
|
52 initializer: function () { |
|
53 this.nodeExtensions = this.nodeExtensions.concat(Y.Tree.Node.Openable); |
|
54 }, |
|
55 |
|
56 // -- Public Methods ------------------------------------------------------- |
|
57 |
|
58 /** |
|
59 Closes the specified node if it isn't already closed. |
|
60 |
|
61 @method closeNode |
|
62 @param {Object} [options] Options. |
|
63 @param {Boolean} [options.silent=false] If `true`, the `close` event |
|
64 will be suppressed. |
|
65 @param {String} [options.src] Source of the change, to be passed along |
|
66 to the event facade of the resulting event. This can be used to |
|
67 distinguish between changes triggered by a user and changes |
|
68 triggered programmatically, for example. |
|
69 @chainable |
|
70 **/ |
|
71 closeNode: function (node, options) { |
|
72 if (node.canHaveChildren && node.isOpen()) { |
|
73 this._fireTreeEvent(EVT_CLOSE, { |
|
74 node: node, |
|
75 src : options && options.src |
|
76 }, { |
|
77 defaultFn: this._defCloseFn, |
|
78 silent : options && options.silent |
|
79 }); |
|
80 } |
|
81 |
|
82 return this; |
|
83 }, |
|
84 |
|
85 /** |
|
86 Opens the specified node if it isn't already open. |
|
87 |
|
88 @method openNode |
|
89 @param {Object} [options] Options. |
|
90 @param {Boolean} [options.silent=false] If `true`, the `open` event |
|
91 will be suppressed. |
|
92 @param {String} [options.src] Source of the change, to be passed along |
|
93 to the event facade of the resulting event. This can be used to |
|
94 distinguish between changes triggered by a user and changes |
|
95 triggered programmatically, for example. |
|
96 @chainable |
|
97 **/ |
|
98 openNode: function (node, options) { |
|
99 if (node.canHaveChildren && !node.isOpen()) { |
|
100 this._fireTreeEvent(EVT_OPEN, { |
|
101 node: node, |
|
102 src : options && options.src |
|
103 }, { |
|
104 defaultFn: this._defOpenFn, |
|
105 silent : options && options.silent |
|
106 }); |
|
107 } |
|
108 |
|
109 return this; |
|
110 }, |
|
111 |
|
112 /** |
|
113 Toggles the open/closed state of the specified node, closing it if it's |
|
114 currently open or opening it if it's currently closed. |
|
115 |
|
116 @method toggleOpenNode |
|
117 @param {Tree.Node} node Node to toggle. |
|
118 @param {Object} [options] Options. |
|
119 @param {Boolean} [options.silent=false] If `true`, events will be |
|
120 suppressed. |
|
121 @param {String} [options.src] Source of the change, to be passed along |
|
122 to the event facade of the resulting event. This can be used to |
|
123 distinguish between changes triggered by a user and changes |
|
124 triggered programmatically, for example. |
|
125 @chainable |
|
126 **/ |
|
127 toggleOpenNode: function (node, options) { |
|
128 return node.isOpen() ? this.closeNode(node, options) : |
|
129 this.openNode(node, options); |
|
130 }, |
|
131 |
|
132 // -- Default Event Handlers ----------------------------------------------- |
|
133 |
|
134 /** |
|
135 Default handler for the `close` event. |
|
136 |
|
137 @method _defCloseFn |
|
138 @param {EventFacade} e |
|
139 @protected |
|
140 **/ |
|
141 _defCloseFn: function (e) { |
|
142 delete e.node.state.open; |
|
143 }, |
|
144 |
|
145 /** |
|
146 Default handler for the `open` event. |
|
147 |
|
148 @method _defOpenFn |
|
149 @param {EventFacade} e |
|
150 @protected |
|
151 **/ |
|
152 _defOpenFn: function (e) { |
|
153 e.node.state.open = true; |
|
154 } |
|
155 }; |
|
156 |
|
157 Y.Tree.Openable = Openable; |
|
158 /** |
|
159 @module tree |
|
160 @submodule tree-openable |
|
161 **/ |
|
162 |
|
163 /** |
|
164 `Tree.Node` extension that adds methods useful for nodes in trees that use the |
|
165 `Tree.Openable` extension. |
|
166 |
|
167 @class Tree.Node.Openable |
|
168 @constructor |
|
169 @extensionfor Tree.Node |
|
170 **/ |
|
171 |
|
172 function NodeOpenable() {} |
|
173 |
|
174 NodeOpenable.prototype = { |
|
175 /** |
|
176 Closes this node if it's currently open. |
|
177 |
|
178 @method close |
|
179 @param {Object} [options] Options. |
|
180 @param {Boolean} [options.silent=false] If `true`, the `close` event |
|
181 will be suppressed. |
|
182 @param {String} [options.src] Source of the change, to be passed along |
|
183 to the event facade of the resulting event. This can be used to |
|
184 distinguish between changes triggered by a user and changes |
|
185 triggered programmatically, for example. |
|
186 @chainable |
|
187 **/ |
|
188 close: function (options) { |
|
189 this.tree.closeNode(this, options); |
|
190 return this; |
|
191 }, |
|
192 |
|
193 /** |
|
194 Returns `true` if this node is currently open. |
|
195 |
|
196 Note: the root node of a tree is always considered to be open. |
|
197 |
|
198 @method isOpen |
|
199 @return {Boolean} `true` if this node is currently open, `false` otherwise. |
|
200 **/ |
|
201 isOpen: function () { |
|
202 return !!this.state.open || this.isRoot(); |
|
203 }, |
|
204 |
|
205 /** |
|
206 Opens this node if it's currently closed. |
|
207 |
|
208 @method open |
|
209 @param {Object} [options] Options. |
|
210 @param {Boolean} [options.silent=false] If `true`, the `open` event |
|
211 will be suppressed. |
|
212 @param {String} [options.src] Source of the change, to be passed along |
|
213 to the event facade of the resulting event. This can be used to |
|
214 distinguish between changes triggered by a user and changes |
|
215 triggered programmatically, for example. |
|
216 @chainable |
|
217 **/ |
|
218 open: function (options) { |
|
219 this.tree.openNode(this, options); |
|
220 return this; |
|
221 }, |
|
222 |
|
223 /** |
|
224 Toggles the open/closed state of this node, closing it if it's currently |
|
225 open or opening it if it's currently closed. |
|
226 |
|
227 @method toggleOpen |
|
228 @param {Object} [options] Options. |
|
229 @param {Boolean} [options.silent=false] If `true`, events will be |
|
230 suppressed. |
|
231 @param {String} [options.src] Source of the change, to be passed along |
|
232 to the event facade of the resulting event. This can be used to |
|
233 distinguish between changes triggered by a user and changes |
|
234 triggered programmatically, for example. |
|
235 @chainable |
|
236 **/ |
|
237 toggleOpen: function (options) { |
|
238 this.tree.toggleOpenNode(this, options); |
|
239 return this; |
|
240 } |
|
241 }; |
|
242 |
|
243 Y.Tree.Node.Openable = NodeOpenable; |
|
244 |
|
245 |
|
246 }, '3.10.3', {"requires": ["tree"]}); |