Skip to content

Commit

Permalink
chore: remove map.dirty_datalayers
Browse files Browse the repository at this point in the history
Let's try to make it simpler.
map.datalayers stores all datalayers
map.datalayers_index the non deleted ones sorted
  • Loading branch information
yohanboniface committed Oct 4, 2024
1 parent a35b37f commit 03ae31c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 40 deletions.
14 changes: 7 additions & 7 deletions umap/static/umap/js/modules/data/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export class DataLayer {
set isDirty(status) {
this._isDirty = status
if (status) {
this.map.addDirtyDatalayer(this)
this.map.isDirty = true
// A layer can be made dirty by indirect action (like dragging layers)
// we need to have it loaded before saving it.
if (!this.isLoaded()) this.fetchData()
} else {
this.map.removeDirtyDatalayer(this)
this.map.checkDirty()
this.isDeleted = false
}
}
Expand Down Expand Up @@ -339,11 +339,11 @@ export class DataLayer {
const id = stamp(this)
if (!this.map.datalayers[id]) {
this.map.datalayers[id] = this
if (!this.map.datalayers_index.includes(this)) {
this.map.datalayers_index.push(this)
}
this.map.onDataLayersChanged()
}
if (!this.map.datalayers_index.includes(this)) {
this.map.datalayers_index.push(this)
}
this.map.onDataLayersChanged()
}

_dataUrl() {
Expand Down Expand Up @@ -559,7 +559,6 @@ export class DataLayer {

erase() {
this.hide()
delete this.map.datalayers[stamp(this)]
this.map.datalayers_index.splice(this.getRank(), 1)
this.parentPane.removeChild(this.pane)
this.map.onDataLayersChanged()
Expand Down Expand Up @@ -1090,6 +1089,7 @@ export class DataLayer {
if (this.umap_id) {
await this.map.server.post(this.getDeleteUrl())
}
delete this.map.datalayers[stamp(this)]
this.isDirty = false
}

Expand Down
8 changes: 5 additions & 3 deletions umap/static/umap/js/modules/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,11 @@ export class MapPermissions {
}

getShareStatusDisplay() {
return Object.fromEntries(this.map.options.share_statuses)[
this.options.share_status
]
if (this.map.options.share_statuses) {
return Object.fromEntries(this.map.options.share_statuses)[
this.options.share_status
]
}
}
}

Expand Down
41 changes: 14 additions & 27 deletions umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ L.Map.mergeOptions({
// we cannot rely on this because of the y is overriden by Leaflet
// See https://github.com/Leaflet/Leaflet/pull/9201
// And let's remove this -y when this PR is merged and released.
demoTileInfos: { 's': 'a', 'z': 9, 'x': 265, 'y': 181, '-y': 181, 'r': '' },
demoTileInfos: { s: 'a', z: 9, x: 265, y: 181, '-y': 181, r: '' },
licences: [],
licence: '',
enableMarkerDraw: true,
Expand Down Expand Up @@ -110,10 +110,9 @@ U.Map = L.Map.extend({
delete this.options.advancedFilterKey
}

// Global storage for retrieving datalayers and features
this.datalayers = {}
this.datalayers_index = []
this.dirty_datalayers = []
// Global storage for retrieving datalayers and features.
this.datalayers = {} // All datalayers, including deleted.
this.datalayers_index = [] // Datalayers actually on the map and ordered.
this.features_index = {}

// Needed for actions labels
Expand Down Expand Up @@ -489,7 +488,7 @@ U.Map = L.Map.extend({
loadDataLayers: async function () {
this.datalayersLoaded = true
this.fire('datalayersloaded')
for (const datalayer of Object.values(this.datalayers)) {
for (const datalayer of this.datalayers_index) {
if (datalayer.showAtLoad()) await datalayer.show()
}
this.dataloaded = true
Expand Down Expand Up @@ -935,6 +934,7 @@ U.Map = L.Map.extend({
if (mustReindex) datalayer.reindex()
datalayer.redraw()
})
this.propagate()
this.fire('postsync')
this.isDirty = true
},
Expand Down Expand Up @@ -998,12 +998,12 @@ U.Map = L.Map.extend({
if (this.editTools) this.editTools.stopDrawing()
this.resetOptions()
this.datalayers_index = [].concat(this._datalayers_index_bk)
this.dirty_datalayers.slice().forEach((datalayer) => {
// Iter over all datalayers, including deleted if any.
for (const datalayer of Object.values(this.datalayers)) {
if (datalayer.isDeleted) datalayer.connectToMap()
datalayer.reset()
})
if (datalayer.isDirty) datalayer.reset()
}
this.ensurePanesOrder()
this.dirty_datalayers = []
this.initTileLayers()
this.isDirty = false
this.onDataLayersChanged()
Expand All @@ -1013,20 +1013,6 @@ U.Map = L.Map.extend({
this._container.classList.toggle('umap-is-dirty', this.isDirty)
},

addDirtyDatalayer: function (datalayer) {
if (this.dirty_datalayers.indexOf(datalayer) === -1) {
this.dirty_datalayers.push(datalayer)
this.isDirty = true
}
},

removeDirtyDatalayer: function (datalayer) {
if (this.dirty_datalayers.indexOf(datalayer) !== -1) {
this.dirty_datalayers.splice(this.dirty_datalayers.indexOf(datalayer), 1)
this.checkDirty()
}
},

exportOptions: function () {
const properties = {}
for (const option of Object.keys(U.SCHEMA)) {
Expand Down Expand Up @@ -1113,8 +1099,9 @@ U.Map = L.Map.extend({
if (!ok) return
}
await this.permissions.save()
for (const datalayer of this.dirty_datalayers) {
await datalayer.save()
// Iter over all datalayers, including deleted.
for (const datalayer of Object.values(this.datalayers)) {
if (datalayer.isDirty) await datalayer.save()
}
this.isDirty = false
this.renderEditToolbar()
Expand Down Expand Up @@ -1859,7 +1846,7 @@ U.Map = L.Map.extend({

getFeatureById: function (id) {
let feature
for (const datalayer of Object.values(this.datalayers)) {
for (const datalayer of this.datalayers_index) {
feature = datalayer.getFeatureById(id)
if (feature) return feature
}
Expand Down
2 changes: 0 additions & 2 deletions umap/tests/integration/test_edit_datalayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def test_cancel_deleting_datalayer_should_restore(
page.once("dialog", lambda dialog: dialog.accept())
page.locator(".panel.right").get_by_title("Delete layer").click()
expect(markers).to_have_count(0)
page.get_by_role("button", name="Open browser").click()
expect(page.get_by_text("test datalayer")).to_be_hidden()
page.once("dialog", lambda dialog: dialog.accept())
page.get_by_role("button", name="Cancel edits").click()
page.locator("dialog").get_by_role("button", name="OK").click()
expect(markers).to_have_count(1)
Expand Down
2 changes: 1 addition & 1 deletion umap/tests/integration/test_edit_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_map_name_impacts_ui(live_server, page, tilelayer):

name_input.fill("something else")

expect(page.get_by_role("button", name="something else").nth(1)).to_be_visible()
expect(page.get_by_role("button", name="something else").first).to_be_visible()


def test_zoomcontrol_impacts_ui(live_server, page, tilelayer):
Expand Down

0 comments on commit 03ae31c

Please sign in to comment.