Do not try to save map itself unless it has been modified

This commit is contained in:
Yohan Boniface 2023-09-08 15:32:37 +02:00
parent ee9acf3427
commit 9189262855
2 changed files with 44 additions and 22 deletions

View file

@ -192,16 +192,31 @@ L.U.Map.include({
this this
) )
let isDirty = false // global status // FIXME naming
let hasDirty = false // global status
try {
Object.defineProperty(this, 'hasDirty', {
get: function () {
return hasDirty || this.dirty_datalayers.length
},
set: function (status) {
if (!hasDirty && status) self.fire('hasdirty')
hasDirty = status
self.checkDirty()
},
})
} catch (e) {
// Certainly IE8, which has a limited version of defineProperty
}
let isDirty = false // self status
try { try {
Object.defineProperty(this, 'isDirty', { Object.defineProperty(this, 'isDirty', {
get: function () { get: function () {
return isDirty || this.dirty_datalayers.length return isDirty
}, },
set: function (status) { set: function (status) {
if (!isDirty && status) self.fire('isdirty')
isDirty = status isDirty = status
self.checkDirty() if (status) hasDirty = true
}, },
}) })
} catch (e) { } catch (e) {
@ -267,7 +282,7 @@ L.U.Map.include({
if (L.Util.queryString('download')) this.download() if (L.Util.queryString('download')) this.download()
}) })
window.onbeforeunload = () => this.isDirty || null window.onbeforeunload = () => this.hasDirty || null
this.backup() this.backup()
this.initContextMenu() this.initContextMenu()
this.on('click contextmenu.show', this.closeInplaceToolbar) this.on('click contextmenu.show', this.closeInplaceToolbar)
@ -506,7 +521,7 @@ L.U.Map.include({
key === L.U.Keys.E && key === L.U.Keys.E &&
modifierKey && modifierKey &&
this.editEnabled && this.editEnabled &&
!this.isDirty !this.hasDirty
) { ) {
L.DomEvent.stop(e) L.DomEvent.stop(e)
this.disableEdit() this.disableEdit()
@ -514,11 +529,11 @@ L.U.Map.include({
} }
if (key === L.U.Keys.S && modifierKey) { if (key === L.U.Keys.S && modifierKey) {
L.DomEvent.stop(e) L.DomEvent.stop(e)
if (this.isDirty) { if (this.hasDirty) {
this.save() this.save()
} }
} }
if (key === L.U.Keys.Z && modifierKey && this.isDirty) { if (key === L.U.Keys.Z && modifierKey && this.hasDirty) {
L.DomEvent.stop(e) L.DomEvent.stop(e)
this.askForReset() this.askForReset()
} }
@ -1047,17 +1062,18 @@ L.U.Map.include({
this.dirty_datalayers = [] this.dirty_datalayers = []
this.updateDatalayersControl() this.updateDatalayersControl()
this.initTileLayers() this.initTileLayers()
this.hasDirty = false
this.isDirty = false this.isDirty = false
}, },
checkDirty: function () { checkDirty: function () {
L.DomUtil.classIf(this._container, 'umap-is-dirty', this.isDirty) L.DomUtil.classIf(this._container, 'umap-is-dirty', this.hasDirty)
}, },
addDirtyDatalayer: function (datalayer) { addDirtyDatalayer: function (datalayer) {
if (this.dirty_datalayers.indexOf(datalayer) === -1) { if (this.dirty_datalayers.indexOf(datalayer) === -1) {
this.dirty_datalayers.push(datalayer) this.dirty_datalayers.push(datalayer)
this.isDirty = true this.hasDirty = true
} }
}, },
@ -1161,15 +1177,12 @@ L.U.Map.include({
return JSON.stringify(umapfile, null, 2) return JSON.stringify(umapfile, null, 2)
}, },
save: function () { saveSelf: function () {
if (!this.isDirty) return
if (this._default_extent) this.updateExtent()
const geojson = { const geojson = {
type: 'Feature', type: 'Feature',
geometry: this.geometry(), geometry: this.geometry(),
properties: this.exportOptions(), properties: this.exportOptions(),
} }
this.backup()
const formData = new FormData() const formData = new FormData()
formData.append('name', this.options.name) formData.append('name', this.options.name)
formData.append('center', JSON.stringify(this.geometry())) formData.append('center', JSON.stringify(this.geometry()))
@ -1215,7 +1228,7 @@ L.U.Map.include({
}, },
] ]
} }
} else if (!this.permissions.isDirty) { } else if (!this.permissions.hasDirty) {
// Do not override local changes to permissions, // Do not override local changes to permissions,
// but update in case some other editors changed them in the meantime. // but update in case some other editors changed them in the meantime.
this.permissions.setOptions(data.permissions) this.permissions.setOptions(data.permissions)
@ -1225,16 +1238,25 @@ L.U.Map.include({
history.pushState({}, this.options.name, data.url) history.pushState({}, this.options.name, data.url)
else window.location = data.url else window.location = data.url
alert.content = data.info || alert.content alert.content = data.info || alert.content
this.once('saved', function () { this.once('saved', () => this.ui.alert(alert))
this.isDirty = false
this.ui.alert(alert)
})
this.ui.closePanel() this.ui.closePanel()
this.permissions.save() this.permissions.save()
}, },
}) })
}, },
save: function () {
if (!this.hasDirty) return
if (this._default_extent) this.updateExtent()
this.backup()
this.once('saved', () => {
this.hasDirty = false
this.isDirty = false
})
if (this.isDirty) this.saveSelf()
else this.permissions.save() // Map itself has no change, check permissions and continue
},
sendEditLink: function () { sendEditLink: function () {
const url = L.Util.template(this.options.urls.map_send_edit_link, { const url = L.Util.template(this.options.urls.map_send_edit_link, {
map_id: this.options.umap_id, map_id: this.options.umap_id,
@ -1761,7 +1783,7 @@ L.U.Map.include({
}, },
disableEdit: function () { disableEdit: function () {
if (this.isDirty) return if (this.hasDirty) return
L.DomUtil.removeClass(document.body, 'umap-edit-enabled') L.DomUtil.removeClass(document.body, 'umap-edit-enabled')
this.editedFeature = null this.editedFeature = null
this.editEnabled = false this.editEnabled = false
@ -1927,7 +1949,7 @@ L.U.Map.include({
if (this.options.allowEdit) { if (this.options.allowEdit) {
items.push('-') items.push('-')
if (this.editEnabled) { if (this.editEnabled) {
if (!this.isDirty) { if (!this.hasDirty) {
items.push({ items.push({
text: `${L._('Stop editing')} (Ctrl+E)`, text: `${L._('Stop editing')} (Ctrl+E)`,
callback: this.disableEdit, callback: this.disableEdit,

View file

@ -19,7 +19,7 @@ L.U.MapPermissions = L.Class.extend({
}, },
set: function (status) { set: function (status) {
isDirty = status isDirty = status
if (status) self.map.isDirty = status if (status) self.map.hasDirty = status
}, },
}) })
} catch (e) { } catch (e) {