From 2add1085399db993354e1ef9eef9771c3a1a2347 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 29 Jan 2024 10:39:55 -0500 Subject: [PATCH] reject adding handlers for certain event names --- src/eventsource.js | 32 +++++++++++++++----------------- src/htmldrawer.js | 3 +++ src/webgldrawer.js | 4 ++++ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/eventsource.js b/src/eventsource.js index 63b3cc38..cb7d09c3 100644 --- a/src/eventsource.js +++ b/src/eventsource.js @@ -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 )); diff --git a/src/htmldrawer.js b/src/htmldrawer.js index 3b716673..ac77f681 100644 --- a/src/htmldrawer.js +++ b/src/htmldrawer.js @@ -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"); } /** diff --git a/src/webgldrawer.js b/src/webgldrawer.js index 923d5b33..a3cda706 100644 --- a/src/webgldrawer.js +++ b/src/webgldrawer.js @@ -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.