1/* 2 * Copyright (C) 2013 University of Washington. All rights reserved. 3 * Copyright (C) 2014 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 15 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 17 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27// This class supports adding and removing many listeners at once. 28// Add DOM or Inspector event listeners to the set using `register()`. 29// Use `install()` and `uninstall()` to enable or disable all listeners 30// in the set at once. 31WebInspector.EventListenerSet = function(defaultThisObject, name) 32{ 33 this.name = name; 34 this._defaultThisObject = defaultThisObject; 35 36 this._listeners = []; 37 this._installed = false; 38} 39 40WebInspector.EventListenerSet.prototype = { 41 register: function(emitter, type, listener, thisObject, useCapture) 42 { 43 console.assert(listener, "Missing listener for event: " + type); 44 console.assert(emitter, "Missing event emitter for event: " + type); 45 console.assert(emitter instanceof WebInspector.Object || emitter instanceof Node || (typeof emitter.addEventListener === "function"), "Event emitter", emitter, " (type:" + type + ") does not implement Node or WebInspector.Object!"); 46 47 if (emitter instanceof Node) 48 listener = listener.bind(thisObject || this._defaultThisObject); 49 50 this._listeners.push({emitter: emitter, type: type, listener: listener, thisObject: thisObject, useCapture: useCapture}); 51 }, 52 53 unregister: function() 54 { 55 if (this._installed) 56 this.uninstall(); 57 this._listeners = []; 58 }, 59 60 install: function() 61 { 62 console.assert(!this._installed, "Already installed listener group: " + this.name); 63 64 this._installed = true; 65 66 for (var listenerData of this._listeners) { 67 if (listenerData.emitter instanceof Node) 68 listenerData.emitter.addEventListener(listenerData.type, listenerData.listener, listenerData.useCapture); 69 else 70 listenerData.emitter.addEventListener(listenerData.type, listenerData.listener, listenerData.thisObject || this._defaultThisObject); 71 } 72 }, 73 74 uninstall: function(unregisterListeners) 75 { 76 console.assert(this._installed, "Trying to uninstall listener group " + this.name + ", but it isn't installed."); 77 78 this._installed = false; 79 80 for (var listenerData of this._listeners) { 81 if (listenerData.emitter instanceof Node) 82 listenerData.emitter.removeEventListener(listenerData.type, listenerData.listener, listenerData.useCapture); 83 else 84 listenerData.emitter.removeEventListener(listenerData.type, listenerData.listener, listenerData.thisObject || this._defaultThisObject); 85 } 86 87 if (unregisterListeners) { 88 this._listeners = []; 89 delete this._defaultThisObject; 90 } 91 }, 92} 93