UNPKG

3.42 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
6 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
7 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
8 * Google as part of the polymer project is also subject to an additional IP
9 * rights grant found at http://polymer.github.io/PATENTS.txt
10 */
11// defaultPrevented is broken in IE.
12// https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called
13const workingDefaultPrevented = (() => {
14 const e = document.createEvent('Event');
15 e.initEvent('foo', true, true);
16 e.preventDefault();
17 return e.defaultPrevented;
18})();
19if (!workingDefaultPrevented) {
20 const origPreventDefault = Event.prototype.preventDefault;
21 Event.prototype.preventDefault = function () {
22 if (!this.cancelable) {
23 return;
24 }
25 origPreventDefault.call(this);
26 Object.defineProperty(this, 'defaultPrevented', {
27 get() {
28 return true;
29 },
30 configurable: true
31 });
32 };
33}
34const isIE = /Trident/.test(navigator.userAgent);
35// Event constructor shim
36if (!window.Event || isIE && (typeof window.Event !== 'function')) {
37 const origEvent = window.Event;
38 window['Event'] =
39 ((inType, params) => {
40 params = params || {};
41 const e = document.createEvent('Event');
42 e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable));
43 return e;
44 });
45 if (origEvent) {
46 // tslint:disable-next-line:forin
47 for (const i in origEvent) {
48 // tslint:disable-next-line:no-any
49 window['Event'][i] = origEvent[i];
50 }
51 window['Event'].prototype = origEvent.prototype;
52 }
53}
54// CustomEvent constructor shim
55if (!window.CustomEvent || isIE && (typeof window.CustomEvent !== 'function')) {
56 window['CustomEvent'] =
57 ((inType, params) => {
58 params = params || {};
59 const e = document.createEvent('CustomEvent');
60 e.initCustomEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable), params.detail);
61 return e;
62 });
63 window['CustomEvent'].prototype = window.Event.prototype;
64}
65if (!window.MouseEvent || isIE && (typeof window.MouseEvent !== 'function')) {
66 const origMouseEvent = window.MouseEvent;
67 window['MouseEvent'] =
68 ((inType, params) => {
69 params = params || {};
70 const e = document.createEvent('MouseEvent');
71 e.initMouseEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable), params.view || window, params.detail, params.screenX, params.screenY, params.clientX, params.clientY, params.ctrlKey, params.altKey, params.shiftKey, params.metaKey, params.button, params.relatedTarget);
72 return e;
73 });
74 if (origMouseEvent) {
75 // tslint:disable-next-line:forin
76 for (const j in origMouseEvent) {
77 // tslint:disable-next-line:no-any
78 window.MouseEvent[j] = origMouseEvent[j];
79 }
80 }
81 window['MouseEvent'].prototype = origMouseEvent.prototype;
82}
83//# sourceMappingURL=custom-event.js.map
\No newline at end of file