From df56b77591d1b4c4bd50dfc83d37f10117817b2e Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Thu, 24 Jan 2013 11:56:30 -0500 Subject: [PATCH] LP#1086458: add class to manage event listeners EventListenerList allows one to maintain a list of event listeners, then remove them all when it's time to clean up a window. Usage is: var list = new EventListenerList(); // attach an event listener list.add(node, 'command', function(ev) { alert('BOO!'); }, false); ... // get rid of them list.removeAll(); Based on an idea by Jason Etheridge. Signed-off-by: Galen Charlton Signed-off-by: Ben Shum --- .../chrome/content/OpenILS/global_util.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js b/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js index 09c8671cec..615108fcab 100644 --- a/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js +++ b/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js @@ -2,6 +2,43 @@ xulG = window.arguments[0]; } + function EventListenerList() { + this._listeners = []; + return this; + } + + EventListenerList.prototype = { + 'add' : function(node, type, listener, useCapture) { + try { + node.addEventListener(type,listener,useCapture); + this._listeners.push({ + 'node' : node, + 'type' : type, + 'listener' : listener, + 'useCapture' : useCapture + }); + } catch(E) { + alert(location.href + ' Error adding event listener ' + type + ': ' + E); + } + }, + + 'removeAll' : function() { + try { + if (typeof this._listeners != 'undefined') { + for (var i = 0; i < this._listeners.length; i++) { + this._listeners[i].node.removeEventListener( + this._listeners[i].type, + this._listeners[i].listener, + this._listeners[i].useCapture + ); + } + } + } catch(E) { + alert(location.href + ' Error in unloadEventListeners(): ' + E); + } + } + } + function $(id) { return document.getElementById(id); } function oils_unsaved_data_V() { -- 2.43.2