-
-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathelement_select.js
More file actions
70 lines (59 loc) · 1.54 KB
/
element_select.js
File metadata and controls
70 lines (59 loc) · 1.54 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
import { hightlightTerm } from "alchemy_admin/components/remote_select"
const formatSelection = (option) => {
return `
<div class="element-select-name">
${option.icon}<span>${option.text}</span>
</div>
`
}
const formatItem = (icon, text, hint) => {
const description = hint
? `<div class="element-select-description">${hint}</div>`
: ""
return `
<div class="element-select-item">
${formatSelection({ icon, text })}
${description}
</div>
`
}
class ElementSelect extends HTMLElement {
#select2 = null
connectedCallback() {
const results = this.options
const options = {
minimumResultsForSearch: 3,
dropdownAutoWidth: true,
data() {
return { results }
},
formatResult: (option, _el, search) => {
let text
if (option.id === "") return option.text
if (search.term !== "") {
text = hightlightTerm(option.text, search.term)
} else {
text = option.text
}
return formatItem(option.icon, text, option.hint)
},
formatSelection,
placeholder: this.placeholder
}
this.#select2 = $(this.inputField).select2(options)
}
disconnectedCallback() {
this.#select2?.select2("destroy")
this.#select2 = null
}
get options() {
return JSON.parse(this.getAttribute("options"))
}
get placeholder() {
return this.getAttribute("placeholder")
}
get inputField() {
return this.querySelector("input")
}
}
customElements.define("alchemy-element-select", ElementSelect)