From 68f9d675fc6aa34d44ac2431d2f8bdd352d8e9d7 Mon Sep 17 00:00:00 2001 From: Luke Murray Date: Thu, 8 Aug 2013 17:49:24 +1000 Subject: [PATCH] fix: calling viewer.destroy() multiple times throw an error. And a simple destroy to mousetracker --- src/eventhandler.js | 22 ++++++++-------------- src/mousetracker.js | 9 +++++++++ src/viewer.js | 26 ++++++++++++++++++-------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/eventhandler.js b/src/eventhandler.js index 430c23fc..b834b214 100644 --- a/src/eventhandler.js +++ b/src/eventhandler.js @@ -91,25 +91,19 @@ $.EventHandler.prototype = { /** - * Remove all event handler for a given event type. + * Remove all event handler for a given event type. If no type is given all event handlers for every event type is removed * @function * @param {String} eventName - Name of event for which all handlers are to be removed. */ 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] = []; + if (eventName){ + this.events[ eventName ] = []; + } else{ + for (var eventType in this.events) { + this.events[eventType] = []; + } } - }, - + }, /** * Retrive the list of all handlers registered for a given event. diff --git a/src/mousetracker.js b/src/mousetracker.js index 4d81dc6b..1b4de92d 100644 --- a/src/mousetracker.js +++ b/src/mousetracker.js @@ -171,6 +171,15 @@ $.MouseTracker.prototype = { + /** + * Clean up any events or objects created the mouse tracker + * @function + */ + destroy: function() { + stopTracking( this ); + this.element = null; + }, + /** * Are we currently tracking events on this element. * @deprecated Just use this.tracking diff --git a/src/viewer.js b/src/viewer.js index 51eaf7b3..d3dd7579 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -499,7 +499,9 @@ $.extend( $.Viewer.prototype, $.EventHandler.prototype, $.ControlDock.prototype, this.viewport = this.preserveViewport ? this.viewport : null; //this.profiler = null; - this.canvas.innerHTML = ""; + if (this.canvas){ + this.canvas.innerHTML = ""; + } VIEWERS[ this.hash ] = null; delete VIEWERS[ this.hash ]; @@ -518,19 +520,27 @@ $.extend( $.Viewer.prototype, $.EventHandler.prototype, $.ControlDock.prototype, destroy: function( ) { this.close(); - this.removeAllHandlersForAllEvents(); + this.removeAllHandlers(); // 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); + if (this.element){ + 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; + // destroy the mouse trackers + if (this.keyboardCommandArea){ + this.keyboardCommandArea.innerTracker.destroy(); + } + if (this.innerTracker){ + this.innerTracker.destroy(); + } + if (this.outerTracker){ + this.outerTracker.destroy(); + } // clear all our references to dom objects this.canvas = null;