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)
129 var o = (!opt || typeof opt == "boolean") ? {} : opt;
130 fn = fn || o.fn; scope = scope || o.scope;
131 var el = Roo.getDom(element);
135 throw "Error listening for \"" + ename + '\". Element "' + element + '" doesn\'t exist.';
138 if (ename == 'transitionend') {
139 ename = transitionEnd();
142 e = Roo.EventObject.setEvent(e);
145 t = e.getTarget(o.delegate, el);
152 if(o.stopEvent === true){
155 if(o.preventDefault === true){
158 if(o.stopPropagation === true){
162 if(o.normalized === false){
166 fn.call(scope || el, e, t, o);
169 h = createDelayed(h, o);
172 h = createSingle(h, el, ename, fn);
175 h = createBuffered(h, o);
178 fn._handlers = fn._handlers || [];
181 fn._handlers.push([Roo.id(el), ename, h]);
185 E.on(el, ename, h); // this adds the actuall listener to the object..
188 if(ename == "mousewheel" && el.addEventListener){ // workaround for jQuery
189 el.addEventListener("DOMMouseScroll", h, false);
190 E.on(window, 'unload', function(){
191 el.removeEventListener("DOMMouseScroll", h, false);
194 if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document
195 Roo.EventManager.stoppedMouseDownEvent.addListener(h);
200 var stopListening = function(el, ename, fn){
201 var id = Roo.id(el), hds = fn._handlers, hd = fn;
203 for(var i = 0, len = hds.length; i < len; i++){
205 if(h[0] == id && h[1] == ename){
214 if(ename == "mousewheel" && el.addEventListener){
215 el.removeEventListener("DOMMouseScroll", hd, false);
217 if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document
218 Roo.EventManager.stoppedMouseDownEvent.removeListener(hd);
222 var propRe = /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/;
229 * @scope Roo.EventManager
234 * This is no longer needed and is deprecated. Places a simple wrapper around an event handler to override the browser event
235 * object with a Roo.EventObject
236 * @param {Function} fn The method the event invokes
237 * @param {Object} scope An object that becomes the scope of the handler
238 * @param {boolean} override If true, the obj passed in becomes
239 * the execution scope of the listener
240 * @return {Function} The wrapped function
243 wrap : function(fn, scope, override){
245 Roo.EventObject.setEvent(e);
246 fn.call(override ? scope || window : window, Roo.EventObject, scope);
251 * Appends an event handler to an element (shorthand for addListener)
252 * @param {String/HTMLElement} element The html element or id to assign the
253 * @param {String} eventName The type of event to listen for
254 * @param {Function} handler The method the event invokes
255 * @param {Object} scope (optional) The scope in which to execute the handler
256 * function. The handler function's "this" context.
257 * @param {Object} options (optional) An object containing handler configuration
258 * properties. This may contain any of the following properties:<ul>
259 * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
260 * <li>delegate {String} A simple selector to filter the target or look for a descendant of the target</li>
261 * <li>stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.</li>
262 * <li>preventDefault {Boolean} True to prevent the default action</li>
263 * <li>stopPropagation {Boolean} True to prevent event propagation</li>
264 * <li>normalized {Boolean} False to pass a browser event to the handler function instead of an Roo.EventObject</li>
265 * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
266 * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
267 * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
268 * by the specified number of milliseconds. If the event fires again within that time, the original
269 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
272 * <b>Combining Options</b><br>
273 * Using the options argument, it is possible to combine different types of listeners:<br>
275 * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)<div style="margin: 5px 20px 20px;">
277 el.on('click', this.onClick, this, {
284 * <b>Attaching multiple handlers in 1 call</b><br>
285 * The method also allows for a single argument to be passed which is a config object containing properties
286 * which specify multiple handlers.
305 * Or a shorthand syntax:<br>
308 'click' : this.onClick,
309 'mouseover' : this.onMouseOver,
310 'mouseout' : this.onMouseOut
314 addListener : function(element, eventName, fn, scope, options){
315 if(typeof eventName == "object"){
321 if(typeof o[e] == "function"){
323 listen(element, e, o, o[e], o.scope);
325 // individual options
326 listen(element, e, o[e]);
331 return listen(element, eventName, options, fn, scope);
335 * Removes an event handler
337 * @param {String/HTMLElement} element The id or html element to remove the
339 * @param {String} eventName The type of event
340 * @param {Function} fn
341 * @return {Boolean} True if a listener was actually removed
343 removeListener : function(element, eventName, fn){
344 return stopListening(element, eventName, fn);
348 * Fires when the document is ready (before onload and before images are loaded). Can be
349 * accessed shorthanded Roo.onReady().
350 * @param {Function} fn The method the event invokes
351 * @param {Object} scope An object that becomes the scope of the handler
352 * @param {boolean} options
354 onDocumentReady : function(fn, scope, options){
355 if(docReadyState){ // if it already fired
356 docReadyEvent.addListener(fn, scope, options);
357 docReadyEvent.fire();
358 docReadyEvent.clearListeners();
364 docReadyEvent.addListener(fn, scope, options);
368 * Fires when the window is resized and provides resize event buffering (50 milliseconds), passes new viewport width and height to handlers.
369 * @param {Function} fn The method the event invokes
370 * @param {Object} scope An object that becomes the scope of the handler
371 * @param {boolean} options
373 onWindowResize : function(fn, scope, options){
375 resizeEvent = new Roo.util.Event();
376 resizeTask = new Roo.util.DelayedTask(function(){
377 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
379 E.on(window, "resize", function(){
381 resizeTask.delay(50);
383 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
387 resizeEvent.addListener(fn, scope, options);
391 * Fires when the user changes the active text size. Handler gets called with 2 params, the old size and the new size.
392 * @param {Function} fn The method the event invokes
393 * @param {Object} scope An object that becomes the scope of the handler
394 * @param {boolean} options
396 onTextResize : function(fn, scope, options){
398 textEvent = new Roo.util.Event();
399 var textEl = new Roo.Element(document.createElement('div'));
400 textEl.dom.className = 'x-text-resize';
401 textEl.dom.innerHTML = 'X';
402 textEl.appendTo(document.body);
403 textSize = textEl.dom.offsetHeight;
404 setInterval(function(){
405 if(textEl.dom.offsetHeight != textSize){
406 textEvent.fire(textSize, textSize = textEl.dom.offsetHeight);
408 }, this.textResizeInterval);
410 textEvent.addListener(fn, scope, options);
414 * Removes the passed window resize listener.
415 * @param {Function} fn The method the event invokes
416 * @param {Object} scope The scope of handler
418 removeResizeListener : function(fn, scope){
420 resizeEvent.removeListener(fn, scope);
425 fireResize : function(){
427 resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
431 * Url used for onDocumentReady with using SSL (defaults to Roo.SSL_SECURE_URL)
435 * The frequency, in milliseconds, to check for text resize events (defaults to 50)
437 textResizeInterval : 50
442 * @scopeAlias pub=Roo.EventManager
446 * Appends an event handler to an element (shorthand for addListener)
447 * @param {String/HTMLElement} element The html element or id to assign the
448 * @param {String} eventName The type of event to listen for
449 * @param {Function} handler The method the event invokes
450 * @param {Object} scope (optional) The scope in which to execute the handler
451 * function. The handler function's "this" context.
452 * @param {Object} options (optional) An object containing handler configuration
453 * properties. This may contain any of the following properties:<ul>
454 * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
455 * <li>delegate {String} A simple selector to filter the target or look for a descendant of the target</li>
456 * <li>stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.</li>
457 * <li>preventDefault {Boolean} True to prevent the default action</li>
458 * <li>stopPropagation {Boolean} True to prevent event propagation</li>
459 * <li>normalized {Boolean} False to pass a browser event to the handler function instead of an Roo.EventObject</li>
460 * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
461 * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
462 * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
463 * by the specified number of milliseconds. If the event fires again within that time, the original
464 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
467 * <b>Combining Options</b><br>
468 * Using the options argument, it is possible to combine different types of listeners:<br>
470 * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)<div style="margin: 5px 20px 20px;">
472 el.on('click', this.onClick, this, {
479 * <b>Attaching multiple handlers in 1 call</b><br>
480 * The method also allows for a single argument to be passed which is a config object containing properties
481 * which specify multiple handlers.
500 * Or a shorthand syntax:<br>
503 'click' : this.onClick,
504 'mouseover' : this.onMouseOver,
505 'mouseout' : this.onMouseOut
509 pub.on = pub.addListener;
510 pub.un = pub.removeListener;
512 pub.stoppedMouseDownEvent = new Roo.util.Event();
516 * Fires when the document is ready (before onload and before images are loaded). Shorthand of {@link Roo.EventManager#onDocumentReady}.
517 * @param {Function} fn The method the event invokes
518 * @param {Object} scope An object that becomes the scope of the handler
519 * @param {boolean} override If true, the obj passed in becomes
520 * the execution scope of the listener
524 Roo.onReady = Roo.EventManager.onDocumentReady;
526 Roo.onReady(function(){
527 var bd = Roo.get(document.body);
532 : Roo.isIE11 ? "roo-ie11"
533 : Roo.isEdge ? "roo-edge"
534 : Roo.isGecko ? "roo-gecko"
535 : Roo.isOpera ? "roo-opera"
536 : Roo.isSafari ? "roo-safari" : ""];
542 cls.push("roo-linux");
548 cls.push("roo-touch");
551 cls.push('roo-border-box');
553 if(Roo.isStrict){ // add to the parent to allow for selectors like ".ext-strict .ext-ie"
554 var p = bd.dom.parentNode;
556 p.className += ' roo-strict';
559 bd.addClass(cls.join(' '));
563 * @class Roo.EventObject
564 * EventObject exposes the Yahoo! UI Event functionality directly on the object
565 * passed to your event handler. It exists mostly for convenience. It also fixes the annoying null checks automatically to cleanup your code
568 function handleClick(e){ // e is not a standard event object, it is a Roo.EventObject
570 var target = e.getTarget();
573 var myDiv = Roo.get("myDiv");
574 myDiv.on("click", handleClick);
576 Roo.EventManager.on("myDiv", 'click', handleClick);
577 Roo.EventManager.addListener("myDiv", 'click', handleClick);
581 Roo.EventObject = function(){
583 var E = Roo.lib.Event;
585 // safari keypress events for special keys return bad keycodes
591 63276 : 33, // page up
592 63277 : 34, // page down
593 63272 : 46, // delete
598 // normalize button clicks
599 var btnMap = Roo.isIE ? {1:0,4:1,2:2} :
600 (Roo.isSafari ? {1:0,2:1,3:2} : {0:0,1:1,2:2});
602 Roo.EventObjectImpl = function(e){
604 this.setEvent(e.browserEvent || e);
607 Roo.EventObjectImpl.prototype = {
609 * Used to fix doc tools.
610 * @scope Roo.EventObject.prototype
616 /** The normal browser event */
618 /** The button pressed in a mouse event */
620 /** True if the shift key was down during the event */
622 /** True if the control key was down during the event */
624 /** True if the alt key was down during the event */
683 setEvent : function(e){
684 if(e == this || (e && e.browserEvent)){ // already wrapped
687 this.browserEvent = e;
690 this.button = e.button ? btnMap[e.button] : (e.which ? e.which-1 : -1);
691 if(e.type == 'click' && this.button == -1){
695 this.shiftKey = e.shiftKey;
696 // mac metaKey behaves like ctrlKey
697 this.ctrlKey = e.ctrlKey || e.metaKey;
698 this.altKey = e.altKey;
699 // in getKey these will be normalized for the mac
700 this.keyCode = e.keyCode;
701 // keyup warnings on firefox.
702 this.charCode = (e.type == 'keyup' || e.type == 'keydown') ? 0 : e.charCode;
703 // cache the target for the delayed and or buffered events
704 this.target = E.getTarget(e);
706 this.xy = E.getXY(e);
709 this.shiftKey = false;
710 this.ctrlKey = false;
721 * Stop the event (preventDefault and stopPropagation)
723 stopEvent : function(){
724 if(this.browserEvent){
725 if(this.browserEvent.type == 'mousedown'){
726 Roo.EventManager.stoppedMouseDownEvent.fire(this);
728 E.stopEvent(this.browserEvent);
733 * Prevents the browsers default handling of the event.
735 preventDefault : function(){
736 if(this.browserEvent){
737 E.preventDefault(this.browserEvent);
742 isNavKeyPress : function(){
743 var k = this.keyCode;
744 k = Roo.isSafari ? (safariKeys[k] || k) : k;
745 return (k >= 33 && k <= 40) || k == this.RETURN || k == this.TAB || k == this.ESC;
748 isSpecialKey : function(){
749 var k = this.keyCode;
750 return (this.type == 'keypress' && this.ctrlKey) || k == 9 || k == 13 || k == 40 || k == 27 ||
751 (k == 16) || (k == 17) ||
752 (k >= 18 && k <= 20) ||
753 (k >= 33 && k <= 35) ||
754 (k >= 36 && k <= 39) ||
755 (k >= 44 && k <= 45);
758 * Cancels bubbling of the event.
760 stopPropagation : function(){
761 if(this.browserEvent){
762 if(this.type == 'mousedown'){
763 Roo.EventManager.stoppedMouseDownEvent.fire(this);
765 E.stopPropagation(this.browserEvent);
770 * Gets the key code for the event.
773 getCharCode : function(){
774 return this.charCode || this.keyCode;
778 * Returns a normalized keyCode for the event.
779 * @return {Number} The key code
782 var k = this.keyCode || this.charCode;
783 return Roo.isSafari ? (safariKeys[k] || k) : k;
787 * Gets the x coordinate of the event.
790 getPageX : function(){
795 * Gets the y coordinate of the event.
798 getPageY : function(){
803 * Gets the time of the event.
806 getTime : function(){
807 if(this.browserEvent){
808 return E.getTime(this.browserEvent);
814 * Gets the page coordinates of the event.
815 * @return {Array} The xy values like [x, y]
822 * Gets the target for the event.
823 * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
824 * @param {Number/String/HTMLElement/Element} maxDepth (optional) The max depth to
825 search as a number or element (defaults to 10 || document.body)
826 * @param {Boolean} returnEl (optional) True to return a Roo.Element object instead of DOM node
827 * @return {HTMLelement}
829 getTarget : function(selector, maxDepth, returnEl){
830 return selector ? Roo.fly(this.target).findParent(selector, maxDepth, returnEl) : this.target;
833 * Gets the related target.
834 * @return {HTMLElement}
836 getRelatedTarget : function(){
837 if(this.browserEvent){
838 return E.getRelatedTarget(this.browserEvent);
844 * Normalizes mouse wheel delta across browsers
845 * @return {Number} The delta
847 getWheelDelta : function(){
848 var e = this.browserEvent;
850 if(e.wheelDelta){ /* IE/Opera. */
851 delta = e.wheelDelta/120;
852 }else if(e.detail){ /* Mozilla case. */
859 * Returns true if the control, meta, shift or alt key was pressed during this event.
862 hasModifier : function(){
863 return !!((this.ctrlKey || this.altKey) || this.shiftKey);
867 * Returns true if the target of this event equals el or is a child of el
868 * @param {String/HTMLElement/Element} el
869 * @param {Boolean} related (optional) true to test if the related target is within el instead of the target
872 within : function(el, related){
873 var t = this[related ? "getRelatedTarget" : "getTarget"]();
874 return t && Roo.fly(el).contains(t);
877 getPoint : function(){
878 return new Roo.lib.Point(this.xy[0], this.xy[1]);
882 return new Roo.EventObjectImpl();