-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
101 lines (80 loc) · 2.65 KB
/
main.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
class SimpleLangSelect extends HTMLSelectElement {
static get observedAttributes() {
return ["value", "availableLangs"];
}
constructor() {
// Always call super first in constructor
super();
const shadow = this.attachShadow({ mode: "open" });
this.select = document.createElement("select");
shadow.appendChild(this.select);
}
buildOptions() {
const selectedLang = this.attributes.value.value
const availableLangsValue = this.attributes.availableLangs.value
const availableLangs = availableLangsValue.split(',');
availableLangs.map(locale => {
const option = document.createElement("option");
const langName = new Intl.DisplayNames([locale], { type: 'language' });
option.text = langName.of(locale)
option.value = locale;
if (locale === selectedLang) {
option.selected = true;
}
this.select.add(option);
});
}
attributeChangedCallback(name, oldValue, newValue) {
console.log(
`Attribute ${name} has changed from ${oldValue} to ${newValue}.`,
);
if (name === "value") {
this.removeOptions();
this.buildOptions();
}
}
removeOptions() {
while(this.select.options.length > 0) {
this.select.remove(0)
}
}
connectedCallback(){
console.log("Custom lang selector element added to the page.");
const shadow = this.shadowRoot;
if (!this.hasAttribute('value')) {
this.setAttribute('value', 'en');
}
if (!this.hasAttribute('availableLangs')) {
this.setAttribute('availableLangs', 'en,ar,de,zh,ko,ja,fr');
}
const changeEvt = new CustomEvent("change", {
bubbles: true,
cancelable: false,
composed: true
});
const ele = this;
// shadow.querySelector("select").addEventListener("change", function (e) {
this.select.addEventListener("change", function (e) {
console.log('listend to change event');
// console.log(e);
console.log(`Selected value: `,e.target.value);
changeEvt.value = e.target.value;
// console.log(e.value);
ele.dispatchEvent(changeEvt);
});
}
handleEvent(evt){
console.warn('handle Event :: ', evt, evt.target.value);
}
disconnectedCallback() {
console.log("Custom lang selector element removed from page.");
const shadow = this.shadowRoot;
// shadow.querySelector("select")
// shadow.querySelector("select").removeEventListener("change");
this.select.removeEventListener("change");
}
adoptedCallback() {
console.log("Custom lang selector element moved to new page.");
}
}
customElements.define("simple-lang-select", SimpleLangSelect, { extends: "select" });