Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('button-core', function (Y, NAME) {
2
 
3
/**
4
 * Provides an interface for working with button-like DOM nodes
5
 *
6
 * @module button-core
7
 * @since 3.5.0
8
 */
9
var getClassName = Y.ClassNameManager.getClassName,
10
    AttributeCore = Y.AttributeCore;
11
 
12
/**
13
 * Creates a button
14
 *
15
 * @class ButtonCore
16
 * @uses AttributeCore
17
 * @param config {Object} Configuration object
18
 * @constructor
19
 */
20
function ButtonCore(config) {
21
    this.initializer(config);
22
}
23
 
24
ButtonCore.prototype = {
25
 
26
    /**
27
     *
28
     * @property TEMPLATE
29
     * @type {String}
30
     * @default <button/>
31
     */
32
    TEMPLATE: '<button/>',
33
 
34
    /**
35
     *
36
     * @property constructor
37
     * @type {Object}
38
     * @default ButtonCore
39
     * @private
40
     */
41
    constructor: ButtonCore,
42
 
43
    /**
44
     * @method initializer
45
     * @description Internal init() handler.
46
     * @param config {Object} Config object.
47
     * @private
48
     */
49
    initializer: function(config) {
50
        this._initNode(config);
51
        this._initAttributes(config);
52
        this._renderUI(config);
53
    },
54
 
55
    /**
56
     * @method _initNode
57
     * @description Node initializer
58
     * @param config {Object} Config object.
59
     * @private
60
     */
61
    _initNode: function(config) {
62
        if (config.host) {
63
            this._host = Y.one(config.host);
64
        } else {
65
            this._host = Y.Node.create(this.TEMPLATE);
66
        }
67
    },
68
 
69
    /**
70
     * @method _initAttributes
71
     * @description  Attribute initializer
72
     * @param config {Object} Config object.
73
     * @private
74
     */
75
    _initAttributes: function(config) {
76
        AttributeCore.call(this, ButtonCore.ATTRS, config);
77
    },
78
 
79
    /**
80
     * @method renderUI
81
     * @description Renders any UI/DOM elements for Button instances
82
     * @param config {Object} Config object.
83
     * @private
84
     */
85
    _renderUI: function() {
86
        var node = this.getNode(),
87
            nodeName = node.get('nodeName').toLowerCase();
88
 
89
        // Set some default node attributes
90
        node.addClass(ButtonCore.CLASS_NAMES.BUTTON);
91
 
92
        if (nodeName !== 'button' && nodeName !== 'input') {
93
            node.set('role', 'button');
94
        }
95
    },
96
 
97
    /**
98
     * @method enable
99
     * @description Sets the button's `disabled` DOM attribute to `false`
100
     * @public
101
     */
102
    enable: function() {
103
        this.set('disabled', false);
104
    },
105
 
106
    /**
107
     * @method disable
108
     * @description Sets the button's `disabled` DOM attribute to `true`
109
     * @public
110
     */
111
    disable: function() {
112
        this.set('disabled', true);
113
    },
114
 
115
    /**
116
     * @method getNode
117
     * @description Gets the button's host node
118
     * @return {Node} The host node instance
119
     * @public
120
     */
121
    getNode: function() {
122
        if (!this._host) {
123
            // If this._host doesn't exist, that means this._initNode
124
            // was never executed, meaning this is likely a Widget and
125
            // the host node should point to the boundingBox.
126
            this._host = this.get('boundingBox');
127
        }
128
 
129
        return this._host;
130
    },
131
 
132
    /**
133
     * @method _getLabel
134
     * @description Getter for a button's `label` ATTR
135
     * @return {String} The text label of the button
136
     * @private
137
     */
138
    _getLabel: function () {
139
        var node = this.getNode(),
140
            label = ButtonCore._getTextLabelFromNode(node);
141
 
142
        return label;
143
    },
144
 
145
    /**
146
     * @method _getLabelHTML
147
     * @description Getter for a button's `labelHTML` ATTR
148
     * @return {String} The HTML label of the button
149
     * @private
150
     */
151
    _getLabelHTML: function () {
152
        var node = this.getNode(),
153
            labelHTML = ButtonCore._getHTMLFromNode(node);
154
 
155
        return labelHTML;
156
    },
157
 
158
    /**
159
     * @method _setLabel
160
     * @description Setter for a button's `label` ATTR
161
     * @param value {String} The value to set for `label`
162
     * @param name {String} The name of this ATTR (`label`)
163
     * @param opts {Object} Additional options
164
     *    @param opts.src {String} A string identifying the callee.
165
     *        `internal` will not sync this value with the `labelHTML` ATTR
166
     * @return {String} The text label for the given node
167
     * @private
168
     */
169
    _setLabel: function (value, name, opts) {
170
        var label = Y.Escape.html(value);
171
 
172
        if (!opts || opts.src !== 'internal') {
173
            this.set('labelHTML', label, {src: 'internal'});
174
        }
175
 
176
        return label;
177
    },
178
 
179
    /**
180
     * @method _setLabelHTML
181
     * @description Setter for a button's `labelHTML` ATTR
182
     * @param value {String} The value to set for `labelHTML`
183
     * @param name {String} The name of this ATTR (`labelHTML`)
184
     * @param opts {Object} Additional options
185
     *    @param opts.src {String} A string identifying the callee.
186
     *        `internal` will not sync this value with the `label` ATTR
187
     * @return {String} The HTML label for the given node
188
     * @private
189
     */
190
    _setLabelHTML: function (value, name, opts) {
191
        var node = this.getNode(),
192
            labelNode = ButtonCore._getLabelNodeFromParent(node),
193
            nodeName = node.get('nodeName').toLowerCase();
194
 
195
        if (nodeName === 'input') {
196
            labelNode.set('value', value);
197
        }
198
        else {
199
            labelNode.setHTML(value);
200
        }
201
 
202
        if (!opts || opts.src !== 'internal') {
203
            this.set('label', value, {src: 'internal'});
204
        }
205
 
206
        return value;
207
    },
208
 
209
    /**
210
     * @method _setDisabled
211
     * @description Setter for the `disabled` ATTR
212
     * @param value {boolean}
213
     * @private
214
     */
215
    _setDisabled: function(value) {
216
        var node = this.getNode();
217
 
218
        node.getDOMNode().disabled = value; // avoid rerunning setter when this === node
219
        node.toggleClass(ButtonCore.CLASS_NAMES.DISABLED, value);
220
 
221
        return value;
222
    }
223
};
224
 
225
// ButtonCore inherits from AttributeCore
226
Y.mix(ButtonCore.prototype, AttributeCore.prototype);
227
 
228
/**
229
 * Attribute configuration.
230
 *
231
 * @property ATTRS
232
 * @type {Object}
233
 * @protected
234
 * @static
235
 */
236
ButtonCore.ATTRS = {
237
 
238
    /**
239
     * The text of the button's label
240
     *
241
     * @config label
242
     * @type String
243
     */
244
    label: {
245
        setter: '_setLabel',
246
        getter: '_getLabel',
247
        lazyAdd: false
248
    },
249
 
250
    /**
251
     * The HTML of the button's label
252
     *
253
     * This attribute accepts HTML and inserts it into the DOM **without**
254
     * sanitization.  This attribute should only be used with HTML that has
255
     * either been escaped (using `Y.Escape.html`), or sanitized according to
256
     * the requirements of your application.
257
     *
258
     * If all you need is support for text labels, please use the `label`
259
     * attribute instead.
260
     *
261
     * @config labelHTML
262
     * @type HTML
263
     */
264
    labelHTML: {
265
        setter: '_setLabelHTML',
266
        getter: '_getLabelHTML',
267
        lazyAdd: false
268
    },
269
 
270
    /**
271
     * The button's enabled/disabled state
272
     *
273
     * @config disabled
274
     * @type Boolean
275
     */
276
    disabled: {
277
        value: false,
278
        setter: '_setDisabled',
279
        lazyAdd: false
280
    }
281
};
282
 
283
/**
284
 * Name of this component.
285
 *
286
 * @property NAME
287
 * @type String
288
 * @static
289
 */
290
ButtonCore.NAME = "button";
291
 
292
/**
293
 * Array of static constants used to identify the classnames applied to DOM nodes
294
 *
295
 * @property CLASS_NAMES
296
 * @type {Object}
297
 * @public
298
 * @static
299
 */
300
ButtonCore.CLASS_NAMES = {
301
    BUTTON  : getClassName('button'),
302
    DISABLED: getClassName('button', 'disabled'),
303
    SELECTED: getClassName('button', 'selected'),
304
    LABEL   : getClassName('button', 'label')
305
};
306
 
307
/**
308
 * Array of static constants used to for applying ARIA states
309
 *
310
 * @property ARIA_STATES
311
 * @type {Object}
312
 * @private
313
 * @static
314
 */
315
ButtonCore.ARIA_STATES = {
316
    PRESSED : 'aria-pressed',
317
    CHECKED : 'aria-checked'
318
};
319
 
320
/**
321
 * Array of static constants used to for applying ARIA roles
322
 *
323
 * @property ARIA_ROLES
324
 * @type {Object}
325
 * @private
326
 * @static
327
 */
328
ButtonCore.ARIA_ROLES = {
329
    BUTTON  : 'button',
330
    CHECKBOX: 'checkbox',
331
    TOGGLE  : 'toggle'
332
};
333
 
334
/**
335
 * Finds the label node within a button
336
 *
337
 * @method _getLabelNodeFromParent
338
 * @param node {Node} The parent node
339
 * @return {Node} The label node
340
 * @private
341
 * @static
342
 */
343
ButtonCore._getLabelNodeFromParent = function (node) {
344
    var labelNode = (node.one('.' + ButtonCore.CLASS_NAMES.LABEL) || node);
345
 
346
    return labelNode;
347
};
348
 
349
/**
350
 * Gets a text label from a node
351
 *
352
 * @method _getTextLabelFromNode
353
 * @param node {Node} The parent node
354
 * @return {String} The text label for a given node
355
 * @private
356
 * @static
357
 */
358
ButtonCore._getTextLabelFromNode = function (node) {
359
    var labelNode = ButtonCore._getLabelNodeFromParent(node),
360
        nodeName = labelNode.get('nodeName').toLowerCase(),
361
        label = labelNode.get(nodeName === 'input' ? 'value' : 'text');
362
 
363
    return label;
364
};
365
 
366
/**
367
 * A utility method that gets an HTML label from a given node
368
 *
369
 * @method _getHTMLFromNode
370
 * @param node {Node} The parent node
371
 * @return {String} The HTML label for a given node
372
 * @private
373
 * @static
374
 */
375
ButtonCore._getHTMLFromNode = function (node) {
376
    var labelNode = ButtonCore._getLabelNodeFromParent(node),
377
        label = labelNode.getHTML();
378
 
379
    return label;
380
};
381
 
382
/**
383
 * Gets the disabled attribute from a node
384
 *
385
 * @method _getDisabledFromNode
386
 * @param node {Node} The parent node
387
 * @return {boolean} The disabled state for a given node
388
 * @private
389
 * @static
390
 */
391
ButtonCore._getDisabledFromNode = function (node) {
392
    return node.get('disabled');
393
};
394
 
395
// Export ButtonCore
396
Y.ButtonCore = ButtonCore;
397
 
398
 
399
}, '3.18.1', {"requires": ["attribute-core", "classnamemanager", "node-base", "escape"]});