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.
|
|
|
|
*/
|
|
|
|
|
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",
|
2013-06-19 21:33:25 +04:00
|
|
|
* not an "event handler". PLEASE change the to EventSource. Also please
|
|
|
|
* change 'addHandler', 'removeHandler' and 'raiseEvent' to 'bind',
|
2012-06-05 15:52:00 +04:00
|
|
|
* 'unbind', and 'trigger' respectively. Finally add a method 'one' which
|
2013-06-19 21:33:25 +04:00
|
|
|
* automatically unbinds a listener after the first triggered event that
|
2012-06-05 15:52:00 +04:00
|
|
|
* 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 ) {
|
2013-02-14 02:11:33 +04:00
|
|
|
var events = this.events[ eventName ],
|
|
|
|
handlers = [],
|
|
|
|
i;
|
2013-06-19 21:33:25 +04:00
|
|
|
if ( !events ){
|
|
|
|
return;
|
2012-01-25 23:14:02 +04:00
|
|
|
}
|
2013-02-14 02:11:33 +04:00
|
|
|
if( $.isArray( events ) ){
|
|
|
|
for( i = 0; i < events.length; i++ ){
|
|
|
|
if( events[ i ] !== handler ){
|
2013-03-21 21:54:39 +04:00
|
|
|
handlers.push( events[ i ] );
|
2013-02-14 02:11:33 +04:00
|
|
|
}
|
2013-06-19 21:33:25 +04:00
|
|
|
}
|
2013-02-14 02:11:33 +04:00
|
|
|
this.events[ eventName ] = handlers;
|
|
|
|
}
|
2012-01-25 23:14:02 +04:00
|
|
|
},
|
2011-12-17 07:14:10 +04:00
|
|
|
|
2013-02-14 02:11:33 +04:00
|
|
|
|
|
|
|
/**
|
2013-08-12 10:38:37 +04:00
|
|
|
* Remove all event handlers for a given event type. If no type is given all
|
|
|
|
* event handlers for every event type is removed.
|
2013-02-14 02:11:33 +04:00
|
|
|
* @function
|
|
|
|
* @param {String} eventName - Name of event for which all handlers are to be removed.
|
|
|
|
*/
|
2013-08-07 04:54:20 +04:00
|
|
|
removeAllHandlers: function( eventName ) {
|
2013-08-08 11:49:24 +04:00
|
|
|
if (eventName){
|
|
|
|
this.events[ eventName ] = [];
|
|
|
|
} else{
|
|
|
|
for (var eventType in this.events) {
|
|
|
|
this.events[eventType] = [];
|
|
|
|
}
|
2013-08-07 04:54:20 +04:00
|
|
|
}
|
2013-08-08 11:49:24 +04:00
|
|
|
},
|
2013-08-07 04:54:20 +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 ) {
|
2013-06-19 21:33:25 +04:00
|
|
|
var events = this.events[ eventName ];
|
|
|
|
if ( !events || !events.length ){
|
|
|
|
return null;
|
2012-01-25 23:14:02 +04:00
|
|
|
}
|
2013-06-19 21:33:25 +04:00
|
|
|
events = events.length === 1 ?
|
|
|
|
[ events[ 0 ] ] :
|
2012-01-25 23:14:02 +04:00
|
|
|
Array.apply( null, events );
|
|
|
|
return function( source, args ) {
|
2013-06-19 21:33:25 +04:00
|
|
|
var i,
|
2012-01-25 23:14:02 +04:00
|
|
|
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 ) {
|
2013-05-20 20:39:57 +04:00
|
|
|
//uncomment if you want to get a log of all events
|
2013-02-14 04:44:23 +04:00
|
|
|
//$.console.log( eventName );
|
2012-01-25 23:14:02 +04:00
|
|
|
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 ));
|