4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
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.
19 Roo.EventManager = function(){
20 var docReadyEvent, docReadyProcId, docReadyState = false;
21 var resizeEvent, resizeTask, textEvent, textSize;
22 var E = Roo.lib.Event;
28 var fireDocReady = function(){
33 clearInterval(docReadyProcId);
35 if(Roo.isGecko || Roo.isOpera) {
36 document.removeEventListener("DOMContentLoaded", fireDocReady, false);
39 var defer = document.getElementById("ie-deferred-loader");
41 defer.onreadystatechange = null;
42 defer.parentNode.removeChild(defer);
47 docReadyEvent.clearListeners();
52 var initDocReady = function(){
53 docReadyEvent = new Roo.util.Event();
54 if(Roo.isGecko || Roo.isOpera) {
55 document.addEventListener("DOMContentLoaded", fireDocReady, false);
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"){
64 }else if(Roo.isSafari){
65 docReadyProcId = setInterval(function(){
66 var rs = document.readyState;
67 if(rs == "complete") {
72 // no matter what, make sure it fires on load
73 E.on(window, "load", fireDocReady);
76 var createBuffered = function(h, o){
77 var task = new Roo.util.DelayedTask(h);
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]);
85 var createSingle = function(h, el, ename, fn){
87 Roo.EventManager.removeListener(el, ename, fn);
92 var createDelayed = function(h, o){
94 // create new event object impl so new events don't wipe out properties
95 e = new Roo.EventObjectImpl(e);
96 setTimeout(function(){
101 var transitionEndVal = false;
103 var transitionEnd = function()
105 if (transitionEndVal) {
106 return transitionEndVal;
108 var el = document.createElement('div');
110 var transEndEventNames = {
111 WebkitTransition : 'webkitTransitionEnd',
112 MozTransition : 'transitionend',
113 OTransition : 'oTransitionEnd otransitionend',
114 transition : 'transitionend'
117 for (var name in transEndEventNames) {
118 if (el.style[name] !== undefined) {
119 transitionEndVal = transEndEventNames[name];
120 return transitionEndVal ;
127 var listen = function(element, ename, opt, fn, scope){
128 var o = (!opt || typeof opt == "boolean") ? {} : opt;
129 fn = fn || o.fn; scope = scope || o.scope;
130 var el = Roo.getDom(element);
134 throw "Error listening for \"" + ename + '\". Element "' + element + '" doesn\'t exist.';
137 if (ename == 'transitionend') {
138 ename = transitionEnd();
141 e = Roo.EventObject.setEvent(e);
144 t = e.getTarget(o.delegate, el);
151 if(o.stopEvent === true){
154 if(o.preventDefault === true){
157 if(o.stopPropagation === true){
161 if(o.normalized === false){
165 fn.call(scope || el, e, t, o);
168 h = createDelayed(h, o);
171 h = createSingle(h, el, ename, fn);
174 h = createBuffered(h, o);
177 fn._handlers = fn._handlers || [];
180 fn._handlers.push([Roo.id(el), ename, h]);
185 if(ename == "mousewheel" && el.addEventListener){ // workaround for jQuery
186 el.addEventListener("DOMMouseScroll", h, false);
187 E.on(window, 'unload', function(){
188 el.removeEventListener("DOMMouseScroll", h, false);
191 if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document
192 Roo.EventManager.stoppedMouseDownEvent.addListener(h);
197 var stopListening = function(el, ename, fn){
198 var id = Roo.id(el), hds = fn._handlers, hd = fn;
200 for(var i = 0, len = hds.length; i < len; i++){
202 if(h[0] == id && h[1] == ename){
211 if(ename == "mousewheel" && el.addEventListener){
212 el.removeEventListener("DOMMouseScroll", hd, false);
214 if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document
215 Roo.EventManager.stoppedMouseDownEvent.removeListener(hd);
219 var propRe = /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/;
226 * @scope Roo.EventManager
231 * This is no longer needed and is deprecated. Places a simple wrapper around an event handler to override the browser event
232 * object with a Roo.EventObject
233 * @param {Function} fn The method the event invokes
234 * @param {Object} scope An object that becomes the scope of the handler
235 * @param {boolean} override If true, the obj passed in becomes
236 * the execution scope of the listener
237 * @return {Function} The wrapped function
240 wrap : function(fn, scope, override){
242 Roo.EventObject.setEvent(e);
243 fn.call(override ? scope || window : window, Roo.EventObject, scope);
248 * Appends an event handler to an element (shorthand for addListener)
249 * @param {String/HTMLElement} element The html element or id to assign the
250 * @param {String} eventName The type of event to listen for
251 * @param {Function} handler The method the event invokes
252 * @param {Object} scope (optional) The scope in which to execute the handler
253 * function. The handler function's "this" context.
254 * @param {Object} options (optional) An object containing handler configuration
255 * properties. This may contain any of the following properties:<ul>
256 * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
257 * <li>delegate {String} A simple selector to filter the target or look for a descendant of the target</li>
258 * <li>stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.</li>
259 * <li>preventDefault {Boolean} True to prevent the default action</li>
260 * <li>stopPropagation {Boolean} True to prevent event propagation</li>
261 * <li>normalized {Boolean} False to pass a browser event to the handler function instead of an Roo.EventObject</li>
262 * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
263 * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
264 * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
265 * by the specified number of milliseconds. If the event fires again within that time, the original
266 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
269 * <b>Combining Options</b><br>
270 * Using the options argument, it is possible to combine different types of listeners:<br>
272 * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)<div style="margin: 5px 20px 20px;">
274 el.on('click', this.onClick, this, {
281 * <b>Attaching multiple handlers in 1 call</b><br>
282 * The method also allows for a single argument to be passed which is a config object containing properties
283 * which specify multiple handlers.
302 * Or a shorthand syntax:<br>
305 'click' : this.onClick,
306 'mouseover' : this.onMouseOver,
307 'mouseout' : this.onMouseOut
311 addListener : function(element, eventName, fn, scope, options){
312 if(typeof eventName == "object"){
318 if(typeof o[e] == "function"){
320 listen(element, e, o, o[e], o.scope);
322 // individual options
323 listen(element, e, o[e]);
328 return listen(element, eventName, options, fn, scope);
332 * Removes an event handler
334 * @param {String/HTMLElement} element The id or html element to remove the
336 * @param {String} eventName The type of event
337 * @param {Function} fn
338 * @return {Boolean} True if a listener was actually removed
340 removeListener : function(element, eventName, fn){
341 return stopListening(element, eventName, fn);
345 * Fires when the document is ready (before onload and before images are loaded). Can be
346 * accessed shorthanded Roo.onReady().
347 * @param {Function} fn The method the event invokes
348 * @param {Object} scope An object that becomes the scope of the handler
349 * @param {boolean} options
351 onDocumentReady : function(fn, scope, options){
352 if(docReadyState){ // if it already fired
353 docReadyEvent.addListener(fn, scope, options);
354 docReadyEvent.fire();
355 docReadyEvent.clearListeners();
361 docReadyEvent.addListener(fn, scope, options);
365 * Fires when the window is resized and provides resize event buffering (50 milliseconds), passes new viewport width and height to handlers.
366 * @param {Function} fn The method the event invokes
367 * @param {Object} scope An object that becomes the scope of the handler
368 * @param {boolean} options
370 onWindowResize : function(fn, scope, options){
372 resizeEvent = new Roo.util.Event();
373 resizeTask = new Roo.util.DelayedTask(function(){
374 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
376 E.on(window, "resize", function(){
378 resizeTask.delay(50);
380 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
384 resizeEvent.addListener(fn, scope, options);
388 * Fires when the user changes the active text size. Handler gets called with 2 params, the old size and the new size.
389 * @param {Function} fn The method the event invokes
390 * @param {Object} scope An object that becomes the scope of the handler
391 * @param {boolean} options
393 onTextResize : function(fn, scope, options){
395 textEvent = new Roo.util.Event();
396 var textEl = new Roo.Element(document.createElement('div'));
397 textEl.dom.className = 'x-text-resize';
398 textEl.dom.innerHTML = 'X';
399 textEl.appendTo(document.body);
400 textSize = textEl.dom.offsetHeight;
401 setInterval(function(){
402 if(textEl.dom.offsetHeight != textSize){
403 textEvent.fire(textSize, textSize = textEl.dom.offsetHeight);
405 }, this.textResizeInterval);
407 textEvent.addListener(fn, scope, options);
411 * Removes the passed window resize listener.
412 * @param {Function} fn The method the event invokes
413 * @param {Object} scope The scope of handler
415 removeResizeListener : function(fn, scope){
417 resizeEvent.removeListener(fn, scope);
422 fireResize : function(){
424 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
428 * Url used for onDocumentReady with using SSL (defaults to Roo.SSL_SECURE_URL)
432 * The frequency, in milliseconds, to check for text resize events (defaults to 50)
434 textResizeInterval : 50
439 * @scopeAlias pub=Roo.EventManager
443 * Appends an event handler to an element (shorthand for addListener)
444 * @param {String/HTMLElement} element The html element or id to assign the
445 * @param {String} eventName The type of event to listen for
446 * @param {Function} handler The method the event invokes
447 * @param {Object} scope (optional) The scope in which to execute the handler
448 * function. The handler function's "this" context.
449 * @param {Object} options (optional) An object containing handler configuration
450 * properties. This may contain any of the following properties:<ul>
451 * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
452 * <li>delegate {String} A simple selector to filter the target or look for a descendant of the target</li>
453 * <li>stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.</li>
454 * <li>preventDefault {Boolean} True to prevent the default action</li>
455 * <li>stopPropagation {Boolean} True to prevent event propagation</li>
456 * <li>normalized {Boolean} False to pass a browser event to the handler function instead of an Roo.EventObject</li>
457 * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
458 * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
459 * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
460 * by the specified number of milliseconds. If the event fires again within that time, the original
461 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
464 * <b>Combining Options</b><br>
465 * Using the options argument, it is possible to combine different types of listeners:<br>
467 * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)<div style="margin: 5px 20px 20px;">
469 el.on('click', this.onClick, this, {
476 * <b>Attaching multiple handlers in 1 call</b><br>
477 * The method also allows for a single argument to be passed which is a config object containing properties
478 * which specify multiple handlers.
497 * Or a shorthand syntax:<br>
500 'click' : this.onClick,
501 'mouseover' : this.onMouseOver,
502 'mouseout' : this.onMouseOut
506 pub.on = pub.addListener;
507 pub.un = pub.removeListener;
509 pub.stoppedMouseDownEvent = new Roo.util.Event();
513 * Fires when the document is ready (before onload and before images are loaded). Shorthand of {@link Roo.EventManager#onDocumentReady}.
514 * @param {Function} fn The method the event invokes
515 * @param {Object} scope An object that becomes the scope of the handler
516 * @param {boolean} override If true, the obj passed in becomes
517 * the execution scope of the listener
521 Roo.onReady = Roo.EventManager.onDocumentReady;
523 Roo.onReady(function(){
524 var bd = Roo.get(document.body);
529 : Roo.isIE11 ? "roo-ie11"
530 : Roo.isEdge ? "roo-edge"
531 : Roo.isGecko ? "roo-gecko"
532 : Roo.isOpera ? "roo-opera"
533 : Roo.isSafari ? "roo-safari" : ""];
539 cls.push("roo-linux");
545 cls.push("roo-touch");
548 cls.push('roo-border-box');
550 if(Roo.isStrict){ // add to the parent to allow for selectors like ".ext-strict .ext-ie"
551 var p = bd.dom.parentNode;
553 p.className += ' roo-strict';
556 bd.addClass(cls.join(' '));
560 * @class Roo.EventObject
561 * EventObject exposes the Yahoo! UI Event functionality directly on the object
562 * passed to your event handler. It exists mostly for convenience. It also fixes the annoying null checks automatically to cleanup your code
565 function handleClick(e){ // e is not a standard event object, it is a Roo.EventObject
567 var target = e.getTarget();
570 var myDiv = Roo.get("myDiv");
571 myDiv.on("click", handleClick);
573 Roo.EventManager.on("myDiv", 'click', handleClick);
574 Roo.EventManager.addListener("myDiv", 'click', handleClick);
578 Roo.EventObject = function(){
580 var E = Roo.lib.Event;
582 // safari keypress events for special keys return bad keycodes
588 63276 : 33, // page up
589 63277 : 34, // page down
590 63272 : 46, // delete
595 // normalize button clicks
596 var btnMap = Roo.isIE ? {1:0,4:1,2:2} :
597 (Roo.isSafari ? {1:0,2:1,3:2} : {0:0,1:1,2:2});
599 Roo.EventObjectImpl = function(e){
601 this.setEvent(e.browserEvent || e);
604 Roo.EventObjectImpl.prototype = {
606 * Used to fix doc tools.
607 * @scope Roo.EventObject.prototype
613 /** The normal browser event */
615 /** The button pressed in a mouse event */
617 /** True if the shift key was down during the event */
619 /** True if the control key was down during the event */
621 /** True if the alt key was down during the event */
680 setEvent : function(e){
681 if(e == this || (e && e.browserEvent)){ // already wrapped
684 this.browserEvent = e;
687 this.button = e.button ? btnMap[e.button] : (e.which ? e.which-1 : -1);
688 if(e.type == 'click' && this.button == -1){
692 this.shiftKey = e.shiftKey;
693 // mac metaKey behaves like ctrlKey
694 this.ctrlKey = e.ctrlKey || e.metaKey;
695 this.altKey = e.altKey;
696 // in getKey these will be normalized for the mac
697 this.keyCode = e.keyCode;
698 // keyup warnings on firefox.
699 this.charCode = (e.type == 'keyup' || e.type == 'keydown') ? 0 : e.charCode;
700 // cache the target for the delayed and or buffered events
701 this.target = E.getTarget(e);
703 this.xy = E.getXY(e);
706 this.shiftKey = false;
707 this.ctrlKey = false;
718 * Stop the event (preventDefault and stopPropagation)
720 stopEvent : function(){
721 if(this.browserEvent){
722 if(this.browserEvent.type == 'mousedown'){
723 Roo.EventManager.stoppedMouseDownEvent.fire(this);
725 E.stopEvent(this.browserEvent);
730 * Prevents the browsers default handling of the event.
732 preventDefault : function(){
733 if(this.browserEvent){
734 E.preventDefault(this.browserEvent);
739 isNavKeyPress : function(){
740 var k = this.keyCode;
741 k = Roo.isSafari ? (safariKeys[k] || k) : k;
742 return (k >= 33 && k <= 40) || k == this.RETURN || k == this.TAB || k == this.ESC;
745 isSpecialKey : function(){
746 var k = this.keyCode;
747 return (this.type == 'keypress' && this.ctrlKey) || k == 9 || k == 13 || k == 40 || k == 27 ||
748 (k == 16) || (k == 17) ||
749 (k >= 18 && k <= 20) ||
750 (k >= 33 && k <= 35) ||
751 (k >= 36 && k <= 39) ||
752 (k >= 44 && k <= 45);
755 * Cancels bubbling of the event.
757 stopPropagation : function(){
758 if(this.browserEvent){
759 if(this.type == 'mousedown'){
760 Roo.EventManager.stoppedMouseDownEvent.fire(this);
762 E.stopPropagation(this.browserEvent);
767 * Gets the key code for the event.
770 getCharCode : function(){
771 return this.charCode || this.keyCode;
775 * Returns a normalized keyCode for the event.
776 * @return {Number} The key code
779 var k = this.keyCode || this.charCode;
780 return Roo.isSafari ? (safariKeys[k] || k) : k;
784 * Gets the x coordinate of the event.
787 getPageX : function(){
792 * Gets the y coordinate of the event.
795 getPageY : function(){
800 * Gets the time of the event.
803 getTime : function(){
804 if(this.browserEvent){
805 return E.getTime(this.browserEvent);
811 * Gets the page coordinates of the event.
812 * @return {Array} The xy values like [x, y]
819 * Gets the target for the event.
820 * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
821 * @param {Number/String/HTMLElement/Element} maxDepth (optional) The max depth to
822 search as a number or element (defaults to 10 || document.body)
823 * @param {Boolean} returnEl (optional) True to return a Roo.Element object instead of DOM node
824 * @return {HTMLelement}
826 getTarget : function(selector, maxDepth, returnEl){
827 return selector ? Roo.fly(this.target).findParent(selector, maxDepth, returnEl) : this.target;
830 * Gets the related target.
831 * @return {HTMLElement}
833 getRelatedTarget : function(){
834 if(this.browserEvent){
835 return E.getRelatedTarget(this.browserEvent);
841 * Normalizes mouse wheel delta across browsers
842 * @return {Number} The delta
844 getWheelDelta : function(){
845 var e = this.browserEvent;
847 if(e.wheelDelta){ /* IE/Opera. */
848 delta = e.wheelDelta/120;
849 }else if(e.detail){ /* Mozilla case. */
856 * Returns true if the control, meta, shift or alt key was pressed during this event.
859 hasModifier : function(){
860 return !!((this.ctrlKey || this.altKey) || this.shiftKey);
864 * Returns true if the target of this event equals el or is a child of el
865 * @param {String/HTMLElement/Element} el
866 * @param {Boolean} related (optional) true to test if the related target is within el instead of the target
869 within : function(el, related){
870 var t = this[related ? "getRelatedTarget" : "getTarget"]();
871 return t && Roo.fly(el).contains(t);
874 getPoint : function(){
875 return new Roo.lib.Point(this.xy[0], this.xy[1]);
879 return new Roo.EventObjectImpl();