|
| 1 | +/** |
| 2 | + * Not type checking this file because flow doesn't like attaching |
| 3 | + * properties to Elements. |
| 4 | + */ |
| 5 | + |
| 6 | +export const inBrowser = typeof window !== 'undefined' |
| 7 | +export const UA = inBrowser && window.navigator.userAgent.toLowerCase() |
| 8 | +export const isIE9 = UA && UA.indexOf('msie 9.0') > 0 |
| 9 | +function makeMap ( |
| 10 | + str, |
| 11 | + expectsLowerCase |
| 12 | +) { |
| 13 | + const map = Object.create(null) |
| 14 | + const list = str.split(',') |
| 15 | + for (let i = 0; i < list.length; i++) { |
| 16 | + map[list[i]] = true |
| 17 | + } |
| 18 | + return expectsLowerCase |
| 19 | + ? val => map[val.toLowerCase()] |
| 20 | + : val => map[val] |
| 21 | +} |
| 22 | +const isTextInputType = makeMap('text,number,password,search,email,tel,url') |
| 23 | + |
| 24 | +function onCompositionStart (e) { |
| 25 | + e.target.composing = true |
| 26 | +} |
| 27 | + |
| 28 | +function onCompositionEnd (e) { |
| 29 | + // prevent triggering an input event for no reason |
| 30 | + if (!e.target.composing) return |
| 31 | + e.target.composing = false |
| 32 | + trigger(e.target, 'input') |
| 33 | +} |
| 34 | + |
| 35 | +function trigger (el, type) { |
| 36 | + const e = document.createEvent('HTMLEvents') |
| 37 | + e.initEvent(type, true, true) |
| 38 | + el.dispatchEvent(e) |
| 39 | +} |
| 40 | + |
| 41 | +/* istanbul ignore if */ |
| 42 | +if (isIE9) { |
| 43 | + // http://www.matts411.com/post/internet-explorer-9-oninput/ |
| 44 | + document.addEventListener('selectionchange', () => { |
| 45 | + const el = document.activeElement |
| 46 | + if (el && el.vmodel) { |
| 47 | + trigger(el, 'input') |
| 48 | + } |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +export default { |
| 53 | + install: (Vue, options) => { |
| 54 | + Vue.directive('ant-input', { |
| 55 | + inserted (el, binding, vnode, oldVnode) { |
| 56 | + if (vnode.tag === 'textarea' || isTextInputType(el.type)) { |
| 57 | + if (!binding.modifiers || !binding.modifiers.lazy) { |
| 58 | + el.addEventListener('compositionstart', onCompositionStart) |
| 59 | + el.addEventListener('compositionend', onCompositionEnd) |
| 60 | + // Safari < 10.2 & UIWebView doesn't fire compositionend when |
| 61 | + // switching focus before confirming composition choice |
| 62 | + // this also fixes the issue where some browsers e.g. iOS Chrome |
| 63 | + // fires "change" instead of "input" on autocomplete. |
| 64 | + el.addEventListener('change', onCompositionEnd) |
| 65 | + /* istanbul ignore if */ |
| 66 | + if (isIE9) { |
| 67 | + el.vmodel = true |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + }) |
| 73 | + }, |
| 74 | +} |
0 commit comments