event-facade-dom.js revision f6baa527839e75655081768c365a749c59edc80d
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley(function() {
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * Custom event engine, DOM event listener abstraction layer, synthetic DOM
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @module event
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @submodule event-base
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * Wraps a DOM event, properties requiring browser abstraction are
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * fixed here. Provids a security layer when required.
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @class DOMEventFacade
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @param ev {Event} the DOM event
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @param currentTarget {HTMLElement} the element the listener was attached to
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley * @TODO constants? LEFTBUTTON, MIDDLEBUTTON, RIGHTBUTTON, keys
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewsvar whitelist = {
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley // "button" : 1, // we supply
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley // "bubbles" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "cancelable" : 1, // needed?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "charCode" : 1, // we supply
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley cancelBubble : 1,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "currentTarget" : 1, // we supply
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews ctrlKey : 1,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews clientX : 1, // needed?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley clientY : 1, // needed?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley detail : 1, // not fully implemented
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "fromElement" : 1,
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley keyCode : 1,
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "height" : 1, // needed?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "initEvent" : 1, // need the init events?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "initMouseEvent" : 1,
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "initUIEvent" : 1,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "layerX" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "layerY" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews metaKey : 1,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "modifiers" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "offsetX" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "offsetY" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "preventDefault" : 1, // we supply
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "reason" : 1, // IE proprietary
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "relatedTarget" : 1,
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "returnValue" : 1, // needed?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley shiftKey : 1,
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews // "srcUrn" : 1, // IE proprietary
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews // "srcElement" : 1,
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews // "srcFilter" : 1, IE proprietary
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews // "stopPropagation" : 1, // we supply
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews // "target" : 1,
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews // "timeStamp" : 1, // needed?
985687b4e080182c1a3a2173df02b94a5b78b24fBob Halley // "toElement" : 1,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff // "view" : 1,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "which" : 1, // we supply
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // "width" : 1, // needed?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * webkit key remapping required for Safari < 3.1
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property webkitKeymap
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews 25: 9, // SHIFT-TAB (Safari provides a different key code in
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews // this case, even though the shiftKey modifier is set)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * Returns a wrapped node. Intended to be used on event targets,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * so it will return the node's parent if the target is a text
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * If accessing a property of the node throws an error, this is
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * probably the anonymous div wrapper Gecko adds inside text
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * nodes. This likely will only occur when attempting to access
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * the relatedTarget. In this case, we now return null because
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * the anonymous div is completely useless and we do not know
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * what the related target was because we can't even get to
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * the element's parent node.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @method resolve
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews resolve = function(n) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews } catch(e) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews return null;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews return Y.one(n);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews// provide a single event with browser abstractions resolved
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews// include all properties for both browers?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews// include only DOM2 spec properties?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews// provide browser-specific facade?
52637f592f705ca93fadc218e403fd55e8ce4aeaMark AndrewsY.DOMEventFacade = function(ev, currentTarget, wrapper) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews var e = ev, ot = currentTarget, d = Y.config.doc, b = d.body,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews x = e.pageX, y = e.pageY, c, t, de = d.documentElement,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews //////////////////////////////////////////////////////
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The native event
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property _event
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The X location of the event on the page (including scroll)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property pageX
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The Y location of the event on the page (including scroll)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property pageY
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews //////////////////////////////////////////////////////
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The keyCode for key events. Uses charCode if keyCode is not available
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property keyCode
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The charCode for key events. Same as keyCode
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property charCode
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews //////////////////////////////////////////////////////
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The button that was pushed.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property button
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The button that was pushed. Same as button.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @property which
de10b83a231451e03d08b3293c503d11ea03a90bMark Andrews //////////////////////////////////////////////////////
de10b83a231451e03d08b3293c503d11ea03a90bMark Andrews * Node reference for the targeted element
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @propery target
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @type Node
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews this.target = resolve(e.target || e.srcElement);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * Node reference for the element that the listener was attached to.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * @propery currentTarget
c1e7aff941dbf40090fec49300e728ad017d4f0cMark Andrews * @type Node
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * Node reference to the relatedTarget
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * @propery relatedTarget
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * @type Node
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * Number representing the direction and velocity of the movement of the mousewheel.
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * Negative is down, the higher the number, the faster. Applies to the mousewheel event.
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * @property wheelDelta
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews if (e.type == "mousewheel" || e.type == "DOMMouseScroll") {
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews this.wheelDelta = (e.detail) ? (e.detail * -1) : Math.round(e.wheelDelta / 80) || ((e.wheelDelta < 0) ? -1 : 1);
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews //////////////////////////////////////////////////////
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * Stops the propagation to the next bubble target
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * @method stopPropagation
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews this.stopPropagation = function() {
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * Stops the propagation to the next bubble target and
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * prevents any additional listeners from being exectued
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * on the current target.
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * @method stopImmediatePropagation
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews this.stopImmediatePropagation = function() {
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * Prevents the event's default behavior
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * @method preventDefault
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * @param returnValue {string} sets the returnValue of the event to this value
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * (rather than the default false value). This can be used to add a customized
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * confirmation query to the beforeunload event).
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * Stops the event propagation and prevents the default
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * event behavior.
d0221bfa6a08f7b284b231886928aa5d8ac87fc2Mark Andrews * @method halt
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * @param immediate {boolean} if true additional listeners
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * on the current target will not be executed
if (this._touch) {