/** * A FunctionDelegate is a wrapper object around a function to be invoked, optionally * with the specified parameter values, when a particular event is fired * * @param {Object} delegateFunction The function object to be invoked * @param {Array} [args] An optional set of argument values to be passed in during * invocation. */ FunctionDelegate = function(delegateFunction, args) { this.delegateFunction = delegateFunction; if (args == null) { this.args = []; } else if (!args.length) { this.args = [args]; } else { this.args = args; } } /** * The invoke method actually invokes the contained function object. Should not be * called externally. */ FunctionDelegate.prototype.invoke = function(parameterMap) { switch (this.args.length) { case 0: this.delegateFunction(parameterMap); break; case 1: this.delegateFunction(this.args[0], parameterMap); break; case 2: this.delegateFunction(this.args[0], this.args[1], parameterMap); break; case 3: this.delegateFunction(this.args[0], this.args[1], this.args[2], parameterMap); break; case 4: this.delegateFunction(this.args[0], this.args[1], this.args[2], this.args[3], parameterMap); break; case 5: this.delegateFunction(this.args[0], this.args[1], this.args[2], this.args[3], this.args[4], parameterMap); break; case 6: this.delegateFunction(this.args[0], this.args[1], this.args[2], this.args[3], this.args[4], this.args[5], parameterMap); break; case 7: this.delegateFunction(this.args[0], this.args[1], this.args[2], this.args[3], this.args[4], this.args[5], this.args[6], parameterMap); break; case 8: this.delegateFunction(this.args[0], this.args[1], this.args[2], this.args[3], this.args[4], this.args[5], this.args[6], this.args[7], parameterMap); break; case 9: this.delegateFunction(this.args[0], this.args[1], this.args[2], this.args[3], this.args[4], this.args[5], this.args[6], this.args[7], this.args[8], parameterMap); break; default: alert(/* xlate:897160 */"Could not invoke delegate function: argument length must be between 0 and 9."/* xlate:end */); break; } } /** * The EventManager class provides a central repository of FunctionDelegates that can * be stored for event firing at a later point. When an event needs to be fired, call * the fireEvent method of EventManager, which will invoke the FunctionDelegates who * have been listening for that event. */ EventManager = function() { this.events = {}; } /** * addDelegate adds a FunctionDelegate that will be invoked when the specified Event type * is fired. * @param {Any} eventType The event type that will be listened for. This can be of any type; * just understand that the event type that is fired later must match with ==. * @param {FunctionDelegate} functionDelegate The function delegate that will be invoked when * an event matching the type specified is fired. * @param {Boolean} [persistent] Whether or not this delegate will continue to listent to events * once a matching event has been fired. In other words, if persistent is false, the FunctionDelegate * will only be invoked 0 or 1 times. If persistent is true, the delegate can be invoked 0:n times. * Default value is false. */ EventManager.prototype.addDelegate = function(eventType, functionDelegate, persistent) { var delegateArray = this.events[eventType]; if (delegateArray == null) { delegateArray = []; this.events[eventType] = delegateArray; } if (typeof(functionDelegate) == "function") { functionDelegate = new FunctionDelegate(functionDelegate); } delegateArray.push([functionDelegate, persistent]); } /** * Fires an event of the specified type * @param {Any} eventType The event type to fire. Can be of any type, just needs to match * the types being listened for. * @param {Object} [parameterMap] Optional object-map of return parameters to include in the call to * listening delegate functions */ EventManager.prototype.fireEvent = function(eventType, parameterMap) { var delegateArray = this.events[eventType]; if (delegateArray != null) { // fire each delegate, then reassign the array to keep only the // persistent deletgates var newArr = []; var len = delegateArray.length; this.events[eventType] = newArr; for (var i=0; i < len; i++) { var delEntry = delegateArray[i]; if (delEntry[1]) { newArr.push(delEntry); } delEntry[0].invoke(parameterMap); } var x=0; } } /** * Removes any listening FunctionDelegates. If the event type is specified then * this method will remove any delegates for that type, otherwise will remove * all current delegates. * * @param {Any} [eventType] Optionally specifies an event type; if specified, only * the FunctionDelegates listening to that event will be removed. */ EventManager.prototype.clearDelegates = function(eventType) { if (eventType) { if (this.events[eventType]) { delete this.events[eventType]; } } else { delete this.events; this.events = {}; } }