-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathmodel.ts
More file actions
153 lines (143 loc) · 4.42 KB
/
Copy pathmodel.ts
File metadata and controls
153 lines (143 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { isArray, looseEqual, looseIndexOf, toNumber } from '@vue/shared'
import { Directive } from '.'
import { listen } from '../utils'
export const model: Directive<
HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
> = ({ el, exp, get, effect, modifiers }) => {
const type = el.type
const assign = get(`(val) => { ${exp} = val }`)
const { trim, number = type === 'number' || type === 'range' } = modifiers || {}
if (el.tagName === 'SELECT') {
const sel = el as HTMLSelectElement
listen(el, 'change', () => {
const selectedVal = Array.prototype.filter
.call(sel.options, (o: HTMLOptionElement) => o.selected)
.map((o: HTMLOptionElement) =>
number ? toNumber(getValue(o)) : getValue(o)
)
assign(sel.multiple ? selectedVal : selectedVal[0])
})
effect(() => {
const value = get()
const isMultiple = sel.multiple
for (let i = 0, l = sel.options.length; i < l; i++) {
const option = sel.options[i]
const optionValue = getValue(option)
if (isMultiple) {
if (isArray(value)) {
option.selected = looseIndexOf(value, optionValue) > -1
} else {
option.selected = value.has(optionValue)
}
} else {
if (looseEqual(getValue(option), value)) {
if (sel.selectedIndex !== i) sel.selectedIndex = i
return
}
}
}
if (!isMultiple && sel.selectedIndex !== -1) {
sel.selectedIndex = -1
}
})
} else if (type === 'checkbox') {
listen(el, 'change', () => {
const modelValue = get()
const checked = (el as HTMLInputElement).checked
if (isArray(modelValue)) {
const elementValue = getValue(el)
const index = looseIndexOf(modelValue, elementValue)
const found = index !== -1
if (checked && !found) {
assign(modelValue.concat(elementValue))
} else if (!checked && found) {
const filtered = [...modelValue]
filtered.splice(index, 1)
assign(filtered)
}
} else {
assign(getCheckboxValue(el as HTMLInputElement, checked))
}
})
let oldValue: any
effect(() => {
const value = get()
if (isArray(value)) {
;(el as HTMLInputElement).checked =
looseIndexOf(value, getValue(el)) > -1
} else if (value !== oldValue) {
;(el as HTMLInputElement).checked = looseEqual(
value,
getCheckboxValue(el as HTMLInputElement, true)
)
}
oldValue = value
})
} else if (type === 'radio') {
listen(el, 'change', () => {
assign(getValue(el))
})
let oldValue: any
effect(() => {
const value = get()
if (value !== oldValue) {
;(el as HTMLInputElement).checked = looseEqual(value, getValue(el))
}
})
} else {
// text-like
const resolveValue = (val: string) => {
if (trim) return val.trim()
if (number) return toNumber(val)
return val
}
listen(el, 'compositionstart', onCompositionStart)
listen(el, 'compositionend', onCompositionEnd)
listen(el, modifiers?.lazy ? 'change' : 'input', () => {
if ((el as any).composing) return
assign(resolveValue(el.value))
})
if (trim) {
listen(el, 'change', () => {
el.value = el.value.trim()
})
}
effect(() => {
if ((el as any).composing) {
return
}
const curVal = el.value
const newVal = get()
if (document.activeElement === el && resolveValue(curVal) === newVal) {
return
}
if (curVal !== newVal) {
el.value = newVal
}
})
}
}
const getValue = (el: any) => ('_value' in el ? el._value : el.value)
// retrieve raw value for true-value and false-value set via :true-value or :false-value bindings
const getCheckboxValue = (
el: HTMLInputElement & { _trueValue?: any; _falseValue?: any },
checked: boolean
) => {
const key = checked ? '_trueValue' : '_falseValue'
return key in el ? el[key] : checked
}
const onCompositionStart = (e: Event) => {
;(e.target as any).composing = true
}
const onCompositionEnd = (e: Event) => {
const target = e.target as any
if (target.composing) {
target.composing = false
trigger(target, 'input')
}
}
const trigger = (el: HTMLElement, type: string) => {
const e = document.createEvent('HTMLEvents')
e.initEvent(type, true, true)
el.dispatchEvent(e)
}