// Generated by github.com/steida/coffee2closure 0.1.12 goog.provide('spark.core.Object'); goog.require('spark.utils'); goog.require('goog.events.EventTarget'); /** Base object class which provides custom event management. In theory, every framework class should extend this class. @constructor @export @param {Object=} options Class options. @param {*=} data Class data @extends {goog.events.EventTarget} */ spark.core.Object = function(options, data) { var _ref; if (options == null) { options = {}; } if (options.frozen == null) { options.frozen = (_ref = options['frozen']) != null ? _ref : false; } spark.core.Object.superClass_.constructor.apply(this, arguments); this.setUid_(); if (options) { this.setOptions(options); } if (data) { this.setData(data); } if (options.frozen) { this.freeze(); } } goog.inherits(spark.core.Object, goog.events.EventTarget); /** Sets options object of this class. @export @param {!Object} options Options object. */ spark.core.Object.prototype.setOptions = function(options) { return this.options = options; }; /** Returns all options object. @export @return {Object} Options object. */ spark.core.Object.prototype.getOptions = function() { return this.options; }; /** Returns value of a key in options object. @export @return {*} Value of key or null. */ spark.core.Object.prototype.getOption = function(key) { return this.options[key] || null; }; /** Sets data of this class. @export @param {*} data Data passed to class. */ spark.core.Object.prototype.setData = function(data) { return this.data = data; }; /** Returns class data. @export @return {*} Class data. */ spark.core.Object.prototype.getData = function() { return this.data; }; /** Sets uid. @private */ spark.core.Object.prototype.setUid_ = function() { return this.uid_ = spark.utils.getUid(); }; /** Return unique id. @export @return {string} Unique id of this component. */ spark.core.Object.prototype.getUid = function() { return this.uid_; }; /** Freezes object to prevent adding new properties, updating or deleting existing properties. @export */ spark.core.Object.prototype.freeze = function() { if (window.Object.freeze) { return window.Object.freeze(this); } }; /** Adds an event listener. See also {@link goog.events.Listenable::listen} @export @param {!string} eventName Name of the event. @param {!Function} callback Callback function for the event. @return {goog.events.ListenableKey|number} Unique key for the listener. */ spark.core.Object.prototype.on = function(eventName, callback) { return goog.events.listen(this, eventName, callback); }; /** Adds an event listener that is removed automatically after the listener fired once. See also {@link goog.events.Listenable.prototype.listenOnce} @export @param {!string} eventName Name of the event. @param {!Function} callback Callback function for the event. @return {goog.events.ListenableKey|number} Unique key for the listener. */ spark.core.Object.prototype.once = function(eventName, callback) { return goog.events.listenOnce(this, eventName, callback); }; /** Dispatches an event and calls all listeners listening for events of this event. See also {@link goog.events.Listenable.prototype.dispatchEvent} @export @param {!string} eventName Name of the event. @param {*=} data Data which will passed to listeners */ spark.core.Object.prototype.emit = function(eventName, data) { if (this.isDestroyed()) { return false; } return this.dispatchEvent({ type: eventName, data: data }); }; /** Removes an event listener which was added with @on or @once. See also {@link goog.events.Listenable.prototype.unlisten} @export @param {!string} eventName Name of the event. @param {!Function} callback Callback function for the event. @return {boolean} Whether any listener was removed. */ spark.core.Object.prototype.off = function(eventName, callback) { return this.unlisten(eventName, callback); }; /** Destroys the object. Emits the following events before and after the object is destroyed. Sets data and options to null and calls the `dispose` and `disposeInternal` on parent to make a proper clean up. Calling `dispose` will set the `Object.isDisposed` to true and calling `disposeInternal` will unlisten all binded events so there won't be any event and/or memory leak. @export */ spark.core.Object.prototype.destroy = function() { this.emit(spark.core.Object.EventTypes.DESTROYED); this.options = null; this.data = null; this.dispose(); this.disposeInternal(); return this.destroyed = true; }; /** Returns the object's destroy state. @export @return {boolean} Whether the object is destroyed or not. */ spark.core.Object.prototype.isDestroyed = function() { return this.destroyed || false; }; /** Events enum emitted by the Object at some point. @enum {string} */ spark.core.Object.EventTypes = { DESTROYED: 'Destroyed' };