/** * @param {webfont.DomHelper} domHelper * @param {HTMLElement} htmlElement * @param {Object} callbacks * @param {string=} opt_namespace * @constructor */ webfont.EventDispatcher = function(domHelper, htmlElement, callbacks, opt_namespace) { this.domHelper_ = domHelper; this.htmlElement_ = htmlElement; this.callbacks_ = callbacks; this.namespace_ = opt_namespace || webfont.EventDispatcher.DEFAULT_NAMESPACE; this.cssClassName_ = new webfont.CssClassName('-'); }; /** * @const * @type {string} */ webfont.EventDispatcher.DEFAULT_NAMESPACE = 'wf'; /** * @const * @type {string} */ webfont.EventDispatcher.LOADING = 'loading'; /** * @const * @type {string} */ webfont.EventDispatcher.ACTIVE = 'active'; /** * @const * @type {string} */ webfont.EventDispatcher.INACTIVE = 'inactive'; /** * @const * @type {string} */ webfont.EventDispatcher.FONT = 'font'; webfont.EventDispatcher.prototype.dispatchLoading = function() { this.domHelper_.appendClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, webfont.EventDispatcher.LOADING)); this.dispatch_(webfont.EventDispatcher.LOADING); }; /** * @param {string} fontFamily * @param {string} fontDescription */ webfont.EventDispatcher.prototype.dispatchFontLoading = function(fontFamily, fontDescription) { this.domHelper_.appendClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, fontFamily, fontDescription, webfont.EventDispatcher.LOADING)); this.dispatch_( webfont.EventDispatcher.FONT + webfont.EventDispatcher.LOADING, fontFamily, fontDescription); }; /** * @param {string} fontFamily * @param {string} fontDescription */ webfont.EventDispatcher.prototype.dispatchFontActive = function(fontFamily, fontDescription) { this.domHelper_.removeClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, fontFamily, fontDescription, webfont.EventDispatcher.LOADING)); this.domHelper_.appendClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, fontFamily, fontDescription, webfont.EventDispatcher.ACTIVE)); this.dispatch_( webfont.EventDispatcher.FONT + webfont.EventDispatcher.ACTIVE, fontFamily, fontDescription); }; /** * @param {string} fontFamily * @param {string} fontDescription */ webfont.EventDispatcher.prototype.dispatchFontInactive = function(fontFamily, fontDescription) { this.domHelper_.removeClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, fontFamily, fontDescription, webfont.EventDispatcher.LOADING)); this.domHelper_.appendClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, fontFamily, fontDescription, webfont.EventDispatcher.INACTIVE)); this.dispatch_( webfont.EventDispatcher.FONT + webfont.EventDispatcher.INACTIVE, fontFamily, fontDescription); }; webfont.EventDispatcher.prototype.dispatchInactive = function() { this.domHelper_.removeClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, webfont.EventDispatcher.LOADING)); this.domHelper_.appendClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, webfont.EventDispatcher.INACTIVE)); this.dispatch_(webfont.EventDispatcher.INACTIVE); }; webfont.EventDispatcher.prototype.dispatchActive = function() { // what about inactive? maybe if all fonts failed to load? this.domHelper_.removeClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, webfont.EventDispatcher.LOADING)); this.domHelper_.appendClassName(this.htmlElement_, this.cssClassName_.build( this.namespace_, webfont.EventDispatcher.ACTIVE)); this.dispatch_(webfont.EventDispatcher.ACTIVE); }; /** * @param {string} event * @param {string=} opt_arg1 * @param {string=} opt_arg2 */ webfont.EventDispatcher.prototype.dispatch_ = function(event, opt_arg1, opt_arg2) { if (this.callbacks_[event]) { this.callbacks_[event](opt_arg1, opt_arg2); } };