feat: allow to set the new layer name at import

This commit is contained in:
Yohan Boniface 2024-06-06 11:21:23 +02:00
parent 3998a88d43
commit 43f7e6a467
6 changed files with 45 additions and 17 deletions

View file

@ -397,7 +397,7 @@ fieldset legend {
content: attr(data-badge); content: attr(data-badge);
} }
[hidden] { [hidden] {
display: none; display: none!important;
} }
/* Switch */ /* Switch */

View file

@ -21,15 +21,16 @@ const TEMPLATE = `
<legend class="counter" data-help="importFormats">${translate('Choose the data format')}</legend> <legend class="counter" data-help="importFormats">${translate('Choose the data format')}</legend>
<select name="format" onchange></select> <select name="format" onchange></select>
</fieldset> </fieldset>
<fieldset class="destination formbox"> <fieldset id="destination" class="formbox">
<legend class="counter">${translate('Choose the layer to import in')}</legend> <legend class="counter">${translate('Choose the layer to import in')}</legend>
<select name="layer-id"></select> <select name="layer-id" onchange></select>
<label> <label id="clear">
<input type="checkbox" name="clear" /> <input type="checkbox" name="clear" />
${translate('Replace layer content')} ${translate('Replace layer content')}
</label> </label>
<input type="text" name="layer-name" placeholder="${translate('Layer name')}" />
</fieldset> </fieldset>
<fieldset class="import-mode formbox"> <fieldset id="import-mode" class="formbox">
<legend class="counter" data-help="importMode">${translate('Choose import mode')}</legend> <legend class="counter" data-help="importMode">${translate('Choose import mode')}</legend>
<label> <label>
<input type="radio" name="action" value="copy" /> <input type="radio" name="action" value="copy" />
@ -98,9 +99,25 @@ export default class Importer {
return this.qs('[name=action]:checked').value return this.qs('[name=action]:checked').value
} }
get layerId() {
return this.qs('[name=layer-id]').value
}
set layerId(value) {
this.qs('[name=layer-id]').value = value
}
get layerName() {
return this.qs('[name=layer-name]').value
}
set layerName(name) {
this.qs('[name=layer-name]').value = name
this.onChange()
}
get layer() { get layer() {
const layerId = this.qs('[name=layer-id]').value return this.map.datalayers[this.layerId] || this.map.createDataLayer({name: this.layerName})
return this.map.datalayers[layerId] || this.map.createDataLayer()
} }
build() { build() {
@ -134,11 +151,13 @@ export default class Importer {
} }
onChange() { onChange() {
this.qs('.destination').toggleAttribute('hidden', this.format === 'umap') this.qs('#destination').toggleAttribute('hidden', this.format === 'umap')
this.qs('.import-mode').toggleAttribute( this.qs('#import-mode').toggleAttribute(
'hidden', 'hidden',
this.format === 'umap' || !this.url this.format === 'umap' || !this.url
) )
this.qs('[name=layer-name]').toggleAttribute('hidden', Boolean(this.layerId))
this.qs('#clear').toggleAttribute('hidden', !Boolean(this.layerId))
} }
onFileChange(e) { onFileChange(e) {
@ -159,6 +178,7 @@ export default class Importer {
this.qs('[type=file]').value = null this.qs('[type=file]').value = null
this.url = null this.url = null
this.format = undefined this.format = undefined
this.layerName = null
const layerSelect = this.qs('[name="layer-id"]') const layerSelect = this.qs('[name="layer-id"]')
layerSelect.innerHTML = '' layerSelect.innerHTML = ''
this.map.eachDataLayerReverse((datalayer) => { this.map.eachDataLayerReverse((datalayer) => {
@ -176,6 +196,7 @@ export default class Importer {
value: '', value: '',
textContent: translate('Import in a new layer'), textContent: translate('Import in a new layer'),
parent: layerSelect, parent: layerSelect,
selected: true
}) })
} }

View file

@ -28,6 +28,7 @@ export class Importer {
on_select: (choice) => { on_select: (choice) => {
importer.url = `https://geo.api.gouv.fr/communes?code=${choice.item.value}&format=geojson&geometry=contour` importer.url = `https://geo.api.gouv.fr/communes?code=${choice.item.value}&format=geojson&geometry=contour`
importer.format = 'geojson' importer.format = 'geojson'
importer.layerName = choice.item.label
importer.dialog.close() importer.dialog.close()
}, },
} }

View file

