model.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Not type checking this file because flow doesn't like attaching
  3. * properties to Elements.
  4. */
  5. import { looseEqual, looseIndexOf } from 'shared/util'
  6. import { warn, nextTick, isAndroid, isIE9, isIE, isEdge } from 'core/util/index'
  7. const modelableTagRE = /^input|select|textarea|vue-component-[0-9]+(-[0-9a-zA-Z_\-]*)?$/
  8. /* istanbul ignore if */
  9. if (isIE9) {
  10. // http://www.matts411.com/post/internet-explorer-9-oninput/
  11. document.addEventListener('selectionchange', () => {
  12. const el = document.activeElement
  13. if (el && el.vmodel) {
  14. trigger(el, 'input')
  15. }
  16. })
  17. }
  18. export default {
  19. bind (el, binding, vnode) {
  20. if (process.env.NODE_ENV !== 'production') {
  21. if (!modelableTagRE.test(vnode.tag)) {
  22. warn(
  23. `v-model is not supported on element type: <${vnode.tag}>. ` +
  24. 'If you are working with contenteditable, it\'s recommended to ' +
  25. 'wrap a library dedicated for that purpose inside a custom component.',
  26. vnode.context
  27. )
  28. }
  29. }
  30. if (vnode.tag === 'select') {
  31. setSelected(el, binding, vnode.context)
  32. /* istanbul ignore if */
  33. if (isIE || isEdge) {
  34. nextTick(() => {
  35. setSelected(el, binding, vnode.context)
  36. })
  37. }
  38. } else if (vnode.tag === 'textarea' || el.type === 'text') {
  39. if (!isAndroid) {
  40. el.addEventListener('compositionstart', onCompositionStart)
  41. el.addEventListener('compositionend', onCompositionEnd)
  42. }
  43. /* istanbul ignore if */
  44. if (isIE9) {
  45. el.vmodel = true
  46. }
  47. }
  48. },
  49. componentUpdated (el, binding, vnode) {
  50. if (vnode.tag === 'select') {
  51. setSelected(el, binding, vnode.context)
  52. // in case the options rendered by v-for have changed,
  53. // it's possible that the value is out-of-sync with the rendered options.
  54. // detect such cases and filter out values that no longer has a matchig
  55. // option in the DOM.
  56. const needReset = el.multiple
  57. ? binding.value.some(v => hasNoMatchingOption(v, el.options))
  58. : hasNoMatchingOption(binding.value, el.options)
  59. if (needReset) {
  60. trigger(el, 'change')
  61. }
  62. }
  63. }
  64. }
  65. function setSelected (el, binding, vm) {
  66. const value = binding.value
  67. const isMultiple = el.multiple
  68. if (isMultiple && !Array.isArray(value)) {
  69. process.env.NODE_ENV !== 'production' && warn(
  70. `<select multiple v-model="${binding.expression}"> ` +
  71. `expects an Array value for its binding, but got ${
  72. Object.prototype.toString.call(value).slice(8, -1)
  73. }`,
  74. vm
  75. )
  76. return
  77. }
  78. let selected, option
  79. for (let i = 0, l = el.options.length; i < l; i++) {
  80. option = el.options[i]
  81. if (isMultiple) {
  82. selected = looseIndexOf(value, getValue(option)) > -1
  83. if (option.selected !== selected) {
  84. option.selected = selected
  85. }
  86. } else {
  87. if (looseEqual(getValue(option), value)) {
  88. if (el.selectedIndex !== i) {
  89. el.selectedIndex = i
  90. }
  91. return
  92. }
  93. }
  94. }
  95. if (!isMultiple) {
  96. el.selectedIndex = -1
  97. }
  98. }
  99. function hasNoMatchingOption (value, options) {
  100. for (let i = 0, l = options.length; i < l; i++) {
  101. if (looseEqual(getValue(options[i]), value)) {
  102. return false
  103. }
  104. }
  105. return true
  106. }
  107. function getValue (option) {
  108. return '_value' in option
  109. ? option._value
  110. : option.value
  111. }
  112. function onCompositionStart (e) {
  113. e.target.composing = true
  114. }
  115. function onCompositionEnd (e) {
  116. e.target.composing = false
  117. trigger(e.target, 'input')
  118. }
  119. function trigger (el, type) {
  120. const e = document.createEvent('HTMLEvents')
  121. e.initEvent(type, true, true)
  122. el.dispatchEvent(e)
  123. }