Open Infrastructure Map, a view of infrastructure data in OpenStreetMap
New edit control
Russ Garrett80b29e0b57cb
diff --git a/web/src/edit-control.css b/web/src/edit-control.cssnew file mode 100644index 0000000..ef8a707--- /dev/null+++ b/web/src/edit-control.css@@ -0,0 +1,60 @@+.oim-edit-control,+.oim-editor-menu {+ background-color: white;+}++.oim-edit-control button,+.oim-editor-menu button {+ font-size: inherit;+}++.oim-edit-control {+ font-size: 1rem;+ border-radius: 4px;+ box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);+ transition:+ opacity 0.25s,+ visibility 0.25s;+ opacity: 0;+ visibility: hidden;+}++.oim-edit-control.active {+ opacity: 1;+ visibility: visible;+}++.oim-editor-menu {+ font-size: 0.9rem;+ box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);+ border-radius: 4px;+ padding: 0;+ list-style-type: none;+ margin-bottom: 3px !important;+ transition:+ opacity 0.1s,+ visibility 0.1s;+ opacity: 0;+ visibility: hidden;+}++.oim-editor-menu.active {+ opacity: 1;+ visibility: visible;+}++.oim-editor-menu button {+ cursor: pointer;+ background-color: transparent;+ padding: 0.25rem 0.5rem;+ border: 0;+ width: 100%;+ text-align: left;+}++.oim-edit-control button {+ cursor: pointer;+ background-color: transparent;+ padding: 0.25rem 0.5rem;+ border: 0;+}diff --git a/web/src/edit-control.ts b/web/src/edit-control.tsnew file mode 100644index 0000000..a2d9399--- /dev/null+++ b/web/src/edit-control.ts@@ -0,0 +1,106 @@+import { IControl } from 'maplibre-gl'+import { el } from 'redom'+import './edit-control.css'++class EditControl implements IControl {+ _map: any+ _control!: HTMLDivElement+ _menu!: HTMLUListElement++ onAdd(map: maplibregl.Map) {+ this._map = map++ const edit_button = el('button', 'Edit')+ edit_button.onclick = (ev: MouseEvent) => {+ this.doEdit()+ ev.preventDefault()+ }++ this._menu = el(+ 'ul',+ el(+ 'li',+ el('button', 'Edit with remote control (JOSM, etc)', { onclick: () => this.doEdit('remote') })+ ),+ el('li', el('button', 'Edit in browser (iD)', { onclick: () => this.doEdit('id') })),+ {+ class: 'maplibregl-ctrl oim-editor-menu'+ }+ )++ const more_button = el('button', '^', {+ class: 'oim-edit-more',+ onclick: () => this._menu.classList.toggle('active')+ })++ this._control = el('div', edit_button, more_button, {+ class: 'maplibregl-ctrl oim-edit-control'+ })+ const container = el('div', this._menu, this._control)++ map.on('zoomend', () => this.updateVisibility())+ map.on('movestart', () => this._menu.classList.remove('active'))++ this.updateVisibility()+ return container+ }++ doEdit(setEditor?: string) {+ let editor = setEditor || localStorage.getItem('editor')+ if (!editor) {+ editor = 'id'+ }+ localStorage.setItem('editor', editor)++ this._menu.classList.remove('active')++ if (editor === 'id') {+ window.open(this._getIdURL(), '_blank')+ } else {+ const url = this._getRemoteURL()+ fetch(url).catch((error) => {+ alert(+ "Unable to load remote editor - make sure it's running and has remote control enabled.\nError: " ++ error+ )+ })+ }+ }++ onRemove(): void {+ this._map = undefined+ this._control.remove()+ }++ updateVisibility() {+ if (this._map.getZoom() > 14) {+ this._control.classList.add('active')+ } else {+ this._control.classList.remove('active')+ }+ }++ _getRemoteURL() {+ const url = 'http://127.0.0.1:8111/load_and_zoom'+ const bounds = this._map.getBounds()+ return (+ url ++ '?left=' ++ bounds.getWest() ++ '&right=' ++ bounds.getEast() ++ '&top=' ++ bounds.getNorth() ++ '&bottom=' ++ bounds.getSouth()+ )+ }++ _getIdURL() {+ const zoom = Math.round(this._map.getZoom())+ const center = this._map.getCenter()+ return `https://www.openstreetmap.org/edit?editor=id#map=${zoom}/${center.lat.toFixed(5)}/${center.lng.toFixed(5)}`+ }+}++export { EditControl as default }diff --git a/web/src/editbutton.ts b/web/src/editbutton.tsdeleted file mode 100644index 9296dd1..0000000--- a/web/src/editbutton.ts+++ /dev/null@@ -1,67 +0,0 @@-import { IControl } from 'maplibre-gl'-import { el, setStyle } from 'redom'--class EditButton implements IControl {- _map: any- _control!: HTMLDivElement- onAdd(map: maplibregl.Map) {- this._map = map-- const button = el('button', 'Edit in JOSM')- button.onclick = (ev: MouseEvent) => {- this._click()- ev.preventDefault()- }-- this._control = el('div', button, {- class: 'maplibregl-ctrl'- })-- map.on('zoomend', () => {- this.updateVisibility()- })-- this.updateVisibility()- return this._control- }-- onRemove(): void {- this._map = undefined- this._control.remove()- }-- updateVisibility() {- if (this._map.getZoom() > 14) {- setStyle(this._control, { display: 'block' })- } else {- setStyle(this._control, { display: 'none' })- }- }-- _getJosmURL() {- const url = 'http://127.0.0.1:8111/load_and_zoom'- const bounds = this._map.getBounds()- return (- url +- '?left=' +- bounds.getWest() +- '&right=' +- bounds.getEast() +- '&top=' +- bounds.getNorth() +- '&bottom=' +- bounds.getSouth()- )- }-- _click() {- const url = this._getJosmURL()- fetch(url).catch((error) => {- alert(- "Unable to edit in JOSM - make sure it's running and has remote control enabled.\nError: " + error- )- })- }-}--export { EditButton as default }diff --git a/web/src/index.ts b/web/src/index.tsindex 57e93e8..b09e04e 100644--- a/web/src/index.ts+++ b/web/src/index.ts@@ -5,7 +5,7 @@ import { mount } from 'redom'import LayerSwitcher from '@russss/maplibregl-layer-switcher'import URLHash from '@russss/maplibregl-layer-switcher/urlhash'-import EditButton from './editbutton.js'+import EditButton from './edit-control.js'import InfoPopup from './infopopup.js'import KeyControl from './key/key.js'import WarningBox from './warning-box/warning-box.js'