goog.provide('bot.events');
goog.provide('bot.events.EventArgs');
goog.provide('bot.events.EventType');
goog.provide('bot.events.KeyboardArgs');
goog.provide('bot.events.MSGestureArgs');
goog.provide('bot.events.MSPointerArgs');
goog.provide('bot.events.MouseArgs');
goog.provide('bot.events.Touch');
goog.provide('bot.events.TouchArgs');
goog.require('bot');
goog.require('bot.Error');
goog.require('bot.ErrorCode');
goog.require('bot.userAgent');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.events.BrowserEvent');
goog.require('goog.style');
goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
bot.events.SUPPORTS_TOUCH_EVENTS = !(goog.userAgent.IE &&
!bot.userAgent.isEngineVersion(10));
bot.events.BROKEN_TOUCH_API_ = (function () {
if (goog.userAgent.product.ANDROID) {
return !bot.userAgent.isProductVersion(4);
}
return !bot.userAgent.IOS;
})();
bot.events.SUPPORTS_MSPOINTER_EVENTS =
goog.userAgent.IE && bot.getWindow().navigator.msPointerEnabled;
bot.events.EventArgs;
bot.events.MouseArgs;
bot.events.KeyboardArgs;
bot.events.TouchArgs;
bot.events.Touch;
bot.events.MSGestureArgs;
bot.events.MSPointerArgs;
bot.events.EventFactory_ = function (type, bubbles, cancelable) {
this.type_ = type;
this.bubbles_ = bubbles;
this.cancelable_ = cancelable;
};
bot.events.EventFactory_.prototype.create = function (target, opt_args) {
var doc = goog.dom.getOwnerDocument(target);
var event = doc.createEvent('HTMLEvents');
event.initEvent(this.type_, this.bubbles_, this.cancelable_);
return event;
};
bot.events.EventFactory_.prototype.toString = function () {
return this.type_;
};
bot.events.MouseEventFactory_ = function (type, bubbles, cancelable) {
goog.base(this, type, bubbles, cancelable);
};
goog.inherits(bot.events.MouseEventFactory_, bot.events.EventFactory_);
bot.events.MouseEventFactory_.prototype.create = function (target, opt_args) {
if (!goog.userAgent.GECKO && this == bot.events.EventType.MOUSEPIXELSCROLL) {
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
'Browser does not support a mouse pixel scroll event.');
}
var args = (opt_args);
var doc = goog.dom.getOwnerDocument(target);
var event;
var view = goog.dom.getWindow(doc);
event = doc.createEvent('MouseEvents');
var detail = 1;
if (this == bot.events.EventType.MOUSEWHEEL) {
if (!goog.userAgent.GECKO) {
event.wheelDelta = args.wheelDelta;
}
if (goog.userAgent.GECKO) {
detail = args.wheelDelta / -40;
}
}
if (goog.userAgent.GECKO && this == bot.events.EventType.MOUSEPIXELSCROLL) {
detail = args.wheelDelta;
}
event.initMouseEvent(this.type_, this.bubbles_, this.cancelable_, view,
detail, args.clientX, args.clientY,
args.clientX, args.clientY, args.ctrlKey, args.altKey,
args.shiftKey, args.metaKey, args.button, args.relatedTarget);
if (goog.userAgent.IE &&
event.pageX === 0 && event.pageY === 0 && Object.defineProperty) {
var scrollElem = goog.dom.getDomHelper(target).getDocumentScrollElement();
var clientElem = goog.style.getClientViewportElement(doc);
var pageX = args.clientX + scrollElem.scrollLeft - clientElem.clientLeft;
var pageY = args.clientY + scrollElem.scrollTop - clientElem.clientTop;
Object.defineProperty(event, 'pageX', {
get: function () {
return pageX;
}
});
Object.defineProperty(event, 'pageY', {
get: function () {
return pageY;
}
});
}
return event;
};
bot.events.KeyboardEventFactory_ = function (type, bubbles, cancelable) {
goog.base(this, type, bubbles, cancelable);
};
goog.inherits(bot.events.KeyboardEventFactory_, bot.events.EventFactory_);
bot.events.KeyboardEventFactory_.prototype.create = function (target, opt_args) {
var args = (opt_args);
var doc = goog.dom.getOwnerDocument(target);
var event;
if (goog.userAgent.GECKO && !bot.userAgent.isEngineVersion(93)) {
var view = goog.dom.getWindow(doc);
var keyCode = args.charCode ? 0 : args.keyCode;
event = doc.createEvent('KeyboardEvent');
event.initKeyEvent(this.type_, this.bubbles_, this.cancelable_, view,
args.ctrlKey, args.altKey, args.shiftKey, args.metaKey, keyCode,
args.charCode);
if (this.type_ == bot.events.EventType.KEYPRESS && args.preventDefault) {
event.preventDefault();
}
} else {
event = doc.createEvent('Events');
event.initEvent(this.type_, this.bubbles_, this.cancelable_);
event.altKey = args.altKey;
event.ctrlKey = args.ctrlKey;
event.metaKey = args.metaKey;
event.shiftKey = args.shiftKey;
event.keyCode = args.charCode || args.keyCode;
if (goog.userAgent.WEBKIT || goog.userAgent.EDGE) {
event.charCode = (this == bot.events.EventType.KEYPRESS) ?
event.keyCode : 0;
}
}
return event;
};
bot.events.TouchEventStrategy_ = {
MOUSE_EVENTS: 1,
INIT_TOUCH_EVENT: 2,
TOUCH_EVENT_CTOR: 3
};
bot.events.TouchEventFactory_ = function (type, bubbles, cancelable) {
goog.base(this, type, bubbles, cancelable);
};
goog.inherits(bot.events.TouchEventFactory_, bot.events.EventFactory_);
bot.events.TouchEventFactory_.prototype.create = function (target, opt_args) {
if (!bot.events.SUPPORTS_TOUCH_EVENTS) {
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
'Browser does not support firing touch events.');
}
var args = (opt_args);
var doc = goog.dom.getOwnerDocument(target);
var view = goog.dom.getWindow(doc);
function createNativeTouchList(touchListArgs) {
var touches = goog.array.map(touchListArgs, function (touchArg) {
return doc.createTouch(view, target, touchArg.identifier,
touchArg.pageX, touchArg.pageY, touchArg.screenX, touchArg.screenY);
});
return doc.createTouchList.apply(doc, touches);
}
function createGenericTouchList(touchListArgs) {
var touches = goog.array.map(touchListArgs, function (touchArg) {
return {
identifier: touchArg.identifier,
screenX: touchArg.screenX,
screenY: touchArg.screenY,
clientX: touchArg.clientX,
clientY: touchArg.clientY,
pageX: touchArg.pageX,
pageY: touchArg.pageY,
target: target
};
});
touches.item = function (i) {
return touches[i];
};
return touches;
}
function createTouchEventTouchList(touchListArgs) {
var touches = goog.array.map(touchListArgs, function (touchArg) {
return new Touch({
identifier: touchArg.identifier,
screenX: touchArg.screenX,
screenY: touchArg.screenY,
clientX: touchArg.clientX,
clientY: touchArg.clientY,
pageX: touchArg.pageX,
pageY: touchArg.pageY,
target: target
});
});
return touches;
}
function createTouchList(touchStrategy, touches) {
switch (touchStrategy) {
case bot.events.TouchEventStrategy_.MOUSE_EVENTS:
return createGenericTouchList(touches);
case bot.events.TouchEventStrategy_.INIT_TOUCH_EVENT:
return createNativeTouchList(touches);
case bot.events.TouchEventStrategy_.TOUCH_EVENT_CTOR:
return createTouchEventTouchList(touches);
}
return null;
}
var strategy;
if (bot.events.BROKEN_TOUCH_API_) {
strategy = bot.events.TouchEventStrategy_.MOUSE_EVENTS;
} else {
if (TouchEvent.prototype.initTouchEvent) {
strategy = bot.events.TouchEventStrategy_.INIT_TOUCH_EVENT;
} else if (TouchEvent && TouchEvent.length > 0) {
strategy = bot.events.TouchEventStrategy_.TOUCH_EVENT_CTOR;
} else {
throw new bot.Error(
bot.ErrorCode.UNSUPPORTED_OPERATION,
'Not able to create touch events in this browser');
}
}
var changedTouches = createTouchList(strategy, args.changedTouches);
var touches = (args.touches == args.changedTouches) ?
changedTouches : createTouchList(strategy, args.touches);
var targetTouches = (args.targetTouches == args.changedTouches) ?
changedTouches : createTouchList(strategy, args.targetTouches);
var event;
if (strategy == bot.events.TouchEventStrategy_.MOUSE_EVENTS) {
event = doc.createEvent('MouseEvents');
event.initMouseEvent(this.type_, this.bubbles_, this.cancelable_, view,
1, 0, 0, args.clientX, args.clientY,
args.ctrlKey, args.altKey, args.shiftKey, args.metaKey, 0,
args.relatedTarget);
event.touches = touches;
event.targetTouches = targetTouches;
event.changedTouches = changedTouches;
event.scale = args.scale;
event.rotation = args.rotation;
} else if (strategy == bot.events.TouchEventStrategy_.INIT_TOUCH_EVENT) {
event = doc.createEvent('TouchEvent');
if (event.initTouchEvent.length == 0) {
event.initTouchEvent(touches, targetTouches, changedTouches,
this.type_, view, 0, 0, args.clientX,
args.clientY, args.ctrlKey, args.altKey, args.shiftKey, args.metaKey);
} else {
event.initTouchEvent(this.type_, this.bubbles_, this.cancelable_, view,
1, 0, 0, args.clientX,
args.clientY, args.ctrlKey, args.altKey, args.shiftKey, args.metaKey,
touches, targetTouches, changedTouches, args.scale, args.rotation);
}
event.relatedTarget = args.relatedTarget;
} else if (strategy == bot.events.TouchEventStrategy_.TOUCH_EVENT_CTOR) {
var touchProperties = ({
touches: touches,
targetTouches: targetTouches,
changedTouches: changedTouches,
bubbles: this.bubbles_,
cancelable: this.cancelable_,
ctrlKey: args.ctrlKey,
shiftKey: args.shiftKey,
altKey: args.altKey,
metaKey: args.metaKey
});
event = new TouchEvent(this.type_, touchProperties);
} else {
throw new bot.Error(
bot.ErrorCode.UNSUPPORTED_OPERATION,
'Illegal TouchEventStrategy_ value (this is a bug)');
}
return event;
};
bot.events.MSGestureEventFactory_ = function (type, bubbles, cancelable) {
goog.base(this, type, bubbles, cancelable);
};
goog.inherits(bot.events.MSGestureEventFactory_, bot.events.EventFactory_);
bot.events.MSGestureEventFactory_.prototype.create = function (target,
opt_args) {
if (!bot.events.SUPPORTS_MSPOINTER_EVENTS) {
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
'Browser does not support MSGesture events.');
}
var args = (opt_args);
var doc = goog.dom.getOwnerDocument(target);
var view = goog.dom.getWindow(doc);
var event = doc.createEvent('MSGestureEvent');
var timestamp = (new Date).getTime();
event.initGestureEvent(this.type_, this.bubbles_, this.cancelable_, view,
1, 0, 0,
args.clientX, args.clientY, 0,
0, args.translationX, args.translationY,
args.scale, args.expansion, args.rotation,
args.velocityX, args.velocityY, args.velocityExpansion,
args.velocityAngular, timestamp, args.relatedTarget);
return event;
};
bot.events.MSPointerEventFactory_ = function (type, bubbles, cancelable) {
goog.base(this, type, bubbles, cancelable);
};
goog.inherits(bot.events.MSPointerEventFactory_, bot.events.EventFactory_);
bot.events.MSPointerEventFactory_.prototype.create = function (target,
opt_args) {
if (!bot.events.SUPPORTS_MSPOINTER_EVENTS) {
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
'Browser does not support MSPointer events.');
}
var args = (opt_args);
var doc = goog.dom.getOwnerDocument(target);
var view = goog.dom.getWindow(doc);
var event = doc.createEvent('MSPointerEvent');
event.initPointerEvent(this.type_, this.bubbles_, this.cancelable_, view,
0, 0, 0,
args.clientX, args.clientY, args.ctrlKey, args.altKey,
args.shiftKey, args.metaKey, args.button,
args.relatedTarget, 0, 0,
args.width, args.height, args.pressure, args.rotation,
args.tiltX, args.tiltY, args.pointerId,
args.pointerType, 0, args.isPrimary);
return event;
};
bot.events.EventType = {
BLUR: new bot.events.EventFactory_('blur', false, false),
CHANGE: new bot.events.EventFactory_('change', true, false),
FOCUS: new bot.events.EventFactory_('focus', false, false),
FOCUSIN: new bot.events.EventFactory_('focusin', true, false),
FOCUSOUT: new bot.events.EventFactory_('focusout', true, false),
INPUT: new bot.events.EventFactory_('input', true, false),
ORIENTATIONCHANGE: new bot.events.EventFactory_(
'orientationchange', false, false),
PROPERTYCHANGE: new bot.events.EventFactory_('propertychange', false, false),
SELECT: new bot.events.EventFactory_('select', true, false),
SUBMIT: new bot.events.EventFactory_('submit', true, true),
TEXTINPUT: new bot.events.EventFactory_('textInput', true, true),
CLICK: new bot.events.MouseEventFactory_('click', true, true),
CONTEXTMENU: new bot.events.MouseEventFactory_('contextmenu', true, true),
DBLCLICK: new bot.events.MouseEventFactory_('dblclick', true, true),
MOUSEDOWN: new bot.events.MouseEventFactory_('mousedown', true, true),
MOUSEMOVE: new bot.events.MouseEventFactory_('mousemove', true, false),
MOUSEOUT: new bot.events.MouseEventFactory_('mouseout', true, true),
MOUSEOVER: new bot.events.MouseEventFactory_('mouseover', true, true),
MOUSEUP: new bot.events.MouseEventFactory_('mouseup', true, true),
MOUSEWHEEL: new bot.events.MouseEventFactory_(
goog.userAgent.GECKO ? 'DOMMouseScroll' : 'mousewheel', true, true),
MOUSEPIXELSCROLL: new bot.events.MouseEventFactory_(
'MozMousePixelScroll', true, true),
KEYDOWN: new bot.events.KeyboardEventFactory_('keydown', true, true),
KEYPRESS: new bot.events.KeyboardEventFactory_('keypress', true, true),
KEYUP: new bot.events.KeyboardEventFactory_('keyup', true, true),
TOUCHEND: new bot.events.TouchEventFactory_('touchend', true, true),
TOUCHMOVE: new bot.events.TouchEventFactory_('touchmove', true, true),
TOUCHSTART: new bot.events.TouchEventFactory_('touchstart', true, true),
MSGESTURECHANGE: new bot.events.MSGestureEventFactory_(
'MSGestureChange', true, true),
MSGESTUREEND: new bot.events.MSGestureEventFactory_(
'MSGestureEnd', true, true),
MSGESTUREHOLD: new bot.events.MSGestureEventFactory_(
'MSGestureHold', true, true),
MSGESTURESTART: new bot.events.MSGestureEventFactory_(
'MSGestureStart', true, true),
MSGESTURETAP: new bot.events.MSGestureEventFactory_(
'MSGestureTap', true, true),
MSINERTIASTART: new bot.events.MSGestureEventFactory_(
'MSInertiaStart', true, true),
MSGOTPOINTERCAPTURE: new bot.events.MSPointerEventFactory_(
'MSGotPointerCapture', true, false),
MSLOSTPOINTERCAPTURE: new bot.events.MSPointerEventFactory_(
'MSLostPointerCapture', true, false),
MSPOINTERCANCEL: new bot.events.MSPointerEventFactory_(
'MSPointerCancel', true, true),
MSPOINTERDOWN: new bot.events.MSPointerEventFactory_(
'MSPointerDown', true, true),
MSPOINTERMOVE: new bot.events.MSPointerEventFactory_(
'MSPointerMove', true, true),
MSPOINTEROVER: new bot.events.MSPointerEventFactory_(
'MSPointerOver', true, true),
MSPOINTEROUT: new bot.events.MSPointerEventFactory_(
'MSPointerOut', true, true),
MSPOINTERUP: new bot.events.MSPointerEventFactory_(
'MSPointerUp', true, true)
};
bot.events.fire = function (target, type, opt_args) {
var factory = (type);
var event = factory.create(target, opt_args);
if (!('isTrusted' in event)) {
event['isTrusted'] = false;
}
return target.dispatchEvent(event);
};
bot.events.isSynthetic = function (event) {
var e = event.getBrowserEvent ? event.getBrowserEvent() : event;
return 'isTrusted' in e ? !e['isTrusted'] : false;
};