-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathx-select.js
280 lines (252 loc) · 6.83 KB
/
x-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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { on } from '@ember/object/evented';
import { once } from '@ember/runloop';
import Component from '@ember/component';
import { isArray, A } from '@ember/array';
import { computed, observer } from '@ember/object';
const isSelectedOption = option => option.element.selected;
/**
* Wraps a native <select> element so that it can be object and
* binding aware. It is used in conjuction with the
* `x-option` component to construct select boxes. E.g.
*
* {{#x-select value="bob" action="selectPerson"}}
* {{x-option value="fred"}}Fred Flintstone{{/x-option}}
* {{x-option value="bob"}}Bob Newhart{{/x-option}}
* {{/x-select}}
*
* the options are always up to date, so that when the object bound to
* `value` changes, the corresponding option becomes selected.
*
* Whenever the select tag receives a change event, it will fire
* `action`
*
* @class Ember.XSelectComponent
* @extends Ember.Component
*/
export default Component.extend({
tagName: 'select',
classNameBindings: [':x-select'],
attributeBindings: [
'disabled',
'tabindex',
'multiple',
'form',
'name',
'autofocus',
'required',
'size',
'title'
],
/**
* Bound to the `disabled` attribute on the native <select> tag.
*
* @property disabled
* @type Boolean
* @default false
*/
disabled: false,
/**
* Bound to the `multiple` attribute on the native <select> tag.
*
* @property multiple
* @type Boolean
* @default false
*/
multiple: false,
/**
* The collection of options for this select box. When options are
* rendered as a child from x-select, they will register themselves with their
* containing `x-select`. This is for internal book-keeping only and should
* not be changed from outside.
*
* @private
* @property options
*/
options: computed(function() {
return A([]);
}),
/**
* Bound to the `tabindex` attribute on the native <select> tag.
*
* @property tabindex
* @type Integer
* @default null
*/
tabindex: null,
/**
* Function for the `onBlur` action
*
* @property onBlur
* @type Function
*/
onBlur() {},
/**
* Function for the `onClick` action
*
* @property onClick
* @type Function
*/
onClick() {},
/**
* Function for the `onChange` action
*
* @property onChange
* @type Function
*/
onChange() {},
/**
* Function for the `onFocusOut` action
*
* @property onFocusOut
* @type Function
*/
onFocusOut() {},
/**
* Function that calls an action and sends the proper arguments.
*
* @method _handleAction
* @type Function
* @param {String} action - string name of the action to invoke
* @param {String|Object} value - current value of the component
* @param {Object} event - DOM event from the current action
*/
_handleAction(action, value, event) {
this.get(action)(value, event);
},
/**
* When the select DOM event fires on the element, trigger the
* component's action with the current value.
*/
change(event) {
this._handleAction('onChange', this._getValue(), event);
},
/**
* When the click DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the DOM event.
*/
click(event) {
this._handleAction('onClick', this._getValue(), event);
},
/**
* When the blur DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the DOM event.
*/
blur(event) {
this._handleAction('onBlur', this._getValue(), event);
},
/**
* When the focusOut DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the DOM event.
*/
focusOut(event) {
this._handleAction('onFocusOut', this._getValue(), event);
},
/**
* Reads the current selection from this select's options.
*
* If this is a multi-select, then the value will be an
* array. Otherwise, it will be a single value which could be null.
*
* @private
* @return {Array|Object} the current selection
*/
_getValue() {
return this.get('multiple') ? this._findMultipleValues() : this._findSingleValue();
},
/**
* Finds all selected values from all `x-option`
* children. Used when this.get('multiple') === true
*
* @private
* @return {Array} all the values from selected x-options
*/
_findMultipleValues() {
return this.get('options')
.filter(isSelectedOption)
.map(option => option.get('value'));
},
/**
* Returns the value of the first selected `x-option`.
* Used when `this.get('multiple') !== true`
*
* @private
* @return {Object} the value of the first select `x-option`, or null
*/
_findSingleValue() {
let selectedValue = this.get('options').find(isSelectedOption);
return selectedValue ? selectedValue.get('value') : null;
},
/**
* If no explicit value is set, apply default values based on selected=true in
* the template.
*
* @private
*/
_setDefaultValues() {
once(this, this.__setDefaultValues);
},
__setDefaultValues() {
let canSet = !this.isDestroying && !this.isDestroyed;
if (canSet && this.get('value') == null) {
// `onChange` is the default event we use
this._handleAction('onChange', this._getValue(), event);
}
},
/**
* @override
*/
didInsertElement() {
this._super.apply(this, arguments);
this.element.addEventListener('blur', event => this.blur(event));
},
/**
* @override
*/
willDestroyElement: function() {
this.element.removeEventListener('blur', this.blur);
this._super.apply(this, arguments);
},
/**
* If this is a multi-select, and the value is not an array, that
* probably indicates a misconfiguration somewhere, so we error out.
*
* @private
*/
/* eslint-disable */
ensureProperType: on(
'init',
observer('value', function() {
let value = this.get('value');
if (value != null && this.get('multiple') && !isArray(value)) {
throw new Error(`x-select multiple=true was set, but value ${value} is not enumerable.`);
}
})
),
/* eslint-enable */
actions: {
/**
* Registers a new option that is contained within x-select.
*
* This is called whenever an x-option component is inserted into the DOM.
*
* @param {<x-option>} option - x-option component.
* @private
*/
registerOption(option) {
this.get('options').push(option);
this._setDefaultValues();
},
/**
* Removes a the passed option that is contained within x-select.
*
* This is called whenever an x-option component is begining teardown.
*
* @param {<x-option>} option - x-option component.
* @private
*/
unregisterOption(option) {
this.get('options').removeObject(option);
this._setDefaultValues();
}
}
});