-
Notifications
You must be signed in to change notification settings - Fork 0
/
select-input.js
160 lines (136 loc) · 4.99 KB
/
select-input.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
(function (document, window) {
if (!window.customElements || !HTMLElement.prototype.attachShadow) {
loadScript('https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.2.0/webcomponents-sd-ce.js', loadComponents)
} else {
loadComponents();
}
function loadScript(url, callback) {
const script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) {
script.onreadystatechange = function () {
if (script.readyState === "loaded" || script.readyState === "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = function () { callback() };
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
function loadComponents() {
const txtInput = window.customElements.get('text-input');
const optList = window.customElements.get('option-list');
if (!txtInput && !optList) {
loadAll();
} else {
if (!optList || !txtInput) {
if (!optList) { loadOptionList(); }
if (!txtInput) { loadTextInput(); }
} else {
loadSelectInput();
}
}
}
function loadAll(callback) {
loadScript('https://rawgit.com/Nevraeka/text-input/master/text-input.js', loadOptionList);
}
function loadTextInput() {
loadScript('https://rawgit.com/Nevraeka/text-input/master/text-input.js', loadSelectInput);
}
function loadOptionList() {
loadScript('https://rawgit.com/Nevraeka/option-list/master/option-list.js', loadSelectInput);
}
function loadSelectInput() {
if (!window.customElements.get('select-input')) {
window.customElements.define('select-input',
class SelectInput extends HTMLElement {
static get observedAttributes() { return []; }
get info() { return Object.freeze({ dependencies: [{ "text-input": [{ "img-icon": [] }], "options-list": [] }], name: 'select-input', version: 'v1.6.0' }); }
constructor() {
super();
this._state = {
isOpen: false,
placeholder: ''
};
}
connectedCallback() {
render(this);
}
attributeChangedCallback(name, oldValue, newValue) {
if (newValue === oldValue) { return };
this._state[name] = newValue;
}
});
}
}
function render(component) {
const html = Array.from(component.querySelectorAll('li'), (item)=> item.outerHTML).join('');
const selectedItem = component.querySelector('li[selected]');
const initVal = selectedItem !== null ? selectedItem.innerText : '';
component.innerHTML = `
<style>
select-input {
display: flex;
flex-direction: column;
width: auto;
position: relative;
}
option-list {
width: 100%;
position: absolute;
z-index: 9999;
top: 55px;
}
.select_input__text_input input {
cursor: pointer;
user-select: none;
}
.select_input__text_input {
border-radius: 4px;
z-index: 10;
}
</style>
<text-input size="small" icon="arrowDropDown" class="select_input__text_input" initial-value="${initVal}" is-valid="${component._state.isValid}" placeholder="${component._state.placeholder}"></text-input>
<option-list caret="top left" style="display: none;">
${html}
</option-list>
`;
component.querySelector('text-input').removeEventListener('textInputFocused', openHandler.bind(component));
component.querySelector('text-input').addEventListener('textInputFocused', openHandler.bind(component));
component.querySelector('option-list').removeEventListener('optionSelected', closeHandler.bind(component));
component.querySelector('option-list').addEventListener('optionSelected', closeHandler.bind(component));
}
function selectInputClosedEvent(value) {
return new CustomEvent('selectInputClosed', {
composed: true,
cancelable: true,
detail: { value: value }
});
}
function selectInputOpenedEvent(value) {
return new CustomEvent('selectInputOpened', {
composed: true,
cancelable: true,
detail: { value: value }
});
}
function openHandler(evt) {
const textInput = this.querySelector('text-input');
textInput.setAttribute('icon', 'arrowDropUp');
this._state.isOpen = true;
this.dispatchEvent(selectInputOpenedEvent(textInput.value.split('<')[0]));
this.querySelector('option-list').style.display = 'block';
}
function closeHandler(evt) {
const filteredValue = evt.detail.value.split('<')[0];
const textInput = this.querySelector('text-input');
textInput.setValue(filteredValue);
textInput.setAttribute('icon', 'arrowDropDown');
this._state.isOpen = false;
this.dispatchEvent(selectInputClosedEvent(filteredValue));
this.querySelector('option-list').style.display = 'none';
}
})(document, window);