|
| 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 http://polymer.github.io/LICENSE.txt |
| 5 | + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 | + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt |
| 7 | + * Code distributed by Google as part of the polymer project is also |
| 8 | + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt |
| 9 | + */ |
| 10 | + |
| 11 | +(function() { |
| 12 | + // defaultPrevented is broken in IE. |
| 13 | + // https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called |
| 14 | + const workingDefaultPrevented = (function() { |
| 15 | + const e = document.createEvent('Event'); |
| 16 | + e.initEvent('foo', true, true); |
| 17 | + e.preventDefault(); |
| 18 | + return e.defaultPrevented; |
| 19 | + })(); |
| 20 | + |
| 21 | + if (!workingDefaultPrevented) { |
| 22 | + const origPreventDefault = Event.prototype.preventDefault; |
| 23 | + Event.prototype.preventDefault = function() { |
| 24 | + if (!this.cancelable) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + origPreventDefault.call(this); |
| 29 | + |
| 30 | + Object.defineProperty(this, 'defaultPrevented', { |
| 31 | + get() { |
| 32 | + return true; |
| 33 | + }, |
| 34 | + configurable: true, |
| 35 | + }); |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + const isIE = /Trident/.test(navigator.userAgent); |
| 40 | + |
| 41 | + // Event constructor shim |
| 42 | + if (!window.Event || (isIE && typeof window.Event !== 'function')) { |
| 43 | + const origEvent = window.Event; |
| 44 | + /** |
| 45 | + * @param {!string} inType |
| 46 | + * @param {?(EventInit)=} params |
| 47 | + */ |
| 48 | + window.Event = function(inType, params) { |
| 49 | + params = params || {}; |
| 50 | + const e = document.createEvent('Event'); |
| 51 | + e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable)); |
| 52 | + return e; |
| 53 | + }; |
| 54 | + if (origEvent) { |
| 55 | + for (const i in origEvent) { |
| 56 | + window.Event[i] = origEvent[i]; |
| 57 | + } |
| 58 | + window.Event.prototype = origEvent.prototype; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // CustomEvent constructor shim |
| 63 | + if ( |
| 64 | + !window.CustomEvent || |
| 65 | + (isIE && typeof window.CustomEvent !== 'function') |
| 66 | + ) { |
| 67 | + /** |
| 68 | + * @template T |
| 69 | + * @param {!string} inType |
| 70 | + * @param {?(CustomEventInit<T>)=} params |
| 71 | + */ |
| 72 | + window.CustomEvent = function(inType, params) { |
| 73 | + params = params || {}; |
| 74 | + const e = document.createEvent('CustomEvent'); |
| 75 | + e.initCustomEvent( |
| 76 | + inType, |
| 77 | + Boolean(params.bubbles), |
| 78 | + Boolean(params.cancelable), |
| 79 | + params.detail |
| 80 | + ); |
| 81 | + return e; |
| 82 | + }; |
| 83 | + window.CustomEvent.prototype = window.Event.prototype; |
| 84 | + } |
| 85 | + |
| 86 | + if (!window.MouseEvent || (isIE && typeof window.MouseEvent !== 'function')) { |
| 87 | + const origMouseEvent = window.MouseEvent; |
| 88 | + /** |
| 89 | + * |
| 90 | + * @param {!string} inType |
| 91 | + * @param {?(MouseEventInit)=} params |
| 92 | + */ |
| 93 | + window.MouseEvent = function(inType, params) { |
| 94 | + params = params || {}; |
| 95 | + const e = document.createEvent('MouseEvent'); |
| 96 | + e.initMouseEvent( |
| 97 | + inType, |
| 98 | + Boolean(params.bubbles), |
| 99 | + Boolean(params.cancelable), |
| 100 | + params.view || window, |
| 101 | + params.detail, |
| 102 | + params.screenX, |
| 103 | + params.screenY, |
| 104 | + params.clientX, |
| 105 | + params.clientY, |
| 106 | + params.ctrlKey, |
| 107 | + params.altKey, |
| 108 | + params.shiftKey, |
| 109 | + params.metaKey, |
| 110 | + params.button, |
| 111 | + params.relatedTarget |
| 112 | + ); |
| 113 | + return e; |
| 114 | + }; |
| 115 | + if (origMouseEvent) { |
| 116 | + for (const j in origMouseEvent) { |
| 117 | + window.MouseEvent[j] = origMouseEvent[j]; |
| 118 | + } |
| 119 | + } |
| 120 | + window.MouseEvent.prototype = origMouseEvent.prototype; |
| 121 | + } |
| 122 | +})(); |
0 commit comments