From c639dd15e3b28d01e663f1932bf8870ee3723a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Cervera?= Date: Tue, 29 Jun 2021 19:14:46 -0500 Subject: [PATCH] Provide a static method in OpenSeadragon to get an existing viewer From limited testing, this method allows retrieval even when the viewer was instantiated in a different file. OpenSeadragon keeps internal private state in _viewers, following the convention of the underscore prefix for private members. --- src/openseadragon.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/openseadragon.js b/src/openseadragon.js index d7362ae2..e9ca02fb 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -749,7 +749,9 @@ /* eslint-disable no-redeclare */ function OpenSeadragon( options ){ - return new OpenSeadragon.Viewer( options ); + var viewer = new OpenSeadragon.Viewer( options ); + OpenSeadragon._viewers.set(viewer.element, viewer); + return viewer; } (function( $ ){ @@ -1403,6 +1405,26 @@ function OpenSeadragon( options ){ CHROMEEDGE: 7 }, + /** + * Keep track of which {@link Viewer}s have been created. + * - Key: {@link Element} to which a Viewer is attached. + * - Value: {@link Viewer} of the element defined by the key. + * @static + * @type {Object} + */ + // eslint-disable-next-line no-undef + _viewers: new Map(), + + /** + * Returns the {@link Viewer} attached to a given DOM element. If there is + * no viewer attached to the provided element, undefined is returned. + * @function + * @param {String|Element} element Accepts an id or element. + * @returns {Viewer} The viewer attached to the given element, or undefined. + */ + getViewer: function(element) { + return $._viewers.get(this.getElement(element)); + }, /** * Returns a DOM Element for the given id or element.