|
2 | 2 |
|
3 | 3 | import { isDef, isUndef } from 'shared/util'
|
4 | 4 | import { updateListeners } from 'core/vdom/helpers/index'
|
5 |
| -import { isIE, supportsPassive } from 'core/util/index' |
| 5 | +import { isIE, isPhantomJS, supportsPassive } from 'core/util/index' |
6 | 6 | import { RANGE_TOKEN, CHECKBOX_RADIO_TOKEN } from 'web/compiler/directives/model'
|
7 | 7 |
|
8 | 8 | // normalize v-model event tokens that can only be determined at runtime.
|
@@ -38,32 +38,130 @@ function createOnceHandler (event, handler, capture) {
|
38 | 38 | }
|
39 | 39 | }
|
40 | 40 |
|
| 41 | +const delegateRE = /^(?:click|dblclick|submit|(?:key|mouse|touch|pointer).*)$/ |
| 42 | +const eventCounts = {} |
| 43 | +const attachedGlobalHandlers = {} |
| 44 | + |
| 45 | +type TargetRef = { el: Element | Document } |
| 46 | + |
41 | 47 | function add (
|
42 |
| - event: string, |
| 48 | + name: string, |
43 | 49 | handler: Function,
|
44 | 50 | capture: boolean,
|
45 | 51 | passive: boolean
|
46 | 52 | ) {
|
47 |
| - target.addEventListener( |
48 |
| - event, |
49 |
| - handler, |
50 |
| - supportsPassive |
51 |
| - ? { capture, passive } |
52 |
| - : capture |
53 |
| - ) |
| 53 | + if (!capture && !passive && delegateRE.test(name)) { |
| 54 | + const count = eventCounts[name] |
| 55 | + let store = target.__events |
| 56 | + if (!count) { |
| 57 | + attachGlobalHandler(name) |
| 58 | + } |
| 59 | + if (!store) { |
| 60 | + store = target.__events = {} |
| 61 | + } |
| 62 | + if (!store[name]) { |
| 63 | + eventCounts[name]++ |
| 64 | + } |
| 65 | + store[name] = handler |
| 66 | + } else { |
| 67 | + target.addEventListener( |
| 68 | + name, |
| 69 | + handler, |
| 70 | + supportsPassive |
| 71 | + ? { capture, passive } |
| 72 | + : capture |
| 73 | + ) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function attachGlobalHandler(name: string) { |
| 78 | + const handler = (attachedGlobalHandlers[name] = (e: any) => { |
| 79 | + const isClick = e.type === 'click' || e.type === 'dblclick' |
| 80 | + if (isClick && e.button !== 0) { |
| 81 | + e.stopPropagation() |
| 82 | + return false |
| 83 | + } |
| 84 | + const targetRef: TargetRef = { el: document } |
| 85 | + dispatchEvent(e, name, isClick, targetRef) |
| 86 | + }) |
| 87 | + document.addEventListener(name, handler) |
| 88 | + eventCounts[name] = 0 |
| 89 | +} |
| 90 | + |
| 91 | +function stopPropagation() { |
| 92 | + this.cancelBubble = true |
| 93 | + if (!this.immediatePropagationStopped) { |
| 94 | + this.stopImmediatePropagation() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function dispatchEvent( |
| 99 | + e: Event, |
| 100 | + name: string, |
| 101 | + isClick: boolean, |
| 102 | + targetRef: TargetRef |
| 103 | +) { |
| 104 | + let el: any = e.target |
| 105 | + let userEvent |
| 106 | + if (isPhantomJS) { |
| 107 | + // in PhantomJS it throws if we try to re-define currentTarget, |
| 108 | + // so instead we create a wrapped event to the user |
| 109 | + userEvent = Object.create((e: any)) |
| 110 | + userEvent.stopPropagation = stopPropagation.bind((e: any)) |
| 111 | + userEvent.preventDefault = e.preventDefault.bind(e) |
| 112 | + } else { |
| 113 | + userEvent = e |
| 114 | + } |
| 115 | + Object.defineProperty(userEvent, 'currentTarget', ({ |
| 116 | + configurable: true, |
| 117 | + get() { |
| 118 | + return targetRef.el |
| 119 | + } |
| 120 | + }: any)) |
| 121 | + while (el != null) { |
| 122 | + // Don't process clicks on disabled elements |
| 123 | + if (isClick && el.disabled) { |
| 124 | + break |
| 125 | + } |
| 126 | + const store = el.__events |
| 127 | + if (store) { |
| 128 | + const handler = store[name] |
| 129 | + if (handler) { |
| 130 | + targetRef.el = el |
| 131 | + handler(userEvent) |
| 132 | + if (e.cancelBubble) { |
| 133 | + break |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + el = el.parentNode |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +function removeGlobalHandler(name: string) { |
| 142 | + document.removeEventListener(name, attachedGlobalHandlers[name]) |
| 143 | + attachedGlobalHandlers[name] = null |
54 | 144 | }
|
55 | 145 |
|
56 | 146 | function remove (
|
57 |
| - event: string, |
| 147 | + name: string, |
58 | 148 | handler: Function,
|
59 | 149 | capture: boolean,
|
60 | 150 | _target?: HTMLElement
|
61 | 151 | ) {
|
62 |
| - (_target || target).removeEventListener( |
63 |
| - event, |
64 |
| - handler._withTask || handler, |
65 |
| - capture |
66 |
| - ) |
| 152 | + const el: any = _target || target |
| 153 | + if (!capture && delegateRE.test(name)) { |
| 154 | + el.__events[name] = null |
| 155 | + if (--eventCounts[name] === 0) { |
| 156 | + removeGlobalHandler(name) |
| 157 | + } |
| 158 | + } else { |
| 159 | + el.removeEventListener( |
| 160 | + name, |
| 161 | + handler._withTask || handler, |
| 162 | + capture |
| 163 | + ) |
| 164 | + } |
67 | 165 | }
|
68 | 166 |
|
69 | 167 | function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
|
|
0 commit comments