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
This commit is contained in:
Hernán Cervera 2021-07-02 07:38:01 -05:00
parent 94f269d6d0
commit 69aba71f79
2 changed files with 7 additions and 4 deletions

View File

@ -749,9 +749,7 @@
/* eslint-disable no-redeclare */ /* eslint-disable no-redeclare */
function OpenSeadragon( options ){ function OpenSeadragon( options ){
var viewer = new OpenSeadragon.Viewer( options ); return new OpenSeadragon.Viewer( options );
OpenSeadragon._viewers.set(viewer.element, viewer);
return viewer;
} }
(function( $ ){ (function( $ ){
@ -1409,10 +1407,10 @@ function OpenSeadragon( options ){
* Keep track of which {@link Viewer}s have been created. * Keep track of which {@link Viewer}s have been created.
* - Key: {@link Element} to which a Viewer is attached. * - Key: {@link Element} to which a Viewer is attached.
* - Value: {@link Viewer} of the element defined by the key. * - Value: {@link Viewer} of the element defined by the key.
* @private
* @static * @static
* @type {Object} * @type {Object}
*/ */
// eslint-disable-next-line no-undef
_viewers: new Map(), _viewers: new Map(),
/** /**

View File

@ -481,6 +481,8 @@ $.Viewer = function( options ) {
this.drawer.setImageSmoothingEnabled(this.imageSmoothingEnabled); this.drawer.setImageSmoothingEnabled(this.imageSmoothingEnabled);
} }
// Register the viewer
$._viewers.set(this.element, this);
}; };
$.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, /** @lends OpenSeadragon.Viewer.prototype */{ $.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.canvas = null;
this.container = 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 // clear our reference to the main element - they will need to pass it in again, creating a new viewer
this.element = null; this.element = null;
}, },