2013-01-29 21:32:58 +04:00
|
|
|
/*globals OpenSeadragon */
|
2011-12-17 07:14:10 +04:00
|
|
|
|
|
|
|
(function($){
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2012-02-15 23:50:27 +04:00
|
|
|
* For use by classes which want to support custom, non-browser events.
|
2012-06-05 15:52:00 +04:00
|
|
|
* TODO: This is an aweful name! This thing represents an "event source",
|
|
|
|
* not an "event handler". PLEASE change the to EventSource. Also please
|
|
|
|
* change 'addHandler', 'removeHandler' and 'raiseEvent' to 'bind',
|
|
|
|
* 'unbind', and 'trigger' respectively. Finally add a method 'one' which
|
|
|
|
* automatically unbinds a listener after the first triggered event that
|
|
|
|
* matches.
|
2012-01-25 23:14:02 +04:00
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
$.EventHandler = function() {
|
|
|
|
this.events = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
$.EventHandler.prototype = {
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* Add an event handler for a given event.
|
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event to register.
|
|
|
|
* @param {Function} handler - Function to call when event is triggered.
|
|
|
|
*/
|
|
|
|
addHandler: function( eventName, handler ) {
|
|
|
|
var events = this.events[ eventName ];
|
2012-01-25 23:14:02 +04:00
|
|
|
if( !events ){
|
2012-02-15 23:50:27 +04:00
|
|
|
this.events[ eventName ] = events = [];
|
2012-01-25 23:14:02 +04:00
|
|
|
}
|
2012-02-28 17:07:56 +04:00
|
|
|
if( handler && $.isFunction( handler ) ){
|
|
|
|
events[ events.length ] = handler;
|
|
|
|
}
|
2012-01-25 23:14:02 +04:00
|
|
|
},
|
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* Remove a specific event handler for a given event.
|
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event for which the handler is to be removed.
|
|
|
|
* @param {Function} handler - Function to be removed.
|
|
|
|
*/
|
|
|
|
removeHandler: function( eventName, handler ) {
|
2012-01-25 23:14:02 +04:00
|
|
|
//Start Thatcher - unneccessary indirection. Also, because events were
|
|
|
|
// - not actually being removed, we need to add the code
|
|
|
|
// - to do the removal ourselves. TODO
|
2012-02-15 23:50:27 +04:00
|
|
|
var events = this.events[ eventName ];
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( !events ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//End Thatcher
|
|
|
|
},
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* Retrive the list of all handlers registered for a given event.
|
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event to get handlers for.
|
|
|
|
*/
|
|
|
|
getHandler: function( eventName ) {
|
|
|
|
var events = this.events[ eventName ];
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( !events || !events.length ){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
events = events.length === 1 ?
|
|
|
|
[ events[ 0 ] ] :
|
|
|
|
Array.apply( null, events );
|
|
|
|
return function( source, args ) {
|
|
|
|
var i,
|
|
|
|
length = events.length;
|
|
|
|
for ( i = 0; i < length; i++ ) {
|
2012-02-28 17:07:56 +04:00
|
|
|
if( events[ i ] ){
|
|
|
|
events[ i ]( source, args );
|
|
|
|
}
|
2012-01-25 23:14:02 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* Trigger an event, optionally passing additional information.
|
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event to register.
|
|
|
|
* @param {Function} handler - Function to call when event is triggered.
|
|
|
|
*/
|
2012-01-25 23:14:02 +04:00
|
|
|
raiseEvent: function( eventName, eventArgs ) {
|
|
|
|
var handler = this.getHandler( eventName );
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
if ( handler ) {
|
|
|
|
if ( !eventArgs ) {
|
2013-01-29 21:32:58 +04:00
|
|
|
eventArgs = {};
|
2011-12-17 07:14:10 +04:00
|
|
|
}
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
handler( this, eventArgs );
|
2011-12-17 07:14:10 +04:00
|
|
|
}
|
2012-01-25 23:14:02 +04:00
|
|
|
}
|
|
|
|
};
|
2011-12-17 07:14:10 +04:00
|
|
|
|
|
|
|
}( OpenSeadragon ));
|