· 7 years ago · Mar 06, 2018, 04:44 AM
1Index: src/event.js
2===================================================================
3--- src/event.js (revision 7001)
4+++ src/event.js (working copy)
5@@ -62,31 +62,31 @@
6 Observer.guid = 1;
7
8 Object.extend(Observer.prototype, {
9- initialize: function(element, type, observer, useCapture) {
10+ initialize: function(element, type, callback, useCapture) {
11 this.element = $(element);
12+ this.type = this._type = type;
13+ this.callback = this._callback = callback;
14+ this.useCapture = useCapture || false;
15+
16 switch(type) {
17 case 'keypress':
18- if (B.IE || B.WebKit) type = 'keydown';
19+ if (B.IE || B.WebKit) this._type = 'keydown';
20 break;
21 case 'mouseenter':
22 case 'mouseleave':
23 if (B.IE) break;
24- type = type == 'mouseenter' ? 'mouseover' : 'mouseout'
25- var originalObserver = this.originalObserver = observer;
26- observer = function(event) {
27+ this._type = type == 'mouseenter' ? 'mouseover' : 'mouseout';
28+ this._callback = function(event) {
29 var rel = event.relatedTarget, cur = event.currentTarget;
30 rel = rel.nodeType == Node.TEXT_NODE ? rel.parentNode : rel;
31 if (rel && rel !== cur && !rel.descendantOf(cur))
32- return originalObserver(event);
33+ return callback(event);
34 };
35 }
36- this.type = type;
37- this.observer = observer;
38- this.useCapture = useCapture || false;
39 },
40 add: function() {
41 if (this.type == 'DOMContentLoaded' && this.element == document) {
42- Event.onReady(this.observer);
43+ Event.onReady(this.callback);
44 return;
45 }
46 this._add();
47@@ -96,27 +96,12 @@
48 if (!this.element._observers) this.element._observers = {};
49 var local = this.element._observers[this.type];
50 if (!local) local = this.element._observers[this.type] = {};
51- if (!this.observer.$$guid) this.observer.$$guid = Observer.guid++;
52- local[this.observer.$$guid] = this;
53+ if (!this.callback.$$guid) this.callback.$$guid = Observer.guid++;
54+ local[this.callback.$$guid] = this;
55 },
56 remove: function() {
57- if (!this.observer) {
58- if (this.element._observers) {
59- var collection = this.element._observers;
60- if (this.type) {
61- collection = collection[this.type]
62- if (!collection) return;
63- }
64- for (var i in collection) {
65- if (i > 0) collection[i].remove();
66- else for (var j in collection[i]) collection[i][j].remove();
67- }
68- }
69- return;
70- }
71- if (!this.observer.$$guid) return;
72 this._remove();
73- delete this.element._observers[this.type][this.observer.$$guid];
74+ delete this.element._observers[this.type][this.callback.$$guid];
75 }
76 });
77
78@@ -156,10 +141,10 @@
79 if (document.addEventListener) {
80 Object.extend(Observer.prototype, {
81 _add: function() {
82- this.element.addEventListener(this.type, this.observer, this.useCapture);
83+ this.element.addEventListener(this._type, this._callback, this.useCapture);
84 },
85 _remove: function() {
86- this.element.removeEventListener(this.type, this.observer, this.useCapture);
87+ this.element.removeEventListener(this._type, this._callback, this.useCapture);
88 }
89 });
90
91@@ -170,14 +155,14 @@
92 Object.extend(Observer.prototype, {
93 _add: function() {
94 // create a wrapper for scope correction and event object normalization
95- var ob = this.observer, el = this.element, klass = this.constructor;
96+ var ob = this._callback, el = this.element, klass = this.constructor;
97 this.wrapper = function(e) { return ob.call(el, klass.extendEvent(e, el)) };
98- this.element.attachEvent('on' + this.type, this.wrapper);
99+ this.element.attachEvent('on' + this._type, this.wrapper);
100 this.constructor.globalCache.push(this);
101 },
102 _remove: function() {
103- if (!this.wrapper) this.wrapper = this.element._observers[this.type][this.observer.$$guid].wrapper;
104- this.element.detachEvent('on' + this.type, this.wrapper);
105+ if (!this.wrapper) this.wrapper = this.element._observers[this.type][this.callback.$$guid].wrapper;
106+ this.element.detachEvent('on' + this._type, this.wrapper);
107 }
108 });
109
110@@ -231,20 +216,33 @@
111 };
112
113 return {
114- observe: function(element, type, observer, useCapture) {
115+ observe: function(element, type, callback, useCapture) {
116 if (applyToCollection(arguments)) return;
117- new Observer(element, type, observer, useCapture).add();
118+ new Observer(element, type, callback, useCapture).add();
119 },
120- stopObserving: function(element, type, observer, useCapture) {
121+
122+ stopObserving: function(element, type, callback, useCapture) {
123+ useCapture = useCapture || false;
124 if (applyToCollection(arguments)) return;
125- if (!Prototype.Browser.IE && (type == 'mouseenter' || type == 'mouseleave')) {
126- type = type == 'mouseenter' ? 'mouseover' : 'mouseout';
127- var cachedObserver = $H(element._observers[type]).find(function(obs) {
128- return obs.value.originalObserver === observer;
129- });
130- if(cachedObserver) observer = cachedObserver.observer;
131+ var collection = element._observers;
132+ if (!collection) return;
133+
134+ if (!callback) {
135+ if (type) {
136+ collection = collection[type]
137+ if (!collection) return;
138+ }
139+ for (var i in collection) {
140+ if (i > 0) collection[i].remove();
141+ else for (var j in collection[i]) collection[i][j].remove();
142+ }
143+ return;
144 }
145- new Observer(element, type, observer, useCapture).remove();
146+ collection = collection[type];
147+ if (!collection || !callback.$$guid) return;
148+ var observer = collection[callback.$$guid];
149+ if(observer && observer.useCapture === useCapture)
150+ observer.remove();
151 },
152
153 // based on work by Dan Webb, Matthias Miller, Dean Edwards and John Resig