~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/tree-openable/tree-openable.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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