MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / event-base-ie / event-base-ie-debug.js
blob4eb1455182e29363804525b5a1a2b63b9e4b3af5
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 (function() {
9 var stateChangeListener,
10     GLOBAL_ENV   = YUI.Env,
11     config       = YUI.config,
12     doc          = config.doc,
13     docElement   = doc && doc.documentElement,
14     EVENT_NAME   = 'onreadystatechange',
15     pollInterval = config.pollInterval || 40;
17 if (docElement.doScroll && !GLOBAL_ENV._ieready) {
18     GLOBAL_ENV._ieready = function() {
19         GLOBAL_ENV._ready();
20     };
22 /*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller/Diego Perini */
23 // Internet Explorer: use the doScroll() method on the root element.
24 // This isolates what appears to be a safe moment to manipulate the
25 // DOM prior to when the document's readyState suggests it is safe to do so.
26     if (self !== self.top) {
27         stateChangeListener = function() {
28             if (doc.readyState == 'complete') {
29                 GLOBAL_ENV.remove(doc, EVENT_NAME, stateChangeListener);
30                 GLOBAL_ENV.ieready();
31             }
32         };
33         GLOBAL_ENV.add(doc, EVENT_NAME, stateChangeListener);
34     } else {
35         GLOBAL_ENV._dri = setInterval(function() {
36             try {
37                 docElement.doScroll('left');
38                 clearInterval(GLOBAL_ENV._dri);
39                 GLOBAL_ENV._dri = null;
40                 GLOBAL_ENV._ieready();
41             } catch (domNotReady) { }
42         }, pollInterval);
43     }
46 })();
47 YUI.add('event-base-ie', function(Y) {
50  * Custom event engine, DOM event listener abstraction layer, synthetic DOM
51  * events.
52  * @module event
53  * @submodule event-base
54  */
56 function IEEventFacade() {
57     // IEEventFacade.superclass.constructor.apply(this, arguments);
58     Y.DOM2EventFacade.apply(this, arguments);
62  * (intentially left out of API docs)
63  * Alternate Facade implementation that is based on Object.defineProperty, which
64  * is partially supported in IE8.  Properties that involve setup work are
65  * deferred to temporary getters using the static _define method.
66  */
67 function IELazyFacade(e) {
68     var proxy = Y.config.doc.createEventObject(e),
69         proto = IELazyFacade.prototype;
71     // TODO: necessary?
72     proxy.hasOwnProperty = function () { return true; };
74     proxy.init = proto.init;
75     proxy.halt = proto.halt;
76     proxy.preventDefault           = proto.preventDefault;
77     proxy.stopPropagation          = proto.stopPropagation;
78     proxy.stopImmediatePropagation = proto.stopImmediatePropagation;
80     Y.DOM2EventFacade.apply(proxy, arguments);
82     return proxy;
86 var imp = Y.config.doc && Y.config.doc.implementation,
87     useLazyFacade = Y.config.lazyEventFacade,
89     buttonMap = {
90         0: 1, // left click
91         4: 2, // middle click
92         2: 3  // right click
93     },
94     relatedTargetMap = {
95         mouseout: 'toElement',
96         mouseover: 'fromElement'
97     },
99     resolve = Y.DOM2EventFacade.resolve,
101     proto = {
102         init: function() {
104             IEEventFacade.superclass.init.apply(this, arguments);
106             var e = this._event,
107                 x, y, d, b, de, t;
109             this.target = resolve(e.srcElement);
111             if (('clientX' in e) && (!x) && (0 !== x)) {
112                 x = e.clientX;
113                 y = e.clientY;
115                 d = Y.config.doc;
116                 b = d.body;
117                 de = d.documentElement;
119                 x += (de.scrollLeft || (b && b.scrollLeft) || 0);
120                 y += (de.scrollTop  || (b && b.scrollTop)  || 0);
122                 this.pageX = x;
123                 this.pageY = y;
124             }
126             if (e.type == "mouseout") {
127                 t = e.toElement;
128             } else if (e.type == "mouseover") {
129                 t = e.fromElement;
130             }
132             // fallback to t.relatedTarget to support simulated events.
133             // IE doesn't support setting toElement or fromElement on generic
134             // events, so Y.Event.simulate sets relatedTarget instead.
135             this.relatedTarget = resolve(t || e.relatedTarget);
137             // which should contain the unicode key code if this is a key event.
138             // For click events, which is normalized for which mouse button was
139             // clicked.
140             this.which = // chained assignment
141             this.button = e.keyCode || buttonMap[e.button] || e.button;
142         },
144         stopPropagation: function() {
145             this._event.cancelBubble = true;
146             this._wrapper.stopped = 1;
147             this.stopped = 1;
148         },
150         stopImmediatePropagation: function() {
151             this.stopPropagation();
152             this._wrapper.stopped = 2;
153             this.stopped = 2;
154         },
156         preventDefault: function(returnValue) {
157             this._event.returnValue = returnValue || false;
158             this._wrapper.prevented = 1;
159             this.prevented = 1;
160         }
161     };
163 Y.extend(IEEventFacade, Y.DOM2EventFacade, proto);
165 Y.extend(IELazyFacade, Y.DOM2EventFacade, proto);
166 IELazyFacade.prototype.init = function () {
167     var e         = this._event,
168         overrides = this._wrapper.overrides,
169         define    = IELazyFacade._define,
170         lazyProperties = IELazyFacade._lazyProperties,
171         prop;
173     this.altKey   = e.altKey;
174     this.ctrlKey  = e.ctrlKey;
175     this.metaKey  = e.metaKey;
176     this.shiftKey = e.shiftKey;
177     this.type     = (overrides && overrides.type) || e.type;
178     this.clientX  = e.clientX;
179     this.clientY  = e.clientY;
180     this.keyCode  = // chained assignment
181     this.charCode = e.keyCode;
182     this.which    = // chained assignment
183     this.button   = e.keyCode || buttonMap[e.button] || e.button;
185     for (prop in lazyProperties) {
186         if (lazyProperties.hasOwnProperty(prop)) {
187             define(this, prop, lazyProperties[prop]);
188         }
189     }
191     if (this._touch) {
192         this._touch(e, this._currentTarget, this._wrapper);
193     }
196 IELazyFacade._lazyProperties = {
197     target: function () {
198         return resolve(this._event.srcElement);
199     },
200     relatedTarget: function () {
201         var e = this._event,
202             targetProp = relatedTargetMap[e.type] || 'relatedTarget';
204         // fallback to t.relatedTarget to support simulated events.
205         // IE doesn't support setting toElement or fromElement on generic
206         // events, so Y.Event.simulate sets relatedTarget instead.
207         return resolve(e[targetProp] || e.relatedTarget);
208     },
209     currentTarget: function () {
210         return resolve(this._currentTarget);
211     },
213     wheelDelta: function () {
214         var e = this._event;
216         if (e.type === "mousewheel" || e.type === "DOMMouseScroll") {
217             return (e.detail) ?
218                 (e.detail * -1) :
219                 // wheelDelta between -80 and 80 result in -1 or 1
220                 Math.round(e.wheelDelta / 80) || ((e.wheelDelta < 0) ? -1 : 1);
221         }
222     },
224     pageX: function () {
225         var e = this._event,
226             val = e.pageX,
227             doc, bodyScroll, docScroll;
228                 
229         if (val === undefined) {
230             doc = Y.config.doc;
231             bodyScroll = doc.body && doc.body.scrollLeft;
232             docScroll = doc.documentElement.scrollLeft;
234             val = e.clientX + (docScroll || bodyScroll || 0);
235         }
237         return val;
238     },
239     pageY: function () {
240         var e = this._event,
241             val = e.pageY,
242             doc, bodyScroll, docScroll;
243                 
244         if (val === undefined) {
245             doc = Y.config.doc;
246             bodyScroll = doc.body && doc.body.scrollTop;
247             docScroll = doc.documentElement.scrollTop;
249             val = e.clientY + (docScroll || bodyScroll || 0);
250         }
252         return val;
253     }
258  * Wrapper function for Object.defineProperty that creates a property whose
259  * value will be calulated only when asked for.  After calculating the value,
260  * the getter wll be removed, so it will behave as a normal property beyond that
261  * point.  A setter is also assigned so assigning to the property will clear
262  * the getter, so foo.prop = 'a'; foo.prop; won't trigger the getter,
263  * overwriting value 'a'.
265  * Used only by the DOMEventFacades used by IE8 when the YUI configuration
266  * <code>lazyEventFacade</code> is set to true.
268  * @method _define
269  * @param o {DOMObject} A DOM object to add the property to
270  * @param prop {String} The name of the new property
271  * @param valueFn {Function} The function that will return the initial, default
272  *                  value for the property.
273  * @static
274  * @private
275  */
276 IELazyFacade._define = function (o, prop, valueFn) {
277     function val(v) {
278         var ret = (arguments.length) ? v : valueFn.call(this);
280         delete o[prop];
281         Object.defineProperty(o, prop, {
282             value: ret,
283             configurable: true,
284             writable: true
285         });
286         return ret;
287     }
288     Object.defineProperty(o, prop, {
289         get: val,
290         set: val,
291         configurable: true
292     });
295 if (imp && (!imp.hasFeature('Events', '2.0'))) {
296     if (useLazyFacade) {
297         // Make sure we can use the lazy facade logic
298         try {
299             Object.defineProperty(Y.config.doc.createEventObject(), 'z', {});
300         } catch (e) {
301             useLazyFacade = false;
302         }
303     }
304         
305     Y.DOMEventFacade = (useLazyFacade) ? IELazyFacade : IEEventFacade;
309 }, '3.5.1' ,{after:['event-base'], requires:['node-base']});