events.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* @flow */
  2. import { isDef, isUndef } from 'shared/util'
  3. import { updateListeners } from 'core/vdom/helpers/index'
  4. import { isIE, isFF, supportsPassive, isUsingMicroTask } from 'core/util/index'
  5. import { RANGE_TOKEN, CHECKBOX_RADIO_TOKEN } from 'web/compiler/directives/model'
  6. import { currentFlushTimestamp } from 'core/observer/scheduler'
  7. // normalize v-model event tokens that can only be determined at runtime.
  8. // it's important to place the event as the first in the array because
  9. // the whole point is ensuring the v-model callback gets called before
  10. // user-attached handlers.
  11. function normalizeEvents (on) {
  12. /* istanbul ignore if */
  13. if (isDef(on[RANGE_TOKEN])) {
  14. // IE input[type=range] only supports `change` event
  15. const event = isIE ? 'change' : 'input'
  16. on[event] = [].concat(on[RANGE_TOKEN], on[event] || [])
  17. delete on[RANGE_TOKEN]
  18. }
  19. // This was originally intended to fix #4521 but no longer necessary
  20. // after 2.5. Keeping it for backwards compat with generated code from < 2.4
  21. /* istanbul ignore if */
  22. if (isDef(on[CHECKBOX_RADIO_TOKEN])) {
  23. on.change = [].concat(on[CHECKBOX_RADIO_TOKEN], on.change || [])
  24. delete on[CHECKBOX_RADIO_TOKEN]
  25. }
  26. }
  27. let target: any
  28. function createOnceHandler (event, handler, capture) {
  29. const _target = target // save current target element in closure
  30. return function onceHandler () {
  31. const res = handler.apply(null, arguments)
  32. if (res !== null) {
  33. remove(event, onceHandler, capture, _target)
  34. }
  35. }
  36. }
  37. // #9446: Firefox <= 53 (in particular, ESR 52) has incorrect Event.timeStamp
  38. // implementation and does not fire microtasks in between event propagation, so
  39. // safe to exclude.
  40. const useMicrotaskFix = isUsingMicroTask && !(isFF && Number(isFF[1]) <= 53)
  41. function add (
  42. name: string,
  43. handler: Function,
  44. capture: boolean,
  45. passive: boolean
  46. ) {
  47. // async edge case #6566: inner click event triggers patch, event handler
  48. // attached to outer element during patch, and triggered again. This
  49. // happens because browsers fire microtask ticks between event propagation.
  50. // the solution is simple: we save the timestamp when a handler is attached,
  51. // and the handler would only fire if the event passed to it was fired
  52. // AFTER it was attached.
  53. if (useMicrotaskFix) {
  54. const attachedTimestamp = currentFlushTimestamp
  55. const original = handler
  56. handler = original._wrapper = function (e) {
  57. if (e.timeStamp >= attachedTimestamp) {
  58. return original.apply(this, arguments)
  59. }
  60. }
  61. }
  62. target.addEventListener(
  63. name,
  64. handler,
  65. supportsPassive
  66. ? { capture, passive }
  67. : capture
  68. )
  69. }
  70. function remove (
  71. name: string,
  72. handler: Function,
  73. capture: boolean,
  74. _target?: HTMLElement
  75. ) {
  76. (_target || target).removeEventListener(
  77. name,
  78. handler._wrapper || handler,
  79. capture
  80. )
  81. }
  82. function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  83. if (isUndef(oldVnode.data.on) && isUndef(vnode.data.on)) {
  84. return
  85. }
  86. const on = vnode.data.on || {}
  87. const oldOn = oldVnode.data.on || {}
  88. target = vnode.elm
  89. normalizeEvents(on)
  90. updateListeners(on, oldOn, add, remove, createOnceHandler, vnode.context)
  91. target = undefined
  92. }
  93. export default {
  94. create: updateDOMListeners,
  95. update: updateDOMListeners
  96. }