2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-09-25 00:36:13 +04:00
|
|
|
* OpenSeadragon - EventSource
|
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-11-16 10:19:53 +04:00
|
|
|
/**
|
|
|
|
* Event handler method signature used by all OpenSeadragon events.
|
|
|
|
*
|
|
|
|
* @callback EventHandler
|
|
|
|
* @memberof OpenSeadragon
|
2013-11-19 22:08:04 +04:00
|
|
|
* @param {Object} event - See individual events for event-specific properties.
|
2013-11-16 10:19:53 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-08-27 02:47:21 +04:00
|
|
|
/**
|
2013-11-16 10:19:53 +04:00
|
|
|
* @class EventSource
|
2013-11-25 20:48:44 +04:00
|
|
|
* @classdesc For use by classes which want to support custom, non-browser events.
|
|
|
|
*
|
2013-11-16 10:19:53 +04:00
|
|
|
* @memberof OpenSeadragon
|
2013-08-27 02:47:21 +04:00
|
|
|
*/
|
2013-09-25 00:36:13 +04:00
|
|
|
$.EventSource = function() {
|
2013-08-27 02:47:21 +04:00
|
|
|
this.events = {};
|
|
|
|
};
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2016-01-25 00:09:18 +03:00
|
|
|
/** @lends OpenSeadragon.EventSource.prototype */
|
|
|
|
$.EventSource.prototype = {
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2013-11-25 20:48:44 +04:00
|
|
|
// TODO: Add a method 'one' which automatically unbinds a listener after the first triggered event that matches.
|
|
|
|
|
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.
|
2013-11-16 10:19:53 +04:00
|
|
|
* @param {OpenSeadragon.EventHandler} handler - Function to call when event is triggered.
|
2013-09-12 21:05:50 +04:00
|
|
|
* @param {Object} [userData=null] - Arbitrary object to be passed unchanged 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.
|
2013-11-16 10:19:53 +04:00
|
|
|
* @param {OpenSeadragon.EventHandler} handler - Function to be removed.
|
2013-08-27 02:47:21 +04:00
|
|
|
*/
|
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
|
|
|
/**
|
2013-11-16 10:19:53 +04:00
|
|
|
* Get a function which iterates the list of all handlers registered for a given event, calling the handler for each.
|
2013-08-27 02:47:21 +04:00
|
|
|
* @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 21:10:55 +04:00
|
|
|
length = events.length;
|
2013-08-27 02:47:21 +04:00
|
|
|
for ( i = 0; i < length; i++ ) {
|
2013-08-27 21:10:55 +04:00
|
|
|
if ( events[ i ] ) {
|
2013-10-11 04:00:15 +04:00
|
|
|
args.eventSource = source;
|
2013-08-27 03:48:59 +04:00
|
|
|
args.userData = events[ i ].userData;
|
2013-10-11 04:00:15 +04:00
|
|
|
events[ i ].handler( 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.
|
2013-11-16 10:19:53 +04:00
|
|
|
* @param {Object} eventArgs - Event-specific data.
|
2013-08-27 02:47:21 +04:00
|
|
|
*/
|
|
|
|
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 ));
|