Move importer panel to a separate class

This commit is contained in:
Yohan Boniface 2023-12-01 10:30:06 +01:00
parent e251c84298
commit 84b5e2188a
5 changed files with 171 additions and 130 deletions

View file

@ -19,7 +19,7 @@ L.U.ImportAction = L.U.BaseAction.extend({
},
addHooks: function () {
this.map.importPanel()
this.map.importer.open()
},
})
@ -1172,134 +1172,6 @@ L.U.Map.include({
this.ui.openPanel({ data: { html: container } })
},
importPanel: function () {
const container = L.DomUtil.create('div', 'umap-upload')
const title = L.DomUtil.create('h4', '', container)
const presetBox = L.DomUtil.create('div', 'formbox', container)
const presetSelect = L.DomUtil.create('select', '', presetBox)
const fileBox = L.DomUtil.create('div', 'formbox', container)
const fileInput = L.DomUtil.create('input', '', fileBox)
const urlInput = L.DomUtil.create('input', '', container)
const rawInput = L.DomUtil.create('textarea', '', container)
const typeLabel = L.DomUtil.create('label', '', container)
const layerLabel = L.DomUtil.create('label', '', container)
const clearLabel = L.DomUtil.create('label', '', container)
const submitInput = L.DomUtil.create('input', '', container)
const map = this
let option
const types = ['geojson', 'csv', 'gpx', 'kml', 'osm', 'georss', 'umap']
title.textContent = L._('Import data')
fileInput.type = 'file'
fileInput.multiple = 'multiple'
submitInput.type = 'button'
submitInput.value = L._('Import')
submitInput.className = 'button'
typeLabel.textContent = L._('Choose the format of the data to import')
this.help.button(typeLabel, 'importFormats')
const typeInput = L.DomUtil.create('select', '', typeLabel)
typeInput.name = 'format'
layerLabel.textContent = L._('Choose the layer to import in')
const layerInput = L.DomUtil.create('select', '', layerLabel)
layerInput.name = 'datalayer'
urlInput.type = 'text'
urlInput.placeholder = L._('Provide an URL here')
rawInput.placeholder = L._('Paste your data here')
clearLabel.textContent = L._('Replace layer content')
const clearFlag = L.DomUtil.create('input', '', clearLabel)
clearFlag.type = 'checkbox'
clearFlag.name = 'clear'
this.eachDataLayerReverse((datalayer) => {
if (datalayer.isLoaded() && !datalayer.isRemoteLayer()) {
const id = L.stamp(datalayer)
option = L.DomUtil.create('option', '', layerInput)
option.value = id
option.textContent = datalayer.options.name
}
})
L.DomUtil.element(
'option',
{ value: '', textContent: L._('Import in a new layer') },
layerInput
)
L.DomUtil.element(
'option',
{ value: '', textContent: L._('Choose the data format') },
typeInput
)
for (let i = 0; i < types.length; i++) {
option = L.DomUtil.create('option', '', typeInput)
option.value = option.textContent = types[i]
}
if (this.options.importPresets.length) {
const noPreset = L.DomUtil.create('option', '', presetSelect)
noPreset.value = noPreset.textContent = L._('Choose a preset')
for (let j = 0; j < this.options.importPresets.length; j++) {
option = L.DomUtil.create('option', '', presetSelect)
option.value = this.options.importPresets[j].url
option.textContent = this.options.importPresets[j].label
}
} else {
presetBox.style.display = 'none'
}
const submit = function () {
let type = typeInput.value
const layerId = layerInput[layerInput.selectedIndex].value
let layer
if (type === 'umap') {
this.once('postsync', function () {
this.setView(this.latLng(this.options.center), this.options.zoom)
})
}
if (layerId) layer = map.datalayers[layerId]
if (layer && clearFlag.checked) layer.empty()
if (fileInput.files.length) {
for (let i = 0, file; (file = fileInput.files[i]); i++) {
this.processFileToImport(file, layer, type)
}
} else {
if (!type)
return this.ui.alert({
content: L._('Please choose a format'),
level: 'error',
})
if (rawInput.value && type === 'umap') {
try {
this.importRaw(rawInput.value, type)
} catch (e) {
this.ui.alert({ content: L._('Invalid umap data'), level: 'error' })
console.error(e)
}
} else {
if (!layer) layer = this.createDataLayer()
if (rawInput.value) layer.importRaw(rawInput.value, type)
else if (urlInput.value) layer.importFromUrl(urlInput.value, type)
else if (presetSelect.selectedIndex > 0)
layer.importFromUrl(presetSelect[presetSelect.selectedIndex].value, type)
}
}
}
L.DomEvent.on(submitInput, 'click', submit, this)
L.DomEvent.on(
fileInput,
'change',
(e) => {
let type = '',
newType
for (let i = 0; i < e.target.files.length; i++) {
newType = L.Util.detectFileType(e.target.files[i])
if (!type && newType) type = newType
if (type && newType !== type) {
type = ''
break
}
}
typeInput.value = type
},
this
)
this.ui.openPanel({ data: { html: container }, className: 'dark' })
},
})
L.U.TileLayerControl = L.Control.extend({

View file

@ -0,0 +1,166 @@
L.U.Importer = L.Class.extend({
TYPES: ['geojson', 'csv', 'gpx', 'kml', 'osm', 'georss', 'umap'],
initialize: function (map) {
this.map = map
this.presets = map.options.importPresets
},
build: function () {
this.container = L.DomUtil.create('div', 'umap-upload')
this.title = L.DomUtil.add('h4', '', this.container, L._('Import data'))
this.presetBox = L.DomUtil.create('div', 'formbox', this.container)
this.presetSelect = L.DomUtil.create('select', '', this.presetBox)
this.fileBox = L.DomUtil.create('div', 'formbox', this.container)
this.fileInput = L.DomUtil.element(
'input',
{ type: 'file', multiple: 'multiple' },
this.fileBox
)
this.urlInput = L.DomUtil.element(
'input',
{ type: 'text', placeholder: L._('Provide an URL here') },
this.container
)
this.rawInput = L.DomUtil.element(
'textarea',
{ placeholder: L._('Paste your data here') },
this.container
)
this.typeLabel = L.DomUtil.add(
'label',
'',
this.container,
L._('Choose the format of the data to import')
)
this.layerLabel = L.DomUtil.add(
'label',
'',
this.container,
L._('Choose the layer to import in')
)
this.clearLabel = L.DomUtil.add(
'label',
'',
this.container,
L._('Replace layer content')
)
this.submitInput = L.DomUtil.element(
'input',
{ type: 'button', value: L._('Import'), className: 'button' },
this.container
)
this.map.help.button(this.typeLabel, 'importFormats')
this.typeInput = L.DomUtil.element('select', { name: 'format' }, this.typeLabel)
this.layerInput = L.DomUtil.element(
'select',
{ name: 'datalayer' },
this.layerLabel
)
this.clearFlag = L.DomUtil.element(
'input',
{ type: 'checkbox', name: 'clear' },
this.clearLabel
)
let option
this.map.eachDataLayerReverse((datalayer) => {
if (datalayer.isLoaded() && !datalayer.isRemoteLayer()) {
const id = L.stamp(datalayer)
option = L.DomUtil.add('option', '', this.layerInput, datalayer.options.name)
option.value = id
}
})
L.DomUtil.element(
'option',
{ value: '', textContent: L._('Import in a new layer') },
this.layerInput
)
L.DomUtil.element(
'option',
{ value: '', textContent: L._('Choose the data format') },
this.typeInput
)
for (let i = 0; i < this.TYPES.length; i++) {
option = L.DomUtil.create('option', '', this.typeInput)
option.value = option.textContent = this.TYPES[i]
}
if (this.presets.length) {
const noPreset = L.DomUtil.create('option', '', this.presetSelect)
noPreset.value = noPreset.textContent = L._('Choose a preset')
for (let j = 0; j < this.presets.length; j++) {
option = L.DomUtil.create('option', '', presetSelect)
option.value = this.presets[j].url
option.textContent = this.presets[j].label
}
} else {
this.presetBox.style.display = 'none'
}
L.DomEvent.on(this.submitInput, 'click', this.submit, this)
L.DomEvent.on(
this.fileInput,
'change',
(e) => {
let type = '',
newType
for (let i = 0; i < e.target.files.length; i++) {
newType = L.Util.detectFileType(e.target.files[i])
if (!type && newType) type = newType
if (type && newType !== type) {
type = ''
break
}
}
this.typeInput.value = type
},
this
)
},
open: function () {
if (!this.container) this.build()
this.map.ui.openPanel({ data: { html: this.container }, className: 'dark' })
},
openFiles: function () {
this.open()
this.fileInput.click()
},
submit: function () {
let type = this.typeInput.value
const layerId = this.layerInput[this.layerInput.selectedIndex].value
let layer
if (type === 'umap') {
this.map.once('postsync', this.map._setDefaultCenter)
}
if (layerId) layer = this.map.datalayers[layerId]
if (layer && this.clearFlag.checked) layer.empty()
if (this.fileInput.files.length) {
for (let i = 0, file; (file = this.fileInput.files[i]); i++) {
this.map.processFileToImport(file, layer, type)
}
} else {
if (!type)
return this.map.ui.alert({
content: L._('Please choose a format'),
level: 'error',
})
if (this.rawInput.value && type === 'umap') {
try {
this.map.importRaw(this.rawInput.value, type)
} catch (e) {
this.ui.alert({ content: L._('Invalid umap data'), level: 'error' })
console.error(e)
}
} else {
if (!layer) layer = this.map.createDataLayer()
if (this.rawInput.value) layer.importRaw(this.rawInput.value, type)
else if (this.urlInput.value) layer.importFromUrl(this.urlInput.value, type)
else if (this.presetSelect.selectedIndex > 0)
layer.importFromUrl(
this.presetSelect[this.presetSelect.selectedIndex].value,
type
)
}
}
},
})

View file

@ -343,6 +343,7 @@ L.U.Map.include({
if (this.options.scrollWheelZoom) this.scrollWheelZoom.enable()
else this.scrollWheelZoom.disable()
this.browser = new L.U.Browser(this)
this.importer = new L.U.Importer(this)
this.drop = new L.U.DropControl(this)
this.renderControls()
},
@ -560,7 +561,7 @@ L.U.Map.include({
}
if (key === L.U.Keys.I && modifierKey && this.editEnabled) {
L.DomEvent.stop(e)
this.importPanel()
this.importer.open()
}
if (key === L.U.Keys.H && modifierKey && this.editEnabled) {
L.DomEvent.stop(e)

View file

@ -41,6 +41,7 @@
<script src="../js/umap.permissions.js"></script>
<script src="../js/umap.datalayer.permissions.js"></script>
<script src="../js/umap.browser.js"></script>
<script src="../js/umap.importer.js"></script>
<script src="../js/umap.js"></script>
<script src="../js/umap.ui.js"></script>
<link rel="stylesheet" href="../vendors/leaflet/leaflet.css" />

View file

@ -43,6 +43,7 @@
<script src="{{ STATIC_URL }}umap/js/umap.slideshow.js"></script>
<script src="{{ STATIC_URL }}umap/js/umap.tableeditor.js"></script>
<script src="{{ STATIC_URL }}umap/js/umap.browser.js"></script>
<script src="{{ STATIC_URL }}umap/js/umap.importer.js"></script>
<script src="{{ STATIC_URL }}umap/js/umap.js"></script>
<script src="{{ STATIC_URL }}umap/js/umap.ui.js"></script>
{% endcompress %}