reject adding handlers for certain event names

This commit is contained in:
Tom 2024-01-29 10:39:55 -05:00
parent c0104c7480
commit 2add108539
3 changed files with 22 additions and 17 deletions

View File

@ -51,6 +51,7 @@
*/
$.EventSource = function() {
this.events = {};
this.rejectedEventList = {};
};
/** @lends OpenSeadragon.EventSource.prototype */
@ -90,13 +91,15 @@ $.EventSource.prototype = {
* @param {OpenSeadragon.EventHandler} handler - Function to call when event is triggered.
* @param {Object} [userData=null] - Arbitrary object to be passed unchanged to the handler.
* @param {Number} [priority=0] - Handler priority. By default, all priorities are 0. Higher number = priority.
* @returns {Boolean} - True if the handler was added, false if it was rejected
*/
addHandler: function ( eventName, handler, userData, priority ) {
let errorMsg = this._validateEvent(eventName);
if(errorMsg){
$.console.error(`Error adding event handler: ${errorMsg}`);
return;
if(Object.prototype.hasOwnProperty.call(this.rejectedEventList, eventName)){
$.console.error(`Error adding handler for ${eventName}. ${this.rejectedEventList[eventName]}`);
return false;
}
var events = this.events[ eventName ];
if ( !events ) {
this.events[ eventName ] = events = [];
@ -111,6 +114,7 @@ $.EventSource.prototype = {
index--;
}
}
return true;
},
/**
@ -209,21 +213,15 @@ $.EventSource.prototype = {
},
/**
* Check
* @param {String} eventName the event to listen for
* @returns {String | null} Error message (if invalid) or null
* @private
* Set an event name as being disabled, and provide an optional error message
* to be printed to the console
* @param {String} eventName
* @param {[String]} errorMessage
*/
_validateEvent(eventName){
// check for listeners on incompatible events
if(eventName === 'tile-drawing' && this instanceof $.Viewer){
return this.drawer.getType() === 'canvas' ? null : 'The tile-drawing event requires the canvas drawer.';
} else if(eventName === 'tile-drawn' && this instanceof $.Viewer){
return ['canvas', 'html'].includes(this.drawer.getType()) ? null : `The tile-drawn event is not valid for the ${this.drawer.getType()} drawer.`;
}
// default to returning true unless a rule has been added specifically for an event type
return null;
rejectEventHandler(eventName, errorMessage = ''){
this.rejectedEventList[eventName] = errorMessage;
}
};
}( OpenSeadragon ));

View File

@ -48,6 +48,9 @@
class HTMLDrawer extends $.DrawerBase{
constructor(options){
super(options);
// Reject listening for the tile-drawing event, which this drawer does not fire
this.viewer.rejectEventHandler("tile-drawing", "The HTMLDrawer does not raise the tile-drawing event");
}
/**

View File

@ -82,6 +82,10 @@
this.viewer.addHandler("tile-ready", ev => this._tileReadyHandler(ev));
this.viewer.addHandler("image-unloaded", ev => this._imageUnloadedHandler(ev));
// Reject listening for the tile-drawing and tile-drawn events, which this drawer does not fire
this.viewer.rejectEventHandler("tile-drawn", "The WebGLDrawer does not raise the tile-drawn event");
this.viewer.rejectEventHandler("tile-drawing", "The WebGLDrawer does not raise the tile-drawing event");
// this.viewer and this.canvas are part of the public DrawerBase API
// and are defined by the parent DrawerBase class. Additional setup is done by
// the private _setupCanvases and _setupRenderer functions.