/** * This file defines an EventManager for propagating events between loosely coupled components * and frames on a web page. Note that IFRAMEs are handled transparently. This means that * if you call register or notify in an IFRAME, all events will be transparently propagated to * and from the parent frame in the page. * * @author Jeff Tassin */ //load("questia.js"); questia.rhino = true; questia.EventManager = function() { var _listeners = new Object(); this.getComponentListeners = function( compName, bCreate ) { var listeners = _listeners[compName]; if ( listeners == null ) { listeners = new Array(); _listeners[compName] = listeners; } return listeners; } this.register = function( compName, msg, callBack ) { if ( callBack == null ) { return; } var compListeners = this.getComponentListeners( compName, true ); var listenerObj = { component: compName, message: msg, notify: callBack }; if(this.isRegistered(compName, msg,callBack)){ //throw "Listener already registered for component: " + compName + " message:" + msg; // ignore here. } compListeners.push( listenerObj ); } this.isRegistered = function( compName, msg, callBack ) { var compListeners = this.getComponentListeners( compName, true ); for( var i=0; i < compListeners.length; i++ ) { if ( compListeners[i].message == msg && compListeners[i].notify == callBack ) { return true; } } return false; } this.notify = function(srcElement, compName, msg, payload) { if ( parent != self ) { parent.questia.Events.notify( srcElement, compName, msg, payload ); } else { this.notifyInternal(srcElement, compName, msg, payload); for( var i=0; i < window.frames.length; i++ ) { var iframe = window.frames[i]; if ( iframe.questia && iframe.questia.Events ) { iframe.questia.Events.notifyInternal(srcElement, compName, msg, payload ); } } } } this.notifyInternal = function(srcElement, compName, msg, payload) { var notifyFunc = null; //try { var evt = { source: srcElement, component: compName, message: msg, value:payload }; var compListeners = this.getComponentListeners( compName, false ); if ( compListeners ) { for( var i=0; i < compListeners.length; i++ ) { try { if ( compListeners[i].message == msg || compListeners[i].message == '*' ) { notifyFunc = compListeners[i].notify; compListeners[i].notify( evt ); } } catch( err ) { if ( questia.Dictionary.isDevelopment ) { questia.Log( err.description ); questia.Log( 'notify failed: ' + notifyFunc + ' comp: ' + compName + ' msg: ' + msg + ' payload: ' + payload ); } } } } } this.remove = function( compName, msg, callBack ) { if ( callBack == null ) { return; } var compListeners = this.getComponentListeners( compName, false ); if ( compListeners ) { for( i=0; i < compListeners.length; i++ ) { if ( compListeners[i].message == msg && compListeners[i].notify == callBack ) { compListeners.splice(i,1); return; } } } } } questia.Events = new questia.EventManager();