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 1/4] 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. From 94f269d6d0b50341e547592c9feb8f6c8124f0c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Cervera?= Date: Fri, 2 Jul 2021 07:33:55 -0500 Subject: [PATCH 2/4] Update globals in .eslintrc files 1. Add Map as a global 2. Use readonly/writable over false/true Change 2 was done to use non-deprecated values (last paragraph of the section): https://eslint.org/docs/user-guide/configuring/language-options#using-configuration-files-1 --- .eslintrc.hound.json | 7 ++++--- .eslintrc.json | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.eslintrc.hound.json b/.eslintrc.hound.json index 027542f4..9f651a1b 100644 --- a/.eslintrc.hound.json +++ b/.eslintrc.hound.json @@ -278,8 +278,9 @@ ] }, "globals": { - "OpenSeadragon": true, - "define": false, - "module": false + "OpenSeadragon": "writable", + "define": "readonly", + "module": "readonly", + "Map": "readonly" } } diff --git a/.eslintrc.json b/.eslintrc.json index e3495e52..75715c85 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -17,9 +17,10 @@ } }, "globals": { - "OpenSeadragon": true, - "define": false, - "module": false + "OpenSeadragon": "writable", + "define": "readonly", + "module": "readonly", + "Map": "readonly" }, "rules": { "no-unused-vars": [ From 69aba71f79c06bc8637d1b0e81a585520ca93b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Cervera?= Date: Fri, 2 Jul 2021 07:38:01 -0500 Subject: [PATCH 3/4] Cleanup on viewer destroy and register on Viewer() Other changes: - ESLint comment about undeclared variable Map removed, since Map is now registered as a global in .eslintrc.* // eslint-disable-next-line no-undef - Mark as private the property _viewers of OpenSeadragon --- src/openseadragon.js | 6 ++---- src/viewer.js | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/openseadragon.js b/src/openseadragon.js index e9ca02fb..5b0accb0 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -749,9 +749,7 @@ /* eslint-disable no-redeclare */ function OpenSeadragon( options ){ - var viewer = new OpenSeadragon.Viewer( options ); - OpenSeadragon._viewers.set(viewer.element, viewer); - return viewer; + return new OpenSeadragon.Viewer( options ); } (function( $ ){ @@ -1409,10 +1407,10 @@ function OpenSeadragon( options ){ * 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. + * @private * @static * @type {Object} */ - // eslint-disable-next-line no-undef _viewers: new Map(), /** diff --git a/src/viewer.js b/src/viewer.js index f86bd0c9..ad0668ed 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -481,6 +481,8 @@ $.Viewer = function( options ) { this.drawer.setImageSmoothingEnabled(this.imageSmoothingEnabled); } + // Register the viewer + $._viewers.set(this.element, this); }; $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, /** @lends OpenSeadragon.Viewer.prototype */{ @@ -824,6 +826,9 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, this.canvas = null; this.container = null; + // Unregister the viewer + $._viewers.delete(this.element); + // clear our reference to the main element - they will need to pass it in again, creating a new viewer this.element = null; }, From b0fb8e836d6ecc67ac8727f91f8ecce4c446a5a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Cervera?= Date: Fri, 2 Jul 2021 12:18:46 -0500 Subject: [PATCH 4/4] Add tests for OpenSeadragon.getViewer --- test/modules/viewerretrieval.js | 93 +++++++++++++++++++++++++++++++++ test/test.html | 1 + 2 files changed, 94 insertions(+) create mode 100644 test/modules/viewerretrieval.js diff --git a/test/modules/viewerretrieval.js b/test/modules/viewerretrieval.js new file mode 100644 index 00000000..79c1c7b2 --- /dev/null +++ b/test/modules/viewerretrieval.js @@ -0,0 +1,93 @@ +/* global QUnit, $, testLog */ + +(function() { + var viewer1; + var viewer2; + + QUnit.module('ViewerRetrieval', { + beforeEach: function () { + $('
') + .appendTo("#qunit-fixture"); + + testLog.reset(); + + viewer1 = OpenSeadragon({ + id: 'example1', + prefixUrl: 'build/openseadragon/images/', + springStiffness: 100 // Faster animation = faster tests + }); + + viewer2 = OpenSeadragon({ + id: 'example2', + prefixUrl: 'build/openseadragon/images/', + springStiffness: 100 + }); + }, + + afterEach: function () { + if (viewer1 && viewer1.destroy) { + viewer1.destroy(); + } + if (viewer2 && viewer2.destroy) { + viewer2.destroy(); + } + viewer1 = viewer2 = null; + } + }); + + QUnit.test('Get Viewers by Id', function(assert) { + var retrievedViewer1 = OpenSeadragon.getViewer('example1'); + assert.ok(retrievedViewer1, 'Attached viewer retrieved'); + assert.equal(retrievedViewer1, viewer1, 'Viewers are same instance'); + + var retrievedViewer2 = OpenSeadragon.getViewer('example2'); + assert.ok(retrievedViewer2, 'Attached viewer retrieved'); + assert.equal(retrievedViewer2, viewer2, 'Viewers are same instance'); + + // Internal state + assert.equal(OpenSeadragon._viewers.size, 2, 'Correct amount of viewers'); + }); + + QUnit.test('Get Viewers by Element', function(assert) { + var retrievedViewer1 = OpenSeadragon.getViewer( + document.getElementById('example1')); + assert.ok(retrievedViewer1, 'Attached viewer retrieved'); + assert.equal(retrievedViewer1, viewer1, 'Viewers are same instance'); + + var retrievedViewer2 = OpenSeadragon.getViewer( + document.getElementById('example2')); + assert.ok(retrievedViewer2, 'Attached viewer retrieved'); + assert.equal(retrievedViewer2, viewer2, 'Viewers are same instance'); + + // Internal state + assert.equal(OpenSeadragon._viewers.size, 2, 'Correct amount of viewers'); + }); + + QUnit.test('Undefined on Get Non-Existent Viewer by Id', function(assert) { + var notFoundViewer = OpenSeadragon.getViewer('no-viewer'); + assert.equal(notFoundViewer, undefined, "Not found viewer is undefined"); + }); + + QUnit.test('Undefined on Get Non-Existent Viewer by Element', function(assert) { + var element = document.createElement('div'); + element.id = 'no-viewer'; + document.body.appendChild(element); + + var notFoundViewer = OpenSeadragon.getViewer(element); + assert.equal(notFoundViewer, undefined, "Not found viewer is undefined"); + }); + + QUnit.test('Cleanup Viewers Registration', function(assert) { + viewer1.destroy(); + viewer2.destroy(); + viewer1 = viewer2 = null; + + var retrievedViewer1 = OpenSeadragon.getViewer('example1'); + var retrievedViewer2 = OpenSeadragon.getViewer('example2'); + assert.equal(retrievedViewer1, undefined, 'Viewer was destroyed'); + assert.equal(retrievedViewer2, undefined, 'Viewer was destroyed'); + + // Internal state + assert.equal(OpenSeadragon._viewers.size, 0, 'No viewers are registered'); + }); +})(); diff --git a/test/test.html b/test/test.html index c740b01c..f971a257 100644 --- a/test/test.html +++ b/test/test.html @@ -22,6 +22,7 @@ +