forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.js
199 lines (182 loc) · 4.59 KB
/
select.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var _ = require('../../util')
var Watcher = require('../../watcher')
var dirParser = require('../../parsers/directive')
module.exports = {
bind: function () {
var self = this
var el = this.el
// update DOM using latest value.
this.forceUpdate = function () {
if (self._watcher) {
self.update(self._watcher.get())
}
}
// check options param
var optionsParam = this._checkParam('options')
if (optionsParam) {
initOptions.call(this, optionsParam)
}
this.number = this._checkParam('number') != null
this.multiple = el.hasAttribute('multiple')
this.listener = function () {
var value = self.multiple
? getMultiValue(el)
: el.value
value = self.number
? _.isArray(value)
? value.map(_.toNumber)
: _.toNumber(value)
: value
self.set(value)
}
_.on(el, 'change', this.listener)
checkInitialValue.call(this)
// All major browsers except Firefox resets
// selectedIndex with value -1 to 0 when the element
// is appended to a new parent, therefore we have to
// force a DOM update whenever that happens...
this.vm.$on('hook:attached', this.forceUpdate)
},
update: function (value) {
var el = this.el
el.selectedIndex = -1
var multi = this.multiple && _.isArray(value)
var options = el.options
var i = options.length
var option
while (i--) {
option = options[i]
/* eslint-disable eqeqeq */
option.selected = multi
? indexOf(value, option.value) > -1
: value == option.value
/* eslint-enable eqeqeq */
}
},
unbind: function () {
_.off(this.el, 'change', this.listener)
this.vm.$off('hook:attached', this.forceUpdate)
if (this.optionWatcher) {
this.optionWatcher.teardown()
}
}
}
/**
* Initialize the option list from the param.
*
* @param {String} expression
*/
function initOptions (expression) {
var self = this
var descriptor = dirParser.parse(expression)[0]
function optionUpdateWatcher (value) {
if (_.isArray(value)) {
self.el.innerHTML = ''
buildOptions(self.el, value)
self.forceUpdate()
} else {
process.env.NODE_ENV !== 'production' && _.warn(
'Invalid options value for v-model: ' + value
)
}
}
this.optionWatcher = new Watcher(
this.vm,
descriptor.expression,
optionUpdateWatcher,
{
deep: true,
filters: descriptor.filters
}
)
// update with initial value
optionUpdateWatcher(this.optionWatcher.value)
}
/**
* Build up option elements. IE9 doesn't create options
* when setting innerHTML on <select> elements, so we have
* to use DOM API here.
*
* @param {Element} parent - a <select> or an <optgroup>
* @param {Array} options
*/
function buildOptions (parent, options) {
var op, el
for (var i = 0, l = options.length; i < l; i++) {
op = options[i]
if (!op.options) {
el = document.createElement('option')
if (typeof op === 'string') {
el.text = el.value = op
} else {
if (op.value != null) {
el.value = op.value
}
el.text = op.text || op.value || ''
if (op.disabled) {
el.disabled = true
}
}
} else {
el = document.createElement('optgroup')
el.label = op.label
buildOptions(el, op.options)
}
parent.appendChild(el)
}
}
/**
* Check the initial value for selected options.
*/
function checkInitialValue () {
var initValue
var options = this.el.options
for (var i = 0, l = options.length; i < l; i++) {
if (options[i].hasAttribute('selected')) {
if (this.multiple) {
(initValue || (initValue = []))
.push(options[i].value)
} else {
initValue = options[i].value
}
}
}
if (typeof initValue !== 'undefined') {
this._initValue = this.number
? _.toNumber(initValue)
: initValue
}
}
/**
* Helper to extract a value array for select[multiple]
*
* @param {SelectElement} el
* @return {Array}
*/
function getMultiValue (el) {
return Array.prototype.filter
.call(el.options, filterSelected)
.map(getOptionValue)
}
function filterSelected (op) {
return op.selected
}
function getOptionValue (op) {
return op.value || op.text
}
/**
* Native Array.indexOf uses strict equal, but in this
* case we need to match string/numbers with soft equal.
*
* @param {Array} arr
* @param {*} val
*/
function indexOf (arr, val) {
var i = arr.length
while (i--) {
/* eslint-disable eqeqeq */
if (arr[i] == val) return i
/* eslint-enable eqeqeq */
}
return -1
}