2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - EventHandler
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-14 07:32:09 +04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of CodePlex Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
(function($){
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* For use by classes which want to support custom, non-browser events.
|
2013-08-27 03:48:59 +04:00
|
|
|
* TODO: This is an awful name! This thing represents an "event source",
|
2013-08-27 02:47:21 +04:00
|
|
|
* 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.
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
$.EventHandler = function() {
|
|
|
|
this.events = {};
|
|
|
|
};
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
$.EventHandler.prototype = {
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2013-08-27 02:47:21 +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.
|
2013-08-27 03:48:59 +04:00
|
|
|
* @param {Object} optional userData - Arbitrary object to be passed to the handler.
|
2013-08-27 02:47:21 +04:00
|
|
|
*/
|
2013-08-27 03:48:59 +04:00
|
|
|
addHandler: function ( eventName, handler, userData ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
var events = this.events[ eventName ];
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( !events ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
this.events[ eventName ] = events = [];
|
|
|
|
}
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( handler && $.isFunction( handler ) ) {
|
|
|
|
events[ events.length ] = { handler: handler, userData: userData || null };
|
2013-08-27 02:47:21 +04:00
|
|
|
}
|
|
|
|
},
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2013-08-27 02:47:21 +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.
|
|
|
|
*/
|
2013-08-27 03:48:59 +04:00
|
|
|
removeHandler: function ( eventName, handler ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
var events = this.events[ eventName ],
|
2013-02-14 02:11:33 +04:00
|
|
|
handlers = [],
|
|
|
|
i;
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( !events ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( $.isArray( events ) ) {
|
|
|
|
for ( i = 0; i < events.length; i++ ) {
|
|
|
|
if ( events[i].handler !== handler ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
handlers.push( events[ i ] );
|
2013-02-14 02:11:33 +04:00
|
|
|
}
|
2013-06-19 21:33:25 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
this.events[ eventName ] = handlers;
|
|
|
|
}
|
|
|
|
},
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2013-02-14 02:11:33 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* Remove all event handlers for a given event type. If no type is given all
|
|
|
|
* event handlers for every event type are removed.
|
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event for which all handlers are to be removed.
|
|
|
|
*/
|
|
|
|
removeAllHandlers: function( eventName ) {
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( eventName ){
|
2013-08-27 02:47:21 +04:00
|
|
|
this.events[ eventName ] = [];
|
|
|
|
} else{
|
2013-08-27 03:48:59 +04:00
|
|
|
for ( var eventType in this.events ) {
|
|
|
|
this.events[ eventType ] = [];
|
2013-08-08 11:49:24 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
}
|
|
|
|
},
|
2013-08-07 04:54:20 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
|
|
|
* Retrive the list of all handlers registered for a given event.
|
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event to get handlers for.
|
|
|
|
*/
|
2013-08-27 03:48:59 +04:00
|
|
|
getHandler: function ( eventName ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
var events = this.events[ eventName ];
|
2013-08-27 03:48:59 +04:00
|
|
|
if ( !events || !events.length ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
events = events.length === 1 ?
|
|
|
|
[ events[ 0 ] ] :
|
|
|
|
Array.apply( null, events );
|
2013-08-27 03:48:59 +04:00
|
|
|
return function ( source, args ) {
|
2013-08-27 02:47:21 +04:00
|
|
|
var i,
|
2013-08-27 03:48:59 +04:00
|
|
|
length = events.length;
|
2013-08-27 02:47:21 +04:00
|
|
|
for ( i = 0; i < length; i++ ) {
|
2013-08-27 03:48:59 +04:00
|
|
|
if (events[ i ]) {
|
|
|
|
args.userData = events[ i ].userData;
|
|
|
|
events[ i ].handler( source, args );
|
2012-02-28 17:07:56 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2013-08-27 02:47:21 +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.
|
|
|
|
*/
|
|
|
|
raiseEvent: function( eventName, eventArgs ) {
|
|
|
|
//uncomment if you want to get a log of all events
|
|
|
|
//$.console.log( eventName );
|
|
|
|
var handler = this.getHandler( eventName );
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
if ( handler ) {
|
|
|
|
if ( !eventArgs ) {
|
|
|
|
eventArgs = {};
|
2013-08-27 02:25:57 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
|
|
|
|
handler( this, eventArgs );
|
2011-12-17 07:14:10 +04:00
|
|
|
}
|
2013-08-27 02:47:21 +04:00
|
|
|
}
|
|
|
|
};
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
}( OpenSeadragon ));
|