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() { $.EventSource = function() {
this.events = {}; this.events = {};
this.rejectedEventList = {};
}; };
/** @lends OpenSeadragon.EventSource.prototype */ /** @lends OpenSeadragon.EventSource.prototype */
@ -90,13 +91,15 @@ $.EventSource.prototype = {
* @param {OpenSeadragon.EventHandler} handler - Function to call when event is triggered. * @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 {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. * @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 ) { addHandler: function ( eventName, handler, userData, priority ) {
let errorMsg = this._validateEvent(eventName);
if(errorMsg){ if(Object.prototype.hasOwnProperty.call(this.rejectedEventList, eventName)){
$.console.error(`Error adding event handler: ${errorMsg}`); $.console.error(`Error adding handler for ${eventName}. ${this.rejectedEventList[eventName]}`);
return; return false;
} }
var events = this.events[ eventName ]; var events = this.events[ eventName ];
if ( !events ) { if ( !events ) {
this.events[ eventName ] = events = []; this.events[ eventName ] = events = [];
@ -111,6 +114,7 @@ $.EventSource.prototype = {
index--; index--;
} }
} }
return true;
}, },
/** /**
@ -209,21 +213,15 @@ $.EventSource.prototype = {
}, },
/** /**
* Check * Set an event name as being disabled, and provide an optional error message
* @param {String} eventName the event to listen for * to be printed to the console
* @returns {String | null} Error message (if invalid) or null * @param {String} eventName
* @private * @param {[String]} errorMessage
*/ */
_validateEvent(eventName){ rejectEventHandler(eventName, errorMessage = ''){
// check for listeners on incompatible events this.rejectedEventList[eventName] = errorMessage;
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;
} }
}; };
}( OpenSeadragon )); }( OpenSeadragon ));

View File

@ -48,6 +48,9 @@
class HTMLDrawer extends $.DrawerBase{ class HTMLDrawer extends $.DrawerBase{
constructor(options){ constructor(options){
super(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("tile-ready", ev => this._tileReadyHandler(ev));
this.viewer.addHandler("image-unloaded", ev => this._imageUnloadedHandler(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 // 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 // and are defined by the parent DrawerBase class. Additional setup is done by
// the private _setupCanvases and _setupRenderer functions. // the private _setupCanvases and _setupRenderer functions.