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.
This commit is contained in:
Luke Murray 2013-08-07 10:54:20 +10:00
parent 967f2e4f32
commit 4ccb141a42
3 changed files with 47 additions and 1 deletions

View File

@ -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

View File

@ -157,6 +157,9 @@
}
}
// clear the onDraw callback
this.onDraw = null;
style.top = "";
style.left = "";
style.position = "";

View File

@ -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