Experimental drag and drop of file on the map container

This commit is contained in:
Yohan Boniface 2023-10-13 11:53:04 +02:00
parent 3c234ead5b
commit b5bf1396f3
2 changed files with 55 additions and 6 deletions

View file

@ -313,6 +313,51 @@ L.U.DrawToolbar = L.Toolbar.Control.extend({
}, },
}) })
L.U.DropControl = L.Class.extend({
initialize: function (map) {
this.map = map
this.dropbox = map._container
},
enable: function () {
L.DomEvent.on(this.dropbox, "dragenter", this.dragenter, this)
L.DomEvent.on(this.dropbox, "dragover", this.dragover, this)
L.DomEvent.on(this.dropbox, "drop", this.drop, this)
L.DomEvent.on(this.dropbox, "dragleave", this.dragleave, this)
},
disable: function () {
L.DomEvent.off(this.dropbox, "dragenter", this.dragenter, this)
L.DomEvent.off(this.dropbox, "dragover", this.dragover, this)
L.DomEvent.off(this.dropbox, "drop", this.drop, this)
L.DomEvent.off(this.dropbox, "dragleave", this.dragleave, this)
},
dragenter: function (e) {
L.DomEvent.stop(e)
this.map.scrollWheelZoom.disable()
},
dragover: function (e) {
L.DomEvent.stop(e)
},
drop: function (e) {
L.DomEvent.stop(e)
this.map.scrollWheelZoom.enable()
for (let i = 0, file; (file = e.dataTransfer.files[i]); i++) {
this.map.processFileToImport(file)
}
this.map.onceDataLoaded(this.map.fitDataBounds)
},
dragleave: function () {
this.map.scrollWheelZoom.enable()
}
})
L.U.EditControl = L.Control.extend({ L.U.EditControl = L.Control.extend({
options: { options: {
position: 'topright', position: 'topright',

View file

@ -340,6 +340,7 @@ L.U.Map.include({
if (this.options.scrollWheelZoom) this.scrollWheelZoom.enable() if (this.options.scrollWheelZoom) this.scrollWheelZoom.enable()
else this.scrollWheelZoom.disable() else this.scrollWheelZoom.disable()
this.browser = new L.U.Browser(this) this.browser = new L.U.Browser(this)
this.drop = new L.U.DropControl(this)
this.renderControls() this.renderControls()
}, },
@ -670,6 +671,12 @@ L.U.Map.include({
} }
}, },
fitDataBounds: function () {
const bounds = this.getLayersBounds()
if (!this.hasData() || !bounds.isValid()) return false
this.fitBounds(bounds)
},
initCenter: function () { initCenter: function () {
if (this.options.hash && this._hash.parseHash(location.hash)) { if (this.options.hash && this._hash.parseHash(location.hash)) {
// FIXME An invalid hash will cause the load to fail // FIXME An invalid hash will cause the load to fail
@ -679,12 +686,7 @@ L.U.Map.include({
this._controls.locate.start() this._controls.locate.start()
} else if (this.options.defaultView === 'data') { } else if (this.options.defaultView === 'data') {
this.onceDataLoaded(() => { this.onceDataLoaded(() => {
const bounds = this.getLayersBounds() if (!this.fitDataBounds()) return this._setDefaultCenter()
if (!this.hasData() || !bounds.isValid()) {
this._setDefaultCenter()
return
}
this.fitBounds(bounds)
}) })
} else if (this.options.defaultView === 'latest') { } else if (this.options.defaultView === 'latest') {
this.onceDataLoaded(() => { this.onceDataLoaded(() => {
@ -1710,11 +1712,13 @@ L.U.Map.include({
enableEdit: function () { enableEdit: function () {
L.DomUtil.addClass(document.body, 'umap-edit-enabled') L.DomUtil.addClass(document.body, 'umap-edit-enabled')
this.editEnabled = true this.editEnabled = true
this.drop.enable()
this.fire('edit:enabled') this.fire('edit:enabled')
}, },
disableEdit: function () { disableEdit: function () {
if (this.isDirty) return if (this.isDirty) return
this.drop.disable()
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