NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / base-observable / base-observable.js
blob5f0eceaf0849ef6c70a42acde546eb84aedfbe55
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('base-observable', function (Y, NAME) {
10     /**
11     The `base-observable` submodule adds observability to Base's lifecycle and
12     attributes, and also make it an `EventTarget`.
14     @module base
15     @submodule base-observable
16     **/
17     var L = Y.Lang,
19         DESTROY = "destroy",
20         INIT = "init",
22         BUBBLETARGETS = "bubbleTargets",
23         _BUBBLETARGETS = "_bubbleTargets",
25         AttributeObservable = Y.AttributeObservable,
26         BaseCore            = Y.BaseCore;
28     /**
29     Provides an augmentable implementation of lifecycle and attribute events for
30     `BaseCore`.
32     @class BaseObservable
33     @extensionfor BaseCore
34     @uses AttributeObservable
35     @uses EventTarget
36     @since 3.8.0
37     **/
38     function BaseObservable() {}
40     BaseObservable._ATTR_CFG      = AttributeObservable._ATTR_CFG.concat();
41     BaseObservable._NON_ATTRS_CFG = ["on", "after", "bubbleTargets"];
43     BaseObservable.prototype = {
45         /**
46          * Initializes Attribute
47          *
48          * @method _initAttribute
49          * @private
50          */
51         _initAttribute: function() {
52             BaseCore.prototype._initAttribute.apply(this, arguments);
53             AttributeObservable.call(this);
55             this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
56             this._yuievt.config.prefix = this._eventPrefix;
57         },
59         /**
60          * Init lifecycle method, invoked during construction.
61          * Fires the init event prior to setting up attributes and
62          * invoking initializers for the class hierarchy.
63          *
64          * @method init
65          * @chainable
66          * @param {Object} config Object with configuration property name/value pairs
67          * @return {Base} A reference to this object
68          */
69         init: function(config) {
71             /**
72              * <p>
73              * Lifecycle event for the init phase, fired prior to initialization.
74              * Invoking the preventDefault() method on the event object provided
75              * to subscribers will prevent initialization from occuring.
76              * </p>
77              * <p>
78              * Subscribers to the "after" momemt of this event, will be notified
79              * after initialization of the object is complete (and therefore
80              * cannot prevent initialization).
81              * </p>
82              *
83              * @event init
84              * @preventable _defInitFn
85              * @param {EventFacade} e Event object, with a cfg property which
86              * refers to the configuration object passed to the constructor.
87              */
89             // PERF: Using lower level _publish() for
90             // critical path performance
92             var type = this._getFullType(INIT),
93                 e = this._publish(type);
95             e.emitFacade = true;
96             e.fireOnce = true;
97             e.defaultTargetOnly = true;
98             e.defaultFn = this._defInitFn;
100             this._preInitEventCfg(config);
102             if (e._hasPotentialSubscribers()) {
103                 this.fire(type, {cfg: config});
104             } else {
106                 this._baseInit(config);
108                 // HACK. Major hack actually. But really fast for no-listeners.
109                 // Since it's fireOnce, subscribers may come along later, so since we're
110                 // bypassing the event stack the first time, we need to tell the published
111                 // event that it's been "fired". Could extract it into a CE method?
112                 e.fired = true;
113                 e.firedWith = [{cfg:config}];
114             }
116             return this;
117         },
119         /**
120          * Handles the special on, after and target properties which allow the user to
121          * easily configure on and after listeners as well as bubble targets during
122          * construction, prior to init.
123          *
124          * @private
125          * @method _preInitEventCfg
126          * @param {Object} config The user configuration object
127          */
128         _preInitEventCfg : function(config) {
129             if (config) {
130                 if (config.on) {
131                     this.on(config.on);
132                 }
133                 if (config.after) {
134                     this.after(config.after);
135                 }
136             }
138             var i, l, target,
139                 userTargets = (config && BUBBLETARGETS in config);
141             if (userTargets || _BUBBLETARGETS in this) {
142                 target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets;
144                 if (L.isArray(target)) {
145                     for (i = 0, l = target.length; i < l; i++) {
146                         this.addTarget(target[i]);
147                     }
148                 } else if (target) {
149                     this.addTarget(target);
150                 }
151             }
152         },
154         /**
155          * <p>
156          * Destroy lifecycle method. Fires the destroy
157          * event, prior to invoking destructors for the
158          * class hierarchy.
159          * </p>
160          * <p>
161          * Subscribers to the destroy
162          * event can invoke preventDefault on the event object, to prevent destruction
163          * from proceeding.
164          * </p>
165          * @method destroy
166          * @return {Base} A reference to this object
167          * @chainable
168          */
169         destroy: function() {
171             /**
172              * <p>
173              * Lifecycle event for the destroy phase,
174              * fired prior to destruction. Invoking the preventDefault
175              * method on the event object provided to subscribers will
176              * prevent destruction from proceeding.
177              * </p>
178              * <p>
179              * Subscribers to the "after" moment of this event, will be notified
180              * after destruction is complete (and as a result cannot prevent
181              * destruction).
182              * </p>
183              * @event destroy
184              * @preventable _defDestroyFn
185              * @param {EventFacade} e Event object
186              */
187             this.publish(DESTROY, {
188                 fireOnce:true,
189                 defaultTargetOnly:true,
190                 defaultFn: this._defDestroyFn
191             });
192             this.fire(DESTROY);
194             this.detachAll();
195             return this;
196         },
198         /**
199          * Default init event handler
200          *
201          * @method _defInitFn
202          * @param {EventFacade} e Event object, with a cfg property which
203          * refers to the configuration object passed to the constructor.
204          * @protected
205          */
206         _defInitFn : function(e) {
207             this._baseInit(e.cfg);
208         },
210         /**
211          * Default destroy event handler
212          *
213          * @method _defDestroyFn
214          * @param {EventFacade} e Event object
215          * @protected
216          */
217         _defDestroyFn : function(e) {
218             this._baseDestroy(e.cfg);
219         }
220     };
222     Y.mix(BaseObservable, AttributeObservable, false, null, 1);
224     Y.BaseObservable = BaseObservable;
227 }, '3.13.0', {"requires": ["attribute-observable"]});