Debian Codegraph @

resources/bean.js

raw symbols SCIP
1 /*!
2 * Bean - copyright (c) Jacob Thornton 2011-2012
3 * https://github.com/fat/bean
4 * MIT license
5 */
6 (function (name, context, definition) {
7 if (typeof module != 'undefined' && module.exports) module.exports = definition()
8 else if (typeof define == 'function' && define.amd) define(definition)
9 else context[name] = definition()
10 })('bean', this, function (name, context) {
11 name = name || 'bean'
12 context = context || this
13
14 var win = window
15 , old = context[name]
16 , namespaceRegex = /[^\.]*(?=\..*)\.|.*/
17 , nameRegex = /\..*/
18 , addEvent = 'addEventListener'
19 , removeEvent = 'removeEventListener'
20 , doc = document || {}
21 , root = doc.documentElement || {}
22 , W3C_MODEL = root[addEvent]
23 , eventSupport = W3C_MODEL ? addEvent : 'attachEvent'
24 , ONE = {} // singleton for quick matching making add() do one()
25
26 , slice = Array.prototype.slice
27 , str2arr = function (s, d) { return s.split(d || ' ') }
28 , isString = function (o) { return typeof o == 'string' }
29 , isFunction = function (o) { return typeof o == 'function' }
30
31 // events that we consider to be 'native', anything not in this list will
32 // be treated as a custom event
33 , standardNativeEvents =
34 'click dblclick mouseup mousedown contextmenu ' + // mouse buttons
35 'mousewheel mousemultiwheel DOMMouseScroll ' + // mouse wheel
36 'mouseover mouseout mousemove selectstart selectend ' + // mouse movement
37 'keydown keypress keyup ' + // keyboard
38 'orientationchange ' + // mobile
39 'focus blur change reset select submit ' + // form elements
40 'load unload beforeunload resize move DOMContentLoaded ' + // window
41 'readystatechange message ' + // window
42 'error abort scroll ' // misc
43 // element.fireEvent('onXYZ'... is not forgiving if we try to fire an event
44 // that doesn't actually exist, so make sure we only do these on newer browsers
45 , w3cNativeEvents =
46 'show ' + // mouse buttons
47 'input invalid ' + // form elements
48 'touchstart touchmove touchend touchcancel ' + // touch
49 'gesturestart gesturechange gestureend ' + // gesture
50 'textinput ' + // TextEvent
51 'readystatechange pageshow pagehide popstate ' + // window
52 'hashchange offline online ' + // window
53 'afterprint beforeprint ' + // printing
54 'dragstart dragenter dragover dragleave drag drop dragend ' + // dnd
55 'loadstart progress suspend emptied stalled loadmetadata ' + // media
56 'loadeddata canplay canplaythrough playing waiting seeking ' + // media
57 'seeked ended durationchange timeupdate play pause ratechange ' + // media
58 'volumechange cuechange ' + // media
59 'checking noupdate downloading cached updateready obsolete ' // appcache
60
61 // convert to a hash for quick lookups
62 , nativeEvents = (function (hash, events, i) {
63 for (i = 0; i < events.length; i++) events[i] && (hash[events[i]] = 1)
64 return hash
65 }({}, str2arr(standardNativeEvents + (W3C_MODEL ? w3cNativeEvents : ''))))
66
67 // custom events are events that we *fake*, they are not provided natively but
68 // we can use native events to generate them
69 , customEvents = (function () {
70 var isAncestor = 'compareDocumentPosition' in root
71 ? function (element, container) {
72 return container.compareDocumentPosition && (container.compareDocumentPosition(element) & 16) === 16
73 }
74 : 'contains' in root
75 ? function (element, container) {
76 container = container.nodeType === 9 || container === window ? root : container
77 return container !== element && container.contains(element)
78 }
79 : function (element, container) {
80 while (element = element.parentNode) if (element === container) return 1
81 return 0
82 }
83 , check = function (event) {
84 var related = event.relatedTarget
85 return !related
86 ? related == null
87 : (related !== this && related.prefix !== 'xul' && !/document/.test(this.toString())
88 && !isAncestor(related, this))
89 }
90
91 return {
92 mouseenter: { base: 'mouseover', condition: check }
93 , mouseleave: { base: 'mouseout', condition: check }
94 , mousewheel: { base: /Firefox/.test(navigator.userAgent) ? 'DOMMouseScroll' : 'mousewheel' }
95 }
96 }())
97
98 // we provide a consistent Event object across browsers by taking the actual DOM
99 // event object and generating a new one from its properties.
100 , Event = (function () {
101 // a whitelist of properties (for different event types) tells us what to check for and copy
102 var commonProps = str2arr('altKey attrChange attrName bubbles cancelable ctrlKey currentTarget ' +
103 'detail eventPhase getModifierState isTrusted metaKey relatedNode relatedTarget shiftKey ' +
104 'srcElement target timeStamp type view which propertyName')
105 , mouseProps = commonProps.concat(str2arr('button buttons clientX clientY dataTransfer ' +
106 'fromElement offsetX offsetY pageX pageY screenX screenY toElement'))
107 , mouseWheelProps = mouseProps.concat(str2arr('wheelDelta wheelDeltaX wheelDeltaY wheelDeltaZ ' +
108 'axis')) // 'axis' is FF specific
109 , keyProps = commonProps.concat(str2arr('char charCode key keyCode keyIdentifier ' +
110 'keyLocation location'))
111 , textProps = commonProps.concat(str2arr('data'))
112 , touchProps = commonProps.concat(str2arr('touches targetTouches changedTouches scale rotation'))
113 , messageProps = commonProps.concat(str2arr('data origin source'))
114 , stateProps = commonProps.concat(str2arr('state'))
115 , overOutRegex = /over|out/
116 // some event types need special handling and some need special properties, do that all here
117 , typeFixers = [
118 { // key events
119 reg: /key/i
120 , fix: function (event, newEvent) {
121 newEvent.keyCode = event.keyCode || event.which
122 return keyProps
123 }
124 }
125 , { // mouse events
126 reg: /click|mouse(?!(.*wheel|scroll))|menu|drag|drop/i
127 , fix: function (event, newEvent, type) {
128 newEvent.rightClick = event.which === 3 || event.button === 2
129 newEvent.pos = { x: 0, y: 0 }
130 if (event.pageX || event.pageY) {
131 newEvent.clientX = event.pageX
132 newEvent.clientY = event.pageY
133 } else if (event.clientX || event.clientY) {
134 newEvent.clientX = event.clientX + doc.body.scrollLeft + root.scrollLeft
135 newEvent.clientY = event.clientY + doc.body.scrollTop + root.scrollTop
136 }
137 if (overOutRegex.test(type)) {
138 newEvent.relatedTarget = event.relatedTarget
139 || event[(type == 'mouseover' ? 'from' : 'to') + 'Element']
140 }
141 return mouseProps
142 }
143 }
144 , { // mouse wheel events
145 reg: /mouse.*(wheel|scroll)/i
146 , fix: function () { return mouseWheelProps }
147 }
148 , { // TextEvent
149 reg: /^text/i
150 , fix: function () { return textProps }
151 }
152 , { // touch and gesture events
153 reg: /^touch|^gesture/i
154 , fix: function () { return touchProps }
155 }
156 , { // message events
157 reg: /^message$/i
158 , fix: function () { return messageProps }
159 }
160 , { // popstate events
161 reg: /^popstate$/i
162 , fix: function () { return stateProps }
163 }
164 , { // everything else
165 reg: /.*/
166 , fix: function () { return commonProps }
167 }
168 ]
169 , typeFixerMap = {} // used to map event types to fixer functions (above), a basic cache mechanism
170
171 , Event = function (event, element, isNative) {
172 if (!arguments.length) return
173 event = event || ((element.ownerDocument || element.document || element).parentWindow || win).event
174 this.originalEvent = event
175 this.isNative = isNative
176 this.isBean = true
177
178 if (!event) return
179
180 var type = event.type
181 , target = event.target || event.srcElement
182 , i, l, p, props, fixer
183
184 this.target = target && target.nodeType === 3 ? target.parentNode : target
185
186 if (isNative) { // we only need basic augmentation on custom events, the rest expensive & pointless
187 fixer = typeFixerMap[type]
188 if (!fixer) { // haven't encountered this event type before, map a fixer function for it
189 for (i = 0, l = typeFixers.length; i < l; i++) {
190 if (typeFixers[i].reg.test(type)) { // guaranteed to match at least one, last is .*
191 typeFixerMap[type] = fixer = typeFixers[i].fix
192 break
193 }
194 }
195 }
196
197 props = fixer(event, this, type)
198 for (i = props.length; i--;) {
199 if (!((p = props[i]) in this) && p in event) this[p] = event[p]
200 }
201 }
202 }
203
204 // preventDefault() and stopPropagation() are a consistent interface to those functions
205 // on the DOM, stop() is an alias for both of them together
206 Event.prototype.preventDefault = function () {
207 if (this.originalEvent.preventDefault) this.originalEvent.preventDefault()
208 else this.originalEvent.returnValue = false
209 }
210 Event.prototype.stopPropagation = function () {
211 if (this.originalEvent.stopPropagation) this.originalEvent.stopPropagation()
212 else this.originalEvent.cancelBubble = true
213 }
214 Event.prototype.stop = function () {
215 this.preventDefault()
216 this.stopPropagation()
217 this.stopped = true
218 }
219 // stopImmediatePropagation() has to be handled internally because we manage the event list for
220 // each element
221 // note that originalElement may be a Bean#Event object in some situations
222 Event.prototype.stopImmediatePropagation = function () {
223 if (this.originalEvent.stopImmediatePropagation) this.originalEvent.stopImmediatePropagation()
224 this.isImmediatePropagationStopped = function () { return true }
225 }
226 Event.prototype.isImmediatePropagationStopped = function () {
227 return this.originalEvent.isImmediatePropagationStopped && this.originalEvent.isImmediatePropagationStopped()
228 }
229 Event.prototype.clone = function (currentTarget) {
230 //TODO: this is ripe for optimisation, new events are *expensive*
231 // improving this will speed up delegated events
232 var ne = new Event(this, this.element, this.isNative)
233 ne.currentTarget = currentTarget
234 return ne
235 }
236
237 return Event
238 }())
239
240 // if we're in old IE we can't do onpropertychange on doc or win so we use doc.documentElement for both
241 , targetElement = function (element, isNative) {
242 return !W3C_MODEL && !isNative && (element === doc || element === win) ? root : element
243 }
244
245 /**
246 * Bean maintains an internal registry for event listeners. We don't touch elements, objects
247 * or functions to identify them, instead we store everything in the registry.
248 * Each event listener has a RegEntry object, we have one 'registry' for the whole instance.
249 */
250 , RegEntry = (function () {
251 // each handler is wrapped so we can handle delegation and custom events
252 var wrappedHandler = function (element, fn, condition, args) {
253 var call = function (event, eargs) {
254 return fn.apply(element, args ? slice.call(eargs, event ? 0 : 1).concat(args) : eargs)
255 }
256 , findTarget = function (event, eventElement) {
257 return fn.__beanDel ? fn.__beanDel.ft(event.target, element) : eventElement
258 }
259 , handler = condition
260 ? function (event) {
261 var target = findTarget(event, this) // deleated event
262 if (condition.apply(target, arguments)) {
263 if (event) event.currentTarget = target
264 return call(event, arguments)
265 }
266 }
267 : function (event) {
268 if (fn.__beanDel) event = event.clone(findTarget(event)) // delegated event, fix the fix
269 return call(event, arguments)
270 }
271 handler.__beanDel = fn.__beanDel
272 return handler
273 }
274
275 , RegEntry = function (element, type, handler, original, namespaces, args, root) {
276 var customType = customEvents[type]
277 , isNative
278
279 if (type == 'unload') {
280 // self clean-up
281 handler = once(removeListener, element, type, handler, original)
282 }
283
284 if (customType) {
285 if (customType.condition) {
286 handler = wrappedHandler(element, handler, customType.condition, args)
287 }
288 type = customType.base || type
289 }
290
291 this.isNative = isNative = nativeEvents[type] && !!element[eventSupport]
292 this.customType = !W3C_MODEL && !isNative && type
293 this.element = element
294 this.type = type
295 this.original = original
296 this.namespaces = namespaces
297 this.eventType = W3C_MODEL || isNative ? type : 'propertychange'
298 this.target = targetElement(element, isNative)
299 this[eventSupport] = !!this.target[eventSupport]
300 this.root = root
301 this.handler = wrappedHandler(element, handler, null, args)
302 }
303
304 // given a list of namespaces, is our entry in any of them?
305 RegEntry.prototype.inNamespaces = function (checkNamespaces) {
306 var i, j, c = 0
307 if (!checkNamespaces) return true
308 if (!this.namespaces) return false
309 for (i = checkNamespaces.length; i--;) {
310 for (j = this.namespaces.length; j--;) {
311 if (checkNamespaces[i] == this.namespaces[j]) c++
312 }
313 }
314 return checkNamespaces.length === c
315 }
316
317 // match by element, original fn (opt), handler fn (opt)
318 RegEntry.prototype.matches = function (checkElement, checkOriginal, checkHandler) {
319 return this.element === checkElement &&
320 (!checkOriginal || this.original === checkOriginal) &&
321 (!checkHandler || this.handler === checkHandler)
322 }
323
324 return RegEntry
325 }())
326
327 , registry = (function () {
328 // our map stores arrays by event type, just because it's better than storing
329 // everything in a single array.
330 // uses '$' as a prefix for the keys for safety and 'r' as a special prefix for
331 // rootListeners so we can look them up fast
332 var map = {}
333
334 // generic functional search of our registry for matching listeners,
335 // `fn` returns false to break out of the loop
336 , forAll = function (element, type, original, handler, root, fn) {
337 var pfx = root ? 'r' : '$'
338 if (!type || type == '*') {
339 // search the whole registry
340 for (var t in map) {
341 if (t.charAt(0) == pfx) {
342 forAll(element, t.substr(1), original, handler, root, fn)
343 }
344 }
345 } else {
346 var i = 0, l, list = map[pfx + type], all = element == '*'
347 if (!list) return
348 for (l = list.length; i < l; i++) {
349 if ((all || list[i].matches(element, original, handler)) && !fn(list[i], list, i, type)) return
350 }
351 }
352 }
353
354 , has = function (element, type, original, root) {
355 // we're not using forAll here simply because it's a bit slower and this
356 // needs to be fast
357 var i, list = map[(root ? 'r' : '$') + type]
358 if (list) {
359 for (i = list.length; i--;) {
360 if (!list[i].root && list[i].matches(element, original, null)) return true
361 }
362 }
363 return false
364 }
365
366 , get = function (element, type, original, root) {
367 var entries = []
368 forAll(element, type, original, null, root, function (entry) {
369 return entries.push(entry)
370 })
371 return entries
372 }
373
374 , put = function (entry) {
375 var has = !entry.root && !this.has(entry.element, entry.type, null, false)
376 , key = (entry.root ? 'r' : '$') + entry.type
377 ;(map[key] || (map[key] = [])).push(entry)
378 return has
379 }
380
381 , del = function (entry) {
382 forAll(entry.element, entry.type, null, entry.handler, entry.root, function (entry, list, i) {
383 list.splice(i, 1)
384 entry.removed = true
385 if (list.length === 0) delete map[(entry.root ? 'r' : '$') + entry.type]
386 return false
387 })
388 }
389
390 // dump all entries, used for onunload
391 , entries = function () {
392 var t, entries = []
393 for (t in map) {
394 if (t.charAt(0) == '$') entries = entries.concat(map[t])
395 }
396 return entries
397 }
398
399 return { has: has, get: get, put: put, del: del, entries: entries }
400 }())
401
402 // we need a selector engine for delegated events, use querySelectorAll if it exists
403 // but for older browsers we need Qwery, Sizzle or similar
404 , selectorEngine
405 , setSelectorEngine = function (e) {
406 if (!arguments.length) {
407 selectorEngine = doc.querySelectorAll
408 ? function (s, r) {
409 return r.querySelectorAll(s)
410 }
411 : function () {
412 throw new Error('Bean: No selector engine installed') // eeek
413 }
414 } else {
415 selectorEngine = e
416 }
417 }
418
419 // we attach this listener to each DOM event that we need to listen to, only once
420 // per event type per DOM element
421 , rootListener = function (event, type) {
422 if (!W3C_MODEL && type && event && event.propertyName != '_on' + type) return
423
424 var listeners = registry.get(this, type || event.type, null, false)
425 , l = listeners.length
426 , i = 0
427
428 event = new Event(event, this, true)
429 if (type) event.type = type
430
431 // iterate through all handlers registered for this type, calling them unless they have
432 // been removed by a previous handler or stopImmediatePropagation() has been called
433 for (; i < l && !event.isImmediatePropagationStopped(); i++) {
434 if (!listeners[i].removed) listeners[i].handler.call(this, event)
435 }
436 }
437
438 // add and remove listeners to DOM elements
439 , listener = W3C_MODEL
440 ? function (element, type, add) {
441 // new browsers
442 element[add ? addEvent : removeEvent](type, rootListener, false)
443 }
444 : function (element, type, add, custom) {
445 // IE8 and below, use attachEvent/detachEvent and we have to piggy-back propertychange events
446 // to simulate event bubbling etc.
447 var entry
448 if (add) {
449 registry.put(entry = new RegEntry(
450 element
451 , custom || type
452 , function (event) { // handler
453 rootListener.call(element, event, custom)
454 }
455 , rootListener
456 , null
457 , null
458 , true // is root
459 ))
460 if (custom && element['_on' + custom] == null) element['_on' + custom] = 0
461 entry.target.attachEvent('on' + entry.eventType, entry.handler)
462 } else {
463 entry = registry.get(element, custom || type, rootListener, true)[0]
464 if (entry) {
465 entry.target.detachEvent('on' + entry.eventType, entry.handler)
466 registry.del(entry)
467 }
468 }
469 }
470
471 , once = function (rm, element, type, fn, originalFn) {
472 // wrap the handler in a handler that does a remove as well
473 return function () {
474 fn.apply(this, arguments)
475 rm(element, type, originalFn)
476 }
477 }
478
479 , removeListener = function (element, orgType, handler, namespaces) {
480 var type = orgType && orgType.replace(nameRegex, '')
481 , handlers = registry.get(element, type, null, false)
482 , removed = {}
483 , i, l
484
485 for (i = 0, l = handlers.length; i < l; i++) {
486 if ((!handler || handlers[i].original === handler) && handlers[i].inNamespaces(namespaces)) {
487 // TODO: this is problematic, we have a registry.get() and registry.del() that
488 // both do registry searches so we waste cycles doing this. Needs to be rolled into
489 // a single registry.forAll(fn) that removes while finding, but the catch is that
490 // we'll be splicing the arrays that we're iterating over. Needs extra tests to
491 // make sure we don't screw it up. @rvagg
492 registry.del(handlers[i])
493 if (!removed[handlers[i].eventType] && handlers[i][eventSupport])
494 removed[handlers[i].eventType] = { t: handlers[i].eventType, c: handlers[i].type }
495 }
496 }
497 // check each type/element for removed listeners and remove the rootListener where it's no longer needed
498 for (i in removed) {
499 if (!registry.has(element, removed[i].t, null, false)) {
500 // last listener of this type, remove the rootListener
501 listener(element, removed[i].t, false, removed[i].c)
502 }
503 }
504 }
505
506 // set up a delegate helper using the given selector, wrap the handler function
507 , delegate = function (selector, fn) {
508 //TODO: findTarget (therefore $) is called twice, once for match and once for
509 // setting e.currentTarget, fix this so it's only needed once
510 var findTarget = function (target, root) {
511 var i, array = isString(selector) ? selectorEngine(selector, root) : selector
512 for (; target && target !== root; target = target.parentNode) {
513 for (i = array.length; i--;) {
514 if (array[i] === target) return target
515 }
516 }
517 }
518 , handler = function (e) {
519 var match = findTarget(e.target, this)
520 if (match) fn.apply(match, arguments)
521 }
522
523 // __beanDel isn't pleasant but it's a private function, not exposed outside of Bean
524 handler.__beanDel = {
525 ft : findTarget // attach it here for customEvents to use too
526 , selector : selector
527 }
528 return handler
529 }
530
531 , fireListener = W3C_MODEL ? function (isNative, type, element) {
532 // modern browsers, do a proper dispatchEvent()
533 var evt = doc.createEvent(isNative ? 'HTMLEvents' : 'UIEvents')
534 evt[isNative ? 'initEvent' : 'initUIEvent'](type, true, true, win, 1)
535 element.dispatchEvent(evt)
536 } : function (isNative, type, element) {
537 // old browser use onpropertychange, just increment a custom property to trigger the event
538 element = targetElement(element, isNative)
539 isNative ? element.fireEvent('on' + type, doc.createEventObject()) : element['_on' + type]++
540 }
541
542 /**
543 * Public API: off(), on(), add(), (remove()), one(), fire(), clone()
544 */
545
546 /**
547 * off(element[, eventType(s)[, handler ]])
548 */
549 , off = function (element, typeSpec, fn) {
550 var isTypeStr = isString(typeSpec)
551 , k, type, namespaces, i
552
553 if (isTypeStr && typeSpec.indexOf(' ') > 0) {
554 // off(el, 't1 t2 t3', fn) or off(el, 't1 t2 t3')
555 typeSpec = str2arr(typeSpec)
556 for (i = typeSpec.length; i--;)
557 off(element, typeSpec[i], fn)
558 return element
559 }
560
561 type = isTypeStr && typeSpec.replace(nameRegex, '')
562 if (type && customEvents[type]) type = customEvents[type].base
563
564 if (!typeSpec || isTypeStr) {
565 // off(el) or off(el, t1.ns) or off(el, .ns) or off(el, .ns1.ns2.ns3)
566 if (namespaces = isTypeStr && typeSpec.replace(namespaceRegex, '')) namespaces = str2arr(namespaces, '.')
567 removeListener(element, type, fn, namespaces)
568 } else if (isFunction(typeSpec)) {
569 // off(el, fn)
570 removeListener(element, null, typeSpec)
571 } else {
572 // off(el, { t1: fn1, t2, fn2 })
573 for (k in typeSpec) {
574 if (typeSpec.hasOwnProperty(k)) off(element, k, typeSpec[k])
575 }
576 }
577
578 return element
579 }
580
581 /**
582 * on(element, eventType(s)[, selector], handler[, args ])
583 */
584 , on = function(element, events, selector, fn) {
585 var originalFn, type, types, i, args, entry, first
586
587 //TODO: the undefined check means you can't pass an 'args' argument, fix this perhaps?
588 if (selector === undefined && typeof events == 'object') {
589 //TODO: this can't handle delegated events
590 for (type in events) {
591 if (events.hasOwnProperty(type)) {
592 on.call(this, element, type, events[type])
593 }
594 }
595 return
596 }
597
598 if (!isFunction(selector)) {
599 // delegated event
600 originalFn = fn
601 args = slice.call(arguments, 4)
602 fn = delegate(selector, originalFn, selectorEngine)
603 } else {
604 args = slice.call(arguments, 3)
605 fn = originalFn = selector
606 }
607
608 types = str2arr(events)
609
610 // special case for one(), wrap in a self-removing handler
611 if (this === ONE) {
612 fn = once(off, element, events, fn, originalFn)
613 }
614
615 for (i = types.length; i--;) {
616 // add new handler to the registry and check if it's the first for this element/type
617 first = registry.put(entry = new RegEntry(
618 element
619 , types[i].replace(nameRegex, '') // event type
620 , fn
621 , originalFn
622 , str2arr(types[i].replace(namespaceRegex, ''), '.') // namespaces
623 , args
624 , false // not root
625 ))
626 if (entry[eventSupport] && first) {
627 // first event of this type on this element, add root listener
628 listener(element, entry.eventType, true, entry.customType)
629 }
630 }
631
632 return element
633 }
634
635 /**
636 * add(element[, selector], eventType(s), handler[, args ])
637 *
638 * Deprecated: kept (for now) for backward-compatibility
639 */
640 , add = function (element, events, fn, delfn) {
641 return on.apply(
642 null
643 , !isString(fn)
644 ? slice.call(arguments)
645 : [ element, fn, events, delfn ].concat(arguments.length > 3 ? slice.call(arguments, 5) : [])
646 )
647 }
648
649 /**
650 * one(element, eventType(s)[, selector], handler[, args ])
651 */
652 , one = function () {
653 return on.apply(ONE, arguments)
654 }
655
656 /**
657 * fire(element, eventType(s)[, args ])
658 *
659 * The optional 'args' argument must be an array, if no 'args' argument is provided
660 * then we can use the browser's DOM event system, otherwise we trigger handlers manually
661 */
662 , fire = function (element, type, args) {
663 var types = str2arr(type)
664 , i, j, l, names, handlers
665
666 for (i = types.length; i--;) {
667 type = types[i].replace(nameRegex, '')
668 if (names = types[i].replace(namespaceRegex, '')) names = str2arr(names, '.')
669 if (!names && !args && element[eventSupport]) {
670 fireListener(nativeEvents[type], type, element)
671 } else {
672 // non-native event, either because of a namespace, arguments or a non DOM element
673 // iterate over all listeners and manually 'fire'
674 handlers = registry.get(element, type, null, false)
675 args = [false].concat(args)
676 for (j = 0, l = handlers.length; j < l; j++) {
677 if (handlers[j].inNamespaces(names)) {
678 handlers[j].handler.apply(element, args)
679 }
680 }
681 }
682 }
683 return element
684 }
685
686 /**
687 * clone(dstElement, srcElement[, eventType ])
688 *
689 * TODO: perhaps for consistency we should allow the same flexibility in type specifiers?
690 */
691 , clone = function (element, from, type) {
692 var handlers = registry.get(from, type, null, false)
693 , l = handlers.length
694 , i = 0
695 , args, beanDel
696
697 for (; i < l; i++) {
698 if (handlers[i].original) {
699 args = [ element, handlers[i].type ]
700 if (beanDel = handlers[i].handler.__beanDel) args.push(beanDel.selector)
701 args.push(handlers[i].original)
702 on.apply(null, args)
703 }
704 }
705 return element
706 }
707
708 , bean = {
709 'on' : on
710 , 'add' : add
711 , 'one' : one
712 , 'off' : off
713 , 'remove' : off
714 , 'clone' : clone
715 , 'fire' : fire
716 , 'Event' : Event
717 , 'setSelectorEngine' : setSelectorEngine
718 , 'noConflict' : function () {
719 context[name] = old
720 return this
721 }
722 }
723
724 // for IE, clean up on unload to avoid leaks
725 if (win.attachEvent) {
726 var cleanup = function () {
727 var i, entries = registry.entries()
728 for (i in entries) {
729 if (entries[i].type && entries[i].type !== 'unload') off(entries[i].element, entries[i].type)
730 }
731 win.detachEvent('onunload', cleanup)
732 win.CollectGarbage && win.CollectGarbage()
733 }
734 win.attachEvent('onunload', cleanup)
735 }
736
737 // initialize selector engine to internal default (qSA or throw Error)
738 setSelectorEngine()
739
740 return bean
741 });