-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
373 lines (298 loc) · 10.6 KB
/
core.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import path from 'path';
import $ from 'jquery';
import KEYS from './keys';
import ModelAdapter from './data/model';
// subcomponents
import SingleSelection from './selection/single';
import MultipleSelection from './selection/multiple';
import { default as SelectionSearch } from './selection/search';
import { default as DropdownSearch } from './dropdown/search';
import Results from './results';
/* Derby Select2 Component.
Views:
"core" is the main view, parent of all others.
"multiple"/"single" is the selection view: it shows what has already been selected.
"results" is the contents of the dropdown and shows the selectable options.
"search" is used to filter the results (by dropdown and multiple)
Model:
input paths:
- data
for Select2 use:
- focus (bool)
- open (bool)
- results (array)
- selections (array)
output paths:
- value
Events:
- open, close, query, queryEnd, select, move, unselect, focus, blur, disable, enable
- opening, closing, selecting, unselecting
- results:select, results:toggle, results:previous, results:next, results:first, results:last
*/
export default class Select2
{
static view = {
is: 'select2',
file: path.join(__dirname, '/core'),
style: path.join(__dirname, '/index'),
dependencies: [
[ 'select2:single', SingleSelection ],
[ 'select2:multiple', MultipleSelection ],
[ 'select2:selection:search', SelectionSearch ],
[ 'select2:dropdown:search', DropdownSearch ],
[ 'select2:results', Results ]
]
}
// TODO: put global defaults somewhere, accessible, changeable
init(model) {
this.options = model.at("options");
// state is kept in the model: focus, open, highlighted
model.setNull("focus", false);
model.setNull("open", false);
// enabled/disabled is kept in the options
this.options.setNull("disabled", false);
// default settings
this.options.setNull("tabindex", 0);
this.options.setNull("multiple", false);
this.options.setNull("duplicates", false);
// store original tabindex
this.options.set("_tabindex", this.options.get("tabindex"));
if (this.options.get("disabled")) {
this.options.set("tabindex", -1);
}
// theme
this.options.setNull("theme", "default");
// default dataAdapter is ModelAdapter
this.options.setNull("dataAdapter", ModelAdapter);
// default view names (and thus default components) TODO: rename to selectionView, selectionItemView, resultsView....
this.options.setNull("selectionAdapter", this.options.get("multiple") ? "multiple" : "single");
this.options.setNull("selectionTemplate", "selection-template");
this.options.setNull("resultsAdapter", "results");
this.options.setNull("resultsTemplate", "results-template");
// instantiate data adapter
this.dataAdapter = new (this.options.get('dataAdapter'))(this, this.options);
}
create(model, dom) {
// attach the select2 controller to the container to be able to identify it later and close
// all the other dropdowns; selection/base uses it
// TODO: is there a better way? Derby global events or so?
$(this.container).data('controller', this);
// Register any internal event handlers first (because e.g. on open we need to create the results view before
// being able to register results event handlers)
this._registerEvents();
this._registerDataEvents();
this._registerSelectionEvents();
// Bind the container to all of the adapters
this._bindAdapters();
}
_bindAdapters() {
this.selection.bind(this);
this.dataAdapter.bind(this); // bind last because it can emit queryEnd immediately
}
_registerDataEvents() {
const relayEvents = ['queryEnd'];
relayEvents.forEach(evt => {
this.dataAdapter.on(evt, params => {
this.emit(evt, params);
});
});
}
_registerSelectionEvents() {
const relayEvents = ['move', 'unselect'];
// register toggle
this.selection.on('toggle', () => {
this.toggleDropdown();
});
this.selection.on('query', params => {
this.emit('open', {});
this.emit('query', params);
});
// relay the rest
relayEvents.forEach(evt => {
this.selection.on(evt, params => {
this.emit(evt, params);
});
});
}
// forward and emit results events as if from Select2
_registerResultsEvents() {
// TOOD: can also emit query/queryEnd with infiniteScroll - not implemented yet
const relayEvents = ['select', 'unselect', 'query', 'close'];
relayEvents.forEach(evt => {
this.results.on(evt, params => {
this.emit(evt, params);
});
});
}
_registerEvents() {
this.on('opening', evt => {
if (this.isOpen())
evt.prevented = true;
});
this.on('closing', evt => {
if (!this.isOpen())
evt.prevented = true;
});
this.on('open', () => {
this.model.set('open', true);
this._registerResultsEvents();
});
this.on('close', () => {
this.model.set('open', false);
});
this.model.on('change', 'focus', (value, prev) => {
if (value === prev) return;
if (value) {
this.emit('focus', {});
} else {
this.emit('blur', {});
}
});
this.options.on('change', 'disabled', (value, prev) => {
if (value === prev) return;
if (value) {
this.close();
this.model.set("focus", false);
this.options.set("tabindex", -1);
this.emit('disable', {});
} else {
this.options.set("tabindex", this.options.set("_tabindex"));
this.emit('enable', {});
// if we have focus already, notify everything
if (this.container == document.activeElement || $.contains(this.container, document.activeElement))
this.focus();
}
});
/* focus/blur events */
this.container.addEventListener('focus', evt => {
this.focus();
}, true); // capturing phase, focus doesn't bubble
this.container.addEventListener('blur', evt => {
this.model.set("focus", false);
}, true); // capturing phase, blur doesn't bubble
this.container.addEventListener('mousedown', evt => {
// we don't blur if mousedown is prevented, and it is prevented if mousedown happens in the container
evt.preventDefault();
this.focus();
});
/* keyboard events */
// handle keydown events that bubbled up because no one stopped and used them
this.container.addEventListener('keydown', evt => {
if (this.options.get('disabled')) {
return;
}
const key = evt.which;
if (this.isOpen()) {
if (key === KEYS.ESC || (key === KEYS.UP && evt.altKey))
{
this.close();
evt.preventDefault();
evt.stopPropagation();
}
else if (key === KEYS.TAB)
{
this.close();
}
else if (key === KEYS.ENTER)
{
this.emit('results:select', {});
evt.preventDefault();
}
else if ((key === KEYS.SPACE && evt.ctrlKey))
{
this.emit('results:toggle', {});
evt.preventDefault();
}
else if (key === KEYS.HOME)
{
this.emit('results:first', {});
evt.preventDefault();
}
else if (key === KEYS.UP)
{
this.emit('results:previous', {});
evt.preventDefault();
}
else if (key === KEYS.DOWN)
{
this.emit('results:next', {});
evt.preventDefault();
}
else if (key === KEYS.END)
{
this.emit('results:last', {});
evt.preventDefault();
}
} else {
if (key === KEYS.ENTER || key === KEYS.SPACE || (key === KEYS.DOWN && evt.altKey))
{
this.open();
evt.preventDefault();
}
}
});
}
/**
* Override the emit method to automatically emit pre-events for events that can be prevented, e.g.:
* this.on('opening', function(evt) {
* evt.prevented = true;
* });
*/
emit(name, args) {
if (this.options && this.options.get('disabled') && name !== 'enable' && name !== 'disable')
return;
const actualEmit = this.__proto__.__proto__.emit;
const preEmitMap = {
'open': 'opening',
'close': 'closing',
'select': 'selecting',
'unselect': 'unselecting',
'clear': 'clearing'
};
if (args === undefined) {
args = {};
}
if (name in preEmitMap) {
const preEmitName = preEmitMap[name];
const preEmitArgs = {
prevented: false,
name,
args
};
actualEmit.call(this, preEmitName, preEmitArgs);
if (preEmitArgs.prevented) {
args.prevented = true;
return;
}
}
actualEmit.call(this, name, args);
}
toggleDropdown() {
if (this.isOpen()) {
this.close();
} else {
this.open();
}
}
open() {
this.emit('open', {});
if (this.isOpen())
this.emit('query', {});
}
close() {
this.emit('close', {});
}
isOpen() {
return this.model.get('open');
}
hasFocus() {
return this.model.get('focus');
}
focus(evt) {
if (this.options.get('disabled')) {
return;
}
this.model.set("focus", true);
}
}
export { Select2 };