From 4ccb141a425777490e7537d0cea11e0232383f69 Mon Sep 17 00:00:00 2001 From: Luke Murray Date: Wed, 7 Aug 2013 10:54:20 +1000 Subject: [PATCH] Add a destroy function on the viewer to clean up and remove elements created by open seadragon. Add removeAllHandlersForAllEvents to clean up all events on destroy. Clear the onDraw callback on Overlay destroy. --- src/eventhandler.js | 13 ++++++++++++- src/overlay.js | 3 +++ src/viewer.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/eventhandler.js b/src/eventhandler.js index e265fbf7..430c23fc 100644 --- a/src/eventhandler.js +++ b/src/eventhandler.js @@ -95,11 +95,22 @@ $.EventHandler.prototype = { * @function * @param {String} eventName - Name of event for which all handlers are to be removed. */ - removeAllHandlers: function( eventName ){ + removeAllHandlers: function( eventName ) { this.events[ eventName ] = []; }, + /** + * Remove every event handler for all event types + * @function + */ + removeAllHandlersForAllEvents: function( ) { + for (var eventType in this.events) { + this.events[eventType] = []; + } + }, + + /** * Retrive the list of all handlers registered for a given event. * @function diff --git a/src/overlay.js b/src/overlay.js index 8635499f..a0e40490 100644 --- a/src/overlay.js +++ b/src/overlay.js @@ -157,6 +157,9 @@ } } + // clear the onDraw callback + this.onDraw = null; + style.top = ""; style.left = ""; style.position = ""; diff --git a/src/viewer.js b/src/viewer.js index 0c6f5969..51eaf7b3 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -509,6 +509,38 @@ $.extend( $.Viewer.prototype, $.EventHandler.prototype, $.ControlDock.prototype, return this; }, + + /** + * Function to destroy the viewer and clean up everything created by Open Seadragon + * @function + * @name OpenSeadragon.Viewer.prototype.destroy + */ + destroy: function( ) { + this.close(); + + this.removeAllHandlersForAllEvents(); + + // Go through top element (passed to us) and remove all children + // Use removeChild to make sure it handles SVG or any non-html + // also it performs better - http://jsperf.com/innerhtml-vs-removechild/15 + while (this.element.firstChild) { + this.element.removeChild(this.element.firstChild); + } + + // remove the mouse trackers - should we be cleaning up their callbacks? + delete this.keyboardCommandArea.innerTracker; + delete this.innerTracker; + delete this.outerTracker; + + // clear all our references to dom objects + this.canvas = null; + this.keyboardCommandArea = null; + this.container = null; + + // clear our reference to the main element - they will need to pass it in again, creating a new viewer + this.element = null; + }, + /** * @function