NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / attribute-extras / attribute-extras.js
blobd9c18bd977fba969bb695e061cf706a23dfc0733
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('attribute-extras', function (Y, NAME) {
10     /**
11      * The attribute module provides an augmentable Attribute implementation, which
12      * adds configurable attributes and attribute change events to the class being
13      * augmented. It also provides a State class, which is used internally by Attribute,
14      * but can also be used independently to provide a name/property/value data structure to
15      * store state.
16      *
17      * @module attribute
18      */
20     /**
21      * The attribute-extras submodule provides less commonly used attribute methods, and can
22      * be augmented/mixed into an implemention which used attribute-core.
23      *
24      * @module attribute
25      * @submodule attribute-extras
26      */
27     var BROADCAST = "broadcast",
28         PUBLISHED = "published",
29         INIT_VALUE = "initValue",
31         MODIFIABLE = {
32             readOnly:1,
33             writeOnce:1,
34             getter:1,
35             broadcast:1
36         };
38     /**
39      * A augmentable implementation for AttributeCore, providing less frequently used
40      * methods for Attribute management such as modifyAttrs(), removeAttr and reset()
41      *
42      * @class AttributeExtras
43      * @extensionfor AttributeCore
44      */
45     function AttributeExtras() {}
47     AttributeExtras.prototype = {
49         /**
50          * Updates the configuration of an attribute which has already been added.
51          * <p>
52          * The properties which can be modified through this interface are limited
53          * to the following subset of attributes, which can be safely modified
54          * after a value has already been set on the attribute: readOnly, writeOnce,
55          * broadcast and getter.
56          * </p>
57          * @method modifyAttr
58          * @param {String} name The name of the attribute whose configuration is to be updated.
59          * @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
60          */
61         modifyAttr: function(name, config) {
62             var host = this, // help compression
63                 prop, state;
65             if (host.attrAdded(name)) {
67                 if (host._isLazyAttr(name)) {
68                     host._addLazyAttr(name);
69                 }
71                 state = host._state;
72                 for (prop in config) {
73                     if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) {
74                         state.add(name, prop, config[prop]);
76                         // If we reconfigured broadcast, need to republish
77                         if (prop === BROADCAST) {
78                             state.remove(name, PUBLISHED);
79                         }
80                     }
81                 }
82             }
83             /*jshint maxlen:200*/
84             /*jshint maxlen:150 */
85         },
87         /**
88          * Removes an attribute from the host object
89          *
90          * @method removeAttr
91          * @param {String} name The name of the attribute to be removed.
92          */
93         removeAttr: function(name) {
94             this._state.removeAll(name);
95         },
97         /**
98          * Resets the attribute (or all attributes) to its initial value, as long as
99          * the attribute is not readOnly, or writeOnce.
100          *
101          * @method reset
102          * @param {String} name Optional. The name of the attribute to reset.  If omitted, all attributes are reset.
103          * @return {Object} A reference to the host object.
104          * @chainable
105          */
106         reset : function(name) {
107             var host = this;  // help compression
109             if (name) {
110                 if (host._isLazyAttr(name)) {
111                     host._addLazyAttr(name);
112                 }
113                 host.set(name, host._state.get(name, INIT_VALUE));
114             } else {
115                 Y.each(host._state.data, function(v, n) {
116                     host.reset(n);
117                 });
118             }
119             return host;
120         },
122         /**
123          * Returns an object with the configuration properties (and value)
124          * for the given attribute. If attrName is not provided, returns the
125          * configuration properties for all attributes.
126          *
127          * @method _getAttrCfg
128          * @protected
129          * @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes.
130          * @return {Object} The configuration properties for the given attribute, or all attributes.
131          */
132         _getAttrCfg : function(name) {
133             var o,
134                 state = this._state;
136             if (name) {
137                 o = state.getAll(name) || {};
138             } else {
139                 o = {};
140                 Y.each(state.data, function(v, n) {
141                     o[n] = state.getAll(n);
142                 });
143             }
145             return o;
146         }
147     };
149     Y.AttributeExtras = AttributeExtras;
152 }, '3.13.0', {"requires": ["oop"]});