@ -37,6 +37,7 @@ export class Importer {
async open(importer) { async open(importer) {
let boundary = null let boundary = null
let boundaryName = null
const container = DomUtil.create('div') const container = DomUtil.create('div')
container.innerHTML = TEMPLATE container.innerHTML = TEMPLATE
const response = await importer.map.request.get(`${this.baseUrl}/themes`) const response = await importer.map.request.get(`${this.baseUrl}/themes`)
@ -59,11 +60,13 @@ export class Importer {
url: `${this.baseUrl}/boundaries/search?text={q}`, url: `${this.baseUrl}/boundaries/search?text={q}`,
on_select: (choice) => { on_select: (choice) => {
boundary = choice.item.value boundary = choice.item.value
boundaryName = choice.item.label
}, },
}) })
const confirm = () => { const confirm = () => {
importer.url = `${this.baseUrl}/data/${select.value}/${boundary}?format=geojson&aspoint=${asPoint.checked}` importer.url = `${this.baseUrl}/data/${select.value}/${boundary}?format=geojson&aspoint=${asPoint.checked}`
importer.format = 'geojson' importer.format = 'geojson'
importer.layerName = `${boundaryName}${select.options[select.selectedIndex].textContent}`
importer.dialog.close() importer.dialog.close()
} }
L.DomUtil.createButton('', container, 'OK', confirm) L.DomUtil.createButton('', container, 'OK', confirm)

View file

@ -799,16 +799,14 @@ U.Map = L.Map.extend({
return L.Map.prototype.setMaxBounds.call(this, bounds) return L.Map.prototype.setMaxBounds.call(this, bounds)
}, },
createDataLayer: function (datalayer, sync) { createDataLayer: function (options = {}, sync) {
datalayer = datalayer || { options.name = options.name || `${L._('Layer')} ${this.datalayers_index.length + 1}`
name: `${L._('Layer')} ${this.datalayers_index.length + 1}`, const datalayer = new U.DataLayer(this, options, sync)
}
const dl = new U.DataLayer(this, datalayer, sync)
if (sync !== false) { if (sync !== false) {
dl.sync.upsert(dl.options) datalayer.sync.upsert(datalayer.options)
} }
return dl return datalayer
}, },
newDataLayer: function () { newDataLayer: function () {

View file

@ -218,6 +218,8 @@ def test_can_import_in_existing_datalayer(live_server, datalayer, page, openmap)
path = Path(__file__).parent.parent / "fixtures/test_upload_data.csv" path = Path(__file__).parent.parent / "fixtures/test_upload_data.csv"
textarea.fill(path.read_text()) textarea.fill(path.read_text())
page.locator('select[name="format"]').select_option("csv") page.locator('select[name="format"]').select_option("csv")
page.locator('select[name="layer-id"]').select_option(datalayer.name)
expect(page.locator('input[name=layer-name]')).to_be_hidden()
page.get_by_role("button", name="Add data", exact=True).click() page.get_by_role("button", name="Add data", exact=True).click()
# No layer has been created # No layer has been created
expect(layers).to_have_count(1) expect(layers).to_have_count(1)
@ -237,6 +239,7 @@ def test_can_replace_datalayer_data(live_server, datalayer, page, openmap):
path = Path(__file__).parent.parent / "fixtures/test_upload_data.csv" path = Path(__file__).parent.parent / "fixtures/test_upload_data.csv"
textarea.fill(path.read_text()) textarea.fill(path.read_text())
page.locator('select[name="format"]').select_option("csv") page.locator('select[name="format"]').select_option("csv")
page.locator('select[name="layer-id"]').select_option(datalayer.name)
page.get_by_label("Replace layer content").check() page.get_by_label("Replace layer content").check()
page.get_by_role("button", name="Add data", exact=True).click() page.get_by_role("button", name="Add data", exact=True).click()
# No layer has been created # No layer has been created
@ -258,10 +261,12 @@ def test_can_import_in_new_datalayer(live_server, datalayer, page, openmap):
textarea.fill(path.read_text()) textarea.fill(path.read_text())
page.locator("select[name=format]").select_option("csv") page.locator("select[name=format]").select_option("csv")
page.locator("[name=layer-id]").select_option(label="Import in a new layer") page.locator("[name=layer-id]").select_option(label="Import in a new layer")
page.locator("[name=layer-name]").fill("My new layer name")
page.get_by_role("button", name="Add data", exact=True).click() page.get_by_role("button", name="Add data", exact=True).click()
# A new layer has been created # A new layer has been created
expect(layers).to_have_count(2) expect(layers).to_have_count(2)
expect(markers).to_have_count(3) expect(markers).to_have_count(3)
expect(page.get_by_text("My new layer name")).to_be_visible()
def test_should_remove_dot_in_property_names(live_server, page, settings, tilelayer): def test_should_remove_dot_in_property_names(live_server, page, settings, tilelayer):
@ -504,7 +509,7 @@ def test_import_geojson_from_url(page, live_server, tilelayer):
} }
) )
# Intercept the route to the proxy # Intercept the route
page.route("https://remote.org/data.json", handle) page.route("https://remote.org/data.json", handle)
page.goto(f"{live_server.url}/map/new/") page.goto(f"{live_server.url}/map/new/")
expect(page.locator(".leaflet-marker-icon")).to_be_hidden() expect(page.locator(".leaflet-marker-icon")).to_be_hidden()