2013-05-01 00:46:16 -04:00
|
|
|
/*
|
2013-05-14 00:00:24 -04:00
|
|
|
* OpenSeadragon - EventHandler
|
2013-05-01 00:46:16 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-13 23:32:09 -04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 00: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-26 15:25:57 -07:00
|
|
|
(function ($) {
|
2011-12-16 22:14:10 -05:00
|
|
|
|
2013-08-26 15:25:57 -07:00
|
|
|
/**
|
|
|
|
* For use by classes which want to support custom, non-browser events.
|
|
|
|
* TODO: This is an awful 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.
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
$.EventHandler = function () {
|
|
|
|
this.events = {};
|
|
|
|
};
|
2012-01-25 14:14:02 -05:00
|
|
|
|
2013-08-26 15:25:57 -07:00
|
|
|
$.EventHandler.prototype = {
|
2012-01-25 14:14:02 -05:00
|
|
|
|
2013-08-26 15:25:57 -07: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.
|
|
|
|
* @param {Object} optional userData - Arbitrary object to be passed to the handler.
|
|
|
|
*/
|
|
|
|
addHandler: function (eventName, handler, userData) {
|
|
|
|
var events = this.events[eventName];
|
|
|
|
if (!events) {
|
|
|
|
this.events[eventName] = events = [];
|
|
|
|
}
|
|
|
|
if (handler && $.isFunction(handler)) {
|
|
|
|
events[events.length] = { handler: handler, userData: userData || null };
|
|
|
|
}
|
|
|
|
},
|
2012-01-25 14:14:02 -05:00
|
|
|
|
2013-08-26 15:25:57 -07: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) {
|
|
|
|
var events = this.events[eventName],
|
2013-02-13 17:11:33 -05:00
|
|
|
handlers = [],
|
|
|
|
i;
|
2013-08-26 15:25:57 -07:00
|
|
|
if (!events) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($.isArray(events)) {
|
|
|
|
for (i = 0; i < events.length; i++) {
|
|
|
|
if (events[i].handler !== handler) {
|
|
|
|
handlers.push(events[i]);
|
|
|
|
}
|
2013-02-13 17:11:33 -05:00
|
|
|
}
|
2013-08-26 15:25:57 -07:00
|
|
|
this.events[eventName] = handlers;
|
2013-06-19 13:33:25 -04:00
|
|
|
}
|
2013-08-26 15:25:57 -07:00
|
|
|
},
|
2011-12-16 22:14:10 -05:00
|
|
|
|
2013-02-13 17:11:33 -05:00
|
|
|
|
2013-08-26 15:25:57 -07: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) {
|
|
|
|
if (eventName) {
|
|
|
|
this.events[eventName] = [];
|
|
|
|
} else {
|
|
|
|
for (var eventType in this.events) {
|
|
|
|
this.events[eventType] = [];
|
|
|
|
}
|
2013-08-08 17:49:24 +10:00
|
|
|
}
|
2013-08-26 15:25:57 -07:00
|
|
|
},
|
2013-08-07 10:54:20 +10:00
|
|
|
|
2013-08-26 15:25:57 -07:00
|
|
|
/**
|
|
|
|
* Retrieve 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];
|
|
|
|
if (!events || !events.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
events = events.length === 1 ?
|
|
|
|
[events[0]] :
|
|
|
|
Array.apply(null, events);
|
|
|
|
return function (source, args) {
|
|
|
|
var i,
|
2012-01-25 14:14:02 -05:00
|
|
|
length = events.length;
|
2013-08-26 15:25:57 -07:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
if (events[i]) {
|
|
|
|
args.userData = events[i].userData;
|
|
|
|
events[i].handler(source, args);
|
|
|
|
}
|
2012-02-28 08:07:56 -05:00
|
|
|
}
|
2013-08-26 15:25:57 -07:00
|
|
|
};
|
|
|
|
},
|
2011-12-16 22:14:10 -05:00
|
|
|
|
2013-08-26 15:25:57 -07: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-16 22:14:10 -05:00
|
|
|
|
2013-08-26 15:25:57 -07:00
|
|
|
if (handler) {
|
|
|
|
if (!eventArgs) {
|
|
|
|
eventArgs = {};
|
|
|
|
}
|
2011-12-16 22:14:10 -05:00
|
|
|
|
2013-08-26 15:25:57 -07:00
|
|
|
handler(this, eventArgs);
|
|
|
|
}
|
2011-12-16 22:14:10 -05:00
|
|
|
}
|
2013-08-26 15:25:57 -07:00
|
|
|
};
|
2011-12-16 22:14:10 -05:00
|
|
|
|
2013-08-26 15:25:57 -07:00
|
|
|
} (OpenSeadragon));
|