Roo/EventManager.js
[roojs1] / Roo / EventManager.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
13  * @class Roo.EventManager
14  * Registers event handlers that want to receive a normalized EventObject instead of the standard browser event and provides 
15  * several useful events directly.
16  * See {@link Roo.EventObject} for more details on normalized event objects.
17  * @singleton
18  */
19 Roo.EventManager = function(){
20     var docReadyEvent, docReadyProcId, docReadyState = false;
21     var resizeEvent, resizeTask, textEvent, textSize;
22     var E = Roo.lib.Event;
23     var D = Roo.lib.Dom;
24
25     
26     
27
28     var fireDocReady = function(){
29         if(!docReadyState){
30             docReadyState = true;
31             Roo.isReady = true;
32             if(docReadyProcId){
33                 clearInterval(docReadyProcId);
34             }
35             if(Roo.isGecko || Roo.isOpera) {
36                 document.removeEventListener("DOMContentLoaded", fireDocReady, false);
37             }
38             if(Roo.isIE){
39                 var defer = document.getElementById("ie-deferred-loader");
40                 if(defer){
41                     defer.onreadystatechange = null;
42                     defer.parentNode.removeChild(defer);
43                 }
44             }
45             if(docReadyEvent){
46                 docReadyEvent.fire();
47                 docReadyEvent.clearListeners();
48             }
49         }
50     };
51     
52     var initDocReady = function(){
53         docReadyEvent = new Roo.util.Event();
54         if(Roo.isGecko || Roo.isOpera) {
55             document.addEventListener("DOMContentLoaded", fireDocReady, false);
56         }else if(Roo.isIE){
57             document.write("<s"+'cript id="ie-deferred-loader" defer="defer" src="/'+'/:"></s'+"cript>");
58             var defer = document.getElementById("ie-deferred-loader");
59             defer.onreadystatechange = function(){
60                 if(this.readyState == "complete"){
61                     fireDocReady();
62                 }
63             };
64         }else if(Roo.isSafari){ 
65             docReadyProcId = setInterval(function(){
66                 var rs = document.readyState;
67                 if(rs == "complete") {
68                     fireDocReady();     
69                  }
70             }, 10);
71         }
72         // no matter what, make sure it fires on load
73         E.on(window, "load", fireDocReady);
74     };
75
76     var createBuffered = function(h, o){
77         var task = new Roo.util.DelayedTask(h);
78         return function(e){
79             // create new event object impl so new events don't wipe out properties
80             e = new Roo.EventObjectImpl(e);
81             task.delay(o.buffer, h, null, [e]);
82         };
83     };
84
85     var createSingle = function(h, el, ename, fn){
86         return function(e){
87             Roo.EventManager.removeListener(el, ename, fn);
88             h(e);
89         };
90     };
91
92     var createDelayed = function(h, o){
93         return function(e){
94             // create new event object impl so new events don't wipe out properties
95             e = new Roo.EventObjectImpl(e);
96             setTimeout(function(){
97                 h(e);
98             }, o.delay || 10);
99         };
100     };
101     var transitionEndVal = false;
102     
103     var transitionEnd = function()
104     {
105         if (transitionEndVal) {
106             return transitionEndVal;
107         }
108         var el = document.createElement('div');
109
110         var transEndEventNames = {
111             WebkitTransition : 'webkitTransitionEnd',
112             MozTransition    : 'transitionend',
113             OTransition      : 'oTransitionEnd otransitionend',
114             transition       : 'transitionend'
115         };
116     
117         for (var name in transEndEventNames) {
118             if (el.style[name] !== undefined) {
119                 transitionEndVal = transEndEventNames[name];
120                 return  transitionEndVal ;
121             }
122         }
123     }
124     
125
126     var listen = function(element, ename, opt, fn, scope){
127         var o = (!opt || typeof opt == "boolean") ? {} : opt;
128         fn = fn || o.fn; scope = scope || o.scope;
129         var el = Roo.getDom(element);
130         
131         
132         if(!el){
133             throw "Error listening for \"" + ename + '\". Element "' + element + '" doesn\'t exist.';
134         }
135         
136         if (ename == 'transitionend') {
137             ename = transitionEnd();
138         }
139         var h = function(e){
140             e = Roo.EventObject.setEvent(e);
141             var t;
142             if(o.delegate){
143                 t = e.getTarget(o.delegate, el);
144                 if(!t){
145                     return;
146                 }
147             }else{
148                 t = e.target;
149             }
150             if(o.stopEvent === true){
151                 e.stopEvent();
152             }
153             if(o.preventDefault === true){
154                e.preventDefault();
155             }
156             if(o.stopPropagation === true){
157                 e.stopPropagation();
158             }
159
160             if(o.normalized === false){
161                 e = e.browserEvent;
162             }
163
164             fn.call(scope || el, e, t, o);
165         };
166         if(o.delay){
167             h = createDelayed(h, o);
168         }
169         if(o.single){
170             h = createSingle(h, el, ename, fn);
171         }
172         if(o.buffer){
173             h = createBuffered(h, o);
174         }
175         fn._handlers = fn._handlers || [];
176         
177         
178         fn._handlers.push([Roo.id(el), ename, h]);
179         
180         
181          
182         E.on(el, ename, h);
183         if(ename == "mousewheel" && el.addEventListener){ // workaround for jQuery
184             el.addEventListener("DOMMouseScroll", h, false);
185             E.on(window, 'unload', function(){
186                 el.removeEventListener("DOMMouseScroll", h, false);
187             });
188         }
189         if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document
190             Roo.EventManager.stoppedMouseDownEvent.addListener(h);
191         }
192         return h;
193     };
194
195     var stopListening = function(el, ename, fn){
196         var id = Roo.id(el), hds = fn._handlers, hd = fn;
197         if(hds){
198             for(var i = 0, len = hds.length; i < len; i++){
199                 var h = hds[i];
200                 if(h[0] == id && h[1] == ename){
201                     hd = h[2];
202                     hds.splice(i, 1);
203                     break;
204                 }
205             }
206         }
207         E.un(el, ename, hd);
208         el = Roo.getDom(el);
209         if(ename == "mousewheel" && el.addEventListener){
210             el.removeEventListener("DOMMouseScroll", hd, false);
211         }
212         if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document
213             Roo.EventManager.stoppedMouseDownEvent.removeListener(hd);
214         }
215     };
216
217     var propRe = /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/;
218     
219     var pub = {
220         
221         
222         /** 
223          * Fix for doc tools
224          * @scope Roo.EventManager
225          */
226         
227         
228         /** 
229          * This is no longer needed and is deprecated. Places a simple wrapper around an event handler to override the browser event
230          * object with a Roo.EventObject
231          * @param {Function} fn        The method the event invokes
232          * @param {Object}   scope    An object that becomes the scope of the handler
233          * @param {boolean}  override If true, the obj passed in becomes
234          *                             the execution scope of the listener
235          * @return {Function} The wrapped function
236          * @deprecated
237          */
238         wrap : function(fn, scope, override){
239             return function(e){
240                 Roo.EventObject.setEvent(e);
241                 fn.call(override ? scope || window : window, Roo.EventObject, scope);
242             };
243         },
244         
245         /**
246      * Appends an event handler to an element (shorthand for addListener)
247      * @param {String/HTMLElement}   element        The html element or id to assign the
248      * @param {String}   eventName The type of event to listen for
249      * @param {Function} handler The method the event invokes
250      * @param {Object}   scope (optional) The scope in which to execute the handler
251      * function. The handler function's "this" context.
252      * @param {Object}   options (optional) An object containing handler configuration
253      * properties. This may contain any of the following properties:<ul>
254      * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
255      * <li>delegate {String} A simple selector to filter the target or look for a descendant of the target</li>
256      * <li>stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.</li>
257      * <li>preventDefault {Boolean} True to prevent the default action</li>
258      * <li>stopPropagation {Boolean} True to prevent event propagation</li>
259      * <li>normalized {Boolean} False to pass a browser event to the handler function instead of an Roo.EventObject</li>
260      * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
261      * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
262      * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
263      * by the specified number of milliseconds. If the event fires again within that time, the original
264      * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
265      * </ul><br>
266      * <p>
267      * <b>Combining Options</b><br>
268      * Using the options argument, it is possible to combine different types of listeners:<br>
269      * <br>
270      * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)<div style="margin: 5px 20px 20px;">
271      * Code:<pre><code>
272 el.on('click', this.onClick, this, {
273     single: true,
274     delay: 100,
275     stopEvent : true,
276     forumId: 4
277 });</code></pre>
278      * <p>
279      * <b>Attaching multiple handlers in 1 call</b><br>
280       * The method also allows for a single argument to be passed which is a config object containing properties
281      * which specify multiple handlers.
282      * <p>
283      * Code:<pre><code>
284 el.on({
285     'click' : {
286         fn: this.onClick
287         scope: this,
288         delay: 100
289     },
290     'mouseover' : {
291         fn: this.onMouseOver
292         scope: this
293     },
294     'mouseout' : {
295         fn: this.onMouseOut
296         scope: this
297     }
298 });</code></pre>
299      * <p>
300      * Or a shorthand syntax:<br>
301      * Code:<pre><code>
302 el.on({
303     'click' : this.onClick,
304     'mouseover' : this.onMouseOver,
305     'mouseout' : this.onMouseOut
306     scope: this
307 });</code></pre>
308      */
309         addListener : function(element, eventName, fn, scope, options){
310             if(typeof eventName == "object"){
311                 var o = eventName;
312                 for(var e in o){
313                     if(propRe.test(e)){
314                         continue;
315                     }
316                     if(typeof o[e] == "function"){
317                         // shared options
318                         listen(element, e, o, o[e], o.scope);
319                     }else{
320                         // individual options
321                         listen(element, e, o[e]);
322                     }
323                 }
324                 return;
325             }
326             return listen(element, eventName, options, fn, scope);
327         },
328         
329         /**
330          * Removes an event handler
331          *
332          * @param {String/HTMLElement}   element        The id or html element to remove the 
333          *                             event from
334          * @param {String}   eventName     The type of event
335          * @param {Function} fn
336          * @return {Boolean} True if a listener was actually removed
337          */
338         removeListener : function(element, eventName, fn){
339             return stopListening(element, eventName, fn);
340         },
341         
342         /**
343          * Fires when the document is ready (before onload and before images are loaded). Can be 
344          * accessed shorthanded Roo.onReady().
345          * @param {Function} fn        The method the event invokes
346          * @param {Object}   scope    An  object that becomes the scope of the handler
347          * @param {boolean}  options
348          */
349         onDocumentReady : function(fn, scope, options){
350             if(docReadyState){ // if it already fired
351                 docReadyEvent.addListener(fn, scope, options);
352                 docReadyEvent.fire();
353                 docReadyEvent.clearListeners();
354                 return;
355             }
356             if(!docReadyEvent){
357                 initDocReady();
358             }
359             docReadyEvent.addListener(fn, scope, options);
360         },
361         
362         /**
363          * Fires when the window is resized and provides resize event buffering (50 milliseconds), passes new viewport width and height to handlers.
364          * @param {Function} fn        The method the event invokes
365          * @param {Object}   scope    An object that becomes the scope of the handler
366          * @param {boolean}  options
367          */
368         onWindowResize : function(fn, scope, options){
369             if(!resizeEvent){
370                 resizeEvent = new Roo.util.Event();
371                 resizeTask = new Roo.util.DelayedTask(function(){
372                     resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
373                 });
374                 E.on(window, "resize", function(){
375                     if(Roo.isIE){
376                         resizeTask.delay(50);
377                     }else{
378                         resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
379                     }
380                 });
381             }
382             resizeEvent.addListener(fn, scope, options);
383         },
384
385         /**
386          * Fires when the user changes the active text size. Handler gets called with 2 params, the old size and the new size.
387          * @param {Function} fn        The method the event invokes
388          * @param {Object}   scope    An object that becomes the scope of the handler
389          * @param {boolean}  options
390          */
391         onTextResize : function(fn, scope, options){
392             if(!textEvent){
393                 textEvent = new Roo.util.Event();
394                 var textEl = new Roo.Element(document.createElement('div'));
395                 textEl.dom.className = 'x-text-resize';
396                 textEl.dom.innerHTML = 'X';
397                 textEl.appendTo(document.body);
398                 textSize = textEl.dom.offsetHeight;
399                 setInterval(function(){
400                     if(textEl.dom.offsetHeight != textSize){
401                         textEvent.fire(textSize, textSize = textEl.dom.offsetHeight);
402                     }
403                 }, this.textResizeInterval);
404             }
405             textEvent.addListener(fn, scope, options);
406         },
407
408         /**
409          * Removes the passed window resize listener.
410          * @param {Function} fn        The method the event invokes
411          * @param {Object}   scope    The scope of handler
412          */
413         removeResizeListener : function(fn, scope){
414             if(resizeEvent){
415                 resizeEvent.removeListener(fn, scope);
416             }
417         },
418
419         // private
420         fireResize : function(){
421             if(resizeEvent){
422                 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
423             }   
424         },
425         /**
426          * Url used for onDocumentReady with using SSL (defaults to Roo.SSL_SECURE_URL)
427          */
428         ieDeferSrc : false,
429         /**
430          * The frequency, in milliseconds, to check for text resize events (defaults to 50)
431          */
432         textResizeInterval : 50
433     };
434     
435     /**
436      * Fix for doc tools
437      * @scopeAlias pub=Roo.EventManager
438      */
439     
440      /**
441      * Appends an event handler to an element (shorthand for addListener)
442      * @param {String/HTMLElement}   element        The html element or id to assign the
443      * @param {String}   eventName The type of event to listen for
444      * @param {Function} handler The method the event invokes
445      * @param {Object}   scope (optional) The scope in which to execute the handler
446      * function. The handler function's "this" context.
447      * @param {Object}   options (optional) An object containing handler configuration
448      * properties. This may contain any of the following properties:<ul>
449      * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
450      * <li>delegate {String} A simple selector to filter the target or look for a descendant of the target</li>
451      * <li>stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.</li>
452      * <li>preventDefault {Boolean} True to prevent the default action</li>
453      * <li>stopPropagation {Boolean} True to prevent event propagation</li>
454      * <li>normalized {Boolean} False to pass a browser event to the handler function instead of an Roo.EventObject</li>
455      * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
456      * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
457      * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
458      * by the specified number of milliseconds. If the event fires again within that time, the original
459      * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
460      * </ul><br>
461      * <p>
462      * <b>Combining Options</b><br>
463      * Using the options argument, it is possible to combine different types of listeners:<br>
464      * <br>
465      * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)<div style="margin: 5px 20px 20px;">
466      * Code:<pre><code>
467 el.on('click', this.onClick, this, {
468     single: true,
469     delay: 100,
470     stopEvent : true,
471     forumId: 4
472 });</code></pre>
473      * <p>
474      * <b>Attaching multiple handlers in 1 call</b><br>
475       * The method also allows for a single argument to be passed which is a config object containing properties
476      * which specify multiple handlers.
477      * <p>
478      * Code:<pre><code>
479 el.on({
480     'click' : {
481         fn: this.onClick
482         scope: this,
483         delay: 100
484     },
485     'mouseover' : {
486         fn: this.onMouseOver
487         scope: this
488     },
489     'mouseout' : {
490         fn: this.onMouseOut
491         scope: this
492     }
493 });</code></pre>
494      * <p>
495      * Or a shorthand syntax:<br>
496      * Code:<pre><code>
497 el.on({
498     'click' : this.onClick,
499     'mouseover' : this.onMouseOver,
500     'mouseout' : this.onMouseOut
501     scope: this
502 });</code></pre>
503      */
504     pub.on = pub.addListener;
505     pub.un = pub.removeListener;
506
507     pub.stoppedMouseDownEvent = new Roo.util.Event();
508     return pub;
509 }();
510 /**
511   * Fires when the document is ready (before onload and before images are loaded).  Shorthand of {@link Roo.EventManager#onDocumentReady}.
512   * @param {Function} fn        The method the event invokes
513   * @param {Object}   scope    An  object that becomes the scope of the handler
514   * @param {boolean}  override If true, the obj passed in becomes
515   *                             the execution scope of the listener
516   * @member Roo
517   * @method onReady
518  */
519 Roo.onReady = Roo.EventManager.onDocumentReady;
520
521 Roo.onReady(function(){
522     var bd = Roo.get(document.body);
523     if(!bd){ return; }
524
525     var cls = [
526             Roo.isIE ? "roo-ie"
527             : Roo.isIE11 ? "roo-ie11"
528             : Roo.isGecko ? "roo-gecko"
529             : Roo.isOpera ? "roo-opera"
530             : Roo.isSafari ? "roo-safari" : ""];
531
532     if(Roo.isMac){
533         cls.push("roo-mac");
534     }
535     if(Roo.isLinux){
536         cls.push("roo-linux");
537     }
538     if(Roo.isIOS){
539         cls.push("roo-ios");
540     }
541     if(Roo.isTouch){
542         cls.push("roo-touch");
543     }
544     if(Roo.isBorderBox){
545         cls.push('roo-border-box');
546     }
547     if(Roo.isStrict){ // add to the parent to allow for selectors like ".ext-strict .ext-ie"
548         var p = bd.dom.parentNode;
549         if(p){
550             p.className += ' roo-strict';
551         }
552     }
553     bd.addClass(cls.join(' '));
554 });
555
556 /**
557  * @class Roo.EventObject
558  * EventObject exposes the Yahoo! UI Event functionality directly on the object
559  * passed to your event handler. It exists mostly for convenience. It also fixes the annoying null checks automatically to cleanup your code 
560  * Example:
561  * <pre><code>
562  function handleClick(e){ // e is not a standard event object, it is a Roo.EventObject
563     e.preventDefault();
564     var target = e.getTarget();
565     ...
566  }
567  var myDiv = Roo.get("myDiv");
568  myDiv.on("click", handleClick);
569  //or
570  Roo.EventManager.on("myDiv", 'click', handleClick);
571  Roo.EventManager.addListener("myDiv", 'click', handleClick);
572  </code></pre>
573  * @singleton
574  */
575 Roo.EventObject = function(){
576     
577     var E = Roo.lib.Event;
578     
579     // safari keypress events for special keys return bad keycodes
580     var safariKeys = {
581         63234 : 37, // left
582         63235 : 39, // right
583         63232 : 38, // up
584         63233 : 40, // down
585         63276 : 33, // page up
586         63277 : 34, // page down
587         63272 : 46, // delete
588         63273 : 36, // home
589         63275 : 35  // end
590     };
591
592     // normalize button clicks
593     var btnMap = Roo.isIE ? {1:0,4:1,2:2} :
594                 (Roo.isSafari ? {1:0,2:1,3:2} : {0:0,1:1,2:2});
595
596     Roo.EventObjectImpl = function(e){
597         if(e){
598             this.setEvent(e.browserEvent || e);
599         }
600     };
601     Roo.EventObjectImpl.prototype = {
602         /**
603          * Used to fix doc tools.
604          * @scope Roo.EventObject.prototype
605          */
606             
607
608         
609         
610         /** The normal browser event */
611         browserEvent : null,
612         /** The button pressed in a mouse event */
613         button : -1,
614         /** True if the shift key was down during the event */
615         shiftKey : false,
616         /** True if the control key was down during the event */
617         ctrlKey : false,
618         /** True if the alt key was down during the event */
619         altKey : false,
620
621         /** Key constant 
622         * @type Number */
623         BACKSPACE : 8,
624         /** Key constant 
625         * @type Number */
626         TAB : 9,
627         /** Key constant 
628         * @type Number */
629         RETURN : 13,
630         /** Key constant 
631         * @type Number */
632         ENTER : 13,
633         /** Key constant 
634         * @type Number */
635         SHIFT : 16,
636         /** Key constant 
637         * @type Number */
638         CONTROL : 17,
639         /** Key constant 
640         * @type Number */
641         ESC : 27,
642         /** Key constant 
643         * @type Number */
644         SPACE : 32,
645         /** Key constant 
646         * @type Number */
647         PAGEUP : 33,
648         /** Key constant 
649         * @type Number */
650         PAGEDOWN : 34,
651         /** Key constant 
652         * @type Number */
653         END : 35,
654         /** Key constant 
655         * @type Number */
656         HOME : 36,
657         /** Key constant 
658         * @type Number */
659         LEFT : 37,
660         /** Key constant 
661         * @type Number */
662         UP : 38,
663         /** Key constant 
664         * @type Number */
665         RIGHT : 39,
666         /** Key constant 
667         * @type Number */
668         DOWN : 40,
669         /** Key constant 
670         * @type Number */
671         DELETE : 46,
672         /** Key constant 
673         * @type Number */
674         F5 : 116,
675
676            /** @private */
677         setEvent : function(e){
678             if(e == this || (e && e.browserEvent)){ // already wrapped
679                 return e;
680             }
681             this.browserEvent = e;
682             if(e){
683                 // normalize buttons
684                 this.button = e.button ? btnMap[e.button] : (e.which ? e.which-1 : -1);
685                 if(e.type == 'click' && this.button == -1){
686                     this.button = 0;
687                 }
688                 this.type = e.type;
689                 this.shiftKey = e.shiftKey;
690                 // mac metaKey behaves like ctrlKey
691                 this.ctrlKey = e.ctrlKey || e.metaKey;
692                 this.altKey = e.altKey;
693                 // in getKey these will be normalized for the mac
694                 this.keyCode = e.keyCode;
695                 // keyup warnings on firefox.
696                 this.charCode = (e.type == 'keyup' || e.type == 'keydown') ? 0 : e.charCode;
697                 // cache the target for the delayed and or buffered events
698                 this.target = E.getTarget(e);
699                 // same for XY
700                 this.xy = E.getXY(e);
701             }else{
702                 this.button = -1;
703                 this.shiftKey = false;
704                 this.ctrlKey = false;
705                 this.altKey = false;
706                 this.keyCode = 0;
707                 this.charCode =0;
708                 this.target = null;
709                 this.xy = [0, 0];
710             }
711             return this;
712         },
713
714         /**
715          * Stop the event (preventDefault and stopPropagation)
716          */
717         stopEvent : function(){
718             if(this.browserEvent){
719                 if(this.browserEvent.type == 'mousedown'){
720                     Roo.EventManager.stoppedMouseDownEvent.fire(this);
721                 }
722                 E.stopEvent(this.browserEvent);
723             }
724         },
725
726         /**
727          * Prevents the browsers default handling of the event.
728          */
729         preventDefault : function(){
730             if(this.browserEvent){
731                 E.preventDefault(this.browserEvent);
732             }
733         },
734
735         /** @private */
736         isNavKeyPress : function(){
737             var k = this.keyCode;
738             k = Roo.isSafari ? (safariKeys[k] || k) : k;
739             return (k >= 33 && k <= 40) || k == this.RETURN || k == this.TAB || k == this.ESC;
740         },
741
742         isSpecialKey : function(){
743             var k = this.keyCode;
744             return (this.type == 'keypress' && this.ctrlKey) || k == 9 || k == 13  || k == 40 || k == 27 ||
745             (k == 16) || (k == 17) ||
746             (k >= 18 && k <= 20) ||
747             (k >= 33 && k <= 35) ||
748             (k >= 36 && k <= 39) ||
749             (k >= 44 && k <= 45);
750         },
751         /**
752          * Cancels bubbling of the event.
753          */
754         stopPropagation : function(){
755             if(this.browserEvent){
756                 if(this.type == 'mousedown'){
757                     Roo.EventManager.stoppedMouseDownEvent.fire(this);
758                 }
759                 E.stopPropagation(this.browserEvent);
760             }
761         },
762
763         /**
764          * Gets the key code for the event.
765          * @return {Number}
766          */
767         getCharCode : function(){
768             return this.charCode || this.keyCode;
769         },
770
771         /**
772          * Returns a normalized keyCode for the event.
773          * @return {Number} The key code
774          */
775         getKey : function(){
776             var k = this.keyCode || this.charCode;
777             return Roo.isSafari ? (safariKeys[k] || k) : k;
778         },
779
780         /**
781          * Gets the x coordinate of the event.
782          * @return {Number}
783          */
784         getPageX : function(){
785             return this.xy[0];
786         },
787
788         /**
789          * Gets the y coordinate of the event.
790          * @return {Number}
791          */
792         getPageY : function(){
793             return this.xy[1];
794         },
795
796         /**
797          * Gets the time of the event.
798          * @return {Number}
799          */
800         getTime : function(){
801             if(this.browserEvent){
802                 return E.getTime(this.browserEvent);
803             }
804             return null;
805         },
806
807         /**
808          * Gets the page coordinates of the event.
809          * @return {Array} The xy values like [x, y]
810          */
811         getXY : function(){
812             return this.xy;
813         },
814
815         /**
816          * Gets the target for the event.
817          * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
818          * @param {Number/String/HTMLElement/Element} maxDepth (optional) The max depth to
819                 search as a number or element (defaults to 10 || document.body)
820          * @param {Boolean} returnEl (optional) True to return a Roo.Element object instead of DOM node
821          * @return {HTMLelement}
822          */
823         getTarget : function(selector, maxDepth, returnEl){
824             return selector ? Roo.fly(this.target).findParent(selector, maxDepth, returnEl) : this.target;
825         },
826         /**
827          * Gets the related target.
828          * @return {HTMLElement}
829          */
830         getRelatedTarget : function(){
831             if(this.browserEvent){
832                 return E.getRelatedTarget(this.browserEvent);
833             }
834             return null;
835         },
836
837         /**
838          * Normalizes mouse wheel delta across browsers
839          * @return {Number} The delta
840          */
841         getWheelDelta : function(){
842             var e = this.browserEvent;
843             var delta = 0;
844             if(e.wheelDelta){ /* IE/Opera. */
845                 delta = e.wheelDelta/120;
846             }else if(e.detail){ /* Mozilla case. */
847                 delta = -e.detail/3;
848             }
849             return delta;
850         },
851
852         /**
853          * Returns true if the control, meta, shift or alt key was pressed during this event.
854          * @return {Boolean}
855          */
856         hasModifier : function(){
857             return !!((this.ctrlKey || this.altKey) || this.shiftKey);
858         },
859
860         /**
861          * Returns true if the target of this event equals el or is a child of el
862          * @param {String/HTMLElement/Element} el
863          * @param {Boolean} related (optional) true to test if the related target is within el instead of the target
864          * @return {Boolean}
865          */
866         within : function(el, related){
867             var t = this[related ? "getRelatedTarget" : "getTarget"]();
868             return t && Roo.fly(el).contains(t);
869         },
870
871         getPoint : function(){
872             return new Roo.lib.Point(this.xy[0], this.xy[1]);
873         }
874     };
875
876     return new Roo.EventObjectImpl();
877 }();
878             
879