-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpureSelect.ts
More file actions
484 lines (408 loc) · 14.9 KB
/
Copy pathpureSelect.ts
File metadata and controls
484 lines (408 loc) · 14.9 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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
interface SelectOption {
id: string;
label: string;
value: any;
}
interface PureSelectQuery {
term: string
more?: boolean
meta?: any
}
interface FetchRemoteResponse {
enteties: SelectOption[];
query: PureSelectQuery
}
interface CreateButton {
text: string;
callback: () => void;
}
interface PureSelectSettings {
filter_placeholder?: string;
class?: string;
filtered?: string;
closeOnSelect?: boolean;
createButton?: CreateButton
filter_threshold?: number;
optionTemplate?: (options?: SelectOption)=> string;
resultTemplate?: (options?: SelectOption | string)=> string;
onSelect?: (options?: SelectOption[], dataIds?: string[]) => void;
onRemove?:(options?: SelectOption[], dataIds?: string[]) => void;
fetchRemote?: (query: PureSelectQuery, callback: Function) => Promise<FetchRemoteResponse>;
}
export class PureSelect {
public target: any = null;
public select: any = null;
public display: any = null;
public selected: any = null;
public list: any = null;
public value: any = null;
public isLarge: boolean = true;
public filter: any = null;
public query: any = {};
public options: any = [];
public multi: boolean = false;
public settings: PureSelectSettings = {};
public ul: any = null;
public selectOptions: SelectOption[] = [];
constructor(target: any, settings: any = {}, selectOptions: any = []) {
this.selected = {};
this.value = {};
this.selectOptions = selectOptions;
console.log('pure select');
this.init(target, settings);
}
private storeManager(li: HTMLLIElement) {
console.log('slected');
li.classList.add('selected');
if(this.multi) {
this.multiSelectedUpdate(li)
} else {
this.singleSelectedUpdate(li);
}
//On select event.
if(this.settings.onSelect) {
this.settings.onSelect(this.value, this.select);
}
}
getValById(id: string) {
return this.selectOptions.find((option: any)=> option && option.id == id);
}
private multiSelectedUpdate(li) {
let id = li.getAttribute('data-id'), value;
//Verify that not already set.
if(!this.value[id]) {
this.display.insertAdjacentHTML('beforeend', this.multiResultTemplate(li.innerHTML));
this.selected[id] = li;
value = this.getValById(id);
if(value) this.value[id] = value;
}
}
private singleSelectedUpdate(li) {
let id = li.getAttribute('data-id');
this.display.innerHTML = this.settings.resultTemplate(li.innerHTML);
this.value[0] = this.getValById(id);
this.selected[0] = li;
}
public setValues(labels: string[]) {
labels.forEach((label)=> {
if(this.options[label]) {
this.storeManager(this.options[label]);
}
});
}
//TODO check if working.
public removeValue(label: string) {
if(this.selected[label]) delete this.selected[label];
if(this.value[label]) delete this.value[label];
let target = this.display.querySelector(`[data-id="${label}"]`);
if(target) target.remove();
this.positionList();
}
/**
* init options and inject ul select to dom.
* @param target
* @param settings
*/
private init(target: any, settings: any) {
console.log('init');
switch(typeof target) {
case 'object':
break;
case 'string':
this.target = document.querySelector(`${target}`);
this.multi = this.target.multiple;
break;
}
this.settings = this.getSettings(settings);
this.buildSelect();
this.setOverflow();
this.target.parentNode.replaceChild(this.select, this.target);
this.target.style.display = 'none';
this.select.appendChild(this.target);
document.addEventListener('click', this.handleClickOff.bind(this));
this.positionList();
if(this.selectOptions.length == 0 && this.settings && this.settings.fetchRemote) {
this.handleFilterKeyup();
this.select.classList.add('large');
}
}
private buildSelect() {
console.log('buildSelect');
this.select = document.createElement('div');
this.select.classList.add(`select`);
this.select.classList.add(this.settings.class);
this.select.setAttribute('tabindex', this.target.tabIndex);
this.select.addEventListener('keydown', this.handleSelectKeydown.bind(this));
this.display = document.createElement('span');
this.display.classList.add('value');
this.display.addEventListener('click', this.handleDisplayClick.bind(this));
this.select.appendChild(this.display);
this.buildList();
};
private setOverflow() {
//TODO set logic for default value select. maybe remove---
// if(this.options.length) {
// let id = this.options[this.target.selectedIndex].getAttribute('data-id');
// this.value[id] = this.options[this.target.selectedIndex].getAttribute('data-value');
// this.selected[id] = this.options[this.target.selectedIndex];
// this.display.innerHTML = this.selected.innerHTML;
// }
//
// if((this.settings.filtered === 'auto' && this.options.length >= this.settings.filter_threshold) ||
// this.settings.filtered === true) {
// this.isLarge = true;
// this.select.classList.add('large');
// }
}
private createButton() {
if(this.settings && this.settings.createButton) {
let createBtn = document.createElement('div');
createBtn.classList.add('create-btn');
this.list.appendChild(createBtn);
}
}
private buildList() {
console.log('buildList');
this.list = document.createElement('div');
this.ul = document.createElement('ul');
this.list.classList.add('list');
this.list.setAttribute('tabindex', '-1');
this.buildFilter();
let self = this;
this.ul.addEventListener('scroll', self.throttle(self.handleScroll.bind(this), 1000));
this.ul.addEventListener('click', (e)=> this.handleOptionClick(e));
this.createButton();
this.list.appendChild(this.ul);
this.buildOptions(this.selectOptions);
this.select.appendChild(this.list);
};
public stringify(v) {
return typeof v === 'object' ? JSON.stringify(v) : v;
}
private checkForCustomTemplate(li: HTMLElement, option: SelectOption) {
if(this.settings && this.settings.optionTemplate) {
this.display.insertAdjacentHTML('beforeend', this.multiResultTemplate(li.innerHTML));
} else {
li.innerHTML = option.label;
}
}
private buildOptions(options: SelectOption[]) {
console.log('buildOptions');
let liFragment = document.createDocumentFragment();
let optionFragment = document.createDocumentFragment();
let optionsLen = options.length;
for(let i = 0; i < optionsLen; i++) {
let li = document.createElement('li');
let option = document.createElement('option');
//TODO stringify if object should create function to check and convert;
li.setAttribute('data-id', options[i].id);
this.checkForCustomTemplate(li, options[i]);
// In case of filter if li already selected we need to mark it as we re rendering the li's.
if(this.selected[options[i].id]) {
li.classList.add('selected');
}
option.setAttribute('value', this.stringify(options[i].value));
option.innerHTML = options[i].label;
liFragment.appendChild(li);
optionFragment.appendChild(option);
this.options[options[i].label] = li;
}
this.target.appendChild(optionFragment);
this.ul.appendChild(liFragment);
};
private handleRemove(target: any) {
let parent = target.parentNode;
let id = parent.getAttribute('data-id');
parent.remove();
if(this.selected[id]) {
this.selected[id].classList.remove('selected');
delete this.selected[id];
}
if(this.value[id]) delete this.value[id];
this.positionList();
if(this.settings && this.settings.onRemove) {
this.settings.onRemove(this.value, this.select);
}
}
private handleDisplayClick(e) {
e.preventDefault();
if(e.target && e.target.classList.contains('ps-chip-del')) {
this.handleRemove(e.target);
} else {
this.list.classList.add('open');
if(this.isLarge) {
this.filter.focus();
}
}
};
private positionList() {
console.log('positionList');
if(this.isLarge) {
this.list.style.top = this.display.offsetHeight + 'px';
}
};
private closeList() {
console.log('closeList');
this.list.classList.remove('open');
};
private toggleList() {
console.log('toggleList');
if(this.list.classList.contains('open')) {
this.list.classList.remove('open');
this.select.focus();
} else {
this.list.classList.add('open');
this.list.focus();
}
};
private buildFilter() {
console.log('buildFilter');
let wrapper = document.createElement('div');
wrapper.classList.add('filter');
this.filter = document.createElement('input');
this.filter.type = 'text';
this.filter.setAttribute('placeholder',this.settings.filter_placeholder);
this.filter.addEventListener('keyup', this.handleFilterKeyup.bind(this));
wrapper.appendChild(this.filter);
this.list.appendChild(wrapper);
};
private handleOptionClick(e) {
console.log('handleOptionClick');
if(e && e.target && e.target.nodeName && e.target.nodeName == 'LI') {
this.storeManager(e.target);
if(this.settings.closeOnSelect) this.closeList();
this.clearFilter();
setTimeout(this.positionList.bind(this), 200);
}
};
public activeSpinner() {
this.select.classList.add('loading');
}
public disableSpinner() {
this.select.classList.remove('loading');
}
//TODO duplicate validation.
private handleRemoteResponseOnFilter(response) {
this.selectOptions = response.entities;
this.query = Object.assign({}, this.query, response.query);
if(this.query.page) this.query.page = this.query.page++;
this.buildOptions(response.entities);
this.disableSpinner();
}
private handleRemoteResponseOnScroll(response) {
this.selectOptions = this.selectOptions.concat(response.entities);
this.query = Object.assign({}, this.query, response.query);
if(this.query.page) this.query.page = this.query.page++;
this.buildOptions(response.entities);
this.disableSpinner();
}
private initArrays() {
this.emptyNode(this.ul);
this.emptyNode(this.target);
this.selectOptions = [];
this.options = [];
}
DisableSelect() {
}
ActiveSelect() {
}
private handleFilterKeyup() {
console.log('handleFilterKeyup');
this.query.term = this.filter.value;
if(this.settings.fetchRemote) {
this.initArrays();
//TODO active spinner
this.activeSpinner();
this.settings.fetchRemote(this.query, this.handleRemoteResponseOnFilter.bind(this));
} else {
for(let k in this.options) {
if (this.options.hasOwnProperty(k)) {
if(this.options[k].innerHTML.substring(0, this.filter.value.length).toLowerCase() == this.filter.value.toLowerCase()) {
this.options[k].style.display = 'block';
} else {
this.options[k].style.display = 'none';
}
}
}
}
};
private clearFilter() {
console.log('clearFilter');
this.filter.value = '';
for(let k in this.options) {
if (this.options.hasOwnProperty(k)) {
this.options[k].style.display = 'block';
}
}
};
private handleClickOff(e) {
if(!this.select.contains(e.target) && !e.target.classList.contains('ps-chip-del')) {
this.closeList();
}
};
private handleScroll(e) {
console.log('handlescroll');
console.log(e.target.scrollHeight);
e.preventDefault();
if(((e.target.scrollTop + e.target.offsetHeight) >= (e.target.scrollHeight - 20))
&& this.settings.fetchRemote) {
this.activeSpinner();
this.settings.fetchRemote(this.query, this.handleRemoteResponseOnScroll.bind(this));
}
}
// EVENT HANDLERS
private handleSelectKeydown(e) {
console.log('handleSelectKeydown');
console.log(e);
if (this.select === document.activeElement && e.keyCode == 32) {
this.toggleList();
}
};
private multiResultTemplate(v: string) {
return `<div class="ps-chip-wrap" data-id="${v}">
<span class="ps-chip-label">${v}</span>
<span class="del-chip-btn ps-chip-del">x</span>
</div>`
}
public getSettings(settings: PureSelectSettings): PureSelectSettings {
console.log('getSettings');
let defaults: PureSelectSettings = {
resultTemplate: (v: string) => ` ${v} `,
fetchRemote: null,
filtered: 'auto',
class: 'default',
filter_threshold: 8,
filter_placeholder: 'Filter options...'
};
for(let p in settings) {
defaults[p] = settings[p];
}
return defaults;
}
public destroy() {
document.removeEventListener('click', this.handleClickOff.bind(this));
this.emptyNode(this.select);
this.select.remove();
}
public emptyNode(elem: HTMLElement) {
if(elem && elem.firstChild) {
while (elem.firstChild) {
elem.removeChild(elem.firstChild);
}
}
}
public throttle(func: any, wait: any, immediate = false) {
let timeout;
return function() {
let context = this, args = arguments;
let later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
}
}
}