Skip to content

Commit b6dde8c

Browse files
committed
More idiomatic JavaScript
1 parent 8c7f80c commit b6dde8c

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

umap/static/umap/js/modules/importer.js

+59-40
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,85 @@ export default class Importer {
55
}
66

77
#buildDatalayerOptions(layerSelect) {
8-
let option
8+
const options = []
99
this.map.eachDataLayerReverse((datalayer) => {
1010
if (datalayer.isLoaded() && !datalayer.isRemoteLayer()) {
11-
const id = L.stamp(datalayer)
12-
option = L.DomUtil.add('option', '', layerSelect, datalayer.options.name)
13-
option.value = id
11+
options.push(
12+
`<option value="${L.stamp(datalayer)}">${datalayer.options.name}</option>`
13+
)
1414
}
1515
})
16-
L.DomUtil.element(
17-
'option',
18-
{ value: '', textContent: L._('Import in a new layer') },
19-
layerSelect
20-
)
16+
options.push(`<option value="">${L._('Import in a new layer')}</option>`)
17+
layerSelect.innerHTML = options.join('')
2118
}
2219

2320
#buildPresetsOptions(presetSelect) {
24-
if (this.presets.length) {
25-
const presetBox = this.form.querySelector('#preset-box')
26-
presetBox.removeAttribute('hidden')
27-
const noPreset = L.DomUtil.create('option', '', presetSelect)
28-
noPreset.value = noPreset.textContent = L._('Choose a preset')
29-
for (const preset of this.presets) {
30-
option = L.DomUtil.create('option', '', presetSelect)
31-
option.value = preset.url
32-
option.textContent = preset.label
33-
}
21+
if (!this.presets.length) return
22+
const options = []
23+
presetSelect.parentElement.removeAttribute('hidden')
24+
options.push(
25+
`<option value="${L._('Choose a preset')}">${L._('Choose a preset')}</option>`
26+
)
27+
for (const preset of this.presets) {
28+
options.push(`<option value="${preset.url}">${preset.label}</option>`)
3429
}
30+
presetSelect.innerHTML = options.join('')
31+
}
32+
33+
#connectedCallback() {
34+
const controller = new AbortController()
35+
const signal = controller.signal
36+
this.form
37+
.querySelector('[name="submit-input"]')
38+
.addEventListener('click', this.submit.bind(this), { signal })
39+
40+
this.fileInput.addEventListener(
41+
'change',
42+
(e) => {
43+
let type = ''
44+
let newType
45+
for (const file of e.target.files) {
46+
newType = L.Util.detectFileType(file)
47+
if (!type && newType) {
48+
type = newType
49+
}
50+
if (type && newType !== type) {
51+
type = ''
52+
break
53+
}
54+
}
55+
this.formatSelect.value = type
56+
},
57+
{ signal }
58+
)
59+
60+
this.map.ui.once(
61+
'panel:closed',
62+
() => {
63+
this.fileInput.value = null
64+
controller.abort()
65+
},
66+
{ signal }
67+
)
3568
}
3669

3770
build() {
3871
const template = document.querySelector('#umap-upload')
3972
this.form = template.content.firstElementChild.cloneNode(true)
40-
this.presetSelect = this.form.querySelector('[name="preset-select"]')
41-
this.fileInput = this.form.querySelector('[name="file-input"]')
42-
this.map.ui.once('panel:closed', () => (this.fileInput.value = null))
73+
4374
this.typeLabel = this.form.querySelector('#type-label')
4475
const helpButton = this.typeLabel.querySelector('button')
4576
this.map.help.button(this.typeLabel, 'importFormats', '', helpButton)
46-
this.formatSelect = this.form.querySelector('[name="format"]')
77+
4778
this.layerSelect = this.form.querySelector('[name="datalayer"]')
48-
this.submitInput = this.form.querySelector('[name="submit-input"]')
4979
this.#buildDatalayerOptions(this.layerSelect)
80+
this.presetSelect = this.form.querySelector('[name="preset-select"]')
5081
this.#buildPresetsOptions(this.presetSelect)
5182

52-
this.submitInput.addEventListener('click', this.submit.bind(this))
53-
this.fileInput.addEventListener('change', (e) => {
54-
let type = ''
55-
let newType
56-
for (const file of e.target.files) {
57-
newType = L.Util.detectFileType(file)
58-
if (!type && newType) {
59-
type = newType
60-
}
61-
if (type && newType !== type) {
62-
type = ''
63-
break
64-
}
65-
}
66-
this.formatSelect.value = type
67-
})
83+
this.fileInput = this.form.querySelector('[name="file-input"]')
84+
this.formatSelect = this.form.querySelector('[name="format"]')
85+
86+
this.#connectedCallback()
6887
}
6988

7089
open() {

0 commit comments

Comments
 (0)