event-facade-dom-ie.js revision cfdfb15e991f069dc5a2c3a724d378a6395d4eac
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove/*
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * Custom event engine, DOM event listener abstraction layer, synthetic DOM
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * events.
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * @module event
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * @submodule event-base
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove */
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grovefunction IEEventFacade() {
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove // IEEventFacade.superclass.constructor.apply(this, arguments);
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove Y.DOM2EventFacade.apply(this, arguments);
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove}
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove/*
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * (intentially left out of API docs)
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * Alternate Facade implementation that is based on Object.defineProperty, which
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * is partially supported in IE8. Properties that involve setup work are
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove * deferred to temporary getters using the static _define method.
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove */
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grovefunction IELazyFacade(e) {
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove var proxy = Y.config.doc.createEventObject(e),
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proto = IELazyFacade.prototype;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove // TODO: necessary?
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proxy.hasOwnProperty = function () { return true; };
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proxy.init = proto.init;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proxy.halt = proto.halt;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proxy.preventDefault = proto.preventDefault;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proxy.stopPropagation = proto.stopPropagation;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove proxy.stopImmediatePropagation = proto.stopImmediatePropagation;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove Y.DOM2EventFacade.apply(proxy, arguments);
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove return proxy;
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove}
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grove
288368074b7a92b28307521f68f41c3dd6e6969cRyan Grovevar imp = Y.config.doc && Y.config.doc.implementation,
useLazyFacade = true, //Y.config.lazyEventFacade,
buttonMap = {
2: 3,
4: 2
},
relatedTargetMap = {
mouseout: 'toElement',
mouseover: 'fromElement'
},
resolve = Y.DOM2EventFacade.resolve,
proto = {
init: function() {
IEEventFacade.superclass.init.apply(this, arguments);
var e = this._event,
x, y, d, b, de, t;
this.target = resolve(e.srcElement);
if (('clientX' in e) && (!x) && (0 !== x)) {
x = e.clientX;
y = e.clientY;
d = Y.config.doc;
b = d.body;
de = d.documentElement;
x += (de.scrollLeft || (b && b.scrollLeft) || 0);
y += (de.scrollTop || (b && b.scrollTop) || 0);
this.pageX = x;
this.pageY = y;
}
if (e.type == "mouseout") {
t = e.toElement;
} else if (e.type == "mouseover") {
t = e.fromElement;
}
this.relatedTarget = resolve(t);
// which should contain the unicode key code if this is a key event
// if (e.charCode) {
// this.which = e.charCode;
// }
// for click events, which is normalized for which mouse button was
// clicked.
if (e.button) {
this.which = this.button = buttonMap[e.button] || e.button;
}
},
stopPropagation: function() {
this._event.cancelBubble = true;
this._wrapper.stopped = 1;
this.stopped = 1;
},
stopImmediatePropagation: function() {
this.stopPropagation();
this._wrapper.stopped = 2;
this.stopped = 2;
},
preventDefault: function(returnValue) {
this._event.returnValue = returnValue || false;
this._wrapper.prevented = 1;
this.prevented = 1;
}
};
Y.extend(IEEventFacade, Y.DOM2EventFacade, proto);
Y.extend(IELazyFacade, Y.DOM2EventFacade, proto);
IELazyFacade.prototype.init = function () {
var e = this._event,
overrides = this._wrapper.overrides,
define = IELazyFacade._define,
lazyProperties = IELazyFacade._lazyProperties,
prop;
this.altKey = e.altKey;
this.ctrlKey = e.ctrlKey;
this.metaKey = e.metaKey;
this.shiftKey = e.shiftKey;
this.type = (overrides && overrides.type) || e.type;
this.clientX = e.clientX;
this.clientY = e.clientY;
for (prop in lazyProperties) {
if (lazyProperties.hasOwnProperty(prop)) {
define(this, prop, lazyProperties[prop]);
}
}
if (this._touch) {
this._touch(e, this._currentTarget, this._wrapper);
}
};
IELazyFacade._lazyProperties = {
charCode: function () {
var e = this._event;
return e.keyCode || e.charCode;
},
keyCode: function () { return this.charCode; },
button: function () {
var e = this._event;
return (e.button) ?
(buttonMap[e.button] || e.button) :
(e.which || e.charCode || this.charCode);
},
which: function () { return this.button; },
target: function () {
return resolve(this._event.srcElement);
},
relatedTarget: function () {
var e = this._event,
targetProp = relatedTargetMap[e.type] || 'relatedTarget';
return resolve(e[targetProp]);
},
currentTarget: function () {
return resolve(this._currentTarget);
},
wheelDelta: function () {
var e = this._event;
if (e.type === "mousewheel" || e.type === "DOMMouseScroll") {
return (e.detail) ?
(e.detail * -1) :
// wheelDelta between -80 and 80 result in -1 or 1
Math.round(e.wheelDelta / 80) || ((e.wheelDelta < 0) ? -1 : 1);
}
},
pageX: function () {
var e = this._event,
val = e.pageX,
doc, bodyScroll, docScroll;
if (val === undefined) {
doc = Y.config.doc;
bodyScroll = doc.body && doc.body.scrollLeft;
docScroll = doc.documentElement.scrollLeft;
val = e.clientX + (docScroll || bodyScroll || 0);
}
return val;
},
pageY: function () {
var e = this._event,
val = e.pageY,
doc, bodyScroll, docScroll;
if (val === undefined) {
doc = Y.config.doc;
bodyScroll = doc.body && doc.body.scrollTop;
docScroll = doc.documentElement.scrollTop;
val = e.clientY + (docScroll || bodyScroll || 0);
}
return val;
}
};
/**
* Wrapper function for Object.defineProperty that creates a property whose
* value will be calulated only when asked for. After calculating the value,
* the getter wll be removed, so it will behave as a normal property beyond that
* point. A setter is also assigned so assigning to the property will clear
* the getter, so foo.prop = 'a'; foo.prop; won't trigger the getter,
* overwriting value 'a'.
*
* Used only by the DOMEventFacades used by IE8 when the YUI configuration
* <code>lazyEventFacade</code> is set to true.
*
* @method _define
* @param o {DOMObject} A DOM object to add the property to
* @param prop {String} The name of the new property
* @param valueFn {Function} The function that will return the initial, default
* value for the property.
* @static
* @private
*/
IELazyFacade._define = function (o, prop, valueFn) {
function val(v) {
var ret = (arguments.length) ? v : valueFn.call(this);
delete o[prop];
Object.defineProperty(o, prop, {
value: ret,
configurable: true,
writable: true
});
return ret;
}
Object.defineProperty(o, prop, {
get: val,
set: val,
configurable: true
});
};
if (imp && (!imp.hasFeature('Events', '2.0'))) {
if (useLazyFacade) {
// Make sure we can use the lazy facade logic
try {
Object.defineProperty(Y.config.doc.createEventObject(), 'z', {});
} catch (e) {
useLazyFacade = false;
}
}
Y.DOMEventFacade = (useLazyFacade) ? IELazyFacade : IEEventFacade;
}