Open Infrastructure Map, a view of infrastructure data in OpenStreetMap
Move layer switcher to new package
Russ Garrettc8bf8d8ac147
diff --git a/package-lock.json b/package-lock.jsonindex 5780bf8..6dc9c47 100644--- a/package-lock.json+++ b/package-lock.json@@ -1600,6 +1600,16 @@"resolved": "https://registry.npmjs.org/@mapbox/whoots-js/-/whoots-js-3.1.0.tgz","integrity": "sha512-Es6WcD0nO5l+2BOQS4uLfNPYQaNDfbot3X1XUoloz+x0mPDS3eeORZJl06HXjwBG1fOGwCRnzK88LMdxKRrd6Q=="},+ "@russss/mapboxgl-layer-switcher": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@russss/mapboxgl-layer-switcher/-/mapboxgl-layer-switcher-1.0.0.tgz",+ "integrity": "sha512-s4vpE9JdozBtpQDeFGDLfBFNMFH898ILAuQBSIVrdKfAn0o4msMDggTiTyDYQh6vearPdEiacu4suNvzk51ccA==",+ "requires": {+ "lodash.invert": "^4.3.0",+ "lodash.isequal": "^4.5.0",+ "redom": "^3.24.2"+ }+ },"@types/events": {"version": "3.0.0","resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz",@@ -2822,6 +2832,16 @@"dev": true,"optional": true},+ "bindings": {+ "version": "1.5.0",+ "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",+ "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",+ "dev": true,+ "optional": true,+ "requires": {+ "file-uri-to-path": "1.0.0"+ }+ },"bluebird": {"version": "3.7.1","resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.1.tgz",@@ -4693,6 +4713,13 @@"integrity": "sha512-Qe/5NJrgIOlwijpq3B7BEpzPFcgzggOTagZmkXQY4LA6bsXKTUstK7Wp12lEJ/mLKTpvIZxmIuRcLYWT6ov9lw==","dev": true},+ "file-uri-to-path": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",+ "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",+ "dev": true,+ "optional": true+ },"fill-range": {"version": "4.0.0","resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz",@@ -9521,6 +9548,7 @@"dev": true,"optional": true,"requires": {+ "bindings": "^1.5.0","nan": "^2.12.1"}},@@ -9902,6 +9930,7 @@"dev": true,"optional": true,"requires": {+ "bindings": "^1.5.0","nan": "^2.12.1"}},diff --git a/package.json b/package.jsonindex 8a404c0..02b2a3e 100644--- a/package.json+++ b/package.json@@ -39,6 +39,7 @@},"dependencies": {"mapbox-gl": "^1.10.1",+ "@russss/mapboxgl-layer-switcher": "^1.0.0","redom": "^3.24.2","title-case": "^2.1.1","lodash.invert": "^4.3.0",diff --git a/src/index.js b/src/index.jsindex 75bf31c..aad2f5d 100644--- a/src/index.js+++ b/src/index.js@@ -1,14 +1,16 @@import './index.css';+import '@russss/mapboxgl-layer-switcher/layerswitcher.css'import mapboxgl from 'mapbox-gl';import {mount} from 'redom';+import LayerSwitcher from '@russss/mapboxgl-layer-switcher';+import URLHash from '@russss/mapboxgl-layer-switcher/urlhash';+import EditButton from './editbutton.js';-import URLHash from './urlhash.js';import InfoBox from './infobox.js';import InfoPopup from './infopopup.js';import KeyControl from './key/key.js';-import LayerSwitcher from './layerswitcher/layerswitcher.js';import map_style from './style/style.json';import style_base from './style/style_base.js';@@ -71,16 +73,15 @@ function init() {//map_style['sources']['openinframap']['url'] = 'http://localhost:8081/capabilities/openinframap.json'}- var map = new mapboxgl.Map(Object.assign({+ var map = new mapboxgl.Map(url_hash.init({container: 'map',style: map_style,- hash: false,minZoom: 2,maxZoom: 17.9,center: [12, 26],- }, url_hash.getPosition()));+ }));- url_hash.onAdd(map);+ url_hash.enable(map);map.addControl(new mapboxgl.NavigationControl(), 'top-right');map.addControl(new mapboxgl.GeolocateControl({diff --git a/src/layerswitcher/layerswitcher.css b/src/layerswitcher/layerswitcher.cssdeleted file mode 100644index d1bcc0a..0000000--- a/src/layerswitcher/layerswitcher.css+++ /dev/null@@ -1,47 +0,0 @@-.layer-switcher {- min-height: 30px;- min-width: 30px;- background: url('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%228%22%20height%3D%228%22%20viewBox%3D%220%200%208%208%22%3E%0A%20%20%3Cpath%20d%3D%22M0%200v4h4v-4h-4zm5%202v3h-3v1h4v-4h-1zm2%202v3h-3v1h4v-4h-1z%22%20/%3E%0A%3C/svg%3E')- white;- background-repeat: no-repeat;- background-size: 15px 15px;- background-position: center;-}--.layer-switcher-list {- background: white;- padding-left: 8px;- padding-right: 8px;- position: relative;- display: none;- margin: 0 !important;- font-size: 15px;-}--.layer-switcher-list h3 {- margin: 0px;- margin-top: 6px;-}--.layer-switcher-list ul {- list-style: none;- padding: 0;- user-select: none;- margin-bottom: 4px;-}--.layer-switcher-list li {- display: block;- width: 100%;- height: 25px;-}--.layer-switcher-list input {- margin-left: 8px;- margin-top: 4px;- float: right;-}--.layer-switcher-list label {- float: left;-}diff --git a/src/layerswitcher/layerswitcher.js b/src/layerswitcher/layerswitcher.jsdeleted file mode 100644index 427109f..0000000--- a/src/layerswitcher/layerswitcher.js+++ /dev/null@@ -1,157 +0,0 @@-import {el, text} from 'redom';-import invert from 'lodash.invert';-import isEqual from 'lodash.isequal';-import './layerswitcher.css';--class LayerSwitcher {- constructor(layers, default_visible = []) {- this._layers = layers;- this._identifiers = this._initLayerIdentifiers();- this._default_visible = default_visible;- this._container = el('div', {class: 'mapboxgl-ctrl layer-switcher-list'});- this._container.appendChild(el('h3', 'Layers'));- this._visible = [...default_visible];- }-- _initLayerIdentifiers() {- let identifiers = {};- Object.keys(this._layers)- .sort()- .forEach(layer_name => {- let size = 1;- let ident = null;- do {- ident = layer_name.slice(0, size);- size++;- } while (ident in identifiers);- identifiers[ident] = layer_name;- });- return identifiers;- }-- _getLayerIdentifiers() {- let identifiers = [];- let id_map = invert(this._identifiers);- this._visible.sort().forEach(layer_name => {- identifiers.push(id_map[layer_name]);- });- return identifiers;- }-- _updateVisibility() {- var layers = this._map.getStyle().layers;- for (let layer of layers) {- let name = layer['id'];- for (let layer_name in this._layers) {- let pref = this._layers[layer_name];- if (name.startsWith(pref)) {- if (this._visible.includes(layer_name)) {- this._map.setLayoutProperty(name, 'visibility', 'visible');- } else {- this._map.setLayoutProperty(name, 'visibility', 'none');- }- }- }- }- if (this.urlhash) {- this.urlhash._updateHash();- }- }-- setInitialVisibility(style) {- /**- * Modify a map style before adding to the map to set initial visibility states.- * This prevents flash-of-invisible-layers.- */- for (let layer of style['layers']) {- for (let layer_name in this._layers) {- let pref = this._layers[layer_name];- if (- layer['id'].startsWith(pref) &&- !this._visible.includes(layer['id'])- ) {- if (!('layout' in layer)) {- layer['layout'] = {};- }- layer['layout']['visibility'] = 'none';- }- }- }- }-- getURLString() {- if (!isEqual(this._visible.sort(), this._default_visible.sort())) {- return this._getLayerIdentifiers().join(',');- }- return null;- }-- setURLString(string) {- if (string) {- const ids = string.split(',');- if (ids.length == 0) {- this._visible = [...this._default_visible];- } else {- this._visible = ids.map(id => this._identifiers[id]).filter(id => id);- }- } else {- this._visible = [...this._default_visible];- }- if (this._map) {- this._updateVisibility();- }- }-- onAdd(map) {- this._map = map;- if (map.isStyleLoaded()) {- this._updateVisibility();- } else {- map.on('load', () => {- this._updateVisibility();- });- }- this._createList();-- const wrapper = el('div', {- class: 'mapboxgl-ctrl mapboxgl-ctrl-group layer-switcher',- });- wrapper.appendChild(this._container);- wrapper.onmouseover = e => {- this._container.style.display = 'block';- };- wrapper.onmouseout = e => {- this._container.style.display = 'none';- };- return wrapper;- }-- _createList() {- var list = el('ul');- var i = 0;- for (let name in this._layers) {- let checkbox = el('input', {- type: 'checkbox',- id: 'layer' + i,- checked: this._visible.includes(name),- });- let label = el('label', name, {for: 'layer' + i});-- checkbox.onchange = e => {- if (e.target.checked) {- this._visible.push(name);- } else {- this._visible = this._visible.filter(item => item !== name);- }- this._updateVisibility();- };-- let li = el('li', [label, checkbox]);- list.appendChild(li);- i++;- }- this._container.appendChild(list);- }-}--export {LayerSwitcher as default};diff --git a/src/urlhash.js b/src/urlhash.jsdeleted file mode 100644index 7a699f4..0000000--- a/src/urlhash.js+++ /dev/null@@ -1,99 +0,0 @@---class URLHash {-- constructor(layerSwitcher) {- this.layerSwitcher = layerSwitcher;- this._onHashChange();- }-- onAdd(map) {- this._map = map;- map.on('moveend', () => {- this._updateHash();- });-- window.addEventListener(- 'hashchange',- () => {- this._onHashChange();- },- false,- );- }-- _onHashChange() {- const loc = window.location.hash.replace('#', '').split('/');- if (loc.length >= 3) {- let layerString = "";- if (this._map) {- this._map.jumpTo({- center: [+loc[2], +loc[1]],- zoom: +loc[0],- });- }- for (let i = 3; i < loc.length; i++) {- let component = loc[i];- let matches = component.match(/([a-z])=(.*)/);- if (matches) {- if (matches[1] == 'm') {- markerString = matches[2];- } else {- return false;- }- } else {- layerString = component;- }- }- this.layerSwitcher.setURLString(layerString);- return true;- }- this.layerSwitcher.setURLString("");- return false;- }-- _updateHash() {- try {- window.history.replaceState(- window.history.state,- '',- this.getHashString(),- );- } catch (e) {- console.log(e);- }- }-- getHashString() {- const center = this._map.getCenter(),- zoom = Math.round(this._map.getZoom() * 100) / 100,- // derived from equation: 512px * 2^z / 360 / 10^d < 0.5px- precision = Math.ceil(- (zoom * Math.LN2 + Math.log(512 / 360 / 0.5)) / Math.LN10,- ),- m = Math.pow(10, precision),- lng = Math.round(center.lng * m) / m,- lat = Math.round(center.lat * m) / m,- bearing = this._map.getBearing(),- pitch = this._map.getPitch();- let hash = `#${zoom}/${lat}/${lng}`;- let layers = this.layerSwitcher.getURLString();- if (layers) {- hash += '/' + layers;- }- return hash;- }-- getPosition() {- const loc = window.location.hash.replace('#', '').split('/');- if (loc.length >= 3) {- return {- center: [+loc[2], +loc[1]],- zoom: +loc[0],- };- }- return {};- }-};--export default URLHash;