]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui3/build/event-simulate/event-simulate.js
Release 6.2.2
[Github/sugarcrm.git] / include / javascript / yui3 / build / event-simulate / event-simulate.js
1 /*
2  Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3  Code licensed under the BSD License:
4  http://developer.yahoo.com/yui/license.html
5  version: 3.3.0
6  build: 3167
7  */
8 YUI.add('event-simulate',function(Y){(function(){var L=Y.Lang,array=Y.Array,isFunction=L.isFunction,isString=L.isString,isBoolean=L.isBoolean,isObject=L.isObject,isNumber=L.isNumber,doc=Y.config.doc,mouseEvents={click:1,dblclick:1,mouseover:1,mouseout:1,mousedown:1,mouseup:1,mousemove:1},keyEvents={keydown:1,keyup:1,keypress:1},uiEvents={blur:1,change:1,focus:1,resize:1,scroll:1,select:1},bubbleEvents={scroll:1,resize:1,reset:1,submit:1,change:1,select:1,error:1,abort:1};Y.mix(bubbleEvents,mouseEvents);Y.mix(bubbleEvents,keyEvents);function simulateKeyEvent(target,type,bubbles,cancelable,view,ctrlKey,altKey,shiftKey,metaKey,keyCode,charCode)
9 {if(!target){Y.error("simulateKeyEvent(): Invalid target.");}
10 if(isString(type)){type=type.toLowerCase();switch(type){case"textevent":type="keypress";break;case"keyup":case"keydown":case"keypress":break;default:Y.error("simulateKeyEvent(): Event type '"+type+"' not supported.");}}else{Y.error("simulateKeyEvent(): Event type must be a string.");}
11 if(!isBoolean(bubbles)){bubbles=true;}
12 if(!isBoolean(cancelable)){cancelable=true;}
13 if(!isObject(view)){view=window;}
14 if(!isBoolean(ctrlKey)){ctrlKey=false;}
15 if(!isBoolean(altKey)){altKey=false;}
16 if(!isBoolean(shiftKey)){shiftKey=false;}
17 if(!isBoolean(metaKey)){metaKey=false;}
18 if(!isNumber(keyCode)){keyCode=0;}
19 if(!isNumber(charCode)){charCode=0;}
20 var customEvent=null;if(isFunction(doc.createEvent)){try{customEvent=doc.createEvent("KeyEvents");customEvent.initKeyEvent(type,bubbles,cancelable,view,ctrlKey,altKey,shiftKey,metaKey,keyCode,charCode);}catch(ex){try{customEvent=doc.createEvent("Events");}catch(uierror){customEvent=doc.createEvent("UIEvents");}finally{customEvent.initEvent(type,bubbles,cancelable);customEvent.view=view;customEvent.altKey=altKey;customEvent.ctrlKey=ctrlKey;customEvent.shiftKey=shiftKey;customEvent.metaKey=metaKey;customEvent.keyCode=keyCode;customEvent.charCode=charCode;}}
21 target.dispatchEvent(customEvent);}else if(isObject(doc.createEventObject)){customEvent=doc.createEventObject();customEvent.bubbles=bubbles;customEvent.cancelable=cancelable;customEvent.view=view;customEvent.ctrlKey=ctrlKey;customEvent.altKey=altKey;customEvent.shiftKey=shiftKey;customEvent.metaKey=metaKey;customEvent.keyCode=(charCode>0)?charCode:keyCode;target.fireEvent("on"+type,customEvent);}else{Y.error("simulateKeyEvent(): No event simulation framework present.");}}
22 function simulateMouseEvent(target,type,bubbles,cancelable,view,detail,screenX,screenY,clientX,clientY,ctrlKey,altKey,shiftKey,metaKey,button,relatedTarget)
23 {if(!target){Y.error("simulateMouseEvent(): Invalid target.");}
24 if(isString(type)){type=type.toLowerCase();if(!mouseEvents[type]){Y.error("simulateMouseEvent(): Event type '"+type+"' not supported.");}}else{Y.error("simulateMouseEvent(): Event type must be a string.");}
25 if(!isBoolean(bubbles)){bubbles=true;}
26 if(!isBoolean(cancelable)){cancelable=(type!="mousemove");}
27 if(!isObject(view)){view=window;}
28 if(!isNumber(detail)){detail=1;}
29 if(!isNumber(screenX)){screenX=0;}
30 if(!isNumber(screenY)){screenY=0;}
31 if(!isNumber(clientX)){clientX=0;}
32 if(!isNumber(clientY)){clientY=0;}
33 if(!isBoolean(ctrlKey)){ctrlKey=false;}
34 if(!isBoolean(altKey)){altKey=false;}
35 if(!isBoolean(shiftKey)){shiftKey=false;}
36 if(!isBoolean(metaKey)){metaKey=false;}
37 if(!isNumber(button)){button=0;}
38 relatedTarget=relatedTarget||null;var customEvent=null;if(isFunction(doc.createEvent)){customEvent=doc.createEvent("MouseEvents");if(customEvent.initMouseEvent){customEvent.initMouseEvent(type,bubbles,cancelable,view,detail,screenX,screenY,clientX,clientY,ctrlKey,altKey,shiftKey,metaKey,button,relatedTarget);}else{customEvent=doc.createEvent("UIEvents");customEvent.initEvent(type,bubbles,cancelable);customEvent.view=view;customEvent.detail=detail;customEvent.screenX=screenX;customEvent.screenY=screenY;customEvent.clientX=clientX;customEvent.clientY=clientY;customEvent.ctrlKey=ctrlKey;customEvent.altKey=altKey;customEvent.metaKey=metaKey;customEvent.shiftKey=shiftKey;customEvent.button=button;customEvent.relatedTarget=relatedTarget;}
39 if(relatedTarget&&!customEvent.relatedTarget){if(type=="mouseout"){customEvent.toElement=relatedTarget;}else if(type=="mouseover"){customEvent.fromElement=relatedTarget;}}
40 target.dispatchEvent(customEvent);}else if(isObject(doc.createEventObject)){customEvent=doc.createEventObject();customEvent.bubbles=bubbles;customEvent.cancelable=cancelable;customEvent.view=view;customEvent.detail=detail;customEvent.screenX=screenX;customEvent.screenY=screenY;customEvent.clientX=clientX;customEvent.clientY=clientY;customEvent.ctrlKey=ctrlKey;customEvent.altKey=altKey;customEvent.metaKey=metaKey;customEvent.shiftKey=shiftKey;switch(button){case 0:customEvent.button=1;break;case 1:customEvent.button=4;break;case 2:break;default:customEvent.button=0;}
41 customEvent.relatedTarget=relatedTarget;target.fireEvent("on"+type,customEvent);}else{Y.error("simulateMouseEvent(): No event simulation framework present.");}}
42 function simulateUIEvent(target,type,bubbles,cancelable,view,detail)
43 {if(!target){Y.error("simulateUIEvent(): Invalid target.");}
44 if(isString(type)){type=type.toLowerCase();if(!uiEvents[type]){Y.error("simulateUIEvent(): Event type '"+type+"' not supported.");}}else{Y.error("simulateUIEvent(): Event type must be a string.");}
45 var customEvent=null;if(!isBoolean(bubbles)){bubbles=(type in bubbleEvents);}
46 if(!isBoolean(cancelable)){cancelable=(type=="submit");}
47 if(!isObject(view)){view=window;}
48 if(!isNumber(detail)){detail=1;}
49 if(isFunction(doc.createEvent)){customEvent=doc.createEvent("UIEvents");customEvent.initUIEvent(type,bubbles,cancelable,view,detail);target.dispatchEvent(customEvent);}else if(isObject(doc.createEventObject)){customEvent=doc.createEventObject();customEvent.bubbles=bubbles;customEvent.cancelable=cancelable;customEvent.view=view;customEvent.detail=detail;target.fireEvent("on"+type,customEvent);}else{Y.error("simulateUIEvent(): No event simulation framework present.");}}
50 Y.Event.simulate=function(target,type,options){options=options||{};if(mouseEvents[type]){simulateMouseEvent(target,type,options.bubbles,options.cancelable,options.view,options.detail,options.screenX,options.screenY,options.clientX,options.clientY,options.ctrlKey,options.altKey,options.shiftKey,options.metaKey,options.button,options.relatedTarget);}else if(keyEvents[type]){simulateKeyEvent(target,type,options.bubbles,options.cancelable,options.view,options.ctrlKey,options.altKey,options.shiftKey,options.metaKey,options.keyCode,options.charCode);}else if(uiEvents[type]){simulateUIEvent(target,type,options.bubbles,options.cancelable,options.view,options.detail);}else{Y.error("simulate(): Event '"+type+"' can't be simulated.");}};})();},'3.3.0',{requires:['event-base']});