simp-select 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +3 -0
- package/.browserslistrc +3 -0
- package/.eslintignore +5 -0
- package/.eslintrc +29 -0
- package/README.md +107 -0
- package/dist/const.ts +42 -0
- package/dist/demo/index.html +63 -63
- package/dist/simpleSelect.d.ts +1 -0
- package/dist/simpleSelect.js +1 -1
- package/dist/simpleSelectItem.d.ts +7 -14
- package/dist/style.css +1 -1
- package/package.json +1 -6
- package/src/const/simpleSelection.const.ts +2 -3
- package/src/demo/index.html +263 -0
- package/src/polyfill.js +136 -0
- package/src/simpleSelect.ts +155 -0
- package/src/simpleSelectItem.ts +515 -0
- package/src/simpleSelectItemDOM.ts +623 -0
- package/src/style.css +416 -0
- package/src/utils/simpleSelection.utils.ts +74 -0
- package/src/utils/store.ts +60 -0
- package/tsconfig.json +16 -0
- package/webpack.config.js +74 -0
@@ -0,0 +1,623 @@
|
|
1
|
+
import { IItemLocalOptions, ISimpleSelectOptions } from './types/simpleSelect.types';
|
2
|
+
import {
|
3
|
+
cloneObj, createButton, getClass, ifTrueDataAttr, toCamelCase,
|
4
|
+
} from './utils/simpleSelection.utils';
|
5
|
+
import { ICreateLiReturn, IOptionItem, IOptionItems } from './types/item.types';
|
6
|
+
import { store } from './utils/store';
|
7
|
+
import { initClass } from './const/simpleSelection.const';
|
8
|
+
|
9
|
+
interface IState {
|
10
|
+
items: IOptionItems[];
|
11
|
+
isOpen: boolean;
|
12
|
+
isFloat: boolean;
|
13
|
+
filterStr: string;
|
14
|
+
}
|
15
|
+
export class SimpleSelectItemDOM {
|
16
|
+
options!: ISimpleSelectOptions;
|
17
|
+
|
18
|
+
$select!:HTMLSelectElement;
|
19
|
+
|
20
|
+
id!: string;
|
21
|
+
|
22
|
+
titlePlaceholder!: string;
|
23
|
+
|
24
|
+
isDisabled: boolean = false;
|
25
|
+
|
26
|
+
isMulti!: boolean;
|
27
|
+
|
28
|
+
state = store<IState>({
|
29
|
+
items: [],
|
30
|
+
isOpen: false,
|
31
|
+
filterStr: '',
|
32
|
+
isFloat: false,
|
33
|
+
});
|
34
|
+
|
35
|
+
// classSelectInit= 'SimpleSel__select_init'
|
36
|
+
classSelectInit = getClass('select_init');
|
37
|
+
|
38
|
+
isNative: boolean;
|
39
|
+
|
40
|
+
elemWrap: HTMLDivElement = document.createElement('div'); // all
|
41
|
+
|
42
|
+
elemTop: HTMLDivElement = document.createElement('div'); // all
|
43
|
+
|
44
|
+
elemTopBody: HTMLDivElement = document.createElement('div'); // all
|
45
|
+
|
46
|
+
elemDropDown: HTMLDivElement | null = null; // not native
|
47
|
+
|
48
|
+
elemDropDownClose: HTMLButtonElement | null = null; // not native
|
49
|
+
|
50
|
+
elemListBody: HTMLUListElement | null = null; // not native
|
51
|
+
|
52
|
+
elemInputSearch: HTMLInputElement | null = null; // not native
|
53
|
+
|
54
|
+
elemTitle!: HTMLDivElement; // not native
|
55
|
+
|
56
|
+
confirmOk: HTMLButtonElement | null = null; // not native
|
57
|
+
|
58
|
+
confirmNo: HTMLButtonElement | null = null; // not native
|
59
|
+
|
60
|
+
elemControl: HTMLDivElement | null = null; // not native
|
61
|
+
|
62
|
+
elemSelectAll: HTMLButtonElement | null = null; // not native
|
63
|
+
|
64
|
+
elemResetAll: HTMLButtonElement | null = null; // not native
|
65
|
+
|
66
|
+
cloneClasses = '';
|
67
|
+
|
68
|
+
isShowCheckbox = false;
|
69
|
+
|
70
|
+
bodyLiHTMLBeforeFromSelect: null | string = null;
|
71
|
+
|
72
|
+
bodyLiHTMLAfterFromSelect: null | string = null;
|
73
|
+
|
74
|
+
isFloatWidth = false;
|
75
|
+
|
76
|
+
bodyOpenClass = `${initClass}--body_open`;
|
77
|
+
|
78
|
+
constructor(select: HTMLSelectElement, options: ISimpleSelectOptions, localOptions: IItemLocalOptions) {
|
79
|
+
const { id, isNative } = localOptions;
|
80
|
+
this.$select = select;
|
81
|
+
this.isMulti = select.multiple;
|
82
|
+
this.id = id;
|
83
|
+
this.isNative = isNative;
|
84
|
+
|
85
|
+
this.options = cloneObj(options);
|
86
|
+
if (this.options.isCloneClass) {
|
87
|
+
this.cloneClasses = this.$select.className;
|
88
|
+
}
|
89
|
+
if (options.callbackInitialization) {
|
90
|
+
this.options.callbackInitialization = options.callbackInitialization;
|
91
|
+
}
|
92
|
+
if (options.callbackInitialized) {
|
93
|
+
this.options.callbackInitialized = options.callbackInitialized;
|
94
|
+
}
|
95
|
+
if (options.callbackOpen) {
|
96
|
+
this.options.callbackOpen = options.callbackOpen;
|
97
|
+
}
|
98
|
+
if (options.callbackClose) {
|
99
|
+
this.options.callbackClose = options.callbackClose;
|
100
|
+
}
|
101
|
+
if (options.callbackDestroyInit) {
|
102
|
+
this.options.callbackDestroyInit = options.callbackDestroyInit;
|
103
|
+
}
|
104
|
+
if (options.callbackDestroy) {
|
105
|
+
this.options.callbackDestroy = options.callbackDestroy;
|
106
|
+
}
|
107
|
+
if (options.callbackChangeSelect) {
|
108
|
+
this.options.callbackChangeSelect = options.callbackChangeSelect;
|
109
|
+
}
|
110
|
+
if (options.changeBodyLi) {
|
111
|
+
this.options.changeBodyLi = options.changeBodyLi;
|
112
|
+
}
|
113
|
+
|
114
|
+
if (this.isMulti && this.$select.hasAttribute('data-simple-is-confirm')) {
|
115
|
+
this.options.isConfirmInMulti = ifTrueDataAttr(this.$select.getAttribute('data-simple-is-confirm'));
|
116
|
+
}
|
117
|
+
|
118
|
+
this.optionOverride();
|
119
|
+
|
120
|
+
this.isDisabled = this.$select.disabled;
|
121
|
+
|
122
|
+
// this.initDom()
|
123
|
+
}
|
124
|
+
|
125
|
+
optionOverride() {
|
126
|
+
const dataPlaceholder = toCamelCase('simple-placeholder');
|
127
|
+
if (this.$select.dataset[dataPlaceholder]) {
|
128
|
+
this.titlePlaceholder = this.$select.dataset[dataPlaceholder] || '';
|
129
|
+
} else {
|
130
|
+
this.titlePlaceholder = this.options.locale.title;
|
131
|
+
}
|
132
|
+
|
133
|
+
const dataResetAll = toCamelCase('simple-reset-all');
|
134
|
+
if (dataResetAll in this.$select.dataset) {
|
135
|
+
const resReset = this.$select.dataset[dataResetAll];
|
136
|
+
this.options.resetAll = !(resReset === 'false' || resReset === '0');
|
137
|
+
}
|
138
|
+
// const dataSelect = toCamelCase('simple-select-all');
|
139
|
+
// if (dataSelect in this.$select.dataset) {
|
140
|
+
// const resSelect = this.$select.dataset[dataSelect];
|
141
|
+
// this.options.selectAll = !(resSelect === 'false' || resSelect === '0');
|
142
|
+
// }
|
143
|
+
if (this.$select.hasAttribute('data-simple-select-all')) {
|
144
|
+
const resSelect = this.$select.getAttribute('data-simple-select-all');
|
145
|
+
this.options.selectAll = ifTrueDataAttr(resSelect);
|
146
|
+
}
|
147
|
+
|
148
|
+
const isShowCheckboxLocal = this.$select.dataset[toCamelCase('simple-show-checkbox')];
|
149
|
+
if (this.isMulti) {
|
150
|
+
this.isShowCheckbox = !((isShowCheckboxLocal && !ifTrueDataAttr(isShowCheckboxLocal)));
|
151
|
+
} else if (isShowCheckboxLocal === 'true') {
|
152
|
+
this.isShowCheckbox = true;
|
153
|
+
}
|
154
|
+
|
155
|
+
const itemHtmlBefore = this.$select.dataset[toCamelCase('simple-item-html-before')];
|
156
|
+
if (itemHtmlBefore) {
|
157
|
+
this.bodyLiHTMLBeforeFromSelect = itemHtmlBefore;
|
158
|
+
}
|
159
|
+
const itemHtmlAfter = this.$select.dataset[toCamelCase('simple-item-html-after')];
|
160
|
+
if (itemHtmlAfter) {
|
161
|
+
this.bodyLiHTMLAfterFromSelect = itemHtmlAfter;
|
162
|
+
}
|
163
|
+
|
164
|
+
if (this.$select.hasAttribute('data-simple-up')) {
|
165
|
+
this.options.isUp = ifTrueDataAttr(this.$select.getAttribute('data-simple-up'));
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
initDom() {
|
170
|
+
this.createList(false);
|
171
|
+
this.createHTML();
|
172
|
+
|
173
|
+
this.state.subscribe('items', (_val: IOptionItems[]) => {
|
174
|
+
this.createListHTML();
|
175
|
+
this.createTitleHTML();
|
176
|
+
});
|
177
|
+
this.state.subscribe('isOpen', (val: boolean) => {
|
178
|
+
this.createListHTML();
|
179
|
+
this.createTitleHTML();
|
180
|
+
|
181
|
+
this.toggleTabIndex(val);
|
182
|
+
});
|
183
|
+
this.state.subscribe('isFloat', (val: boolean) => {
|
184
|
+
this.isFloatWidth = val;
|
185
|
+
const cls = getClass('float', true);
|
186
|
+
if (val) {
|
187
|
+
this.elemWrap.classList.add(cls);
|
188
|
+
} else {
|
189
|
+
this.elemWrap.classList.remove(cls);
|
190
|
+
}
|
191
|
+
});
|
192
|
+
}
|
193
|
+
|
194
|
+
toggleTabIndex(isOpen: boolean) {
|
195
|
+
const tabIndex = isOpen ? 0 : -1;
|
196
|
+
|
197
|
+
if (this.state.getState('isFloat')) {
|
198
|
+
if (isOpen) {
|
199
|
+
document.body.classList.add(this.bodyOpenClass);
|
200
|
+
} else {
|
201
|
+
document.body.classList.remove(this.bodyOpenClass);
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
if (this.elemInputSearch) {
|
206
|
+
this.elemInputSearch.tabIndex = tabIndex;
|
207
|
+
}
|
208
|
+
if (this.elemResetAll) {
|
209
|
+
this.elemResetAll.tabIndex = tabIndex;
|
210
|
+
}
|
211
|
+
if (this.elemSelectAll) {
|
212
|
+
this.elemSelectAll.tabIndex = tabIndex;
|
213
|
+
}
|
214
|
+
if (this.confirmOk) {
|
215
|
+
this.confirmOk.tabIndex = tabIndex;
|
216
|
+
}
|
217
|
+
if (this.confirmNo) {
|
218
|
+
this.confirmNo.tabIndex = tabIndex;
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
public updateHTML() {
|
223
|
+
this.createList(true);
|
224
|
+
}
|
225
|
+
|
226
|
+
private createHTML() {
|
227
|
+
this.$select.classList.add(this.classSelectInit);
|
228
|
+
this.$select.tabIndex = -1;
|
229
|
+
|
230
|
+
this.elemTopBody.className = getClass('top_body');
|
231
|
+
this.elemTopBody.tabIndex = this.isDisabled ? -1 : 0;
|
232
|
+
|
233
|
+
this.createIcon();
|
234
|
+
this.elemTop.appendChild(this.elemTopBody);
|
235
|
+
let resClassesWrap = initClass;
|
236
|
+
if (this.options.isCloneClass) {
|
237
|
+
resClassesWrap += ` ${this.cloneClasses}`;
|
238
|
+
}
|
239
|
+
if (this.$select.hasAttribute('data-simple-add-classes')) {
|
240
|
+
resClassesWrap += ` ${this.$select.getAttribute('data-simple-add-classes')}`;
|
241
|
+
}
|
242
|
+
if (this.isDisabled) {
|
243
|
+
resClassesWrap += ` ${getClass('disabled', true)}`;
|
244
|
+
}
|
245
|
+
if (this.options.isUp) {
|
246
|
+
resClassesWrap += ` ${getClass('up', true)}`;
|
247
|
+
}
|
248
|
+
resClassesWrap += ` ${this.isMulti ? getClass('multi', true) : getClass('single', true)}`;
|
249
|
+
this.elemWrap.className = resClassesWrap;
|
250
|
+
this.elemWrap.dataset.countAll = this.$select.options.length.toString();
|
251
|
+
|
252
|
+
this.elemTop.className = getClass('top');
|
253
|
+
|
254
|
+
// creating an initial structure
|
255
|
+
const parentElement = this.$select.parentNode;
|
256
|
+
if (parentElement) {
|
257
|
+
parentElement.replaceChild(this.elemWrap, this.$select);
|
258
|
+
this.elemWrap.appendChild(this.$select);
|
259
|
+
}
|
260
|
+
this.elemWrap.appendChild(this.elemTop);
|
261
|
+
|
262
|
+
if (this.isNative) {
|
263
|
+
this.$select.classList.add(getClass('native', true, this.classSelectInit));
|
264
|
+
this.elemWrap.classList.add(getClass('native', true));
|
265
|
+
} else {
|
266
|
+
this.createDropDown();
|
267
|
+
|
268
|
+
this.createControlHTML();
|
269
|
+
|
270
|
+
this.createInputHTML();
|
271
|
+
}
|
272
|
+
|
273
|
+
this.createTitleHTML();
|
274
|
+
}
|
275
|
+
|
276
|
+
private createControlHTML() {
|
277
|
+
if (!this.elemDropDown || !this.isMulti) {
|
278
|
+
return;
|
279
|
+
}
|
280
|
+
if (!this.options.selectAll && !this.options.resetAll) {
|
281
|
+
return;
|
282
|
+
}
|
283
|
+
this.elemControl = document.createElement('div');
|
284
|
+
this.elemControl.classList.add(getClass('controls'));
|
285
|
+
|
286
|
+
// this.elemDropDown.prepend(this.elemControl);
|
287
|
+
this.elemDropDown.insertBefore(this.elemControl, this.elemDropDown.childNodes[0]);
|
288
|
+
|
289
|
+
const classControl = getClass('control');
|
290
|
+
if (this.options.selectAll) {
|
291
|
+
this.elemSelectAll = createButton();
|
292
|
+
this.elemSelectAll.className = `${classControl} ${getClass('select_all', true, classControl)}`;
|
293
|
+
|
294
|
+
this.elemSelectAll.innerHTML = `<span class="${getClass('select_all__icon')}"></span> ${this.options.locale.selectAll}`;
|
295
|
+
|
296
|
+
this.elemControl.appendChild(this.elemSelectAll);
|
297
|
+
}
|
298
|
+
|
299
|
+
if (this.options.resetAll) {
|
300
|
+
this.elemResetAll = createButton();
|
301
|
+
this.elemResetAll.className = `${classControl} ${getClass('reset_all', true, classControl)}`;
|
302
|
+
|
303
|
+
this.elemResetAll.innerHTML = `<span class="${getClass('reset_all__icon')}"></span> ${this.options.locale.resetAll}`;
|
304
|
+
|
305
|
+
this.elemControl.appendChild(this.elemResetAll);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
private createIcon() {
|
310
|
+
const icon = document.createElement('span');
|
311
|
+
icon.className = getClass('icon');
|
312
|
+
this.elemTopBody.appendChild(icon);
|
313
|
+
}
|
314
|
+
|
315
|
+
private createDropDown() {
|
316
|
+
if (this.isNative) {
|
317
|
+
return;
|
318
|
+
}
|
319
|
+
this.elemDropDown = document.createElement('div');
|
320
|
+
this.elemDropDown.className = getClass('body');
|
321
|
+
this.elemListBody = document.createElement('ul');
|
322
|
+
|
323
|
+
this.elemListBody.className = getClass('list');
|
324
|
+
|
325
|
+
this.elemWrap.appendChild(this.elemDropDown);
|
326
|
+
this.elemDropDown.appendChild(this.elemListBody);
|
327
|
+
|
328
|
+
this.elemDropDownClose = createButton();
|
329
|
+
this.elemDropDownClose.classList.add(getClass('close'));
|
330
|
+
|
331
|
+
this.elemDropDown.appendChild(this.elemDropDownClose);
|
332
|
+
|
333
|
+
if (this.isMulti) {
|
334
|
+
this.createIsConfirmInMultiHTML();
|
335
|
+
}
|
336
|
+
this.handlerChangeChecked();
|
337
|
+
}
|
338
|
+
|
339
|
+
private createIsConfirmInMultiHTML() {
|
340
|
+
const confirm = document.createElement('div');
|
341
|
+
|
342
|
+
const classesItem = getClass('bottom_control');
|
343
|
+
this.confirmOk = createButton();
|
344
|
+
this.confirmNo = createButton();
|
345
|
+
confirm.appendChild(this.confirmOk);
|
346
|
+
confirm.appendChild(this.confirmNo);
|
347
|
+
|
348
|
+
this.confirmOk.innerHTML = this.options.locale.ok;
|
349
|
+
this.confirmNo.innerHTML = this.options.locale.cansel;
|
350
|
+
|
351
|
+
this.confirmOk.className = `${classesItem} ${getClass('ok', true, classesItem)}`;
|
352
|
+
this.confirmNo.className = `${classesItem} ${getClass('no', true, classesItem)}`;
|
353
|
+
|
354
|
+
let classes = getClass('bottom_controls');
|
355
|
+
if (!this.options.isConfirmInMulti) {
|
356
|
+
classes += ` ${getClass('hide', true, classes)}`;
|
357
|
+
}
|
358
|
+
confirm.className = classes;
|
359
|
+
|
360
|
+
this.elemDropDown?.appendChild(confirm);
|
361
|
+
}
|
362
|
+
|
363
|
+
private createTitleHTML() {
|
364
|
+
if (!this.elemTitle) {
|
365
|
+
this.elemTitle = document.createElement('div');
|
366
|
+
this.elemTitle.className = getClass('title');
|
367
|
+
// this.elemTopBody.prepend(this.elemTitle);
|
368
|
+
this.elemTopBody.insertBefore(this.elemTitle, this.elemTopBody.childNodes[0]);
|
369
|
+
}
|
370
|
+
|
371
|
+
const itemsChecked = this.getChecked();
|
372
|
+
|
373
|
+
this.elemTop.title = '';
|
374
|
+
const isPlaceholder = !itemsChecked.length;
|
375
|
+
let title:string = this.titlePlaceholder;
|
376
|
+
if (itemsChecked.length) {
|
377
|
+
let attrTitle = '';
|
378
|
+
itemsChecked.forEach((item, index) => {
|
379
|
+
if (index !== 0) {
|
380
|
+
attrTitle += `${this.options.sepChars}<span class="${getClass('sep_space', true)}"> </span>`;
|
381
|
+
}
|
382
|
+
attrTitle += `${item.title}`;
|
383
|
+
});
|
384
|
+
this.elemTop.title = attrTitle;
|
385
|
+
|
386
|
+
let maxShow = this.options.countShowSelected;
|
387
|
+
const maxShowAttr = Number(this.$select.dataset.simpleCountShowsSelected);
|
388
|
+
if (maxShowAttr && maxShowAttr > 0) {
|
389
|
+
maxShow = maxShowAttr;
|
390
|
+
}
|
391
|
+
if (itemsChecked.length > maxShow) {
|
392
|
+
title = `${this.options.locale.selected} ${itemsChecked.length}`;
|
393
|
+
|
394
|
+
if (this.$select.querySelectorAll('option').length === itemsChecked.length) {
|
395
|
+
title += ` (${this.options.locale.all})`;
|
396
|
+
}
|
397
|
+
} else if (attrTitle) {
|
398
|
+
title = attrTitle;
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
this.elemTitle.innerHTML = title;
|
403
|
+
if (isPlaceholder) {
|
404
|
+
this.elemTitle.classList.add('SimpleSel__title--placeholder');
|
405
|
+
this.elemTitle.classList.remove('SimpleSel__title--fill');
|
406
|
+
this.elemWrap.classList.remove(getClass('fill', true));
|
407
|
+
} else {
|
408
|
+
this.elemTitle.classList.remove('SimpleSel__title--placeholder');
|
409
|
+
this.elemTitle.classList.add('SimpleSel__title--fill');
|
410
|
+
this.elemWrap.classList.add(getClass('fill', true));
|
411
|
+
}
|
412
|
+
}
|
413
|
+
|
414
|
+
protected createListHTML() {
|
415
|
+
if (!this.elemListBody) {
|
416
|
+
return;
|
417
|
+
}
|
418
|
+
let resBodyList = '';
|
419
|
+
let countShowItem = 0;
|
420
|
+
let countCheckedItems = 0;
|
421
|
+
let countCheckedFullItems = 0;
|
422
|
+
|
423
|
+
// this.items.forEach(group => {
|
424
|
+
this.state.getState('items').forEach((group:IOptionItems) => {
|
425
|
+
if (!group.isGroup) {
|
426
|
+
const {
|
427
|
+
result, countShow, countChecked, countCheckedFull,
|
428
|
+
} = this.createLi(group);
|
429
|
+
resBodyList += result;
|
430
|
+
countShowItem += countShow;
|
431
|
+
countCheckedItems += countChecked;
|
432
|
+
countCheckedFullItems += countCheckedFull;
|
433
|
+
} else {
|
434
|
+
const {
|
435
|
+
result, countShow, countChecked, countCheckedFull,
|
436
|
+
} = this.createLi(group);
|
437
|
+
resBodyList += `<div class="${getClass('group_items')}">`;
|
438
|
+
resBodyList += result;
|
439
|
+
resBodyList += '</div>';
|
440
|
+
|
441
|
+
countCheckedItems += countChecked;
|
442
|
+
countShowItem += countShow;
|
443
|
+
countCheckedFullItems += countCheckedFull;
|
444
|
+
}
|
445
|
+
});
|
446
|
+
|
447
|
+
const isSearch:string = this.state.getState('filterStr');
|
448
|
+
|
449
|
+
if (isSearch && isSearch.length && countShowItem === 0) {
|
450
|
+
resBodyList = `<div class="${getClass('no_match')}">`;
|
451
|
+
resBodyList = `${this.options.locale.noSearch} "${isSearch}"`;
|
452
|
+
resBodyList += '</div>';
|
453
|
+
}
|
454
|
+
|
455
|
+
this.elemWrap.dataset.countChecked = countCheckedItems.toString();
|
456
|
+
this.elemWrap.dataset.countCheckedFull = countCheckedFullItems.toString();
|
457
|
+
if (this.isMulti) {
|
458
|
+
this.elemWrap.dataset.checkAllMulti = (this.$select.options.length === countCheckedItems) ? 'yes' : 'no';
|
459
|
+
}
|
460
|
+
this.elemListBody.innerHTML = resBodyList;
|
461
|
+
}
|
462
|
+
|
463
|
+
private createInputHTML(): void {
|
464
|
+
let { isSearch } = this.options;
|
465
|
+
let { isSearchInDropdown } = this.options;
|
466
|
+
if ('simpleSelectSearch' in this.$select.dataset) {
|
467
|
+
isSearch = this.$select.dataset.simpleSelectSearch !== 'false';
|
468
|
+
}
|
469
|
+
if ('simpleSelectSearchDropdown' in this.$select.dataset) {
|
470
|
+
isSearchInDropdown = this.$select.dataset.simpleSelectSearchDropdown !== 'false';
|
471
|
+
}
|
472
|
+
if (!isSearch && !isSearchInDropdown) {
|
473
|
+
return;
|
474
|
+
}
|
475
|
+
this.elemInputSearch = document.createElement('input');
|
476
|
+
this.elemInputSearch.type = 'text';
|
477
|
+
this.elemInputSearch.tabIndex = -1;
|
478
|
+
this.elemInputSearch.autocomplete = 'off';
|
479
|
+
this.elemInputSearch.ariaAutoComplete = 'none';
|
480
|
+
this.elemInputSearch.inputMode = 'off';
|
481
|
+
this.elemInputSearch.placeholder = this.options.locale.searchText;
|
482
|
+
this.elemInputSearch.name = `${initClass}_name_${this.id}`;
|
483
|
+
|
484
|
+
const className = getClass('search');
|
485
|
+
if (isSearchInDropdown) {
|
486
|
+
if (this.elemDropDown) {
|
487
|
+
this.elemInputSearch.className = `${className} ${getClass('dropdown', true, className)}`;
|
488
|
+
// this.elemDropDown.prepend(this.elemInputSearch);
|
489
|
+
this.elemDropDown.insertBefore(this.elemInputSearch, this.elemDropDown.childNodes[0]);
|
490
|
+
}
|
491
|
+
} else {
|
492
|
+
this.elemInputSearch.className = `${className} ${getClass('top', true, className)}`;
|
493
|
+
this.elemTop.appendChild(this.elemInputSearch);
|
494
|
+
}
|
495
|
+
|
496
|
+
this.inputSearchHandler();
|
497
|
+
}
|
498
|
+
|
499
|
+
getChecked(): IOptionItem[] {
|
500
|
+
const items: IOptionItems[] = this.state.getState('items');
|
501
|
+
let res: IOptionItem[] = [];
|
502
|
+
|
503
|
+
items.forEach((group) => {
|
504
|
+
res = [
|
505
|
+
...res,
|
506
|
+
...group.items.filter((i) => i.checked),
|
507
|
+
];
|
508
|
+
});
|
509
|
+
|
510
|
+
return res;
|
511
|
+
}
|
512
|
+
|
513
|
+
private createLi(data: IOptionItems): ICreateLiReturn {
|
514
|
+
let result = '';
|
515
|
+
let countShow = 0;
|
516
|
+
let countChecked = 0;
|
517
|
+
let countCheckedFull = 0;
|
518
|
+
|
519
|
+
if (!data.isShowFilter) {
|
520
|
+
return {
|
521
|
+
result, countShow, countChecked, countCheckedFull,
|
522
|
+
};
|
523
|
+
}
|
524
|
+
|
525
|
+
if (data.isGroup) {
|
526
|
+
result += `<label class="${getClass('group_title')}">${data.titleGroup}</label>`;
|
527
|
+
result += `<ul class="${getClass('group')}">`;
|
528
|
+
}
|
529
|
+
data.items.forEach((option) => {
|
530
|
+
if (!option.isShowFilter) {
|
531
|
+
return;
|
532
|
+
}
|
533
|
+
countShow++;
|
534
|
+
const classLiInit = getClass('list_item');
|
535
|
+
let classLi = classLiInit;
|
536
|
+
if (option.checked) {
|
537
|
+
countChecked++;
|
538
|
+
classLi += ` ${getClass('checked', true, classLiInit)}`;
|
539
|
+
|
540
|
+
if (option.value) {
|
541
|
+
countCheckedFull++;
|
542
|
+
}
|
543
|
+
}
|
544
|
+
if (option.disabled) {
|
545
|
+
classLi += ` ${getClass('disabled', true, classLiInit)}`;
|
546
|
+
}
|
547
|
+
if (!option.value) {
|
548
|
+
classLi += ` ${getClass('not_value', true, classLiInit)}`;
|
549
|
+
}
|
550
|
+
|
551
|
+
let dataAttr = `data-sel-group-id="${data.idGroup}"`;
|
552
|
+
dataAttr += ` data-sel-position="${option.position}"`;
|
553
|
+
dataAttr += ` data-sel-id="${option.id}"`;
|
554
|
+
|
555
|
+
if (option.value) {
|
556
|
+
dataAttr += ` data-sel-value="${option.value}"`;
|
557
|
+
}
|
558
|
+
|
559
|
+
dataAttr += ' data-sel-opt-item';
|
560
|
+
dataAttr += ` data-sel-opt-checked="${option.checked}"`;
|
561
|
+
dataAttr += ` data-sel-opt-disabled="${option.disabled}"`;
|
562
|
+
|
563
|
+
result += `<li class="${classLi}" ${dataAttr}>`;
|
564
|
+
const createLiBodyRes = this.createLiBody(option, this.$select.options[option.position]);
|
565
|
+
result += typeof createLiBodyRes === 'string' ? createLiBodyRes : createLiBodyRes.outerHTML;
|
566
|
+
result += '</li>';
|
567
|
+
});
|
568
|
+
if (data.isGroup) {
|
569
|
+
result += '</ul>';
|
570
|
+
}
|
571
|
+
return {
|
572
|
+
result, countShow, countChecked, countCheckedFull,
|
573
|
+
};
|
574
|
+
}
|
575
|
+
|
576
|
+
private createLiBody(option: IOptionItem, optionNative: HTMLOptionElement): HTMLElement | string {
|
577
|
+
const item = document.createElement('div');
|
578
|
+
|
579
|
+
item.className = getClass('list_item_body');
|
580
|
+
|
581
|
+
let res:string = '';
|
582
|
+
if (this.isShowCheckbox) {
|
583
|
+
res = `<span class="${getClass('list_item_icon')}"></span>`;
|
584
|
+
}
|
585
|
+
|
586
|
+
if (this.bodyLiHTMLBeforeFromSelect) {
|
587
|
+
res += this.bodyLiHTMLBeforeFromSelect;
|
588
|
+
}
|
589
|
+
|
590
|
+
if (optionNative.hasAttribute('data-simple-html-before')) {
|
591
|
+
res += optionNative.getAttribute('data-simple-html-before');
|
592
|
+
}
|
593
|
+
|
594
|
+
res += `${option.title}`;
|
595
|
+
if (this.bodyLiHTMLAfterFromSelect) {
|
596
|
+
res += this.bodyLiHTMLAfterFromSelect;
|
597
|
+
}
|
598
|
+
if (optionNative.hasAttribute('data-simple-html-after')) {
|
599
|
+
res += optionNative.getAttribute('data-simple-html-after');
|
600
|
+
}
|
601
|
+
|
602
|
+
item.innerHTML = res;
|
603
|
+
|
604
|
+
if (this.options.changeBodyLi) {
|
605
|
+
return this.options.changeBodyLi(item, optionNative);
|
606
|
+
}
|
607
|
+
return item;
|
608
|
+
}
|
609
|
+
|
610
|
+
protected handlerChangeChecked() {
|
611
|
+
console.error('This method need redefine');
|
612
|
+
}
|
613
|
+
|
614
|
+
// only desktop
|
615
|
+
protected createList(_isCompare: boolean) {
|
616
|
+
console.error('This method need redefine');
|
617
|
+
}
|
618
|
+
|
619
|
+
// only desktop
|
620
|
+
protected inputSearchHandler() {
|
621
|
+
console.error('This method need redefine');
|
622
|
+
}
|
623
|
+
}
|