Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / webui / resources / js / event_tracker.js
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * @fileoverview EventTracker is a simple class that manages the addition and
7  * removal of DOM event listeners. In particular, it keeps track of all
8  * listeners that have been added and makes it easy to remove some or all of
9  * them without requiring all the information again. This is particularly handy
10  * when the listener is a generated function such as a lambda or the result of
11  * calling Function.bind.
12  */
13
14 /**
15  * The type of the internal tracking entry. TODO(dbeam): move this back to
16  * EventTracker.Entry when https://github.com/google/closure-compiler/issues/544
17  * is fixed.
18  * @typedef {{node: !Node,
19  *            eventType: string,
20  *            listener: Function,
21  *            capture: boolean}}
22  */
23 var EventTrackerEntry;
24
25 // Use an anonymous function to enable strict mode just for this file (which
26 // will be concatenated with other files when embedded in Chrome).
27 var EventTracker = (function() {
28   'use strict';
29
30   /**
31    * Create an EventTracker to track a set of events.
32    * EventTracker instances are typically tied 1:1 with other objects or
33    * DOM elements whose listeners should be removed when the object is disposed
34    * or the corresponding elements are removed from the DOM.
35    * @constructor
36    */
37   function EventTracker() {
38     /**
39      * @type {Array.<EventTrackerEntry>}
40      * @private
41      */
42     this.listeners_ = [];
43   }
44
45   EventTracker.prototype = {
46     /**
47      * Add an event listener - replacement for Node.addEventListener.
48      * @param {!Node} node The DOM node to add a listener to.
49      * @param {string} eventType The type of event to subscribe to.
50      * @param {Function} listener The listener to add.
51      * @param {boolean} capture Whether to invoke during the capture phase.
52      */
53     add: function(node, eventType, listener, capture) {
54       var h = {
55         node: node,
56         eventType: eventType,
57         listener: listener,
58         capture: capture
59       };
60       this.listeners_.push(h);
61       node.addEventListener(eventType, listener, capture);
62     },
63
64     /**
65      * Remove any specified event listeners added with this EventTracker.
66      * @param {!Node} node The DOM node to remove a listener from.
67      * @param {?string} eventType The type of event to remove.
68      */
69     remove: function(node, eventType) {
70       this.listeners_ = this.listeners_.filter(function(h) {
71         if (h.node == node && (!eventType || (h.eventType == eventType))) {
72           EventTracker.removeEventListener_(h);
73           return false;
74         }
75         return true;
76       });
77     },
78
79     /**
80      * Remove all event listeners added with this EventTracker.
81      */
82     removeAll: function() {
83       this.listeners_.forEach(EventTracker.removeEventListener_);
84       this.listeners_ = [];
85     }
86   };
87
88   /**
89    * Remove a single event listener given it's tracking entry. It's up to the
90    * caller to ensure the entry is removed from listeners_.
91    * @param {EventTrackerEntry} h The entry describing the listener to remove.
92    * @private
93    */
94   EventTracker.removeEventListener_ = function(h) {
95     h.node.removeEventListener(h.eventType, h.listener, h.capture);
96   };
97
98   return EventTracker;
99 })();
100