-
Notifications
You must be signed in to change notification settings - Fork 0
/
t-select.js
148 lines (139 loc) · 4.11 KB
/
t-select.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
import { html, css, LitElement } from "lit"
import { ifDefined } from 'lit-html/directives/if-defined.js';
/**
* `t-select`
*
* @customElement
* @demo demo/index.html
*/
export class TSelect extends LitElement {
static get properties() {
return {
ready: {
type: Boolean
},
currentLanguageCode: {
type: String
},
translationDefinitions: {
type: Array
},
pathToTranslationDefinitions: {
type: String
},
basePath: {
type: String
},
selectedTranslationCode: {
type: String
},
skipInitialTranslationLoad: {
type: Boolean
},
disableJsonTranslations: {
type: Boolean
},
disableLanguageLabelTranslations: {
type: Boolean
},
label: {
type: String
}
}
}
constructor() {
super()
this.ready = false
this.currentLanguageCode = 'en'
this.translations = []
this.pathToTranslationDefinitions = './translation-definitions.json'
this.basePath = './'
this.skipInitialTranslationLoad = false
this.disableJsonTranslations = false
this.disableLanguageLabelTranslations = false
this.label = "Language"
}
render() {
return html`
${!this.ready ? html`
...
`:''}
${this.ready ? html`
<form>
<label for="translation">
<t-translate>
${this.label}
</t-translate>
</label>
<select name="translation" value="${this.currentLanguageCode}" @change="${this.onTranslationSelect}">
${this.translationDefinitions.map(translation => html`
<option
value="${translation.languageCode}"
?selected="${translation.languageCode === this.currentLanguageCode}"
>
${this.disableLanguageLabelTranslations ? html`
${translation.label}
` : html`
<t-translate>
${translation.label}
</t-translate>
`}
</option>
`)}
</select>
</form>
`:''}
`
}
async onTranslationSelect($event) {
await this.setLanguage($event.srcElement.value)
this.dispatchEvent(new CustomEvent('change'))
}
async setLanguage(languageCode) {
this.currentLanguageCode = languageCode
document.documentElement.lang = languageCode;
document.body.dispatchEvent(new CustomEvent('lang-change'));
if (this.disableJsonTranslations) return
const translationDefinition = this
.translationDefinitions
.find(translationDefinition => translationDefinition.languageCode === languageCode)
const translationFilePath = translationDefinition.filePath
? `${this.basePath}${translationDefinition.filePath}`
: `${this.basePath}/translation.${languageCode}.json`
const translationResponse = await fetch(translationFilePath)
const translation = await translationResponse.json()
window.translation = translation
document.body.dispatchEvent(new CustomEvent('lang-ready'));
}
async connectedCallback() {
super.connectedCallback()
this.currentLanguageCode = document.documentElement.lang
if (!this.skipInitialTranslationLoad) {
if (!this.disableJsonTranslations) {
const translationDefinitionsRequest = await fetch(this.pathToTranslationDefinitions)
this.translationDefinitions = await translationDefinitionsRequest.json()
}
if (!window.translations) {
await this.setLanguage(document.documentElement.lang || 'en')
}
}
this.ready = true
}
}
TSelect.styles = [
css`
label {
background: var(--t-select_label_background);
border: var(--t-select_label_border)
margin: var(--t-select_label_margin)
padding: var(--t-select_label_padding)
}
select {
background: var(--t-select_select_background);
border: var(--t-select_select_border)
margin: var(--t-select_select_margin)
padding: var(--t-select_select_padding)
}
`
]
window.customElements.define('t-select', TSelect);