~ubuntuone-pqm-team/yui/stable-min

« back to all changes in this revision

Viewing changes to build/tree-openable/tree-openable.js

  • Committer: Ricardo Kirkner
  • Date: 2014-09-23 20:17:06 UTC
  • Revision ID: ricardo.kirkner@canonical.com-20140923201706-17kwxwckw6orp28k
re-added all .js files

Show diffs side-by-side

added added

removed removed

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