| 1 | /* |
| 2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | /* |
| 19 | Changes: |
| 20 | - v4.0.0: |
| 21 | - created from v3.6.0 |
| 22 | Doc reviewed 202208 |
| 23 | */ |
| 24 | |
| 25 | import theEventDispatcher from '../../core/lib/EventDispatcher.js'; |
| 26 | import theAttributionsUI from '../../uis/attributionsUI/AttributionsUI.js'; |
| 27 | import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js'; |
| 28 | import MapLayer from '../../data/MapLayer.js'; |
| 29 | import MapLayerButtonClickEL from './MapLayerButtonClickEL.js'; |
| 30 | import GeoLocationButtonClickEL from './GeoLocationButtonClickEL.js'; |
| 31 | import ZoomButtonClickEL from './ZoomButtonClickEL.js'; |
| 32 | import { ZERO } from '../../main/Constants.js'; |
| 33 | |
| 34 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 35 | /** |
| 36 | This class is the Layer Toolbar on the left of the viewer screen. |
| 37 | - Displays buttons to change the background maps and manages the background maps list. |
| 38 | - Displays also a geo location button and a zoom to travel button. |
| 39 | See theViewerLayersToolbar for the one and only one instance of this class |
| 40 | */ |
| 41 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 42 | |
| 43 | class ViewerLayersToolbar { |
| 44 | |
| 45 | /** |
| 46 | The toolbar |
| 47 | @type {HTMLElement} |
| 48 | */ |
| 49 | |
| 50 | #mapLayersToolbar; |
| 51 | |
| 52 | /** |
| 53 | An array with the available MapLayer objects |
| 54 | @type {Array.<MapLayer>} |
| 55 | */ |
| 56 | |
| 57 | #mapLayers; |
| 58 | |
| 59 | /** |
| 60 | The constructor |
| 61 | */ |
| 62 | |
| 63 | constructor ( ) { |
| 64 | Object.freeze ( this ); |
| 65 | this.#mapLayers = []; |
| 66 | const osmMapLayer = new MapLayer ( |
| 67 | { |
| 68 | service : 'wmts', |
| 69 | url : 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 70 | name : 'OSM - Color', |
| 71 | toolbar : |
| 72 | { |
| 73 | text : 'OSM', |
| 74 | color : '\u0023ff0000', |
| 75 | backgroundColor : '\u0023ffffff' |
| 76 | }, |
| 77 | providerName : 'OSM', |
| 78 | providerKeyNeeded : false, |
| 79 | attribution : '' |
| 80 | } |
| 81 | ); |
| 82 | this.#mapLayers.push ( osmMapLayer ); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | creates the user interface |
| 88 | */ |
| 89 | |
| 90 | createUI ( ) { |
| 91 | |
| 92 | // Toolbar |
| 93 | this.#mapLayersToolbar = theHTMLElementsFactory.create ( |
| 94 | 'div', |
| 95 | { id : 'TravelNotes-ViewerLayersToolbar' }, |
| 96 | document.body |
| 97 | ); |
| 98 | |
| 99 | // Geolocation button |
| 100 | // Don't test the https protocol. On some mobile devices with an integreted GPS |
| 101 | // the geolocation is working also on http protocol |
| 102 | theHTMLElementsFactory.create ( |
| 103 | 'div', |
| 104 | { |
| 105 | className : 'TravelNotes-ViewerLayersToolbar-Button', |
| 106 | title : 'My position', |
| 107 | textContent : '🌐', |
| 108 | style : 'color:black;background-color:white' |
| 109 | }, |
| 110 | this.#mapLayersToolbar |
| 111 | ).addEventListener ( 'click', new GeoLocationButtonClickEL ( ), false ); |
| 112 | |
| 113 | // Zoom button |
| 114 | theHTMLElementsFactory.create ( |
| 115 | 'div', |
| 116 | { |
| 117 | className : 'TravelNotes-ViewerLayersToolbar-Button', |
| 118 | title : 'Zoom on the travel', |
| 119 | textContent : '🔍', |
| 120 | style : 'color:black;background-color:white' |
| 121 | }, |
| 122 | this.#mapLayersToolbar |
| 123 | ).addEventListener ( 'click', new ZoomButtonClickEL ( ), false ); |
| 124 | |
| 125 | // MapLayer buttons |
| 126 | const mapLayerButtonClickEL = new MapLayerButtonClickEL ( this.#mapLayers ); |
| 127 | for ( let mapLayerCounter = 0; mapLayerCounter < this.#mapLayers.length; mapLayerCounter ++ ) { |
| 128 | const mapLayer = this.#mapLayers [ mapLayerCounter ]; |
| 129 | theHTMLElementsFactory.create ( |
| 130 | 'div', |
| 131 | { |
| 132 | className : 'TravelNotes-ViewerLayersToolbar-Button', |
| 133 | title : mapLayer.name, |
| 134 | dataset : { MapLayerId : mapLayerCounter }, |
| 135 | textContent : mapLayer.toolbarButtonData.text, |
| 136 | style : 'color:' + |
| 137 | mapLayer.toolbarButtonData.color + ';background-color:' + |
| 138 | mapLayer.toolbarButtonData.backgroundColor |
| 139 | }, |
| 140 | this.#mapLayersToolbar |
| 141 | ).addEventListener ( 'click', mapLayerButtonClickEL, false ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | Set a layer as background map. If the layer is not found, the 'OSM - Color' layer is set |
| 147 | @param {String} layerName the name of the layer to set or the index of theMapLayer in the #mapLayers |
| 148 | */ |
| 149 | |
| 150 | setMapLayer ( layerName ) { |
| 151 | const newLayer = |
| 152 | ( layerName.match ( /^[0-9]$/ ) ) |
| 153 | ? |
| 154 | this.#mapLayers [ Number.parseInt ( layerName ) ] || this.#mapLayers [ ZERO ] |
| 155 | : |
| 156 | this.#mapLayers.find ( layer => layer.name === layerName ) || this.#mapLayers [ ZERO ]; |
| 157 | theEventDispatcher.dispatch ( 'layerchange', { layer : newLayer } ); |
| 158 | theAttributionsUI.attributions = newLayer.attribution; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | Add a layer list to the list of available layers |
| 163 | @param {JsonObject} jsonLayers the layer list to add |
| 164 | */ |
| 165 | |
| 166 | addMapLayers ( jsonLayers ) { |
| 167 | jsonLayers.forEach ( |
| 168 | jsonLayer => { |
| 169 | const newLayer = new MapLayer ( jsonLayer ); |
| 170 | if ( ! newLayer.providerKeyNeeded ) { |
| 171 | this.#mapLayers.push ( newLayer ); |
| 172 | } |
| 173 | } |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 179 | /** |
| 180 | The one and only one instance of ViewerLayersToolbar class |
| 181 | @type {ViewerLayersToolbar} |
| 182 | */ |
| 183 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 184 | |
| 185 | const theViewerLayersToolbar = new ViewerLayersToolbar ( ); |
| 186 | |
| 187 | export default theViewerLayersToolbar; |
| 188 | |
| 189 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 190 |