Skip to content

Commit

Permalink
replace fixEvent with Event object
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jul 29, 2012
1 parent 9150f8a commit 989f335
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
122 changes: 58 additions & 64 deletions src/bean.js
Expand Up @@ -48,8 +48,8 @@
'volumechange cuechange ' + // media
'checking noupdate downloading cached updateready obsolete ' // appcache
, str2arr = function (s, d) { return s.split(d || ' ') }
, isString = function (s) { return typeof s == 'string' }
, isArray = function (a) { return Object.prototype.toString.call(a) === '[object Array]' }
, isString = function (o) { return typeof o == 'string' }
, isFunction = function (o) { return typeof o == 'function' }

, nativeEvents = (function (hash, events, i) {
for (i = 0; i < events.length; i++) events[i] && (hash[events[i]] = 1)
Expand Down Expand Up @@ -86,7 +86,7 @@
}
}())

, fixEvent = (function () {
, Event = (function () {
var commonProps = str2arr('altKey attrChange attrName bubbles cancelable ctrlKey currentTarget ' +
'detail eventPhase getModifierState isTrusted metaKey relatedNode relatedTarget shiftKey ' +
'srcElement target timeStamp type view which')
Expand Down Expand Up @@ -155,68 +155,61 @@
}
]
, typeFixerMap = {} // used to map event types to fixer functions (above), a basic cache mechanism
, preventDefault = 'preventDefault'
, createPreventDefault = function (event) {
return function () {
if (event[preventDefault]) event[preventDefault]()
else event.returnValue = false
}
}
, stopPropagation = 'stopPropagation'
, createStopPropagation = function (event) {
return function () {
if (event[stopPropagation]) event[stopPropagation]()
else event.cancelBubble = true
}
}
, stopImmediatePropagation = 'stopImmediatePropagation'
, createStopImmediatePropagation = function (event) {
return function () {
if (event[stopImmediatePropagation]) event[stopImmediatePropagation]()
}
}
, createStop = function (synEvent) {
return function () {
synEvent[preventDefault]()
synEvent[stopPropagation]()
synEvent.stopped = true
}
}
, copyProps = function (event, result, props) {
var i, p
for (i = props.length; i--;) {
if (!((p = props[i]) in result) && p in event) result[p] = event[p]
}
}

return function (event, isNative) {
var result = { originalEvent: event, isNative: isNative, isBean: true }
if (!event) return result

var i, l, fixer
, type = event.type
, target = event[targetS] || event.srcElement

result[preventDefault] = createPreventDefault(event)
result[stopPropagation] = createStopPropagation(event)
result[stopImmediatePropagation] = createStopImmediatePropagation(event)
result.stop = createStop(result)
result[targetS] = target && target.nodeType === 3 ? target.parentNode : target

if (isNative) { // we only need basic augmentation on custom events, the rest expensive & pointless
fixer = typeFixerMap[type]
if (!fixer) { // haven't encountered this event type before, map a fixer function for it
for (i = 0, l = typeFixers.length; i < l; i++) {
if (typeFixers[i].reg.test(type)) { // guaranteed to match at least one, last is .*
typeFixerMap[type] = fixer = typeFixers[i].fix
break
, Event = function (event, isNative) {
this[originalEvent] = event
this.isNative = isNative
this.isBean = true

if (!event) return

var type = event.type
, target = event[targetS] || event.srcElement
, i, l, p, props, fixer

this[targetS] = target && target.nodeType === 3 ? target.parentNode : target

if (isNative) { // we only need basic augmentation on custom events, the rest expensive & pointless
fixer = typeFixerMap[type]
if (!fixer) { // haven't encountered this event type before, map a fixer function for it
for (i = 0, l = typeFixers.length; i < l; i++) {
if (typeFixers[i].reg.test(type)) { // guaranteed to match at least one, last is .*
typeFixerMap[type] = fixer = typeFixers[i].fix
break
}
}
}

props = fixer(event, this, type)
for (i = props.length; i--;) {
if (!((p = props[i]) in this) && p in event) this[p] = event[p]
}
}
}
copyProps(event, result, fixer(event, result, type))
}
return result

, preventDefault = 'preventDefault'
, stopPropagation = 'stopPropagation'
, stopImmediatePropagation = 'stopImmediatePropagation'
, originalEvent = 'originalEvent'

Event.prototype[preventDefault] = function () {
if (this[originalEvent][preventDefault]) this[originalEvent][preventDefault]()
else this[originalEvent].returnValue = false
}
Event.prototype[stopPropagation] = function () {
if (this[originalEvent][stopPropagation]) this[originalEvent][stopPropagation]()
else this[originalEvent].cancelBubble = true
}
Event.prototype[stopImmediatePropagation] = function () {
if (this[originalEvent][stopImmediatePropagation]) this[originalEvent][stopImmediatePropagation]()
}
Event.prototype.stop = function () {
this[preventDefault]()
this[stopPropagation]()
this.stopped = true
}

return Event
}())

// if we're in old IE we can't do onpropertychange on doc or win so we use doc.documentElement for both
Expand Down Expand Up @@ -377,7 +370,7 @@
, nativeHandler = function (element, fn, args) {
var beanDel = fn.__beanDel
, handler = function (event) {
event = fixEvent(event || ((this[ownerDocument] || this.document || this).parentWindow || win).event, true)
event = new Event(event || ((this[ownerDocument] || this.document || this).parentWindow || win).event, true)
if (beanDel) event.currentTarget = beanDel.ft(event[targetS], element) // delegated event, fix the fix
return fn.apply(element, [event].concat(args))
}
Expand All @@ -394,7 +387,7 @@
: W3C_MODEL ? true : event && event.propertyName == '_on' + type || !event
if (handle) {
if (event) {
event = fixEvent(event || ((this[ownerDocument] || this.document || this).parentWindow || win).event, isNative)
event = new Event(event || ((this[ownerDocument] || this.document || this).parentWindow || win).event, isNative)
event.currentTarget = target
}
fn.apply(element, event && (!args || args.length === 0) ? arguments : slice.call(arguments, event ? 0 : 1).concat(args))
Expand Down Expand Up @@ -476,7 +469,7 @@
// remove(el) or remove(el, t1.ns) or remove(el, .ns) or remove(el, .ns1.ns2.ns3)
if (namespaces = isTypeStr && typeSpec.replace(namespaceRegex, '')) namespaces = str2arr(namespaces, '.')
rm(element, type, fn, namespaces)
} else if (typeof typeSpec == 'function') {
} else if (isFunction(typeSpec)) {
// remove(el, fn)
rm(element, null, typeSpec)
} else {
Expand All @@ -502,7 +495,8 @@
return
}

if (isString(selector) || isArray(selector)) {
if (!isFunction(selector)) {
// delegated event
originalFn = fn
args = slice.call(arguments, 4)
fn = delegate(selector, originalFn, selectorEngine)
Expand Down
3 changes: 2 additions & 1 deletion tests/event-object-test.js
Expand Up @@ -145,7 +145,8 @@ buster.testCase('event object', {
, 'event object properties': {
'setUp': function () {
var commonIgnorables = ('cancelBubble clipboardData defaultPrevented explicitOriginalTarget getPreventDefault initEvent initUIEvent isChar ' +
'originalTarget preventCapture preventBubble rangeOffset rangeParent returnValue stopImmediatePropagation synthetic initPopStateEvent').split(' ')
'originalTarget preventCapture preventBubble rangeOffset rangeParent returnValue stopImmediatePropagation synthetic initPopStateEvent ' +
'preventDefault stopPropagation').split(' ')
// stuff from IE8 and below
, oldIEIgnorables = ('recordset altLeft repeat reason data behaviorCookie source contentOverflow behaviorPart url shiftLeft dataFld ' +
'qualifier wheelDelta bookmarks srcFilter nextPage srcUrn origin boundElements propertyName ctrlLeft state').split(' ')
Expand Down

0 comments on commit 989f335

Please sign in to comment.