How to use customEvent method in ng-mocks

Best JavaScript code snippet using ng-mocks

yui2-event-simulate.js

Source:yui2-event-simulate.js Github

copy

Full Screen

1YUI.add('yui2-event-simulate', function(Y) {2 var YAHOO = Y.YUI2;3 /*4Copyright (c) 2011, Yahoo! Inc. All rights reserved.5Code licensed under the BSD License:6http://developer.yahoo.com/yui/license.html7version: 2.9.08*/9/**10 * DOM event simulation utility11 * @module event-simulate12 * @namespace YAHOO.util13 * @requires yahoo,dom,event14 */15/**16 * The UserAction object provides functions that simulate events occurring in17 * the browser. Since these are simulated events, they do not behave exactly18 * as regular, user-initiated events do, but can be used to test simple19 * user interactions safely.20 *21 * @namespace YAHOO.util22 * @class UserAction23 * @static24 */25YAHOO.util.UserAction = {26 //--------------------------------------------------------------------------27 // Generic event methods28 //--------------------------------------------------------------------------29 /**30 * Simulates a key event using the given event information to populate31 * the generated event object. This method does browser-equalizing32 * calculations to account for differences in the DOM and IE event models33 * as well as different browser quirks. Note: keydown causes Safari 2.x to34 * crash.35 * @method simulateKeyEvent36 * @private37 * @static38 * @param {HTMLElement} target The target of the given event.39 * @param {String} type The type of event to fire. This can be any one of40 * the following: keyup, keydown, and keypress.41 * @param {Boolean} bubbles (Optional) Indicates if the event can be42 * bubbled up. DOM Level 3 specifies that all key events bubble by43 * default. The default is true.44 * @param {Boolean} cancelable (Optional) Indicates if the event can be45 * canceled using preventDefault(). DOM Level 3 specifies that all46 * key events can be cancelled. The default47 * is true.48 * @param {Window} view (Optional) The view containing the target. This is49 * typically the window object. The default is window.50 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys51 * is pressed while the event is firing. The default is false.52 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys53 * is pressed while the event is firing. The default is false.54 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys55 * is pressed while the event is firing. The default is false.56 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys57 * is pressed while the event is firing. The default is false.58 * @param {int} keyCode (Optional) The code for the key that is in use.59 * The default is 0.60 * @param {int} charCode (Optional) The Unicode code for the character61 * associated with the key being used. The default is 0.62 */63 simulateKeyEvent : function (target /*:HTMLElement*/, type /*:String*/,64 bubbles /*:Boolean*/, cancelable /*:Boolean*/,65 view /*:Window*/,66 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,67 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,68 keyCode /*:int*/, charCode /*:int*/) /*:Void*/69 {70 //check target71 target = YAHOO.util.Dom.get(target);72 if (!target){73 throw new Error("simulateKeyEvent(): Invalid target.");74 }75 //check event type76 if (YAHOO.lang.isString(type)){77 type = type.toLowerCase();78 switch(type){79 case "keyup":80 case "keydown":81 case "keypress":82 break;83 case "textevent": //DOM Level 384 type = "keypress";85 break;86 // @TODO was the fallthrough intentional, if so throw error87 default:88 throw new Error("simulateKeyEvent(): Event type '" + type + "' not supported.");89 }90 } else {91 throw new Error("simulateKeyEvent(): Event type must be a string.");92 }93 //setup default values94 if (!YAHOO.lang.isBoolean(bubbles)){95 bubbles = true; //all key events bubble96 }97 if (!YAHOO.lang.isBoolean(cancelable)){98 cancelable = true; //all key events can be cancelled99 }100 if (!YAHOO.lang.isObject(view)){101 view = window; //view is typically window102 }103 if (!YAHOO.lang.isBoolean(ctrlKey)){104 ctrlKey = false;105 }106 if (!YAHOO.lang.isBoolean(altKey)){107 altKey = false;108 }109 if (!YAHOO.lang.isBoolean(shiftKey)){110 shiftKey = false;111 }112 if (!YAHOO.lang.isBoolean(metaKey)){113 metaKey = false;114 }115 if (!YAHOO.lang.isNumber(keyCode)){116 keyCode = 0;117 }118 if (!YAHOO.lang.isNumber(charCode)){119 charCode = 0;120 }121 //try to create a mouse event122 var customEvent /*:MouseEvent*/ = null;123 //check for DOM-compliant browsers first124 if (YAHOO.lang.isFunction(document.createEvent)){125 try {126 //try to create key event127 customEvent = document.createEvent("KeyEvents");128 /*129 * Interesting problem: Firefox implemented a non-standard130 * version of initKeyEvent() based on DOM Level 2 specs.131 * Key event was removed from DOM Level 2 and re-introduced132 * in DOM Level 3 with a different interface. Firefox is the133 * only browser with any implementation of Key Events, so for134 * now, assume it's Firefox if the above line doesn't error.135 */136 //TODO: Decipher between Firefox's implementation and a correct one.137 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,138 altKey, shiftKey, metaKey, keyCode, charCode);139 } catch (ex /*:Error*/){140 /*141 * If it got here, that means key events aren't officially supported.142 * Safari/WebKit is a real problem now. WebKit 522 won't let you143 * set keyCode, charCode, or other properties if you use a144 * UIEvent, so we first must try to create a generic event. The145 * fun part is that this will throw an error on Safari 2.x. The146 * end result is that we need another try...catch statement just to147 * deal with this mess.148 */149 try {150 //try to create generic event - will fail in Safari 2.x151 customEvent = document.createEvent("Events");152 } catch (uierror /*:Error*/){153 //the above failed, so create a UIEvent for Safari 2.x154 customEvent = document.createEvent("UIEvents");155 } finally {156 customEvent.initEvent(type, bubbles, cancelable);157 //initialize158 customEvent.view = view;159 customEvent.altKey = altKey;160 customEvent.ctrlKey = ctrlKey;161 customEvent.shiftKey = shiftKey;162 customEvent.metaKey = metaKey;163 customEvent.keyCode = keyCode;164 customEvent.charCode = charCode;165 }166 }167 //fire the event168 target.dispatchEvent(customEvent);169 } else if (YAHOO.lang.isObject(document.createEventObject)){ //IE170 //create an IE event object171 customEvent = document.createEventObject();172 //assign available properties173 customEvent.bubbles = bubbles;174 customEvent.cancelable = cancelable;175 customEvent.view = view;176 customEvent.ctrlKey = ctrlKey;177 customEvent.altKey = altKey;178 customEvent.shiftKey = shiftKey;179 customEvent.metaKey = metaKey;180 /*181 * IE doesn't support charCode explicitly. CharCode should182 * take precedence over any keyCode value for accurate183 * representation.184 */185 customEvent.keyCode = (charCode > 0) ? charCode : keyCode;186 //fire the event187 target.fireEvent("on" + type, customEvent);188 } else {189 throw new Error("simulateKeyEvent(): No event simulation framework present.");190 }191 },192 /**193 * Simulates a mouse event using the given event information to populate194 * the generated event object. This method does browser-equalizing195 * calculations to account for differences in the DOM and IE event models196 * as well as different browser quirks.197 * @method simulateMouseEvent198 * @private199 * @static200 * @param {HTMLElement} target The target of the given event.201 * @param {String} type The type of event to fire. This can be any one of202 * the following: click, dblclick, mousedown, mouseup, mouseout,203 * mouseover, and mousemove.204 * @param {Boolean} bubbles (Optional) Indicates if the event can be205 * bubbled up. DOM Level 2 specifies that all mouse events bubble by206 * default. The default is true.207 * @param {Boolean} cancelable (Optional) Indicates if the event can be208 * canceled using preventDefault(). DOM Level 2 specifies that all209 * mouse events except mousemove can be cancelled. The default210 * is true for all events except mousemove, for which the default211 * is false.212 * @param {Window} view (Optional) The view containing the target. This is213 * typically the window object. The default is window.214 * @param {int} detail (Optional) The number of times the mouse button has215 * been used. The default value is 1.216 * @param {int} screenX (Optional) The x-coordinate on the screen at which217 * point the event occured. The default is 0.218 * @param {int} screenY (Optional) The y-coordinate on the screen at which219 * point the event occured. The default is 0.220 * @param {int} clientX (Optional) The x-coordinate on the client at which221 * point the event occured. The default is 0.222 * @param {int} clientY (Optional) The y-coordinate on the client at which223 * point the event occured. The default is 0.224 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys225 * is pressed while the event is firing. The default is false.226 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys227 * is pressed while the event is firing. The default is false.228 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys229 * is pressed while the event is firing. The default is false.230 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys231 * is pressed while the event is firing. The default is false.232 * @param {int} button (Optional) The button being pressed while the event233 * is executing. The value should be 0 for the primary mouse button234 * (typically the left button), 1 for the terciary mouse button235 * (typically the middle button), and 2 for the secondary mouse button236 * (typically the right button). The default is 0.237 * @param {HTMLElement} relatedTarget (Optional) For mouseout events,238 * this is the element that the mouse has moved to. For mouseover239 * events, this is the element that the mouse has moved from. This240 * argument is ignored for all other events. The default is null.241 */242 simulateMouseEvent : function (target /*:HTMLElement*/, type /*:String*/,243 bubbles /*:Boolean*/, cancelable /*:Boolean*/,244 view /*:Window*/, detail /*:int*/,245 screenX /*:int*/, screenY /*:int*/,246 clientX /*:int*/, clientY /*:int*/,247 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,248 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,249 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/250 {251 //check target252 target = YAHOO.util.Dom.get(target);253 if (!target){254 throw new Error("simulateMouseEvent(): Invalid target.");255 }256 relatedTarget = relatedTarget || null;257 //check event type258 if (YAHOO.lang.isString(type)){259 type = type.toLowerCase();260 switch(type){261 case "mouseover":262 case "mouseout":263 case "mousedown":264 case "mouseup":265 case "click":266 case "dblclick":267 case "mousemove":268 break;269 default:270 throw new Error("simulateMouseEvent(): Event type '" + type + "' not supported.");271 }272 } else {273 throw new Error("simulateMouseEvent(): Event type must be a string.");274 }275 //setup default values276 if (!YAHOO.lang.isBoolean(bubbles)){277 bubbles = true; //all mouse events bubble278 }279 if (!YAHOO.lang.isBoolean(cancelable)){280 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled281 }282 if (!YAHOO.lang.isObject(view)){283 view = window; //view is typically window284 }285 if (!YAHOO.lang.isNumber(detail)){286 detail = 1; //number of mouse clicks must be at least one287 }288 if (!YAHOO.lang.isNumber(screenX)){289 screenX = 0;290 }291 if (!YAHOO.lang.isNumber(screenY)){292 screenY = 0;293 }294 if (!YAHOO.lang.isNumber(clientX)){295 clientX = 0;296 }297 if (!YAHOO.lang.isNumber(clientY)){298 clientY = 0;299 }300 if (!YAHOO.lang.isBoolean(ctrlKey)){301 ctrlKey = false;302 }303 if (!YAHOO.lang.isBoolean(altKey)){304 altKey = false;305 }306 if (!YAHOO.lang.isBoolean(shiftKey)){307 shiftKey = false;308 }309 if (!YAHOO.lang.isBoolean(metaKey)){310 metaKey = false;311 }312 if (!YAHOO.lang.isNumber(button)){313 button = 0;314 }315 //try to create a mouse event316 var customEvent /*:MouseEvent*/ = null;317 //check for DOM-compliant browsers first318 if (YAHOO.lang.isFunction(document.createEvent)){319 customEvent = document.createEvent("MouseEvents");320 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()321 if (customEvent.initMouseEvent){322 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,323 screenX, screenY, clientX, clientY,324 ctrlKey, altKey, shiftKey, metaKey,325 button, relatedTarget);326 } else { //Safari327 //the closest thing available in Safari 2.x is UIEvents328 customEvent = document.createEvent("UIEvents");329 customEvent.initEvent(type, bubbles, cancelable);330 customEvent.view = view;331 customEvent.detail = detail;332 customEvent.screenX = screenX;333 customEvent.screenY = screenY;334 customEvent.clientX = clientX;335 customEvent.clientY = clientY;336 customEvent.ctrlKey = ctrlKey;337 customEvent.altKey = altKey;338 customEvent.metaKey = metaKey;339 customEvent.shiftKey = shiftKey;340 customEvent.button = button;341 customEvent.relatedTarget = relatedTarget;342 }343 /*344 * Check to see if relatedTarget has been assigned. Firefox345 * versions less than 2.0 don't allow it to be assigned via346 * initMouseEvent() and the property is readonly after event347 * creation, so in order to keep YAHOO.util.getRelatedTarget()348 * working, assign to the IE proprietary toElement property349 * for mouseout event and fromElement property for mouseover350 * event.351 */352 if (relatedTarget && !customEvent.relatedTarget){353 if (type == "mouseout"){354 customEvent.toElement = relatedTarget;355 } else if (type == "mouseover"){356 customEvent.fromElement = relatedTarget;357 }358 }359 //fire the event360 target.dispatchEvent(customEvent);361 } else if (YAHOO.lang.isObject(document.createEventObject)){ //IE362 //create an IE event object363 customEvent = document.createEventObject();364 //assign available properties365 customEvent.bubbles = bubbles;366 customEvent.cancelable = cancelable;367 customEvent.view = view;368 customEvent.detail = detail;369 customEvent.screenX = screenX;370 customEvent.screenY = screenY;371 customEvent.clientX = clientX;372 customEvent.clientY = clientY;373 customEvent.ctrlKey = ctrlKey;374 customEvent.altKey = altKey;375 customEvent.metaKey = metaKey;376 customEvent.shiftKey = shiftKey;377 //fix button property for IE's wacky implementation378 switch(button){379 case 0:380 customEvent.button = 1;381 break;382 case 1:383 customEvent.button = 4;384 break;385 case 2:386 //leave as is387 break;388 default:389 customEvent.button = 0;390 }391 /*392 * Have to use relatedTarget because IE won't allow assignment393 * to toElement or fromElement on generic events. This keeps394 * YAHOO.util.customEvent.getRelatedTarget() functional.395 */396 customEvent.relatedTarget = relatedTarget;397 //fire the event398 target.fireEvent("on" + type, customEvent);399 } else {400 throw new Error("simulateMouseEvent(): No event simulation framework present.");401 }402 },403 //--------------------------------------------------------------------------404 // Mouse events405 //--------------------------------------------------------------------------406 /**407 * Simulates a mouse event on a particular element.408 * @param {HTMLElement} target The element to click on.409 * @param {String} type The type of event to fire. This can be any one of410 * the following: click, dblclick, mousedown, mouseup, mouseout,411 * mouseover, and mousemove.412 * @param {Object} options Additional event options (use DOM standard names).413 * @method mouseEvent414 * @static415 */416 fireMouseEvent : function (target /*:HTMLElement*/, type /*:String*/,417 options /*:Object*/) /*:Void*/418 {419 options = options || {};420 this.simulateMouseEvent(target, type, options.bubbles,421 options.cancelable, options.view, options.detail, options.screenX,422 options.screenY, options.clientX, options.clientY, options.ctrlKey,423 options.altKey, options.shiftKey, options.metaKey, options.button,424 options.relatedTarget);425 },426 /**427 * Simulates a click on a particular element.428 * @param {HTMLElement} target The element to click on.429 * @param {Object} options Additional event options (use DOM standard names).430 * @method click431 * @static432 */433 click : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {434 this.fireMouseEvent(target, "click", options);435 },436 /**437 * Simulates a double click on a particular element.438 * @param {HTMLElement} target The element to double click on.439 * @param {Object} options Additional event options (use DOM standard names).440 * @method dblclick441 * @static442 */443 dblclick : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {444 this.fireMouseEvent( target, "dblclick", options);445 },446 /**447 * Simulates a mousedown on a particular element.448 * @param {HTMLElement} target The element to act on.449 * @param {Object} options Additional event options (use DOM standard names).450 * @method mousedown451 * @static452 */453 mousedown : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {454 this.fireMouseEvent(target, "mousedown", options);455 },456 /**457 * Simulates a mousemove on a particular element.458 * @param {HTMLElement} target The element to act on.459 * @param {Object} options Additional event options (use DOM standard names).460 * @method mousemove461 * @static462 */463 mousemove : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {464 this.fireMouseEvent(target, "mousemove", options);465 },466 /**467 * Simulates a mouseout event on a particular element. Use "relatedTarget"468 * on the options object to specify where the mouse moved to.469 * Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so470 * toElement is assigned in its place. IE doesn't allow toElement to be471 * be assigned, so relatedTarget is assigned in its place. Both of these472 * concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly473 * in both browsers.474 * @param {HTMLElement} target The element to act on.475 * @param {Object} options Additional event options (use DOM standard names).476 * @method mouseout477 * @static478 */479 mouseout : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {480 this.fireMouseEvent(target, "mouseout", options);481 },482 /**483 * Simulates a mouseover event on a particular element. Use "relatedTarget"484 * on the options object to specify where the mouse moved from.485 * Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so486 * fromElement is assigned in its place. IE doesn't allow fromElement to be487 * be assigned, so relatedTarget is assigned in its place. Both of these488 * concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly489 * in both browsers.490 * @param {HTMLElement} target The element to act on.491 * @param {Object} options Additional event options (use DOM standard names).492 * @method mouseover493 * @static494 */495 mouseover : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {496 this.fireMouseEvent(target, "mouseover", options);497 },498 /**499 * Simulates a mouseup on a particular element.500 * @param {HTMLElement} target The element to act on.501 * @param {Object} options Additional event options (use DOM standard names).502 * @method mouseup503 * @static504 */505 mouseup : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {506 this.fireMouseEvent(target, "mouseup", options);507 },508 //--------------------------------------------------------------------------509 // Key events510 //--------------------------------------------------------------------------511 /**512 * Fires an event that normally would be fired by the keyboard (keyup,513 * keydown, keypress). Make sure to specify either keyCode or charCode as514 * an option.515 * @private516 * @param {String} type The type of event ("keyup", "keydown" or "keypress").517 * @param {HTMLElement} target The target of the event.518 * @param {Object} options Options for the event. Either keyCode or charCode519 * are required.520 * @method fireKeyEvent521 * @static522 */523 fireKeyEvent : function (type /*:String*/, target /*:HTMLElement*/,524 options /*:Object*/) /*:Void*/525 {526 options = options || {};527 this.simulateKeyEvent(target, type, options.bubbles,528 options.cancelable, options.view, options.ctrlKey,529 options.altKey, options.shiftKey, options.metaKey,530 options.keyCode, options.charCode);531 },532 /**533 * Simulates a keydown event on a particular element.534 * @param {HTMLElement} target The element to act on.535 * @param {Object} options Additional event options (use DOM standard names).536 * @method keydown537 * @static538 */539 keydown : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {540 this.fireKeyEvent("keydown", target, options);541 },542 /**543 * Simulates a keypress on a particular element.544 * @param {HTMLElement} target The element to act on.545 * @param {Object} options Additional event options (use DOM standard names).546 * @method keypress547 * @static548 */549 keypress : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {550 this.fireKeyEvent("keypress", target, options);551 },552 /**553 * Simulates a keyup event on a particular element.554 * @param {HTMLElement} target The element to act on.555 * @param {Object} options Additional event options (use DOM standard names).556 * @method keyup557 * @static558 */559 keyup : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {560 this.fireKeyEvent("keyup", target, options);561 }562};563YAHOO.register("event-simulate", YAHOO.util.UserAction, {version: "2.9.0", build: "2800"});...

Full Screen

Full Screen

yui2-event-simulate-debug.js

Source:yui2-event-simulate-debug.js Github

copy

Full Screen

1YUI.add('yui2-event-simulate', function(Y) {2 var YAHOO = Y.YUI2;3 /*4Copyright (c) 2011, Yahoo! Inc. All rights reserved.5Code licensed under the BSD License:6http://developer.yahoo.com/yui/license.html7version: 2.9.08*/9/**10 * DOM event simulation utility11 * @module event-simulate12 * @namespace YAHOO.util13 * @requires yahoo,dom,event14 */15/**16 * The UserAction object provides functions that simulate events occurring in17 * the browser. Since these are simulated events, they do not behave exactly18 * as regular, user-initiated events do, but can be used to test simple19 * user interactions safely.20 *21 * @namespace YAHOO.util22 * @class UserAction23 * @static24 */25YAHOO.util.UserAction = {26 //--------------------------------------------------------------------------27 // Generic event methods28 //--------------------------------------------------------------------------29 /**30 * Simulates a key event using the given event information to populate31 * the generated event object. This method does browser-equalizing32 * calculations to account for differences in the DOM and IE event models33 * as well as different browser quirks. Note: keydown causes Safari 2.x to34 * crash.35 * @method simulateKeyEvent36 * @private37 * @static38 * @param {HTMLElement} target The target of the given event.39 * @param {String} type The type of event to fire. This can be any one of40 * the following: keyup, keydown, and keypress.41 * @param {Boolean} bubbles (Optional) Indicates if the event can be42 * bubbled up. DOM Level 3 specifies that all key events bubble by43 * default. The default is true.44 * @param {Boolean} cancelable (Optional) Indicates if the event can be45 * canceled using preventDefault(). DOM Level 3 specifies that all46 * key events can be cancelled. The default47 * is true.48 * @param {Window} view (Optional) The view containing the target. This is49 * typically the window object. The default is window.50 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys51 * is pressed while the event is firing. The default is false.52 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys53 * is pressed while the event is firing. The default is false.54 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys55 * is pressed while the event is firing. The default is false.56 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys57 * is pressed while the event is firing. The default is false.58 * @param {int} keyCode (Optional) The code for the key that is in use.59 * The default is 0.60 * @param {int} charCode (Optional) The Unicode code for the character61 * associated with the key being used. The default is 0.62 */63 simulateKeyEvent : function (target /*:HTMLElement*/, type /*:String*/,64 bubbles /*:Boolean*/, cancelable /*:Boolean*/,65 view /*:Window*/,66 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,67 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,68 keyCode /*:int*/, charCode /*:int*/) /*:Void*/69 {70 //check target71 target = YAHOO.util.Dom.get(target);72 if (!target){73 throw new Error("simulateKeyEvent(): Invalid target.");74 }75 //check event type76 if (YAHOO.lang.isString(type)){77 type = type.toLowerCase();78 switch(type){79 case "keyup":80 case "keydown":81 case "keypress":82 break;83 case "textevent": //DOM Level 384 type = "keypress";85 break;86 // @TODO was the fallthrough intentional, if so throw error87 default:88 throw new Error("simulateKeyEvent(): Event type '" + type + "' not supported.");89 }90 } else {91 throw new Error("simulateKeyEvent(): Event type must be a string.");92 }93 //setup default values94 if (!YAHOO.lang.isBoolean(bubbles)){95 bubbles = true; //all key events bubble96 }97 if (!YAHOO.lang.isBoolean(cancelable)){98 cancelable = true; //all key events can be cancelled99 }100 if (!YAHOO.lang.isObject(view)){101 view = window; //view is typically window102 }103 if (!YAHOO.lang.isBoolean(ctrlKey)){104 ctrlKey = false;105 }106 if (!YAHOO.lang.isBoolean(altKey)){107 altKey = false;108 }109 if (!YAHOO.lang.isBoolean(shiftKey)){110 shiftKey = false;111 }112 if (!YAHOO.lang.isBoolean(metaKey)){113 metaKey = false;114 }115 if (!YAHOO.lang.isNumber(keyCode)){116 keyCode = 0;117 }118 if (!YAHOO.lang.isNumber(charCode)){119 charCode = 0;120 }121 //try to create a mouse event122 var customEvent /*:MouseEvent*/ = null;123 //check for DOM-compliant browsers first124 if (YAHOO.lang.isFunction(document.createEvent)){125 try {126 //try to create key event127 customEvent = document.createEvent("KeyEvents");128 /*129 * Interesting problem: Firefox implemented a non-standard130 * version of initKeyEvent() based on DOM Level 2 specs.131 * Key event was removed from DOM Level 2 and re-introduced132 * in DOM Level 3 with a different interface. Firefox is the133 * only browser with any implementation of Key Events, so for134 * now, assume it's Firefox if the above line doesn't error.135 */136 //TODO: Decipher between Firefox's implementation and a correct one.137 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,138 altKey, shiftKey, metaKey, keyCode, charCode);139 } catch (ex /*:Error*/){140 /*141 * If it got here, that means key events aren't officially supported.142 * Safari/WebKit is a real problem now. WebKit 522 won't let you143 * set keyCode, charCode, or other properties if you use a144 * UIEvent, so we first must try to create a generic event. The145 * fun part is that this will throw an error on Safari 2.x. The146 * end result is that we need another try...catch statement just to147 * deal with this mess.148 */149 try {150 //try to create generic event - will fail in Safari 2.x151 customEvent = document.createEvent("Events");152 } catch (uierror /*:Error*/){153 //the above failed, so create a UIEvent for Safari 2.x154 customEvent = document.createEvent("UIEvents");155 } finally {156 customEvent.initEvent(type, bubbles, cancelable);157 //initialize158 customEvent.view = view;159 customEvent.altKey = altKey;160 customEvent.ctrlKey = ctrlKey;161 customEvent.shiftKey = shiftKey;162 customEvent.metaKey = metaKey;163 customEvent.keyCode = keyCode;164 customEvent.charCode = charCode;165 }166 }167 //fire the event168 target.dispatchEvent(customEvent);169 } else if (YAHOO.lang.isObject(document.createEventObject)){ //IE170 //create an IE event object171 customEvent = document.createEventObject();172 //assign available properties173 customEvent.bubbles = bubbles;174 customEvent.cancelable = cancelable;175 customEvent.view = view;176 customEvent.ctrlKey = ctrlKey;177 customEvent.altKey = altKey;178 customEvent.shiftKey = shiftKey;179 customEvent.metaKey = metaKey;180 /*181 * IE doesn't support charCode explicitly. CharCode should182 * take precedence over any keyCode value for accurate183 * representation.184 */185 customEvent.keyCode = (charCode > 0) ? charCode : keyCode;186 //fire the event187 target.fireEvent("on" + type, customEvent);188 } else {189 throw new Error("simulateKeyEvent(): No event simulation framework present.");190 }191 },192 /**193 * Simulates a mouse event using the given event information to populate194 * the generated event object. This method does browser-equalizing195 * calculations to account for differences in the DOM and IE event models196 * as well as different browser quirks.197 * @method simulateMouseEvent198 * @private199 * @static200 * @param {HTMLElement} target The target of the given event.201 * @param {String} type The type of event to fire. This can be any one of202 * the following: click, dblclick, mousedown, mouseup, mouseout,203 * mouseover, and mousemove.204 * @param {Boolean} bubbles (Optional) Indicates if the event can be205 * bubbled up. DOM Level 2 specifies that all mouse events bubble by206 * default. The default is true.207 * @param {Boolean} cancelable (Optional) Indicates if the event can be208 * canceled using preventDefault(). DOM Level 2 specifies that all209 * mouse events except mousemove can be cancelled. The default210 * is true for all events except mousemove, for which the default211 * is false.212 * @param {Window} view (Optional) The view containing the target. This is213 * typically the window object. The default is window.214 * @param {int} detail (Optional) The number of times the mouse button has215 * been used. The default value is 1.216 * @param {int} screenX (Optional) The x-coordinate on the screen at which217 * point the event occured. The default is 0.218 * @param {int} screenY (Optional) The y-coordinate on the screen at which219 * point the event occured. The default is 0.220 * @param {int} clientX (Optional) The x-coordinate on the client at which221 * point the event occured. The default is 0.222 * @param {int} clientY (Optional) The y-coordinate on the client at which223 * point the event occured. The default is 0.224 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys225 * is pressed while the event is firing. The default is false.226 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys227 * is pressed while the event is firing. The default is false.228 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys229 * is pressed while the event is firing. The default is false.230 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys231 * is pressed while the event is firing. The default is false.232 * @param {int} button (Optional) The button being pressed while the event233 * is executing. The value should be 0 for the primary mouse button234 * (typically the left button), 1 for the terciary mouse button235 * (typically the middle button), and 2 for the secondary mouse button236 * (typically the right button). The default is 0.237 * @param {HTMLElement} relatedTarget (Optional) For mouseout events,238 * this is the element that the mouse has moved to. For mouseover239 * events, this is the element that the mouse has moved from. This240 * argument is ignored for all other events. The default is null.241 */242 simulateMouseEvent : function (target /*:HTMLElement*/, type /*:String*/,243 bubbles /*:Boolean*/, cancelable /*:Boolean*/,244 view /*:Window*/, detail /*:int*/,245 screenX /*:int*/, screenY /*:int*/,246 clientX /*:int*/, clientY /*:int*/,247 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,248 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,249 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/250 {251 //check target252 target = YAHOO.util.Dom.get(target);253 if (!target){254 throw new Error("simulateMouseEvent(): Invalid target.");255 }256 relatedTarget = relatedTarget || null;257 //check event type258 if (YAHOO.lang.isString(type)){259 type = type.toLowerCase();260 switch(type){261 case "mouseover":262 case "mouseout":263 case "mousedown":264 case "mouseup":265 case "click":266 case "dblclick":267 case "mousemove":268 break;269 default:270 throw new Error("simulateMouseEvent(): Event type '" + type + "' not supported.");271 }272 } else {273 throw new Error("simulateMouseEvent(): Event type must be a string.");274 }275 //setup default values276 if (!YAHOO.lang.isBoolean(bubbles)){277 bubbles = true; //all mouse events bubble278 }279 if (!YAHOO.lang.isBoolean(cancelable)){280 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled281 }282 if (!YAHOO.lang.isObject(view)){283 view = window; //view is typically window284 }285 if (!YAHOO.lang.isNumber(detail)){286 detail = 1; //number of mouse clicks must be at least one287 }288 if (!YAHOO.lang.isNumber(screenX)){289 screenX = 0;290 }291 if (!YAHOO.lang.isNumber(screenY)){292 screenY = 0;293 }294 if (!YAHOO.lang.isNumber(clientX)){295 clientX = 0;296 }297 if (!YAHOO.lang.isNumber(clientY)){298 clientY = 0;299 }300 if (!YAHOO.lang.isBoolean(ctrlKey)){301 ctrlKey = false;302 }303 if (!YAHOO.lang.isBoolean(altKey)){304 altKey = false;305 }306 if (!YAHOO.lang.isBoolean(shiftKey)){307 shiftKey = false;308 }309 if (!YAHOO.lang.isBoolean(metaKey)){310 metaKey = false;311 }312 if (!YAHOO.lang.isNumber(button)){313 button = 0;314 }315 //try to create a mouse event316 var customEvent /*:MouseEvent*/ = null;317 //check for DOM-compliant browsers first318 if (YAHOO.lang.isFunction(document.createEvent)){319 customEvent = document.createEvent("MouseEvents");320 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()321 if (customEvent.initMouseEvent){322 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,323 screenX, screenY, clientX, clientY,324 ctrlKey, altKey, shiftKey, metaKey,325 button, relatedTarget);326 } else { //Safari327 //the closest thing available in Safari 2.x is UIEvents328 customEvent = document.createEvent("UIEvents");329 customEvent.initEvent(type, bubbles, cancelable);330 customEvent.view = view;331 customEvent.detail = detail;332 customEvent.screenX = screenX;333 customEvent.screenY = screenY;334 customEvent.clientX = clientX;335 customEvent.clientY = clientY;336 customEvent.ctrlKey = ctrlKey;337 customEvent.altKey = altKey;338 customEvent.metaKey = metaKey;339 customEvent.shiftKey = shiftKey;340 customEvent.button = button;341 customEvent.relatedTarget = relatedTarget;342 }343 /*344 * Check to see if relatedTarget has been assigned. Firefox345 * versions less than 2.0 don't allow it to be assigned via346 * initMouseEvent() and the property is readonly after event347 * creation, so in order to keep YAHOO.util.getRelatedTarget()348 * working, assign to the IE proprietary toElement property349 * for mouseout event and fromElement property for mouseover350 * event.351 */352 if (relatedTarget && !customEvent.relatedTarget){353 if (type == "mouseout"){354 customEvent.toElement = relatedTarget;355 } else if (type == "mouseover"){356 customEvent.fromElement = relatedTarget;357 }358 }359 //fire the event360 target.dispatchEvent(customEvent);361 } else if (YAHOO.lang.isObject(document.createEventObject)){ //IE362 //create an IE event object363 customEvent = document.createEventObject();364 //assign available properties365 customEvent.bubbles = bubbles;366 customEvent.cancelable = cancelable;367 customEvent.view = view;368 customEvent.detail = detail;369 customEvent.screenX = screenX;370 customEvent.screenY = screenY;371 customEvent.clientX = clientX;372 customEvent.clientY = clientY;373 customEvent.ctrlKey = ctrlKey;374 customEvent.altKey = altKey;375 customEvent.metaKey = metaKey;376 customEvent.shiftKey = shiftKey;377 //fix button property for IE's wacky implementation378 switch(button){379 case 0:380 customEvent.button = 1;381 break;382 case 1:383 customEvent.button = 4;384 break;385 case 2:386 //leave as is387 break;388 default:389 customEvent.button = 0;390 }391 /*392 * Have to use relatedTarget because IE won't allow assignment393 * to toElement or fromElement on generic events. This keeps394 * YAHOO.util.customEvent.getRelatedTarget() functional.395 */396 customEvent.relatedTarget = relatedTarget;397 //fire the event398 target.fireEvent("on" + type, customEvent);399 } else {400 throw new Error("simulateMouseEvent(): No event simulation framework present.");401 }402 },403 //--------------------------------------------------------------------------404 // Mouse events405 //--------------------------------------------------------------------------406 /**407 * Simulates a mouse event on a particular element.408 * @param {HTMLElement} target The element to click on.409 * @param {String} type The type of event to fire. This can be any one of410 * the following: click, dblclick, mousedown, mouseup, mouseout,411 * mouseover, and mousemove.412 * @param {Object} options Additional event options (use DOM standard names).413 * @method mouseEvent414 * @static415 */416 fireMouseEvent : function (target /*:HTMLElement*/, type /*:String*/,417 options /*:Object*/) /*:Void*/418 {419 options = options || {};420 this.simulateMouseEvent(target, type, options.bubbles,421 options.cancelable, options.view, options.detail, options.screenX,422 options.screenY, options.clientX, options.clientY, options.ctrlKey,423 options.altKey, options.shiftKey, options.metaKey, options.button,424 options.relatedTarget);425 },426 /**427 * Simulates a click on a particular element.428 * @param {HTMLElement} target The element to click on.429 * @param {Object} options Additional event options (use DOM standard names).430 * @method click431 * @static432 */433 click : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {434 this.fireMouseEvent(target, "click", options);435 },436 /**437 * Simulates a double click on a particular element.438 * @param {HTMLElement} target The element to double click on.439 * @param {Object} options Additional event options (use DOM standard names).440 * @method dblclick441 * @static442 */443 dblclick : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {444 this.fireMouseEvent( target, "dblclick", options);445 },446 /**447 * Simulates a mousedown on a particular element.448 * @param {HTMLElement} target The element to act on.449 * @param {Object} options Additional event options (use DOM standard names).450 * @method mousedown451 * @static452 */453 mousedown : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {454 this.fireMouseEvent(target, "mousedown", options);455 },456 /**457 * Simulates a mousemove on a particular element.458 * @param {HTMLElement} target The element to act on.459 * @param {Object} options Additional event options (use DOM standard names).460 * @method mousemove461 * @static462 */463 mousemove : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {464 this.fireMouseEvent(target, "mousemove", options);465 },466 /**467 * Simulates a mouseout event on a particular element. Use "relatedTarget"468 * on the options object to specify where the mouse moved to.469 * Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so470 * toElement is assigned in its place. IE doesn't allow toElement to be471 * be assigned, so relatedTarget is assigned in its place. Both of these472 * concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly473 * in both browsers.474 * @param {HTMLElement} target The element to act on.475 * @param {Object} options Additional event options (use DOM standard names).476 * @method mouseout477 * @static478 */479 mouseout : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {480 this.fireMouseEvent(target, "mouseout", options);481 },482 /**483 * Simulates a mouseover event on a particular element. Use "relatedTarget"484 * on the options object to specify where the mouse moved from.485 * Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so486 * fromElement is assigned in its place. IE doesn't allow fromElement to be487 * be assigned, so relatedTarget is assigned in its place. Both of these488 * concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly489 * in both browsers.490 * @param {HTMLElement} target The element to act on.491 * @param {Object} options Additional event options (use DOM standard names).492 * @method mouseover493 * @static494 */495 mouseover : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {496 this.fireMouseEvent(target, "mouseover", options);497 },498 /**499 * Simulates a mouseup on a particular element.500 * @param {HTMLElement} target The element to act on.501 * @param {Object} options Additional event options (use DOM standard names).502 * @method mouseup503 * @static504 */505 mouseup : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {506 this.fireMouseEvent(target, "mouseup", options);507 },508 //--------------------------------------------------------------------------509 // Key events510 //--------------------------------------------------------------------------511 /**512 * Fires an event that normally would be fired by the keyboard (keyup,513 * keydown, keypress). Make sure to specify either keyCode or charCode as514 * an option.515 * @private516 * @param {String} type The type of event ("keyup", "keydown" or "keypress").517 * @param {HTMLElement} target The target of the event.518 * @param {Object} options Options for the event. Either keyCode or charCode519 * are required.520 * @method fireKeyEvent521 * @static522 */523 fireKeyEvent : function (type /*:String*/, target /*:HTMLElement*/,524 options /*:Object*/) /*:Void*/525 {526 options = options || {};527 this.simulateKeyEvent(target, type, options.bubbles,528 options.cancelable, options.view, options.ctrlKey,529 options.altKey, options.shiftKey, options.metaKey,530 options.keyCode, options.charCode);531 },532 /**533 * Simulates a keydown event on a particular element.534 * @param {HTMLElement} target The element to act on.535 * @param {Object} options Additional event options (use DOM standard names).536 * @method keydown537 * @static538 */539 keydown : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {540 this.fireKeyEvent("keydown", target, options);541 },542 /**543 * Simulates a keypress on a particular element.544 * @param {HTMLElement} target The element to act on.545 * @param {Object} options Additional event options (use DOM standard names).546 * @method keypress547 * @static548 */549 keypress : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ {550 this.fireKeyEvent("keypress", target, options);551 },552 /**553 * Simulates a keyup event on a particular element.554 * @param {HTMLElement} target The element to act on.555 * @param {Object} options Additional event options (use DOM standard names).556 * @method keyup557 * @static558 */559 keyup : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ {560 this.fireKeyEvent("keyup", target, options);561 }562};563YAHOO.register("event-simulate", YAHOO.util.UserAction, {version: "2.9.0", build: "2800"});...

Full Screen

Full Screen

event-simulate-debug.js

Source:event-simulate-debug.js Github

copy

Full Screen

1/*2Copyright (c) 2010, Yahoo! Inc. All rights reserved.3Code licensed under the BSD License:4http://developer.yahoo.com/yui/license.html5version: 3.4.06build: nightly7*/8YUI.add('event-simulate', function(Y) {9(function() {10/**11 * Simulate user interaction by generating native DOM events.12 *13 * @module event-simulate14 * @requires event15 */16//shortcuts17var L = Y.Lang,18 array = Y.Array,19 isFunction = L.isFunction,20 isString = L.isString,21 isBoolean = L.isBoolean,22 isObject = L.isObject,23 isNumber = L.isNumber,24 doc = Y.config.doc,25 //mouse events supported26 mouseEvents = {27 click: 1,28 dblclick: 1,29 mouseover: 1,30 mouseout: 1,31 mousedown: 1,32 mouseup: 1,33 mousemove: 134 },35 //key events supported36 keyEvents = {37 keydown: 1,38 keyup: 1,39 keypress: 140 },41 //HTML events supported42 uiEvents = {43 blur: 1,44 change: 1,45 focus: 1,46 resize: 1,47 scroll: 1,48 select: 149 },50 //events that bubble by default51 bubbleEvents = {52 scroll: 1,53 resize: 1,54 reset: 1,55 submit: 1,56 change: 1,57 select: 1,58 error: 1,59 abort: 160 };61//all key and mouse events bubble62Y.mix(bubbleEvents, mouseEvents);63Y.mix(bubbleEvents, keyEvents);64/*65 * Note: Intentionally not for YUIDoc generation.66 * Simulates a key event using the given event information to populate67 * the generated event object. This method does browser-equalizing68 * calculations to account for differences in the DOM and IE event models69 * as well as different browser quirks. Note: keydown causes Safari 2.x to70 * crash.71 * @method simulateKeyEvent72 * @private73 * @static74 * @param {HTMLElement} target The target of the given event.75 * @param {String} type The type of event to fire. This can be any one of76 * the following: keyup, keydown, and keypress.77 * @param {Boolean} bubbles (Optional) Indicates if the event can be78 * bubbled up. DOM Level 3 specifies that all key events bubble by79 * default. The default is true.80 * @param {Boolean} cancelable (Optional) Indicates if the event can be81 * canceled using preventDefault(). DOM Level 3 specifies that all82 * key events can be cancelled. The default83 * is true.84 * @param {Window} view (Optional) The view containing the target. This is85 * typically the window object. The default is window.86 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys87 * is pressed while the event is firing. The default is false.88 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys89 * is pressed while the event is firing. The default is false.90 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys91 * is pressed while the event is firing. The default is false.92 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys93 * is pressed while the event is firing. The default is false.94 * @param {int} keyCode (Optional) The code for the key that is in use.95 * The default is 0.96 * @param {int} charCode (Optional) The Unicode code for the character97 * associated with the key being used. The default is 0.98 */99function simulateKeyEvent(target /*:HTMLElement*/, type /*:String*/,100 bubbles /*:Boolean*/, cancelable /*:Boolean*/,101 view /*:Window*/,102 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,103 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,104 keyCode /*:int*/, charCode /*:int*/) /*:Void*/105{106 //check target107 if (!target){108 Y.error("simulateKeyEvent(): Invalid target.");109 }110 //check event type111 if (isString(type)){112 type = type.toLowerCase();113 switch(type){114 case "textevent": //DOM Level 3115 type = "keypress";116 break;117 case "keyup":118 case "keydown":119 case "keypress":120 break;121 default:122 Y.error("simulateKeyEvent(): Event type '" + type + "' not supported.");123 }124 } else {125 Y.error("simulateKeyEvent(): Event type must be a string.");126 }127 //setup default values128 if (!isBoolean(bubbles)){129 bubbles = true; //all key events bubble130 }131 if (!isBoolean(cancelable)){132 cancelable = true; //all key events can be cancelled133 }134 if (!isObject(view)){135 view = window; //view is typically window136 }137 if (!isBoolean(ctrlKey)){138 ctrlKey = false;139 }140 if (!isBoolean(altKey)){141 altKey = false;142 }143 if (!isBoolean(shiftKey)){144 shiftKey = false;145 }146 if (!isBoolean(metaKey)){147 metaKey = false;148 }149 if (!isNumber(keyCode)){150 keyCode = 0;151 }152 if (!isNumber(charCode)){153 charCode = 0;154 }155 //try to create a mouse event156 var customEvent /*:MouseEvent*/ = null;157 //check for DOM-compliant browsers first158 if (isFunction(doc.createEvent)){159 try {160 //try to create key event161 customEvent = doc.createEvent("KeyEvents");162 /*163 * Interesting problem: Firefox implemented a non-standard164 * version of initKeyEvent() based on DOM Level 2 specs.165 * Key event was removed from DOM Level 2 and re-introduced166 * in DOM Level 3 with a different interface. Firefox is the167 * only browser with any implementation of Key Events, so for168 * now, assume it's Firefox if the above line doesn't error.169 */170 // @TODO: Decipher between Firefox's implementation and a correct one.171 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,172 altKey, shiftKey, metaKey, keyCode, charCode);173 } catch (ex /*:Error*/){174 /*175 * If it got here, that means key events aren't officially supported.176 * Safari/WebKit is a real problem now. WebKit 522 won't let you177 * set keyCode, charCode, or other properties if you use a178 * UIEvent, so we first must try to create a generic event. The179 * fun part is that this will throw an error on Safari 2.x. The180 * end result is that we need another try...catch statement just to181 * deal with this mess.182 */183 try {184 //try to create generic event - will fail in Safari 2.x185 customEvent = doc.createEvent("Events");186 } catch (uierror /*:Error*/){187 //the above failed, so create a UIEvent for Safari 2.x188 customEvent = doc.createEvent("UIEvents");189 } finally {190 customEvent.initEvent(type, bubbles, cancelable);191 //initialize192 customEvent.view = view;193 customEvent.altKey = altKey;194 customEvent.ctrlKey = ctrlKey;195 customEvent.shiftKey = shiftKey;196 customEvent.metaKey = metaKey;197 customEvent.keyCode = keyCode;198 customEvent.charCode = charCode;199 }200 }201 //fire the event202 target.dispatchEvent(customEvent);203 } else if (isObject(doc.createEventObject)){ //IE204 //create an IE event object205 customEvent = doc.createEventObject();206 //assign available properties207 customEvent.bubbles = bubbles;208 customEvent.cancelable = cancelable;209 customEvent.view = view;210 customEvent.ctrlKey = ctrlKey;211 customEvent.altKey = altKey;212 customEvent.shiftKey = shiftKey;213 customEvent.metaKey = metaKey;214 /*215 * IE doesn't support charCode explicitly. CharCode should216 * take precedence over any keyCode value for accurate217 * representation.218 */219 customEvent.keyCode = (charCode > 0) ? charCode : keyCode;220 //fire the event221 target.fireEvent("on" + type, customEvent);222 } else {223 Y.error("simulateKeyEvent(): No event simulation framework present.");224 }225}226/*227 * Note: Intentionally not for YUIDoc generation.228 * Simulates a mouse event using the given event information to populate229 * the generated event object. This method does browser-equalizing230 * calculations to account for differences in the DOM and IE event models231 * as well as different browser quirks.232 * @method simulateMouseEvent233 * @private234 * @static235 * @param {HTMLElement} target The target of the given event.236 * @param {String} type The type of event to fire. This can be any one of237 * the following: click, dblclick, mousedown, mouseup, mouseout,238 * mouseover, and mousemove.239 * @param {Boolean} bubbles (Optional) Indicates if the event can be240 * bubbled up. DOM Level 2 specifies that all mouse events bubble by241 * default. The default is true.242 * @param {Boolean} cancelable (Optional) Indicates if the event can be243 * canceled using preventDefault(). DOM Level 2 specifies that all244 * mouse events except mousemove can be cancelled. The default245 * is true for all events except mousemove, for which the default246 * is false.247 * @param {Window} view (Optional) The view containing the target. This is248 * typically the window object. The default is window.249 * @param {int} detail (Optional) The number of times the mouse button has250 * been used. The default value is 1.251 * @param {int} screenX (Optional) The x-coordinate on the screen at which252 * point the event occured. The default is 0.253 * @param {int} screenY (Optional) The y-coordinate on the screen at which254 * point the event occured. The default is 0.255 * @param {int} clientX (Optional) The x-coordinate on the client at which256 * point the event occured. The default is 0.257 * @param {int} clientY (Optional) The y-coordinate on the client at which258 * point the event occured. The default is 0.259 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys260 * is pressed while the event is firing. The default is false.261 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys262 * is pressed while the event is firing. The default is false.263 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys264 * is pressed while the event is firing. The default is false.265 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys266 * is pressed while the event is firing. The default is false.267 * @param {int} button (Optional) The button being pressed while the event268 * is executing. The value should be 0 for the primary mouse button269 * (typically the left button), 1 for the terciary mouse button270 * (typically the middle button), and 2 for the secondary mouse button271 * (typically the right button). The default is 0.272 * @param {HTMLElement} relatedTarget (Optional) For mouseout events,273 * this is the element that the mouse has moved to. For mouseover274 * events, this is the element that the mouse has moved from. This275 * argument is ignored for all other events. The default is null.276 */277function simulateMouseEvent(target /*:HTMLElement*/, type /*:String*/,278 bubbles /*:Boolean*/, cancelable /*:Boolean*/,279 view /*:Window*/, detail /*:int*/,280 screenX /*:int*/, screenY /*:int*/,281 clientX /*:int*/, clientY /*:int*/,282 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,283 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,284 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/285{286 //check target287 if (!target){288 Y.error("simulateMouseEvent(): Invalid target.");289 }290 //check event type291 if (isString(type)){292 type = type.toLowerCase();293 //make sure it's a supported mouse event294 if (!mouseEvents[type]){295 Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");296 }297 } else {298 Y.error("simulateMouseEvent(): Event type must be a string.");299 }300 //setup default values301 if (!isBoolean(bubbles)){302 bubbles = true; //all mouse events bubble303 }304 if (!isBoolean(cancelable)){305 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled306 }307 if (!isObject(view)){308 view = window; //view is typically window309 }310 if (!isNumber(detail)){311 detail = 1; //number of mouse clicks must be at least one312 }313 if (!isNumber(screenX)){314 screenX = 0;315 }316 if (!isNumber(screenY)){317 screenY = 0;318 }319 if (!isNumber(clientX)){320 clientX = 0;321 }322 if (!isNumber(clientY)){323 clientY = 0;324 }325 if (!isBoolean(ctrlKey)){326 ctrlKey = false;327 }328 if (!isBoolean(altKey)){329 altKey = false;330 }331 if (!isBoolean(shiftKey)){332 shiftKey = false;333 }334 if (!isBoolean(metaKey)){335 metaKey = false;336 }337 if (!isNumber(button)){338 button = 0;339 }340 relatedTarget = relatedTarget || null;341 //try to create a mouse event342 var customEvent /*:MouseEvent*/ = null;343 //check for DOM-compliant browsers first344 if (isFunction(doc.createEvent)){345 customEvent = doc.createEvent("MouseEvents");346 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()347 if (customEvent.initMouseEvent){348 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,349 screenX, screenY, clientX, clientY,350 ctrlKey, altKey, shiftKey, metaKey,351 button, relatedTarget);352 } else { //Safari353 //the closest thing available in Safari 2.x is UIEvents354 customEvent = doc.createEvent("UIEvents");355 customEvent.initEvent(type, bubbles, cancelable);356 customEvent.view = view;357 customEvent.detail = detail;358 customEvent.screenX = screenX;359 customEvent.screenY = screenY;360 customEvent.clientX = clientX;361 customEvent.clientY = clientY;362 customEvent.ctrlKey = ctrlKey;363 customEvent.altKey = altKey;364 customEvent.metaKey = metaKey;365 customEvent.shiftKey = shiftKey;366 customEvent.button = button;367 customEvent.relatedTarget = relatedTarget;368 }369 /*370 * Check to see if relatedTarget has been assigned. Firefox371 * versions less than 2.0 don't allow it to be assigned via372 * initMouseEvent() and the property is readonly after event373 * creation, so in order to keep YAHOO.util.getRelatedTarget()374 * working, assign to the IE proprietary toElement property375 * for mouseout event and fromElement property for mouseover376 * event.377 */378 if (relatedTarget && !customEvent.relatedTarget){379 if (type == "mouseout"){380 customEvent.toElement = relatedTarget;381 } else if (type == "mouseover"){382 customEvent.fromElement = relatedTarget;383 }384 }385 //fire the event386 target.dispatchEvent(customEvent);387 } else if (isObject(doc.createEventObject)){ //IE388 //create an IE event object389 customEvent = doc.createEventObject();390 //assign available properties391 customEvent.bubbles = bubbles;392 customEvent.cancelable = cancelable;393 customEvent.view = view;394 customEvent.detail = detail;395 customEvent.screenX = screenX;396 customEvent.screenY = screenY;397 customEvent.clientX = clientX;398 customEvent.clientY = clientY;399 customEvent.ctrlKey = ctrlKey;400 customEvent.altKey = altKey;401 customEvent.metaKey = metaKey;402 customEvent.shiftKey = shiftKey;403 //fix button property for IE's wacky implementation404 switch(button){405 case 0:406 customEvent.button = 1;407 break;408 case 1:409 customEvent.button = 4;410 break;411 case 2:412 //leave as is413 break;414 default:415 customEvent.button = 0;416 }417 /*418 * Have to use relatedTarget because IE won't allow assignment419 * to toElement or fromElement on generic events. This keeps420 * YAHOO.util.customEvent.getRelatedTarget() functional.421 */422 customEvent.relatedTarget = relatedTarget;423 //fire the event424 target.fireEvent("on" + type, customEvent);425 } else {426 Y.error("simulateMouseEvent(): No event simulation framework present.");427 }428}429/*430 * Note: Intentionally not for YUIDoc generation.431 * Simulates a UI event using the given event information to populate432 * the generated event object. This method does browser-equalizing433 * calculations to account for differences in the DOM and IE event models434 * as well as different browser quirks.435 * @method simulateHTMLEvent436 * @private437 * @static438 * @param {HTMLElement} target The target of the given event.439 * @param {String} type The type of event to fire. This can be any one of440 * the following: click, dblclick, mousedown, mouseup, mouseout,441 * mouseover, and mousemove.442 * @param {Boolean} bubbles (Optional) Indicates if the event can be443 * bubbled up. DOM Level 2 specifies that all mouse events bubble by444 * default. The default is true.445 * @param {Boolean} cancelable (Optional) Indicates if the event can be446 * canceled using preventDefault(). DOM Level 2 specifies that all447 * mouse events except mousemove can be cancelled. The default448 * is true for all events except mousemove, for which the default449 * is false.450 * @param {Window} view (Optional) The view containing the target. This is451 * typically the window object. The default is window.452 * @param {int} detail (Optional) The number of times the mouse button has453 * been used. The default value is 1.454 */455function simulateUIEvent(target /*:HTMLElement*/, type /*:String*/,456 bubbles /*:Boolean*/, cancelable /*:Boolean*/,457 view /*:Window*/, detail /*:int*/) /*:Void*/458{459 //check target460 if (!target){461 Y.error("simulateUIEvent(): Invalid target.");462 }463 //check event type464 if (isString(type)){465 type = type.toLowerCase();466 //make sure it's a supported mouse event467 if (!uiEvents[type]){468 Y.error("simulateUIEvent(): Event type '" + type + "' not supported.");469 }470 } else {471 Y.error("simulateUIEvent(): Event type must be a string.");472 }473 //try to create a mouse event474 var customEvent = null;475 //setup default values476 if (!isBoolean(bubbles)){477 bubbles = (type in bubbleEvents); //not all events bubble478 }479 if (!isBoolean(cancelable)){480 cancelable = (type == "submit"); //submit is the only one that can be cancelled481 }482 if (!isObject(view)){483 view = window; //view is typically window484 }485 if (!isNumber(detail)){486 detail = 1; //usually not used but defaulted to this487 }488 //check for DOM-compliant browsers first489 if (isFunction(doc.createEvent)){490 //just a generic UI Event object is needed491 customEvent = doc.createEvent("UIEvents");492 customEvent.initUIEvent(type, bubbles, cancelable, view, detail);493 //fire the event494 target.dispatchEvent(customEvent);495 } else if (isObject(doc.createEventObject)){ //IE496 //create an IE event object497 customEvent = doc.createEventObject();498 //assign available properties499 customEvent.bubbles = bubbles;500 customEvent.cancelable = cancelable;501 customEvent.view = view;502 customEvent.detail = detail;503 //fire the event504 target.fireEvent("on" + type, customEvent);505 } else {506 Y.error("simulateUIEvent(): No event simulation framework present.");507 }508}509/**510 * Simulates the event with the given name on a target.511 * @param {HTMLElement} target The DOM element that's the target of the event.512 * @param {String} type The type of event to simulate (i.e., "click").513 * @param {Object} options (Optional) Extra options to copy onto the event object.514 * @return {void}515 * @for Event516 * @method simulate517 * @static518 */519Y.Event.simulate = function(target, type, options){520 options = options || {};521 if (mouseEvents[type]){522 simulateMouseEvent(target, type, options.bubbles,523 options.cancelable, options.view, options.detail, options.screenX,524 options.screenY, options.clientX, options.clientY, options.ctrlKey,525 options.altKey, options.shiftKey, options.metaKey, options.button,526 options.relatedTarget);527 } else if (keyEvents[type]){528 simulateKeyEvent(target, type, options.bubbles,529 options.cancelable, options.view, options.ctrlKey,530 options.altKey, options.shiftKey, options.metaKey,531 options.keyCode, options.charCode);532 } else if (uiEvents[type]){533 simulateUIEvent(target, type, options.bubbles,534 options.cancelable, options.view, options.detail);535 } else {536 Y.error("simulate(): Event '" + type + "' can't be simulated.");537 }538};539})();...

Full Screen

Full Screen

event-simulate.js

Source:event-simulate.js Github

copy

Full Screen

1/*2Copyright (c) 2010, Yahoo! Inc. All rights reserved.3Code licensed under the BSD License:4http://developer.yahoo.com/yui/license.html5version: 3.3.06build: 31677*/8YUI.add('event-simulate', function(Y) {9(function() {10/**11 * Synthetic DOM events12 * @module event-simulate13 * @requires event14 */15//shortcuts16var L = Y.Lang,17 array = Y.Array,18 isFunction = L.isFunction,19 isString = L.isString,20 isBoolean = L.isBoolean,21 isObject = L.isObject,22 isNumber = L.isNumber,23 doc = Y.config.doc,24 //mouse events supported25 mouseEvents = {26 click: 1,27 dblclick: 1,28 mouseover: 1,29 mouseout: 1,30 mousedown: 1,31 mouseup: 1,32 mousemove: 133 },34 //key events supported35 keyEvents = {36 keydown: 1,37 keyup: 1,38 keypress: 139 },40 //HTML events supported41 uiEvents = {42 blur: 1,43 change: 1,44 focus: 1,45 resize: 1,46 scroll: 1,47 select: 148 },49 //events that bubble by default50 bubbleEvents = {51 scroll: 1,52 resize: 1,53 reset: 1,54 submit: 1,55 change: 1,56 select: 1,57 error: 1,58 abort: 159 };60//all key and mouse events bubble61Y.mix(bubbleEvents, mouseEvents);62Y.mix(bubbleEvents, keyEvents);63/*64 * Note: Intentionally not for YUIDoc generation.65 * Simulates a key event using the given event information to populate66 * the generated event object. This method does browser-equalizing67 * calculations to account for differences in the DOM and IE event models68 * as well as different browser quirks. Note: keydown causes Safari 2.x to69 * crash.70 * @method simulateKeyEvent71 * @private72 * @static73 * @param {HTMLElement} target The target of the given event.74 * @param {String} type The type of event to fire. This can be any one of75 * the following: keyup, keydown, and keypress.76 * @param {Boolean} bubbles (Optional) Indicates if the event can be77 * bubbled up. DOM Level 3 specifies that all key events bubble by78 * default. The default is true.79 * @param {Boolean} cancelable (Optional) Indicates if the event can be80 * canceled using preventDefault(). DOM Level 3 specifies that all81 * key events can be cancelled. The default82 * is true.83 * @param {Window} view (Optional) The view containing the target. This is84 * typically the window object. The default is window.85 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys86 * is pressed while the event is firing. The default is false.87 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys88 * is pressed while the event is firing. The default is false.89 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys90 * is pressed while the event is firing. The default is false.91 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys92 * is pressed while the event is firing. The default is false.93 * @param {int} keyCode (Optional) The code for the key that is in use.94 * The default is 0.95 * @param {int} charCode (Optional) The Unicode code for the character96 * associated with the key being used. The default is 0.97 */98function simulateKeyEvent(target /*:HTMLElement*/, type /*:String*/,99 bubbles /*:Boolean*/, cancelable /*:Boolean*/,100 view /*:Window*/,101 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,102 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,103 keyCode /*:int*/, charCode /*:int*/) /*:Void*/104{105 //check target106 if (!target){107 Y.error("simulateKeyEvent(): Invalid target.");108 }109 //check event type110 if (isString(type)){111 type = type.toLowerCase();112 switch(type){113 case "textevent": //DOM Level 3114 type = "keypress";115 break;116 case "keyup":117 case "keydown":118 case "keypress":119 break;120 default:121 Y.error("simulateKeyEvent(): Event type '" + type + "' not supported.");122 }123 } else {124 Y.error("simulateKeyEvent(): Event type must be a string.");125 }126 //setup default values127 if (!isBoolean(bubbles)){128 bubbles = true; //all key events bubble129 }130 if (!isBoolean(cancelable)){131 cancelable = true; //all key events can be cancelled132 }133 if (!isObject(view)){134 view = window; //view is typically window135 }136 if (!isBoolean(ctrlKey)){137 ctrlKey = false;138 }139 if (!isBoolean(altKey)){140 altKey = false;141 }142 if (!isBoolean(shiftKey)){143 shiftKey = false;144 }145 if (!isBoolean(metaKey)){146 metaKey = false;147 }148 if (!isNumber(keyCode)){149 keyCode = 0;150 }151 if (!isNumber(charCode)){152 charCode = 0;153 }154 //try to create a mouse event155 var customEvent /*:MouseEvent*/ = null;156 //check for DOM-compliant browsers first157 if (isFunction(doc.createEvent)){158 try {159 //try to create key event160 customEvent = doc.createEvent("KeyEvents");161 /*162 * Interesting problem: Firefox implemented a non-standard163 * version of initKeyEvent() based on DOM Level 2 specs.164 * Key event was removed from DOM Level 2 and re-introduced165 * in DOM Level 3 with a different interface. Firefox is the166 * only browser with any implementation of Key Events, so for167 * now, assume it's Firefox if the above line doesn't error.168 */169 // @TODO: Decipher between Firefox's implementation and a correct one.170 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,171 altKey, shiftKey, metaKey, keyCode, charCode);172 } catch (ex /*:Error*/){173 /*174 * If it got here, that means key events aren't officially supported.175 * Safari/WebKit is a real problem now. WebKit 522 won't let you176 * set keyCode, charCode, or other properties if you use a177 * UIEvent, so we first must try to create a generic event. The178 * fun part is that this will throw an error on Safari 2.x. The179 * end result is that we need another try...catch statement just to180 * deal with this mess.181 */182 try {183 //try to create generic event - will fail in Safari 2.x184 customEvent = doc.createEvent("Events");185 } catch (uierror /*:Error*/){186 //the above failed, so create a UIEvent for Safari 2.x187 customEvent = doc.createEvent("UIEvents");188 } finally {189 customEvent.initEvent(type, bubbles, cancelable);190 //initialize191 customEvent.view = view;192 customEvent.altKey = altKey;193 customEvent.ctrlKey = ctrlKey;194 customEvent.shiftKey = shiftKey;195 customEvent.metaKey = metaKey;196 customEvent.keyCode = keyCode;197 customEvent.charCode = charCode;198 }199 }200 //fire the event201 target.dispatchEvent(customEvent);202 } else if (isObject(doc.createEventObject)){ //IE203 //create an IE event object204 customEvent = doc.createEventObject();205 //assign available properties206 customEvent.bubbles = bubbles;207 customEvent.cancelable = cancelable;208 customEvent.view = view;209 customEvent.ctrlKey = ctrlKey;210 customEvent.altKey = altKey;211 customEvent.shiftKey = shiftKey;212 customEvent.metaKey = metaKey;213 /*214 * IE doesn't support charCode explicitly. CharCode should215 * take precedence over any keyCode value for accurate216 * representation.217 */218 customEvent.keyCode = (charCode > 0) ? charCode : keyCode;219 //fire the event220 target.fireEvent("on" + type, customEvent);221 } else {222 Y.error("simulateKeyEvent(): No event simulation framework present.");223 }224}225/*226 * Note: Intentionally not for YUIDoc generation.227 * Simulates a mouse event using the given event information to populate228 * the generated event object. This method does browser-equalizing229 * calculations to account for differences in the DOM and IE event models230 * as well as different browser quirks.231 * @method simulateMouseEvent232 * @private233 * @static234 * @param {HTMLElement} target The target of the given event.235 * @param {String} type The type of event to fire. This can be any one of236 * the following: click, dblclick, mousedown, mouseup, mouseout,237 * mouseover, and mousemove.238 * @param {Boolean} bubbles (Optional) Indicates if the event can be239 * bubbled up. DOM Level 2 specifies that all mouse events bubble by240 * default. The default is true.241 * @param {Boolean} cancelable (Optional) Indicates if the event can be242 * canceled using preventDefault(). DOM Level 2 specifies that all243 * mouse events except mousemove can be cancelled. The default244 * is true for all events except mousemove, for which the default245 * is false.246 * @param {Window} view (Optional) The view containing the target. This is247 * typically the window object. The default is window.248 * @param {int} detail (Optional) The number of times the mouse button has249 * been used. The default value is 1.250 * @param {int} screenX (Optional) The x-coordinate on the screen at which251 * point the event occured. The default is 0.252 * @param {int} screenY (Optional) The y-coordinate on the screen at which253 * point the event occured. The default is 0.254 * @param {int} clientX (Optional) The x-coordinate on the client at which255 * point the event occured. The default is 0.256 * @param {int} clientY (Optional) The y-coordinate on the client at which257 * point the event occured. The default is 0.258 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys259 * is pressed while the event is firing. The default is false.260 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys261 * is pressed while the event is firing. The default is false.262 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys263 * is pressed while the event is firing. The default is false.264 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys265 * is pressed while the event is firing. The default is false.266 * @param {int} button (Optional) The button being pressed while the event267 * is executing. The value should be 0 for the primary mouse button268 * (typically the left button), 1 for the terciary mouse button269 * (typically the middle button), and 2 for the secondary mouse button270 * (typically the right button). The default is 0.271 * @param {HTMLElement} relatedTarget (Optional) For mouseout events,272 * this is the element that the mouse has moved to. For mouseover273 * events, this is the element that the mouse has moved from. This274 * argument is ignored for all other events. The default is null.275 */276function simulateMouseEvent(target /*:HTMLElement*/, type /*:String*/,277 bubbles /*:Boolean*/, cancelable /*:Boolean*/,278 view /*:Window*/, detail /*:int*/,279 screenX /*:int*/, screenY /*:int*/,280 clientX /*:int*/, clientY /*:int*/,281 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,282 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,283 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/284{285 //check target286 if (!target){287 Y.error("simulateMouseEvent(): Invalid target.");288 }289 //check event type290 if (isString(type)){291 type = type.toLowerCase();292 //make sure it's a supported mouse event293 if (!mouseEvents[type]){294 Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");295 }296 } else {297 Y.error("simulateMouseEvent(): Event type must be a string.");298 }299 //setup default values300 if (!isBoolean(bubbles)){301 bubbles = true; //all mouse events bubble302 }303 if (!isBoolean(cancelable)){304 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled305 }306 if (!isObject(view)){307 view = window; //view is typically window308 }309 if (!isNumber(detail)){310 detail = 1; //number of mouse clicks must be at least one311 }312 if (!isNumber(screenX)){313 screenX = 0;314 }315 if (!isNumber(screenY)){316 screenY = 0;317 }318 if (!isNumber(clientX)){319 clientX = 0;320 }321 if (!isNumber(clientY)){322 clientY = 0;323 }324 if (!isBoolean(ctrlKey)){325 ctrlKey = false;326 }327 if (!isBoolean(altKey)){328 altKey = false;329 }330 if (!isBoolean(shiftKey)){331 shiftKey = false;332 }333 if (!isBoolean(metaKey)){334 metaKey = false;335 }336 if (!isNumber(button)){337 button = 0;338 }339 relatedTarget = relatedTarget || null;340 //try to create a mouse event341 var customEvent /*:MouseEvent*/ = null;342 //check for DOM-compliant browsers first343 if (isFunction(doc.createEvent)){344 customEvent = doc.createEvent("MouseEvents");345 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()346 if (customEvent.initMouseEvent){347 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,348 screenX, screenY, clientX, clientY,349 ctrlKey, altKey, shiftKey, metaKey,350 button, relatedTarget);351 } else { //Safari352 //the closest thing available in Safari 2.x is UIEvents353 customEvent = doc.createEvent("UIEvents");354 customEvent.initEvent(type, bubbles, cancelable);355 customEvent.view = view;356 customEvent.detail = detail;357 customEvent.screenX = screenX;358 customEvent.screenY = screenY;359 customEvent.clientX = clientX;360 customEvent.clientY = clientY;361 customEvent.ctrlKey = ctrlKey;362 customEvent.altKey = altKey;363 customEvent.metaKey = metaKey;364 customEvent.shiftKey = shiftKey;365 customEvent.button = button;366 customEvent.relatedTarget = relatedTarget;367 }368 /*369 * Check to see if relatedTarget has been assigned. Firefox370 * versions less than 2.0 don't allow it to be assigned via371 * initMouseEvent() and the property is readonly after event372 * creation, so in order to keep YAHOO.util.getRelatedTarget()373 * working, assign to the IE proprietary toElement property374 * for mouseout event and fromElement property for mouseover375 * event.376 */377 if (relatedTarget && !customEvent.relatedTarget){378 if (type == "mouseout"){379 customEvent.toElement = relatedTarget;380 } else if (type == "mouseover"){381 customEvent.fromElement = relatedTarget;382 }383 }384 //fire the event385 target.dispatchEvent(customEvent);386 } else if (isObject(doc.createEventObject)){ //IE387 //create an IE event object388 customEvent = doc.createEventObject();389 //assign available properties390 customEvent.bubbles = bubbles;391 customEvent.cancelable = cancelable;392 customEvent.view = view;393 customEvent.detail = detail;394 customEvent.screenX = screenX;395 customEvent.screenY = screenY;396 customEvent.clientX = clientX;397 customEvent.clientY = clientY;398 customEvent.ctrlKey = ctrlKey;399 customEvent.altKey = altKey;400 customEvent.metaKey = metaKey;401 customEvent.shiftKey = shiftKey;402 //fix button property for IE's wacky implementation403 switch(button){404 case 0:405 customEvent.button = 1;406 break;407 case 1:408 customEvent.button = 4;409 break;410 case 2:411 //leave as is412 break;413 default:414 customEvent.button = 0;415 }416 /*417 * Have to use relatedTarget because IE won't allow assignment418 * to toElement or fromElement on generic events. This keeps419 * YAHOO.util.customEvent.getRelatedTarget() functional.420 */421 customEvent.relatedTarget = relatedTarget;422 //fire the event423 target.fireEvent("on" + type, customEvent);424 } else {425 Y.error("simulateMouseEvent(): No event simulation framework present.");426 }427}428/*429 * Note: Intentionally not for YUIDoc generation.430 * Simulates a UI event using the given event information to populate431 * the generated event object. This method does browser-equalizing432 * calculations to account for differences in the DOM and IE event models433 * as well as different browser quirks.434 * @method simulateHTMLEvent435 * @private436 * @static437 * @param {HTMLElement} target The target of the given event.438 * @param {String} type The type of event to fire. This can be any one of439 * the following: click, dblclick, mousedown, mouseup, mouseout,440 * mouseover, and mousemove.441 * @param {Boolean} bubbles (Optional) Indicates if the event can be442 * bubbled up. DOM Level 2 specifies that all mouse events bubble by443 * default. The default is true.444 * @param {Boolean} cancelable (Optional) Indicates if the event can be445 * canceled using preventDefault(). DOM Level 2 specifies that all446 * mouse events except mousemove can be cancelled. The default447 * is true for all events except mousemove, for which the default448 * is false.449 * @param {Window} view (Optional) The view containing the target. This is450 * typically the window object. The default is window.451 * @param {int} detail (Optional) The number of times the mouse button has452 * been used. The default value is 1.453 */454function simulateUIEvent(target /*:HTMLElement*/, type /*:String*/,455 bubbles /*:Boolean*/, cancelable /*:Boolean*/,456 view /*:Window*/, detail /*:int*/) /*:Void*/457{458 //check target459 if (!target){460 Y.error("simulateUIEvent(): Invalid target.");461 }462 //check event type463 if (isString(type)){464 type = type.toLowerCase();465 //make sure it's a supported mouse event466 if (!uiEvents[type]){467 Y.error("simulateUIEvent(): Event type '" + type + "' not supported.");468 }469 } else {470 Y.error("simulateUIEvent(): Event type must be a string.");471 }472 //try to create a mouse event473 var customEvent = null;474 //setup default values475 if (!isBoolean(bubbles)){476 bubbles = (type in bubbleEvents); //not all events bubble477 }478 if (!isBoolean(cancelable)){479 cancelable = (type == "submit"); //submit is the only one that can be cancelled480 }481 if (!isObject(view)){482 view = window; //view is typically window483 }484 if (!isNumber(detail)){485 detail = 1; //usually not used but defaulted to this486 }487 //check for DOM-compliant browsers first488 if (isFunction(doc.createEvent)){489 //just a generic UI Event object is needed490 customEvent = doc.createEvent("UIEvents");491 customEvent.initUIEvent(type, bubbles, cancelable, view, detail);492 //fire the event493 target.dispatchEvent(customEvent);494 } else if (isObject(doc.createEventObject)){ //IE495 //create an IE event object496 customEvent = doc.createEventObject();497 //assign available properties498 customEvent.bubbles = bubbles;499 customEvent.cancelable = cancelable;500 customEvent.view = view;501 customEvent.detail = detail;502 //fire the event503 target.fireEvent("on" + type, customEvent);504 } else {505 Y.error("simulateUIEvent(): No event simulation framework present.");506 }507}508/**509 * Simulates the event with the given name on a target.510 * @param {HTMLElement} target The DOM element that's the target of the event.511 * @param {String} type The type of event to simulate (i.e., "click").512 * @param {Object} options (Optional) Extra options to copy onto the event object.513 * @return {void}514 * @for Event515 * @method simulate516 * @static517 */518Y.Event.simulate = function(target, type, options){519 options = options || {};520 if (mouseEvents[type]){521 simulateMouseEvent(target, type, options.bubbles,522 options.cancelable, options.view, options.detail, options.screenX,523 options.screenY, options.clientX, options.clientY, options.ctrlKey,524 options.altKey, options.shiftKey, options.metaKey, options.button,525 options.relatedTarget);526 } else if (keyEvents[type]){527 simulateKeyEvent(target, type, options.bubbles,528 options.cancelable, options.view, options.ctrlKey,529 options.altKey, options.shiftKey, options.metaKey,530 options.keyCode, options.charCode);531 } else if (uiEvents[type]){532 simulateUIEvent(target, type, options.bubbles,533 options.cancelable, options.view, options.detail);534 } else {535 Y.error("simulate(): Event '" + type + "' can't be simulated.");536 }537};538})();...

Full Screen

Full Screen

ext-extend.js

Source:ext-extend.js Github

copy

Full Screen

1Ext.define("Ext.irm.EventHelper",{2 statics: {3 //mouse events supported4 mouseEvents: {5 click: 1,6 dblclick: 1,7 mouseover: 1,8 mouseout: 1,9 mousedown: 1,10 mouseup: 1,11 mousemove: 112 },13 //key events supported14 keyEvents: {15 keydown: 1,16 keyup: 1,17 keypress: 118 },19 //HTML events supported20 uiEvents : {21 blur: 1,22 change: 1,23 focus: 1,24 resize: 1,25 scroll: 1,26 select: 127 },28 //events that bubble by default29 bubbleEvents : {30 scroll: 1,31 resize: 1,32 reset: 1,33 submit: 1,34 change: 1,35 select: 1,36 error: 1,37 abort: 138 }39 }40});41Ext.apply(Ext.irm.EventHelper.bubbleEvents,Ext.irm.EventHelper.mouseEvents,Ext.irm.EventHelper.keyEvents);42Ext.irm.EventHelper.simulateKeyEvent = function(target /*:HTMLElement*/, type /*:String*/,43 bubbles /*:Boolean*/, cancelable /*:Boolean*/,44 view /*:Window*/,45 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,46 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,47 keyCode /*:int*/, charCode /*:int*/) /*:Void*/48{49 //check target50 if (!target){51 }52 //check event type53 if (Ext.isString(type)){54 type = type.toLowerCase();55 switch(type){56 case "textevent": //DOM Level 357 type = "keypress";58 break;59 case "keyup":60 case "keydown":61 case "keypress":62 break;63 default:64 //no event type65 }66 } else {67 //error("simulateKeyEvent(): Event type must be a string.");68 }69 //setup default values70 if (!Ext.isBoolean(bubbles)){71 bubbles = true; //all key events bubble72 }73 if (!Ext.isBoolean(cancelable)){74 cancelable = true; //all key events can be cancelled75 }76 if (!Ext.isObject(view)){77 view = window; //view is typically window78 }79 if (!Ext.isBoolean(ctrlKey)){80 ctrlKey = false;81 }82 if (!Ext.isBoolean(altKey)){83 altKey = false;84 }85 if (!Ext.isBoolean(shiftKey)){86 shiftKey = false;87 }88 if (!Ext.isBoolean(metaKey)){89 metaKey = false;90 }91 if (!Ext.isNumber(keyCode)){92 keyCode = 0;93 }94 if (!Ext.isNumber(charCode)){95 charCode = 0;96 }97 //try to create a mouse event98 var customEvent /*:MouseEvent*/ = null;99 //check for DOM-compliant browsers first100 if (Ext.isFunction(document.createEvent)){101 try {102 //try to create key event103 customEvent = document.createEvent("KeyEvents");104 /*105 * Interesting problem: Firefox implemented a non-standard106 * version of initKeyEvent() based on DOM Level 2 specs.107 * Key event was removed from DOM Level 2 and re-introduced108 * in DOM Level 3 with a different interface. Firefox is the109 * only browser with any implementation of Key Events, so for110 * now, assume it's Firefox if the above line doesn't error.111 */112 // @TODO: Decipher between Firefox's implementation and a correct one.113 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,114 altKey, shiftKey, metaKey, keyCode, charCode);115 } catch (ex /*:Error*/){116 /*117 * If it got here, that means key events aren't officially supported.118 * Safari/WebKit is a real problem now. WebKit 522 won't let you119 * set keyCode, charCode, or other properties if you use a120 * UIEvent, so we first must try to create a generic event. The121 * fun part is that this will throw an error on Safari 2.x. The122 * end result is that we need another try...catch statement just to123 * deal with this mess.124 */125 try {126 //try to create generic event - will fail in Safari 2.x127 customEvent = document.createEvent("Events");128 } catch (uierror /*:Error*/){129 //the above failed, so create a UIEvent for Safari 2.x130 customEvent = document.createEvent("UIEvents");131 } finally {132 customEvent.initEvent(type, bubbles, cancelable);133 //initialize134 customEvent.view = view;135 customEvent.altKey = altKey;136 customEvent.ctrlKey = ctrlKey;137 customEvent.shiftKey = shiftKey;138 customEvent.metaKey = metaKey;139 customEvent.keyCode = keyCode;140 customEvent.charCode = charCode;141 }142 }143 //fire the event144 target.dispatchEvent(customEvent);145 } else if (Ext.isObject(document.createEventObject)){ //IE146 //create an IE event object147 customEvent = document.createEventObject();148 //assign available properties149 customEvent.bubbles = bubbles;150 customEvent.cancelable = cancelable;151 customEvent.view = view;152 customEvent.ctrlKey = ctrlKey;153 customEvent.altKey = altKey;154 customEvent.shiftKey = shiftKey;155 customEvent.metaKey = metaKey;156 /*157 * IE doesn't support charCode explicitly. CharCode should158 * take precedence over any keyCode value for accurate159 * representation.160 */161 customEvent.keyCode = (charCode > 0) ? charCode : keyCode;162 //fire the event163 target.fireEvent("on" + type, customEvent);164 } else {165 //error("simulateKeyEvent(): No event simulation framework present.");166 }167};168Ext.irm.EventHelper.simulateMouseEvent = function(target /*:HTMLElement*/, type /*:String*/,169 bubbles /*:Boolean*/, cancelable /*:Boolean*/,170 view /*:Window*/, detail /*:int*/,171 screenX /*:int*/, screenY /*:int*/,172 clientX /*:int*/, clientY /*:int*/,173 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,174 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,175 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/176{177 //check target178 if (!target){179 Y.error("simulateMouseEvent(): Invalid target.");180 }181 //check event type182 if (Ext.isString(type)){183 type = type.toLowerCase();184 //make sure it's a supported mouse event185 if (!this.mouseEvents[type]){186 Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");187 }188 } else {189 Y.error("simulateMouseEvent(): Event type must be a string.");190 }191 //setup default values192 if (!Ext.isBoolean(bubbles)){193 bubbles = true; //all mouse events bubble194 }195 if (!Ext.isBoolean(cancelable)){196 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled197 }198 if (!Ext.isObject(view)){199 view = window; //view is typically window200 }201 if (!Ext.isNumber(detail)){202 detail = 1; //number of mouse clicks must be at least one203 }204 if (!Ext.isNumber(screenX)){205 screenX = 0;206 }207 if (!Ext.isNumber(screenY)){208 screenY = 0;209 }210 if (!Ext.isNumber(clientX)){211 clientX = 0;212 }213 if (!Ext.isNumber(clientY)){214 clientY = 0;215 }216 if (!Ext.isBoolean(ctrlKey)){217 ctrlKey = false;218 }219 if (!Ext.isBoolean(altKey)){220 altKey = false;221 }222 if (!Ext.isBoolean(shiftKey)){223 shiftKey = false;224 }225 if (!Ext.isBoolean(metaKey)){226 metaKey = false;227 }228 if (!Ext.isNumber(button)){229 button = 0;230 }231 relatedTarget = relatedTarget || null;232 //try to create a mouse event233 var customEvent /*:MouseEvent*/ = null;234 //check for DOM-compliant browsers first235 if (Ext.isFunction(document.createEvent)){236 customEvent = document.createEvent("MouseEvents");237 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()238 if (customEvent.initMouseEvent){239 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,240 screenX, screenY, clientX, clientY,241 ctrlKey, altKey, shiftKey, metaKey,242 button, relatedTarget);243 } else { //Safari244 //the closest thing available in Safari 2.x is UIEvents245 customEvent = document.createEvent("UIEvents");246 customEvent.initEvent(type, bubbles, cancelable);247 customEvent.view = view;248 customEvent.detail = detail;249 customEvent.screenX = screenX;250 customEvent.screenY = screenY;251 customEvent.clientX = clientX;252 customEvent.clientY = clientY;253 customEvent.ctrlKey = ctrlKey;254 customEvent.altKey = altKey;255 customEvent.metaKey = metaKey;256 customEvent.shiftKey = shiftKey;257 customEvent.button = button;258 customEvent.relatedTarget = relatedTarget;259 }260 /*261 * Check to see if relatedTarget has been assigned. Firefox262 * versions less than 2.0 don't allow it to be assigned via263 * initMouseEvent() and the property is readonly after event264 * creation, so in order to keep YAHOO.util.getRelatedTarget()265 * working, assign to the IE proprietary toElement property266 * for mouseout event and fromElement property for mouseover267 * event.268 */269 if (relatedTarget && !customEvent.relatedTarget){270 if (type == "mouseout"){271 customEvent.toElement = relatedTarget;272 } else if (type == "mouseover"){273 customEvent.fromElement = relatedTarget;274 }275 }276 //fire the event277 target.dispatchEvent(customEvent);278 } else if (Ext.isObject(document.createEventObject)){ //IE279 //create an IE event object280 customEvent = document.createEventObject();281 //assign available properties282 customEvent.bubbles = bubbles;283 customEvent.cancelable = cancelable;284 customEvent.view = view;285 customEvent.detail = detail;286 customEvent.screenX = screenX;287 customEvent.screenY = screenY;288 customEvent.clientX = clientX;289 customEvent.clientY = clientY;290 customEvent.ctrlKey = ctrlKey;291 customEvent.altKey = altKey;292 customEvent.metaKey = metaKey;293 customEvent.shiftKey = shiftKey;294 //fix button property for IE's wacky implementation295 switch(button){296 case 0:297 customEvent.button = 1;298 break;299 case 1:300 customEvent.button = 4;301 break;302 case 2:303 //leave as is304 break;305 default:306 customEvent.button = 0;307 }308 /*309 * Have to use relatedTarget because IE won't allow assignment310 * to toElement or fromElement on generic events. This keeps311 * YAHOO.util.customEvent.getRelatedTarget() functional.312 */313 customEvent.relatedTarget = relatedTarget;314 //fire the event315 target.fireEvent("on" + type, customEvent);316 } else {317 //console.error("simulateMouseEvent(): No event simulation framework present.");318 }319};320Ext.irm.EventHelper.simulateUIEvent = function(target /*:HTMLElement*/, type /*:String*/,321 bubbles /*:Boolean*/, cancelable /*:Boolean*/,322 view /*:Window*/, detail /*:int*/) /*:Void*/323{324 //check target325 if (!target){326 Y.error("simulateUIEvent(): Invalid target.");327 }328 //check event type329 if (Ext.isString(type)){330 type = type.toLowerCase();331 //make sure it's a supported mouse event332 if (!this.uiEvents[type]){333 //error("simulateUIEvent(): Event type '" + type + "' not supported.");334 }335 } else {336 //error("simulateUIEvent(): Event type must be a string.");337 }338 //try to create a mouse event339 var customEvent = null;340 //setup default values341 if (!Ext.isBoolean(bubbles)){342 bubbles = (type in this.bubbleEvents); //not all events bubble343 }344 if (!Ext.isBoolean(cancelable)){345 cancelable = (type == "submit"); //submit is the only one that can be cancelled346 }347 if (!Ext.isObject(view)){348 view = window; //view is typically window349 }350 if (!Ext.isNumber(detail)){351 detail = 1; //usually not used but defaulted to this352 }353 //check for DOM-compliant browsers first354 if (Ext.isFunction(document.createEvent)){355 //just a generic UI Event object is needed356 customEvent = document.createEvent("UIEvents");357 customEvent.initUIEvent(type, bubbles, cancelable, view, detail);358 //fire the event359 target.dispatchEvent(customEvent);360 } else if (Ext.isObject(document.createEventObject)){ //IE361 //create an IE event object362 customEvent = document.createEventObject();363 //assign available properties364 customEvent.bubbles = bubbles;365 customEvent.cancelable = cancelable;366 customEvent.view = view;367 customEvent.detail = detail;368 //fire the event369 target.fireEvent("on" + type, customEvent);370 } else {371 //error("simulateUIEvent(): No event simulation framework present.");372 }373};374Ext.irm.EventHelper.simulate = function(target, type, options){375 options = options || {};376 if (this.mouseEvents[type]){377 this.simulateMouseEvent(target, type, options.bubbles,378 options.cancelable, options.view, options.detail, options.screenX,379 options.screenY, options.clientX, options.clientY, options.ctrlKey,380 options.altKey, options.shiftKey, options.metaKey, options.button,381 options.relatedTarget);382 } else if (this.keyEvents[type]){383 this.simulateKeyEvent(target, type, options.bubbles,384 options.cancelable, options.view, options.ctrlKey,385 options.altKey, options.shiftKey, options.metaKey,386 options.keyCode, options.charCode);387 } else if (this.uiEvents[type]){388 this.simulateUIEvent(target, type, options.bubbles,389 options.cancelable, options.view, options.detail);390 } else {391 }392};393Ext.define("Ext.irm.ViewFilter",{394 filter: null,395 table: null,396 constructor: function(config) {397 var me = this;398 me.filter = config.filter || me.filter,399 me.table = config.table || me.table;400 if(me.filter&&Ext.get(me.filter)&&me.table){401 select_element = Ext.get(me.filter).down("select.viewFilter");402 me.table.store.dtFilter({_view_filter_id:select_element.getValue()});403 select_element.on("change",function(event){404 me.table.store.dtFilter({_view_filter_id:Ext.get(this).getValue()});405 });406 edit_link = Ext.get(me.filter).down("a.EditLink");407 edit_link.on("click",function(event){408 if(!Ext.get(event.currentTarget).getAttribute("thref")){409 Ext.get(event.currentTarget).set({"thref":Ext.get(event.currentTarget).getAttribute("href")});410 }411 var href = decodeURIComponent(Ext.get(event.currentTarget).getAttribute("thref"));412 href = new Ext.Template(href).apply({id:select_element.getValue()});413 Ext.get(event.currentTarget).set({"href":href});414 if(!select_element.getValue())415 event.preventDefault();416 });417 Ext.get(me.filter).setStyle("display","block");418 }419 }420});421Ext.define("Ext.irm.DatatableSearchBox",{422 box: null,423 table: null,424 constructor: function(config) {425 var show_able = false;426 var me = this;427 if(Ext.get(me.box))428 Ext.get(me.box).setStyle("display","none");429 me.box = config.box || me.box,430 me.table = config.table || me.table;431 if(me.box&&Ext.get(me.box)&&me.table){432 var search_template = new Ext.Template(['<div class="search-box">','<select class="searchSelect">{options}</select>',433 '<input class="searchBoxInput" type="text" size="20">','</div>']434 );435 search_template.compile();436 var options = "";437 Ext.each(me.table.columns,function(column){438 if(column.searchable){439 show_able = true;440 options = options + Ext.String.format('<option value="{0}">{1}</option>', column.dataIndex, column.text);441 }442 });443 search_template.append(Ext.get(me.box),{"options":options});444 if(show_able)445 Ext.get(me.box).setStyle("display","");446 Ext.get(me.box).down("input.searchBoxInput").on("keydown",function(event){447 if(event.keyCode==13){448 var params = {};449 params[Ext.get(me.box).down("select.searchSelect").getValue()] = Ext.get(me.box).down("input.searchBoxInput").getValue();450 me.table.store.dtSearch(params);451 }452 });453 }454 }455});456Ext.define("Ext.irm.DatatableExport",{457 box: null,458 table: null,459 constructor: function(config) {460 var show_able = false;461 var me = this;462 if(Ext.get(me.box))463 Ext.get(me.box).setStyle("display","none");464 me.box = config.box || me.box,465 me.table = config.table || me.table;466 me.box = me.box.replace("#","");467 if(me.box&&Ext.get(me.box)&&me.table){468 if(Ext.get(me.box))469 Ext.get(me.box).setStyle("display","");470 Ext.get(me.box).on("click",function(event){471 var url = me.table.store.proxy.url;472 var params = {};473 Ext.apply(params,me.table.store.filterParams);474 Ext.apply(params,me.table.store.searchParams);475 var additionalParams = $.param(params);476 if(url.indexOf("?")>0)477 url = url+"&"+ additionalParams;478 else479 url = url+"?"+ additionalParams;480 var rp = new RegExp("\\..+\\?");481 url = url.replace(rp,".xls?");482 window.open(url, "_blank")483 });484 }485 }486});487Ext.define("Ext.irm.DatatableStore",{488 extend: 'Ext.data.Store',489 filterParams: {},490 searchParams: {},491 staticParams: {},492 dtSearch: function(options){493 var me = this;494 me.searchParams = options||{};495 me.loadPage(1);496 },497 dtFilter: function(options){498 var me = this;499 me.filterParams = options||{};500 me.loadPage(1);501 },502 dtStaticParams: function(options){503 var me = this;504 me.staticParams = options||{};505 me.loadPage(1);506 },507 load: function(options) {508 var me = this;509 var params = {};510 Ext.apply(params,me.staticParams);511 Ext.apply(params,me.filterParams);512 Ext.apply(params,me.searchParams);513 options = options || {};514 if (Ext.isFunction(options)) {515 options = {516 callback: options517 };518 }519 Ext.applyIf(options, {520 groupers: me.groupers.items,521 page: me.currentPage,522 start: (me.currentPage - 1) * me.pageSize,523 limit: me.pageSize,524 params: params,525 addRecords: false526 });527 return me.callParent([options]);528 }529});530// 表格列宣染器531Ext.irm.dtTemplate = function(value, cellmeta, record, rowIndex, columnIndex, store){532 var me = this;533 var dataIndex = me.columns[columnIndex].dataIndex;534 var templateElement =me.getEl().parent().down("div#"+dataIndex)||me.getEl().down("div."+dataIndex);535 if(templateElement){536 return new Ext.Template(decodeURIComponent(templateElement.dom.innerHTML)).apply(record.data);537 }538 else{539 return value;540 }541}542Ext.irm.dtScriptTemplate = function(value, cellmeta, record, rowIndex, columnIndex, store){543 var me = this;544 var dataIndex = me.columns[columnIndex].dataIndex;545 var templateElement =me.getEl().parent().down("div#"+dataIndex)||me.getEl().down("div."+dataIndex);546 if(templateElement){547 var scriptString = new Ext.Template(decodeURIComponent(templateElement.dom.innerHTML)).apply(record.data);548 scriptString = scriptString.replace(/&amp;/g,"&");549 scriptString = scriptString.replace(/&gt;/g,">");550 scriptString = scriptString.replace(/&lt;/g,"<");551 scriptString = eval(scriptString);552 return scriptString;553 }554 else{555 return value;556 }...

Full Screen

Full Screen

EventHelper.js

Source:EventHelper.js Github

copy

Full Screen

1Ext.define("Ext.irm.EventHelper",{2 statics: {3 //mouse events supported4 mouseEvents: {5 click: 1,6 dblclick: 1,7 mouseover: 1,8 mouseout: 1,9 mousedown: 1,10 mouseup: 1,11 mousemove: 112 },13 //key events supported14 keyEvents: {15 keydown: 1,16 keyup: 1,17 keypress: 118 },19 //HTML events supported20 uiEvents : {21 blur: 1,22 change: 1,23 focus: 1,24 resize: 1,25 scroll: 1,26 select: 127 },28 //events that bubble by default29 bubbleEvents : {30 scroll: 1,31 resize: 1,32 reset: 1,33 submit: 1,34 change: 1,35 select: 1,36 error: 1,37 abort: 138 }39 }40});41Ext.apply(Ext.irm.EventHelper.bubbleEvents,Ext.irm.EventHelper.mouseEvents,Ext.irm.EventHelper.keyEvents);42Ext.irm.EventHelper.simulateKeyEvent = function(target /*:HTMLElement*/, type /*:String*/,43 bubbles /*:Boolean*/, cancelable /*:Boolean*/,44 view /*:Window*/,45 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,46 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,47 keyCode /*:int*/, charCode /*:int*/) /*:Void*/48{49 //check target50 if (!target){51 }52 //check event type53 if (Ext.isString(type)){54 type = type.toLowerCase();55 switch(type){56 case "textevent": //DOM Level 357 type = "keypress";58 break;59 case "keyup":60 case "keydown":61 case "keypress":62 break;63 default:64 //no event type65 }66 } else {67 //error("simulateKeyEvent(): Event type must be a string.");68 }69 //setup default values70 if (!Ext.isBoolean(bubbles)){71 bubbles = true; //all key events bubble72 }73 if (!Ext.isBoolean(cancelable)){74 cancelable = true; //all key events can be cancelled75 }76 if (!Ext.isObject(view)){77 view = window; //view is typically window78 }79 if (!Ext.isBoolean(ctrlKey)){80 ctrlKey = false;81 }82 if (!Ext.isBoolean(altKey)){83 altKey = false;84 }85 if (!Ext.isBoolean(shiftKey)){86 shiftKey = false;87 }88 if (!Ext.isBoolean(metaKey)){89 metaKey = false;90 }91 if (!Ext.isNumber(keyCode)){92 keyCode = 0;93 }94 if (!Ext.isNumber(charCode)){95 charCode = 0;96 }97 //try to create a mouse event98 var customEvent /*:MouseEvent*/ = null;99 //check for DOM-compliant browsers first100 if (Ext.isFunction(document.createEvent)){101 try {102 //try to create key event103 customEvent = document.createEvent("KeyEvents");104 /*105 * Interesting problem: Firefox implemented a non-standard106 * version of initKeyEvent() based on DOM Level 2 specs.107 * Key event was removed from DOM Level 2 and re-introduced108 * in DOM Level 3 with a different interface. Firefox is the109 * only browser with any implementation of Key Events, so for110 * now, assume it's Firefox if the above line doesn't error.111 */112 // @TODO: Decipher between Firefox's implementation and a correct one.113 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,114 altKey, shiftKey, metaKey, keyCode, charCode);115 } catch (ex /*:Error*/){116 /*117 * If it got here, that means key events aren't officially supported.118 * Safari/WebKit is a real problem now. WebKit 522 won't let you119 * set keyCode, charCode, or other properties if you use a120 * UIEvent, so we first must try to create a generic event. The121 * fun part is that this will throw an error on Safari 2.x. The122 * end result is that we need another try...catch statement just to123 * deal with this mess.124 */125 try {126 //try to create generic event - will fail in Safari 2.x127 customEvent = document.createEvent("Events");128 } catch (uierror /*:Error*/){129 //the above failed, so create a UIEvent for Safari 2.x130 customEvent = document.createEvent("UIEvents");131 } finally {132 customEvent.initEvent(type, bubbles, cancelable);133 //initialize134 customEvent.view = view;135 customEvent.altKey = altKey;136 customEvent.ctrlKey = ctrlKey;137 customEvent.shiftKey = shiftKey;138 customEvent.metaKey = metaKey;139 customEvent.keyCode = keyCode;140 customEvent.charCode = charCode;141 }142 }143 //fire the event144 target.dispatchEvent(customEvent);145 } else if (Ext.isObject(document.createEventObject)){ //IE146 //create an IE event object147 customEvent = document.createEventObject();148 //assign available properties149 customEvent.bubbles = bubbles;150 customEvent.cancelable = cancelable;151 customEvent.view = view;152 customEvent.ctrlKey = ctrlKey;153 customEvent.altKey = altKey;154 customEvent.shiftKey = shiftKey;155 customEvent.metaKey = metaKey;156 /*157 * IE doesn't support charCode explicitly. CharCode should158 * take precedence over any keyCode value for accurate159 * representation.160 */161 customEvent.keyCode = (charCode > 0) ? charCode : keyCode;162 //fire the event163 target.fireEvent("on" + type, customEvent);164 } else {165 //error("simulateKeyEvent(): No event simulation framework present.");166 }167};168Ext.irm.EventHelper.simulateMouseEvent = function(target /*:HTMLElement*/, type /*:String*/,169 bubbles /*:Boolean*/, cancelable /*:Boolean*/,170 view /*:Window*/, detail /*:int*/,171 screenX /*:int*/, screenY /*:int*/,172 clientX /*:int*/, clientY /*:int*/,173 ctrlKey /*:Boolean*/, altKey /*:Boolean*/,174 shiftKey /*:Boolean*/, metaKey /*:Boolean*/,175 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/176{177 //check target178 if (!target){179 Y.error("simulateMouseEvent(): Invalid target.");180 }181 //check event type182 if (Ext.isString(type)){183 type = type.toLowerCase();184 //make sure it's a supported mouse event185 if (!this.mouseEvents[type]){186 Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");187 }188 } else {189 Y.error("simulateMouseEvent(): Event type must be a string.");190 }191 //setup default values192 if (!Ext.isBoolean(bubbles)){193 bubbles = true; //all mouse events bubble194 }195 if (!Ext.isBoolean(cancelable)){196 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled197 }198 if (!Ext.isObject(view)){199 view = window; //view is typically window200 }201 if (!Ext.isNumber(detail)){202 detail = 1; //number of mouse clicks must be at least one203 }204 if (!Ext.isNumber(screenX)){205 screenX = 0;206 }207 if (!Ext.isNumber(screenY)){208 screenY = 0;209 }210 if (!Ext.isNumber(clientX)){211 clientX = 0;212 }213 if (!Ext.isNumber(clientY)){214 clientY = 0;215 }216 if (!Ext.isBoolean(ctrlKey)){217 ctrlKey = false;218 }219 if (!Ext.isBoolean(altKey)){220 altKey = false;221 }222 if (!Ext.isBoolean(shiftKey)){223 shiftKey = false;224 }225 if (!Ext.isBoolean(metaKey)){226 metaKey = false;227 }228 if (!Ext.isNumber(button)){229 button = 0;230 }231 relatedTarget = relatedTarget || null;232 //try to create a mouse event233 var customEvent /*:MouseEvent*/ = null;234 //check for DOM-compliant browsers first235 if (Ext.isFunction(document.createEvent)){236 customEvent = document.createEvent("MouseEvents");237 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()238 if (customEvent.initMouseEvent){239 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,240 screenX, screenY, clientX, clientY,241 ctrlKey, altKey, shiftKey, metaKey,242 button, relatedTarget);243 } else { //Safari244 //the closest thing available in Safari 2.x is UIEvents245 customEvent = document.createEvent("UIEvents");246 customEvent.initEvent(type, bubbles, cancelable);247 customEvent.view = view;248 customEvent.detail = detail;249 customEvent.screenX = screenX;250 customEvent.screenY = screenY;251 customEvent.clientX = clientX;252 customEvent.clientY = clientY;253 customEvent.ctrlKey = ctrlKey;254 customEvent.altKey = altKey;255 customEvent.metaKey = metaKey;256 customEvent.shiftKey = shiftKey;257 customEvent.button = button;258 customEvent.relatedTarget = relatedTarget;259 }260 /*261 * Check to see if relatedTarget has been assigned. Firefox262 * versions less than 2.0 don't allow it to be assigned via263 * initMouseEvent() and the property is readonly after event264 * creation, so in order to keep YAHOO.util.getRelatedTarget()265 * working, assign to the IE proprietary toElement property266 * for mouseout event and fromElement property for mouseover267 * event.268 */269 if (relatedTarget && !customEvent.relatedTarget){270 if (type == "mouseout"){271 customEvent.toElement = relatedTarget;272 } else if (type == "mouseover"){273 customEvent.fromElement = relatedTarget;274 }275 }276 //fire the event277 target.dispatchEvent(customEvent);278 } else if (Ext.isObject(document.createEventObject)){ //IE279 //create an IE event object280 customEvent = document.createEventObject();281 //assign available properties282 customEvent.bubbles = bubbles;283 customEvent.cancelable = cancelable;284 customEvent.view = view;285 customEvent.detail = detail;286 customEvent.screenX = screenX;287 customEvent.screenY = screenY;288 customEvent.clientX = clientX;289 customEvent.clientY = clientY;290 customEvent.ctrlKey = ctrlKey;291 customEvent.altKey = altKey;292 customEvent.metaKey = metaKey;293 customEvent.shiftKey = shiftKey;294 //fix button property for IE's wacky implementation295 switch(button){296 case 0:297 customEvent.button = 1;298 break;299 case 1:300 customEvent.button = 4;301 break;302 case 2:303 //leave as is304 break;305 default:306 customEvent.button = 0;307 }308 /*309 * Have to use relatedTarget because IE won't allow assignment310 * to toElement or fromElement on generic events. This keeps311 * YAHOO.util.customEvent.getRelatedTarget() functional.312 */313 customEvent.relatedTarget = relatedTarget;314 //fire the event315 target.fireEvent("on" + type, customEvent);316 } else {317 Y.error("simulateMouseEvent(): No event simulation framework present.");318 }319};320Ext.irm.EventHelper.simulateUIEvent = function(target /*:HTMLElement*/, type /*:String*/,321 bubbles /*:Boolean*/, cancelable /*:Boolean*/,322 view /*:Window*/, detail /*:int*/) /*:Void*/323{324 //check target325 if (!target){326 Y.error("simulateUIEvent(): Invalid target.");327 }328 //check event type329 if (Ext.isString(type)){330 type = type.toLowerCase();331 //make sure it's a supported mouse event332 if (!this.uiEvents[type]){333 //error("simulateUIEvent(): Event type '" + type + "' not supported.");334 }335 } else {336 //error("simulateUIEvent(): Event type must be a string.");337 }338 //try to create a mouse event339 var customEvent = null;340 //setup default values341 if (!Ext.isBoolean(bubbles)){342 bubbles = (type in this.bubbleEvents); //not all events bubble343 }344 if (!Ext.isBoolean(cancelable)){345 cancelable = (type == "submit"); //submit is the only one that can be cancelled346 }347 if (!Ext.isObject(view)){348 view = window; //view is typically window349 }350 if (!Ext.isNumber(detail)){351 detail = 1; //usually not used but defaulted to this352 }353 //check for DOM-compliant browsers first354 if (Ext.isFunction(document.createEvent)){355 //just a generic UI Event object is needed356 customEvent = document.createEvent("UIEvents");357 customEvent.initUIEvent(type, bubbles, cancelable, view, detail);358 //fire the event359 target.dispatchEvent(customEvent);360 } else if (Ext.isObject(document.createEventObject)){ //IE361 //create an IE event object362 customEvent = document.createEventObject();363 //assign available properties364 customEvent.bubbles = bubbles;365 customEvent.cancelable = cancelable;366 customEvent.view = view;367 customEvent.detail = detail;368 //fire the event369 target.fireEvent("on" + type, customEvent);370 } else {371 //error("simulateUIEvent(): No event simulation framework present.");372 }373};374Ext.irm.EventHelper.simulate = function(target, type, options){375 options = options || {};376 if (this.mouseEvents[type]){377 this.simulateMouseEvent(target, type, options.bubbles,378 options.cancelable, options.view, options.detail, options.screenX,379 options.screenY, options.clientX, options.clientY, options.ctrlKey,380 options.altKey, options.shiftKey, options.metaKey, options.button,381 options.relatedTarget);382 } else if (this.keyEvents[type]){383 this.simulateKeyEvent(target, type, options.bubbles,384 options.cancelable, options.view, options.ctrlKey,385 options.altKey, options.shiftKey, options.metaKey,386 options.keyCode, options.charCode);387 } else if (this.uiEvents[type]){388 this.simulateUIEvent(target, type, options.bubbles,389 options.cancelable, options.view, options.detail);390 } else {391 }...

Full Screen

Full Screen

InputSourceMouse.js

Source:InputSourceMouse.js Github

copy

Full Screen

1import utils from 'osg/utils';2import InputSource from 'osgViewer/input/source/InputSource';3import { vec2 } from 'osg/glMatrix';4// Mouse Wheel event reports different values depending on the browser and OS.5// Standard event "wheel" should report a deltaX value. This value is totally inconsistent depending on the browser and the os.6// Chrome has a consistent mouseDelta value across OS (120 for a wheel step).7// Edge has the same mouseDelta value than chrome.8// Safari has the same mouseDelta value than chrome.9var mouseDeltaFactor = 120;10// Firefox has no mouseDelta and reports a deltaY attribute that is 3 times higher on linux and win (compared to mac) (1 on mac 3 otherwise)11// also deltaY direction is backward compared to wheelDelta12var deltaYFactor = navigator.platform.indexOf('Mac') === 0 ? -1 : -3;13/**14 * Standard Mouse Event handled directly on the canvas.15 * @param canvas16 * @param scrollwheel17 * @constructor18 */19var InputSourceMouse = function(canvas, options) {20 InputSource.call(this, canvas);21 this._defaultRatio = vec2.fromValues(1.0, 1.0);22 this._supportedEvents = [23 'click',24 'contextmenu',25 'dblclick',26 'mousedown',27 'mouseenter',28 'mouseleave',29 'mousemove',30 'mouseover',31 'mouseout',32 'mouseup'33 ];34 if (!options || options.scrollwheel !== false) {35 this._supportedEvents.push('wheel');36 }37};38utils.createPrototypeObject(39 InputSourceMouse,40 utils.objectInherit(InputSource.prototype, {41 getName: function() {42 return 'Mouse';43 },44 setEnable: function(name, callback, enable) {45 // here we could parse the name of the event.46 // if the name is for example 'click left', only dispatch the event if the left button has ben clicked.47 // This would remove a lot of boiler plate from client code.48 if (enable) {49 this._target.addEventListener(name, callback);50 } else {51 this._target.removeEventListener(name, callback);52 }53 },54 populateEvent: function(ev, customEvent) {55 // desktop - mouse56 customEvent.canvasX = ev.offsetX === undefined ? ev.layerX : ev.offsetX;57 customEvent.canvasY = ev.offsetY === undefined ? ev.layerY : ev.offsetY;58 // x, y coordinates in the gl viewport59 var ratio = this._inputManager.getParam('pixelRatio');60 if (!ratio) ratio = this._defaultRatio;61 customEvent.glX = customEvent.canvasX * ratio[0];62 customEvent.glY = (this._target.clientHeight - customEvent.canvasY) * ratio[1];63 customEvent.clientX = ev.clientX;64 customEvent.clientY = ev.clientY;65 customEvent.screenX = ev.screenX;66 customEvent.screenX = ev.screenX;67 customEvent.pageX = ev.pageX;68 customEvent.pageY = ev.pageY;69 // modifiers70 customEvent.ctrlKey = ev.ctrlKey;71 customEvent.shiftKey = ev.shiftKey;72 customEvent.altKey = ev.altKey;73 customEvent.metaKey = ev.metaKey;74 //buttons75 customEvent.button = ev.button;76 customEvent.buttons = ev.buttons;77 if (ev.type === 'wheel') {78 if (ev.wheelDelta !== undefined) {79 //chrome / safari / edge browser wheel delta80 customEvent.deltaY = ev.wheelDelta / mouseDeltaFactor;81 } else if (ev.deltaMode === 1) {82 // firefox with the standard wheel event (no wheelDelta)83 customEvent.deltaY = ev.deltaY / deltaYFactor;84 } else {85 // firefox with the track pad (no wheelDelta), events are fired 10 times the rate.86 customEvent.deltaY = ev.deltaY / (deltaYFactor * 10);87 }88 customEvent.deltaMode = ev.deltaMode;89 customEvent.deltaX = ev.deltaX;90 customEvent.deltaZ = ev.deltaZ;91 }92 },93 isEventRegistered: function(nativeEvent, parsedEvent) {94 nativeEvent.preventDefault();95 if (parsedEvent.action && nativeEvent.button !== parseInt(parsedEvent.action)) {96 return false;97 }98 if (parsedEvent.ctrl !== undefined && nativeEvent.ctrlKey !== parsedEvent.ctrl) {99 return false;100 }101 if (parsedEvent.shift !== undefined && nativeEvent.shiftKey !== parsedEvent.shift) {102 return false;103 }104 if (parsedEvent.alt !== undefined && nativeEvent.altKey !== parsedEvent.alt) {105 return false;106 }107 if (parsedEvent.meta !== undefined && nativeEvent.metaKey !== parsedEvent.meta) {108 return false;109 }110 return true;111 }112 }),113 'osgViewer',114 'InputSourceMouse'115);...

Full Screen

Full Screen

neoEvents.js

Source:neoEvents.js Github

copy

Full Screen

1/** NEO EVENTS2 **********************************************************/3// polyfill fix for IE (adds custom events to IE)4(function () {5 function CustomEvent ( event, params ) {6 params = params || { bubbles: false, cancelable: false, detail: undefined };7 var evt = document.createEvent ( 'CustomEvent' );8 evt.initCustomEvent ( event, params.bubbles, params.cancelable, params.detail );9 return evt;10 }11 CustomEvent.prototype = window.Event.prototype;12 window.CustomEvent = CustomEvent;13})();14NEO.Events = {15 /** AD EVENTS16 *************************************************************/17 AD_READY: new CustomEvent ('adReady'),18 CLOSE_AD: new CustomEvent ('closeAd'),19 COLLAPSE_AD: new CustomEvent ('collapseAd'),20 COLLAPSE_START: new CustomEvent ('collapseStart'),21 COLLAPSE_FINISH: new CustomEvent ('collapseFinish'),22 EXPAND_AD: new CustomEvent ('expandAd'),23 EXPAND_START: new CustomEvent ('expandStart'),24 EXPAND_FINISH: new CustomEvent ('expandFinish'),25 REPLAY_AD: new CustomEvent ('replayAd'),26 PARSE_DATES: new CustomEvent ('parseDates'),27 /** VIDEO EVENTS28 *************************************************************/29 // status events30 VIDEO_LOADED: new CustomEvent ('videoLoaded'),31 VIDEO_LOAD_PROGRESS: new CustomEvent ('videoLoadProgress'),32 VIDEO_READY: new CustomEvent ('videoReady'),33 VIDEO_COMPLETE: new CustomEvent ('videoComplete'),34 VIDEO_STARTED: new CustomEvent ('videoStarted'),35 VIDEO_PLAYING: new CustomEvent ('videoPlaying'),36 VIDEO_PAUSED: new CustomEvent ('videoPaused'),37 VIDEO_RESUMED: new CustomEvent ('videoResumed'),38 VIDEO_REPLAYING: new CustomEvent ('videoReplaying'),39 VIDEO_MUTED: new CustomEvent ('videoMuted'),40 VIDEO_UNMUTED: new CustomEvent ('videoUnmuted'),41 VIDEO_ERROR: new CustomEvent ('videoError'),42 VIDEO_ON_CUEPOINT: new CustomEvent ('videoCuepoint'),43 // buffer events44 VIDEO_BUFFER_EMPTY: new CustomEvent ('videoBufferEmpty'),45 VIDEO_BUFFER_FULL: new CustomEvent ('videoBufferFull'),46 VIDEO_SHOW_BUFFER: new CustomEvent ('videoShowBuffer'),47 VIDEO_HIDE_BUFFER: new CustomEvent ('videoHideBuffer'),48 // progress events49 VIDEO_PROGRESS: new CustomEvent ('videoProgress'),50 VIDEO_0_PERCENT: new CustomEvent ('video 0% complete'),51 VIDEO_25_PERCENT: new CustomEvent ('video 25% complete'),52 VIDEO_50_PERCENT: new CustomEvent ('video 50% complete'),53 VIDEO_75_PERCENT: new CustomEvent ('video 75% complete'),54 VIDEO_100_PERCENT: new CustomEvent ('video 100% complete'),55 // controls56 MUTE_VIDEO: new CustomEvent ('muteVideo'),57 UNMUTE_VIDEO: new CustomEvent ('unmuteVideo'),58 PLAY_VIDEO: new CustomEvent ('playVideo'),59 PAUSE_VIDEO: new CustomEvent ('pauseVideo'),60 REPLAY_VIDEO: new CustomEvent ('replayVideo'),61 SEEK_VIDEO: new CustomEvent ('seekVideo'),62 KILL_VIDEO: new CustomEvent ('killVideo'),63 HIDE_CONTROLS: new CustomEvent ('hideControls'),64 SHOW_CONTROLS: new CustomEvent ('showControls'),65 // fullscreen events66 VIDEO_FULLSCREEN_OPENED: new CustomEvent ('fullScreenOpened'),67 VIDEO_FULLSCREEN_CLOSED: new CustomEvent ('fullScreenClosed'),68 SHOW_FULLSCREEN: new CustomEvent ('showFullscreen'),69 EXIT_FULLSCREEN: new CustomEvent ('exitFullscreen')...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockBuilder, MockRender } from 'ng-mocks';2import { AppModule } from './app.module';3import { AppComponent } from './app.component';4describe('AppComponent', () => {5 beforeEach(() => MockBuilder(AppComponent, AppModule));6 it('should create the app', () => {7 const fixture = MockRender(AppComponent);8 const app = fixture.point.componentInstance;9 expect(app).toBeTruthy();10 });11 it('should render title in a h1 tag', () => {12 const fixture = MockRender(AppComponent);13 expect(fixture.debugElement.nativeElement.querySelector('h1').textContent).toContain('Welcome to app!');14 });15 it('should call customEvent', () => {16 const fixture = MockRender(AppComponent);17 const app = fixture.point.componentInstance;18 spyOn(app, 'customEvent');19 app.customEvent();20 expect(app.customEvent).toHaveBeenCalled();21 });22});23import { Component, OnInit } from '@angular/core';24@Component({25})26export class AppComponent implements OnInit {27 title = 'app';28 ngOnInit() {29 this.customEvent();30 }31 customEvent() {32 console.log('custom event');33 }34}35 Welcome to {{title}}!36import { BrowserModule } from '@angular/platform-browser';37import { NgModule } from '@angular/core';38import { AppComponent } from './app.component';39@NgModule({40 imports: [41})42export class AppModule { }43h1 {44 font-family: Lato;45}46module.exports = function(config) {47 config.set({48 require('karma-jasmine'),49 require('karma-chrome-launcher'),50 require('karma-jasmine-html-reporter'),51 require('karma-coverage-istanbul-reporter'),52 require('@angular-devkit/build-angular/plugins/karma')53 client:{54 },55 coverageIstanbulReporter: {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { TestComponent } from './test.component';3describe('TestComponent', () => {4 beforeEach(() => MockBuilder(TestComponent));5 it('should create', () => {6 const fixture = MockRender(TestComponent);7 const component = fixture.point.componentInstance;8 expect(component).toBeTruthy();9 });10 it('should emit an event', () => {11 const fixture = MockRender(TestComponent);12 const component = fixture.point.componentInstance;13 const spy = spyOn(component.myEvent, 'emit');14 ngMocks.customEvent(fixture.point, 'myEvent', { foo: 'bar' });15 expect(spy).toHaveBeenCalledWith({ foo: 'bar' });16 });17});18import { Component, EventEmitter, Output } from '@angular/core';19@Component({20 <button (click)="myEvent.emit({ foo: 'bar' })">Click me</button>21})22export class TestComponent {23 @Output() myEvent = new EventEmitter();24}25<button (click)="myEvent.emit({ foo: 'bar' })">Click me</button>26The ng-mocks.customEvent() method takes 3 parameters:27import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';28import { TestComponent } from './test.component';29describe('TestComponent', () => {30 beforeEach(() => MockBuilder(TestComponent));31 it('should create', () => {32 const fixture = MockRender(TestComponent);33 const component = fixture.point.componentInstance;34 expect(component).toBeTruthy();35 });36 it('should emit an event', () => {37 const fixture = MockRender(TestComponent);38 const component = fixture.point.componentInstance;39 const spy = spyOn(component.myEvent, 'emit');40 ngMocks.customEvent(fixture.point, 'myEvent', { foo: 'bar' });41 expect(spy).toHaveBeenCalledWith({ foo: 'bar' });42 });43 it('should emit

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockBuilder, MockRender, MockInstance, ngMocks } from 'ng-mocks';2import { MyComponent } from './my.component';3import { MyService } from './my.service';4describe('MyComponent', () => {5 beforeEach(() => MockBuilder(MyComponent).keep(MyService));6 it('should create', () => {7 const fixture = MockRender(MyComponent);8 const component = fixture.point.componentInstance;9 expect(component).toBeTruthy();10 });11 it('should not call the service if the event is not triggered', () => {12 const fixture = MockRender(MyComponent);13 const service = MockInstance(MyService);14 expect(service.myMethod).not.toHaveBeenCalled();15 });16 it('should call the service if the event is triggered', () => {17 const fixture = MockRender(MyComponent);18 const service = MockInstance(MyService);19 ngMocks.triggerEvent(fixture.point.componentInstance, 'customEvent');20 expect(service.myMethod).toHaveBeenCalled();21 });22});23The above tests are working fine but the problem is that I am not able to pass any data to the service method. The service method is accepting some data as input and I need to pass that data to the service method from the test case. I tried to pass the data using ngMocks.triggerEvent() method but it is not working. Can anyone please help me with this?

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockBuilder, MockRender, MockedComponentFixture, ngMocks } from 'ng-mocks';2import { MyComponent } from './my.component';3describe('MyComponent', () => {4 ngMocks.faster();5 let component: MyComponent;6 let fixture: MockedComponentFixture<MyComponent>;7 beforeEach(() => MockBuilder(MyComponent));8 beforeEach(() => {9 fixture = MockRender(MyComponent);10 component = fixture.point.componentInstance;11 });12 it('should create', () => {13 expect(component).toBeTruthy();14 });15 it('should emit custom event', () => {16 const spy = spyOn(component.customEvent, 'emit');17 const payload = { id: 1, name: 'test' };18 ngMocks.customEvent(fixture, 'customEvent', payload);19 expect(spy).toHaveBeenCalledWith(payload);20 });21});22import { Component, EventEmitter, Output } from '@angular/core';23@Component({24 template: `<button (click)="emitCustomEvent()">Click</button>`,25})26export class MyComponent {27 @Output() customEvent = new EventEmitter<{ id: number; name: string }>();28 emitCustomEvent() {29 this.customEvent.emit({ id: 1, name: 'test' });30 }31}32import { MyComponent } from './my.component';33describe('MyComponent', () => {34 it('should emit custom event', () => {35 const spy = spyOn(component.customEvent, 'emit');36 const payload = { id: 1, name: 'test' };37 component.emitCustomEvent();38 expect(spy).toHaveBeenCalledWith(payload);39 });40});41The ngMocks.customEvent() method takes three arguments:42In this article, we learned about the ngMocks.customEvent() method to emit a custom event from a component. We also learned about the dispatchEvent() method of the nativeElement of the component. We also

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockNgZone } from 'ng-mocks';2describe('TestComponent', () => {3 let component: TestComponent;4 let fixture: ComponentFixture<TestComponent>;5 let mockNgZone: MockNgZone;6 beforeEach(async(() => {7 TestBed.configureTestingModule({8 { provide: NgZone, useFactory: () => (mockNgZone = new MockNgZone()) },9 }).compileComponents();10 }));11 beforeEach(() => {12 fixture = TestBed.createComponent(TestComponent);13 component = fixture.componentInstance;14 fixture.detectChanges();15 });16 it('should create', () => {17 expect(component).toBeTruthy();18 });19 it('should call customEvent', () => {20 const spy = spyOn(component, 'customEvent');21 component.onClick();22 mockNgZone.triggerEventHandler('click', null);23 expect(spy).toHaveBeenCalled();24 });25});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { Component } from '@angular/core';3import { MyComponent } from './my.component';4beforeEach(() => MockBuilder(MyComponent));5it('triggers a custom event', () => {6 const fixture = MockRender(MyComponent);7 const component = fixture.point.componentInstance;8 const spy = spyOn(component, 'myCustomEvent');9 ngMocks.customEvent(fixture.point, 'myCustomEvent', 'test');10 expect(spy).toHaveBeenCalledWith('test');11});12import { Component, EventEmitter, Output } from '@angular/core';13@Component({14})15export class MyComponent {16 @Output() myCustomEvent = new EventEmitter<string>();17}18import { MockBuilder, MockRender } from 'ng-mocks';19import { Component } from '@angular/core';20import { MyComponent } from './my.component';21beforeEach(() => MockBuilder(MyComponent));22it('triggers a custom event', () => {23 const fixture = MockRender(MyComponent);24 const component = fixture.point.componentInstance;25 const spy = spyOn(component, 'myCustomEvent');26 component.myCustomEvent.emit('test');27 expect(spy).toHaveBeenCalledWith('test');28});29import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';30import { Component } from '@angular/core';31import { MyComponent } from './my.component';32beforeEach(() => MockBuilder(MyComponent));33it('triggers a custom event', () => {34 const fixture = MockRender(MyComponent);35 const component = fixture.point.componentInstance;36 const spy = spyOn(component, 'myCustomEvent');37 ngMocks.customEvent(fixture.point, 'myCustomEvent', 'test');38 expect(spy).toHaveBeenCalledWith('test');39});40import { Component, EventEmitter, Output } from '@angular/core';41@Component({42})43export class MyComponent {44 @Output() myCustomEvent = new EventEmitter<string>();45}46import { MockBuilder, MockRender } from 'ng-mocks';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createCustomEvent } from 'ng-mocks';2it('fires a custom event', () => {3 const spy = spyOn(component, 'onCustomEvent');4 const event = createCustomEvent('customEvent', { detail: 'test' });5 fixture.nativeElement.dispatchEvent(event);6 expect(spy).toHaveBeenCalledWith('test');7});8@Output() customEvent = new EventEmitter<string>();9onCustomEvent(event: string) {10 this.customEvent.emit(event);11}12import { createCustomEvent } from 'ng-mocks';13it('fires a custom event', () => {14 const spy = spyOn(component, 'onCustomEvent');15 const event = createCustomEvent('customEvent', { detail: 'test' });16 fixture.nativeElement.dispatchEvent(event);17 expect(spy).toHaveBeenCalledWith('test');18});19@Output() customEvent = new EventEmitter<string>();20onCustomEvent(event: string) {21 this.customEvent.emit(event);22}23import { createCustomEvent } from 'ng-mocks';24it('fires a custom event', () => {25 const spy = spyOn(component, 'onCustomEvent');26 const event = createCustomEvent('customEvent', { detail: 'test' });27 fixture.nativeElement.dispatchEvent(event);28 expect(spy).toHaveBeenCalledWith('test');29});30@Output() customEvent = new EventEmitter<string>();31onCustomEvent(event: string) {32 this.customEvent.emit(event);33}34import { createCustomEvent } from 'ng-mocks';35it('fires a custom event', () => {36 const spy = spyOn(component, 'onCustomEvent');37 const event = createCustomEvent('customEvent', { detail: 'test' });38 fixture.nativeElement.dispatchEvent(event);39 expect(spy).toHaveBeenCalledWith('test');40});

Full Screen

Using AI Code Generation

copy

Full Screen

1import {customEvent} from 'jasmine-dom-custom-event-mocks';2describe('CustomEvent', () => {3 it('should be defined', () => {4 const event = customEvent('customEvent', {5 });6 expect(event).toBeDefined();7 expect(event.type).toBe('customEvent');8 expect(event.bubbles).toBe(true);9 expect(event.detail).toBe('test');10 });11});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ng-mocks automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful