1
YUI.add('tree-openable', function (Y, NAME) {
3
/*jshint expr:true, onevar:false */
6
Extension for `Tree` that adds the concept of open/closed state for nodes.
9
@submodule tree-openable
14
Extension for `Tree` that adds the concept of open/closed state for nodes.
22
Fired when a node is closed.
25
@param {Tree.Node} node Node being closed.
26
@param {String} src Source of the event.
27
@preventable _defCloseFn
29
var EVT_CLOSE = 'close';
32
Fired when a node is opened.
35
@param {Tree.Node} node Node being opened.
36
@param {String} src Source of the event.
37
@preventable _defOpenFn
39
var EVT_OPEN = 'open';
41
function Openable() {}
43
Openable.prototype = {
44
// -- Lifecycle ------------------------------------------------------------
45
initializer: function () {
46
this.nodeExtensions = this.nodeExtensions.concat(Y.Tree.Node.Openable);
49
// -- Public Methods -------------------------------------------------------
52
Closes the specified node if it isn't already closed.
55
@param {Tree.Node} node Node to close.
56
@param {Object} [options] Options.
57
@param {Boolean} [options.silent=false] If `true`, the `close` event
59
@param {String} [options.src] Source of the change, to be passed along
60
to the event facade of the resulting event. This can be used to
61
distinguish between changes triggered by a user and changes
62
triggered programmatically, for example.
65
closeNode: function (node, options) {
66
if (node.canHaveChildren && node.isOpen()) {
67
this._fireTreeEvent(EVT_CLOSE, {
69
src : options && options.src
71
defaultFn: this._defCloseFn,
72
silent : options && options.silent
80
Opens the specified node if it isn't already open.
83
@param {Tree.Node} node Node to open.
84
@param {Object} [options] Options.
85
@param {Boolean} [options.silent=false] If `true`, the `open` event
87
@param {String} [options.src] Source of the change, to be passed along
88
to the event facade of the resulting event. This can be used to
89
distinguish between changes triggered by a user and changes
90
triggered programmatically, for example.
93
openNode: function (node, options) {
94
if (node.canHaveChildren && !node.isOpen()) {
95
this._fireTreeEvent(EVT_OPEN, {
97
src : options && options.src
99
defaultFn: this._defOpenFn,
100
silent : options && options.silent
108
Toggles the open/closed state of the specified node, closing it if it's
109
currently open or opening it if it's currently closed.
111
@method toggleOpenNode
112
@param {Tree.Node} node Node to toggle.
113
@param {Object} [options] Options.
114
@param {Boolean} [options.silent=false] If `true`, events will be
116
@param {String} [options.src] Source of the change, to be passed along
117
to the event facade of the resulting event. This can be used to
118
distinguish between changes triggered by a user and changes
119
triggered programmatically, for example.
122
toggleOpenNode: function (node, options) {
123
return node.isOpen() ? this.closeNode(node, options) :
124
this.openNode(node, options);
127
// -- Default Event Handlers -----------------------------------------------
130
Default handler for the `close` event.
133
@param {EventFacade} e
136
_defCloseFn: function (e) {
137
delete e.node.state.open;
141
Default handler for the `open` event.
144
@param {EventFacade} e
147
_defOpenFn: function (e) {
148
e.node.state.open = true;
152
Y.Tree.Openable = Openable;
155
@submodule tree-openable
159
`Tree.Node` extension that adds methods useful for nodes in trees that use the
160
`Tree.Openable` extension.
162
@class Tree.Node.Openable
164
@extensionfor Tree.Node
167
function NodeOpenable() {}
169
NodeOpenable.prototype = {
171
Closes this node if it's currently open.
174
@param {Object} [options] Options.
175
@param {Boolean} [options.silent=false] If `true`, the `close` event
177
@param {String} [options.src] Source of the change, to be passed along
178
to the event facade of the resulting event. This can be used to
179
distinguish between changes triggered by a user and changes
180
triggered programmatically, for example.
183
close: function (options) {
184
this.tree.closeNode(this, options);
189
Returns `true` if this node is currently open.
191
Note: the root node of a tree is always considered to be open.
194
@return {Boolean} `true` if this node is currently open, `false` otherwise.
196
isOpen: function () {
197
return !!this.state.open || this.isRoot();
201
Opens this node if it's currently closed.
204
@param {Object} [options] Options.
205
@param {Boolean} [options.silent=false] If `true`, the `open` event
207
@param {String} [options.src] Source of the change, to be passed along
208
to the event facade of the resulting event. This can be used to
209
distinguish between changes triggered by a user and changes
210
triggered programmatically, for example.
213
open: function (options) {
214
this.tree.openNode(this, options);
219
Toggles the open/closed state of this node, closing it if it's currently
220
open or opening it if it's currently closed.
223
@param {Object} [options] Options.
224
@param {Boolean} [options.silent=false] If `true`, events will be
226
@param {String} [options.src] Source of the change, to be passed along
227
to the event facade of the resulting event. This can be used to
228
distinguish between changes triggered by a user and changes
229
triggered programmatically, for example.
232
toggleOpen: function (options) {
233
this.tree.toggleOpenNode(this, options);
238
Y.Tree.Node.Openable = NodeOpenable;
241
}, '@VERSION@', {"requires": ["tree"]});