mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
157 lines
5.6 KiB
JavaScript
157 lines
5.6 KiB
JavaScript
/**
|
|
* EventUtils provides some utility methods for creating and sending DOM events.
|
|
* Current methods:
|
|
* sendMouseEvent
|
|
* sendChar
|
|
* sendString
|
|
* sendKey
|
|
*/
|
|
|
|
/**
|
|
* Send a mouse event to the node with id aTarget. The "event" passed in to
|
|
* aEvent is just a JavaScript object with the properties set that the real
|
|
* mouse event object should have. This includes the type of the mouse event.
|
|
* E.g. to send an click event to the node with id 'node' you might do this:
|
|
*
|
|
* sendMouseEvent({type:'click'}, 'node');
|
|
*/
|
|
function sendMouseEvent(aEvent, aTarget) {
|
|
if (['click', 'mousedown', 'mouseup', 'mouseover', 'mouseout'].indexOf(aEvent.type) == -1) {
|
|
throw new Error("sendMouseEvent doesn't know about event type '"+aEvent.type+"'");
|
|
}
|
|
|
|
// For events to trigger the UA's default actions they need to be "trusted"
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');
|
|
|
|
var event = document.createEvent('MouseEvent');
|
|
|
|
var typeArg = aEvent.type;
|
|
var canBubbleArg = true;
|
|
var cancelableArg = true;
|
|
var viewArg = window;
|
|
var detailArg = aEvent.detail || (aEvent.type == 'click' ||
|
|
aEvent.type == 'mousedown' ||
|
|
aEvent.type == 'mouseup' ? 1 : 0);
|
|
var screenXArg = aEvent.screenX || 0;
|
|
var screenYArg = aEvent.screenY || 0;
|
|
var clientXArg = aEvent.clientX || 0;
|
|
var clientYArg = aEvent.clientY || 0;
|
|
var ctrlKeyArg = aEvent.ctrlKey || false;
|
|
var altKeyArg = aEvent.altKey || false;
|
|
var shiftKeyArg = aEvent.shiftKey || false;
|
|
var metaKeyArg = aEvent.metaKey || false;
|
|
var buttonArg = aEvent.button || 0;
|
|
var relatedTargetArg = aEvent.relatedTarget || null;
|
|
|
|
event.initMouseEvent(typeArg, canBubbleArg, cancelableArg, viewArg, detailArg,
|
|
screenXArg, screenYArg, clientXArg, clientYArg,
|
|
ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg,
|
|
buttonArg, relatedTargetArg);
|
|
|
|
document.getElementById(aTarget).dispatchEvent(event);
|
|
}
|
|
|
|
/**
|
|
* Send the char aChar to the node with id aTarget. If aTarget is not
|
|
* provided, use "target". This method handles casing of chars (sends the
|
|
* right charcode, and sends a shift key for uppercase chars). No other
|
|
* modifiers are handled at this point.
|
|
*
|
|
* For now this method only works for English letters (lower and upper case)
|
|
* and the digits 0-9.
|
|
*
|
|
* Returns true if the keypress event was accepted (no calls to preventDefault
|
|
* or anything like that), false otherwise.
|
|
*/
|
|
function sendChar(aChar, aTarget) {
|
|
// DOM event charcodes match ASCII (JS charcodes) for a-zA-Z0-9.
|
|
var hasShift = (aChar == aChar.toUpperCase());
|
|
var charCode = aChar.charCodeAt(0);
|
|
var keyCode = charCode;
|
|
if (!hasShift) {
|
|
// For lowercase letters, the keyCode is actually 32 less than the charCode
|
|
keyCode -= 0x20;
|
|
}
|
|
|
|
return __doEventDispatch(aTarget, charCode, keyCode, hasShift);
|
|
}
|
|
|
|
/**
|
|
* Send the string aStr to the node with id aTarget. If aTarget is not
|
|
* provided, use "target".
|
|
*
|
|
* For now this method only works for English letters (lower and upper case)
|
|
* and the digits 0-9.
|
|
*/
|
|
function sendString(aStr, aTarget) {
|
|
for (var i = 0; i < aStr.length; ++i) {
|
|
sendChar(aStr.charAt(i), aTarget);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send the non-character key aKey to the node with id aTarget. If aTarget is
|
|
* not provided, use "target". The name of the key should be a lowercase
|
|
* version of the part that comes after "DOM_VK_" in the KeyEvent constant
|
|
* name for this key. No modifiers are handled at this point.
|
|
*
|
|
* Returns true if the keypress event was accepted (no calls to preventDefault
|
|
* or anything like that), false otherwise.
|
|
*/
|
|
function sendKey(aKey, aTarget) {
|
|
keyName = "DOM_VK_" + aKey.toUpperCase();
|
|
|
|
if (!KeyEvent[keyName]) {
|
|
throw "Unknown key: " + keyName;
|
|
}
|
|
|
|
return __doEventDispatch(aTarget, 0, KeyEvent[keyName], false);
|
|
}
|
|
|
|
/**
|
|
* Actually perform event dispatch given a charCode, keyCode, and boolean for
|
|
* whether "shift" was pressed. Send the event to the node with id aTarget. If
|
|
* aTarget is not provided, use "target".
|
|
*
|
|
* Returns true if the keypress event was accepted (no calls to preventDefault
|
|
* or anything like that), false otherwise.
|
|
*/
|
|
function __doEventDispatch(aTarget, aCharCode, aKeyCode, aHasShift) {
|
|
if (aTarget === undefined) {
|
|
aTarget = "target";
|
|
}
|
|
|
|
// Make our events trusted
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
var event = document.createEvent("KeyEvents");
|
|
event.initKeyEvent("keydown", true, true, document.defaultView,
|
|
false, false, aHasShift, false,
|
|
aKeyCode, 0);
|
|
var accepted = $(aTarget).dispatchEvent(event);
|
|
|
|
// Cancelling keydown cancels keypress too
|
|
if (accepted) {
|
|
event = document.createEvent("KeyEvents");
|
|
if (aCharCode) {
|
|
event.initKeyEvent("keypress", true, true, document.defaultView,
|
|
false, false, aHasShift, false,
|
|
0, aCharCode);
|
|
} else {
|
|
event.initKeyEvent("keypress", true, true, document.defaultView,
|
|
false, false, aHasShift, false,
|
|
aKeyCode, 0);
|
|
}
|
|
accepted = $(aTarget).dispatchEvent(event);
|
|
}
|
|
|
|
// Always send keyup
|
|
var event = document.createEvent("KeyEvents");
|
|
event.initKeyEvent("keyup", true, true, document.defaultView,
|
|
false, false, aHasShift, false,
|
|
aKeyCode, 0);
|
|
$(aTarget).dispatchEvent(event);
|
|
return accepted;
|
|
}
|
|
|