| 1 |
"use strict"; |
| 2 |
var Event = require('./Event'); |
| 3 |
var MouseEvent = require('./MouseEvent'); |
| 4 |
var utils = require('./utils'); |
| 5 |
|
| 6 |
module.exports = EventTarget; |
| 7 |
|
| 8 |
function EventTarget() {} |
| 9 |
|
| 10 |
EventTarget.prototype = { |
| 11 |
|
| 12 |
s |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
addEventListener: function addEventListener(type, listener, capture) { |
| 26 |
if (!listener) return; |
| 27 |
if (capture === undefined) capture = false; |
| 28 |
if (!this._listeners) this._listeners = Object.create(null); |
| 29 |
if (!this._listeners[type]) this._listeners[type] = []; |
| 30 |
var list = this._listeners[type]; |
| 31 |
|
| 32 |
|
| 33 |
for(var i = 0, n = list.length; i < n; i++) { |
| 34 |
var l = list[i]; |
| 35 |
if (l.listener === listener && l.capture === capture) |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
var obj = { listener: listener, capture: capture }; |
| 41 |
if (typeof listener === 'function') obj.f = listener; |
| 42 |
list.push(obj); |
| 43 |
}, |
| 44 |
|
| 45 |
removeEventListener: function removeEventListener(type, |
| 46 |
listener, |
| 47 |
capture) { |
| 48 |
if (capture === undefined) capture = false; |
| 49 |
if (this._listeners) { |
| 50 |
var list = this._listeners[type]; |
| 51 |
if (list) { |
| 52 |
|
| 53 |
for(var i = 0, n = list.length; i < n; i++) { |
| 54 |
var l = list[i]; |
| 55 |
if (l.listener === listener && l.capture === capture) { |
| 56 |
if (list.length === 1) { |
| 57 |
this._listeners[type] = undefined; |
| 58 |
} |
| 59 |
else { |
| 60 |
list.splice(i, 1); |
| 61 |
} |
| 62 |
return; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
}, |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
dispatchEvent: function dispatchEvent(event) { |
| 72 |
|
| 73 |
return this._dispatchEvent(event, false); |
| 74 |
}, |
| 75 |
|
| 76 |
|
| 77 |
4 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
_dispatchEvent: function _dispatchEvent(event, trusted) { |
| 93 |
if (typeof trusted !== 'boolean') trusted = false; |
| 94 |
function invoke(target, event) { |
| 95 |
var type = event.type, phase = event.eventPhase; |
| 96 |
event.currentTarget = target; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if (phase !== Event.CAPTURING_PHASE && |
| 101 |
target._handlers && target._handlers[type]) |
| 102 |
{ |
| 103 |
var handler = target._handlers[type]; |
| 104 |
var rv; |
| 105 |
if (typeof handler === 'function') { |
| 106 |
rv=handler.call(event.currentTarget, event); |
| 107 |
} |
| 108 |
else { |
| 109 |
var f = handler.handleEvent; |
| 110 |
if (typeof f !== 'function') |
| 111 |
throw new TypeError('handleEvent property of ' + |
| 112 |
'event handler object is' + |
| 113 |
'not a function.'); |
| 114 |
rv=f.call(handler, event); |
| 115 |
} |
| 116 |
|
| 117 |
switch(event.type) { |
| 118 |
case 'mouseover': |
| 119 |
if (rv === true) |
| 120 |
event.preventDefault(); |
| 121 |
break; |
| 122 |
case 'beforeunload': |
| 123 |
|
| 124 |
|
| 125 |
default: |
| 126 |
if (rv === false) |
| 127 |
event.preventDefault(); |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
var list = target._listeners && target._listeners[type]; |
| 134 |
if (!list) return; |
| 135 |
list = list.slice(); |
| 136 |
for(var i = 0, n = list.length; i < n; i++) { |
| 137 |
if (event._immediatePropagationStopped) return; |
| 138 |
var l = list[i]; |
| 139 |
if ((phase === Event.CAPTURING_PHASE && !l.capture) || |
| 140 |
(phase === Event.BUBBLING_PHASE && l.capture)) |
| 141 |
continue; |
| 142 |
if (l.f) { |
| 143 |
l.f.call(event.currentTarget, event); |
| 144 |
} |
| 145 |
else { |
| 146 |
var fn = l.listener.handleEvent; |
| 147 |
if (typeof fn !== 'function') |
| 148 |
throw new TypeError('handleEvent property of event listener object is not a function.'); |
| 149 |
fn.call(l.listener, event); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if (!event._initialized || event._dispatching) utils.InvalidStateError(); |
| 155 |
event.isTrusted = trusted; |
| 156 |
|
| 157 |
|
| 158 |
event._dispatching = true; |
| 159 |
event.target = this; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
var ancestors = []; |
| 164 |
for(var n = this.parentNode; n; n = n.parentNode) |
| 165 |
ancestors.push(n); |
| 166 |
|
| 167 |
|
| 168 |
event.eventPhase = Event.CAPTURING_PHASE; |
| 169 |
for(var i = ancestors.length-1; i >= 0; i--) { |
| 170 |
invoke(ancestors[i], event); |
| 171 |
if (event._propagationStopped) break; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if (!event._propagationStopped) { |
| 176 |
event.eventPhase = Event.AT_TARGET; |
| 177 |
invoke(this, event); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if (event.bubbles && !event._propagationStopped) { |
| 182 |
event.eventPhase = Event.BUBBLING_PHASE; |
| 183 |
for(var ii = 0, nn = ancestors.length; ii < nn; ii++) { |
| 184 |
invoke(ancestors[ii], event); |
| 185 |
if (event._propagationStopped) break; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
event._dispatching = false; |
| 190 |
event.eventPhase = Event.AT_TARGET; |
| 191 |
event.currentTarget = null; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if (trusted && !event.defaultPrevented && event instanceof MouseEvent) { |
| 196 |
switch(event.type) { |
| 197 |
case 'mousedown': |
| 198 |
this._armed = { |
| 199 |
x: event.clientX, |
| 200 |
y: event.clientY, |
| 201 |
t: event.timeStamp |
| 202 |
}; |
| 203 |
break; |
| 204 |
case 'mouseout': |
| 205 |
case 'mouseover': |
| 206 |
this._armed = null; |
| 207 |
break; |
| 208 |
case 'mouseup': |
| 209 |
if (this._isClick(event)) this._doClick(event); |
| 210 |
this._armed = null; |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
return !event.defaultPrevented; |
| 218 |
}, |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
_isClick: function(event) { |
| 223 |
return (this._armed !== null && |
| 224 |
event.type === 'mouseup' && |
| 225 |
event.isTrusted && |
| 226 |
event.button === 0 && |
| 227 |
event.timeStamp - this._armed.t < 1000 && |
| 228 |
Math.abs(event.clientX - this._armed.x) < 10 && |
| 229 |
Math.abs(event.clientY - this._armed.Y) < 10); |
| 230 |
}, |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
_doClick: function(event) { |
| 238 |
if (this._click_in_progress) return; |
| 239 |
this._click_in_progress = true; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
var activated = this; |
| 245 |
while(activated && !activated._post_click_activation_steps) |
| 246 |
activated = activated.parentNode; |
| 247 |
|
| 248 |
if (activated && activated._pre_click_activation_steps) { |
| 249 |
activated._pre_click_activation_steps(); |
| 250 |
} |
| 251 |
|
| 252 |
var click = this.ownerDocument.createEvent('MouseEvent'); |
| 253 |
click.initMouseEvent('click', true, true, |
| 254 |
this.ownerDocument.defaultView, 1, |
| 255 |
event.screenX, event.screenY, |
| 256 |
event.clientX, event.clientY, |
| 257 |
event.ctrlKey, event.altKey, |
| 258 |
event.shiftKey, event.metaKey, |
| 259 |
event.button, null); |
| 260 |
|
| 261 |
var result = this._dispatchEvent(click, true); |
| 262 |
|
| 263 |
if (activated) { |
| 264 |
if (result) { |
| 265 |
|
| 266 |
if (activated._post_click_activation_steps) |
| 267 |
activated._post_click_activation_steps(click); |
| 268 |
} |
| 269 |
else { |
| 270 |
if (activated._cancelled_activation_steps) |
| 271 |
activated._cancelled_activation_steps(); |
| 272 |
} |
| 273 |
} |
| 274 |
}, |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
_setEventHandler: function _setEventHandler(type, handler) { |
| 290 |
if (!this._handlers) this._handlers = Object.create(null); |
| 291 |
this._handlers[type] = handler; |
| 292 |
}, |
| 293 |
|
| 294 |
_getEventHandler: function _getEventHandler(type) { |
| 295 |
return (this._handlers && this._handlers[type]) || null; |
| 296 |
} |
| 297 |
|
| 298 |
}; |