1 | import DropdownIcon from '../assets/icons/dropdown.svg';
|
2 |
|
3 |
|
4 | class Picker {
|
5 | constructor(select) {
|
6 | this.select = select;
|
7 | this.container = document.createElement('span');
|
8 | this.buildPicker();
|
9 | this.select.style.display = 'none';
|
10 | this.select.parentNode.insertBefore(this.container, this.select);
|
11 | this.label.addEventListener('mousedown', () => {
|
12 | this.container.classList.toggle('ql-expanded');
|
13 | });
|
14 | this.select.addEventListener('change', this.update.bind(this));
|
15 | }
|
16 |
|
17 | buildItem(option) {
|
18 | let item = document.createElement('span');
|
19 | item.classList.add('ql-picker-item');
|
20 | if (option.hasAttribute('value')) {
|
21 | item.setAttribute('data-value', option.getAttribute('value'));
|
22 | }
|
23 | if (option.textContent) {
|
24 | item.setAttribute('data-label', option.textContent);
|
25 | }
|
26 | item.addEventListener('click', () => {
|
27 | this.selectItem(item, true);
|
28 | });
|
29 | return item;
|
30 | }
|
31 |
|
32 | buildLabel() {
|
33 | let label = document.createElement('span');
|
34 | label.classList.add('ql-picker-label');
|
35 | label.innerHTML = DropdownIcon;
|
36 | this.container.appendChild(label);
|
37 | return label;
|
38 | }
|
39 |
|
40 | buildOptions() {
|
41 | let options = document.createElement('span');
|
42 | options.classList.add('ql-picker-options');
|
43 | [].slice.call(this.select.options).forEach((option) => {
|
44 | let item = this.buildItem(option);
|
45 | options.appendChild(item);
|
46 | if (option.selected === true) {
|
47 | this.selectItem(item);
|
48 | }
|
49 | });
|
50 | this.container.appendChild(options);
|
51 | }
|
52 |
|
53 | buildPicker() {
|
54 | [].slice.call(this.select.attributes).forEach((item) => {
|
55 | this.container.setAttribute(item.name, item.value);
|
56 | });
|
57 | this.container.classList.add('ql-picker');
|
58 | this.label = this.buildLabel();
|
59 | this.buildOptions();
|
60 | }
|
61 |
|
62 | close() {
|
63 | this.container.classList.remove('ql-expanded');
|
64 | }
|
65 |
|
66 | selectItem(item, trigger = false) {
|
67 | let selected = this.container.querySelector('.ql-selected');
|
68 | if (item === selected) return;
|
69 | if (selected != null) {
|
70 | selected.classList.remove('ql-selected');
|
71 | }
|
72 | if (item == null) return;
|
73 | item.classList.add('ql-selected');
|
74 | this.select.selectedIndex = [].indexOf.call(item.parentNode.children, item);
|
75 | if (item.hasAttribute('data-value')) {
|
76 | this.label.setAttribute('data-value', item.getAttribute('data-value'));
|
77 | } else {
|
78 | this.label.removeAttribute('data-value');
|
79 | }
|
80 | if (item.hasAttribute('data-label')) {
|
81 | this.label.setAttribute('data-label', item.getAttribute('data-label'));
|
82 | } else {
|
83 | this.label.removeAttribute('data-label');
|
84 | }
|
85 | if (trigger) {
|
86 | if (typeof Event === 'function') {
|
87 | this.select.dispatchEvent(new Event('change'));
|
88 | } else if (typeof Event === 'object') {
|
89 | let event = document.createEvent('Event');
|
90 | event.initEvent('change', true, true);
|
91 | this.select.dispatchEvent(event);
|
92 | }
|
93 | this.close();
|
94 | }
|
95 | }
|
96 |
|
97 | update() {
|
98 | let option;
|
99 | if (this.select.selectedIndex > -1) {
|
100 | let item = this.container.querySelector('.ql-picker-options').children[this.select.selectedIndex];
|
101 | option = this.select.options[this.select.selectedIndex];
|
102 | this.selectItem(item);
|
103 | } else {
|
104 | this.selectItem(null);
|
105 | }
|
106 | let isActive = option != null && option !== this.select.querySelector('option[selected]');
|
107 | this.label.classList.toggle('ql-active', isActive);
|
108 | }
|
109 | }
|
110 |
|
111 |
|
112 | export default Picker;
|