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 theApiKeysManager from '../core/ApiKeysManager.js'; |
26 | import MapLayer from '../data/MapLayer.js'; |
27 | |
28 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
29 | /** |
30 | This class contains all the mapLayers |
31 | |
32 | See theMapLayersCollection for the one and only one instance of this class |
33 | */ |
34 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
35 | |
36 | class MapLayersCollection { |
37 | |
38 | /** |
39 | A JS map to store the mapLayers, ordered by name |
40 | @type {Map.<MapLayer>} |
41 | */ |
42 | |
43 | #mapLayers; |
44 | |
45 | /** |
46 | The mapLayer to use by default |
47 | @type {MapLayer} |
48 | */ |
49 | |
50 | #defaultMapLayer; |
51 | |
52 | /** |
53 | A guard to block a second upload of the mapLayers |
54 | @type {Boolean} |
55 | */ |
56 | |
57 | #mapLayersAdded; |
58 | |
59 | /** |
60 | The constructor |
61 | */ |
62 | |
63 | constructor ( ) { |
64 | Object.freeze ( this ); |
65 | this.#mapLayers = new Map ( ); |
66 | this.#mapLayersAdded = false; |
67 | this.#defaultMapLayer = new MapLayer ( |
68 | { |
69 | service : 'wmts', |
70 | url : 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', |
71 | name : 'OSM - Color', |
72 | toolbar : |
73 | { |
74 | text : 'OSM', |
75 | color : '\u0023ff0000', |
76 | backgroundColor : '\u0023ffffff' |
77 | }, |
78 | providerName : 'OSM', |
79 | providerKeyNeeded : false, |
80 | attribution : '' |
81 | } |
82 | ); |
83 | this.#mapLayers.set ( this.#defaultMapLayer.name, this.#defaultMapLayer ); |
84 | } |
85 | |
86 | /** |
87 | gives a MapLayer object |
88 | @param {String} mapLayerName the name of the MapLayer to give |
89 | @return {MapLayer} The asked MapLayer. If a provider key is needed and the key not available |
90 | the defaultMapLayer is returned. If the layer is not found, the defaultMapLayer |
91 | is returned |
92 | */ |
93 | |
94 | getMapLayer ( mapLayerName ) { |
95 | let mapLayer = this.#mapLayers.get ( mapLayerName ) || this.#defaultMapLayer; |
96 | if ( mapLayer.providerKeyNeeded ) { |
97 | if ( ! theApiKeysManager.hasKey ( mapLayer.providerName.toLowerCase ( ) ) ) { |
98 | mapLayer = this.#defaultMapLayer; |
99 | } |
100 | } |
101 | |
102 | return mapLayer; |
103 | } |
104 | |
105 | /** |
106 | Executes a function on each MapLayer in the collection |
107 | @param {Function} fct The function to execute |
108 | */ |
109 | |
110 | forEach ( fct ) { this.#mapLayers.forEach ( fct ); } |
111 | |
112 | /** |
113 | Add a MapLayer list to the list of available MapLayers. This method can only be called once |
114 | @param {Array.<Object>} jsonLayers the layer list to add (json object from TravelNotesLayers.json) |
115 | */ |
116 | |
117 | addMapLayers ( jsonLayers ) { |
118 | |
119 | if ( this.#mapLayersAdded ) { |
120 | return; |
121 | } |
122 | jsonLayers.forEach ( |
123 | jsonLayer => { |
124 | const newLayer = new MapLayer ( jsonLayer ); |
125 | if ( ! this.#mapLayers.get ( newLayer.name ) ) { |
126 | this.#mapLayers.set ( newLayer.name, newLayer ); |
127 | } |
128 | } |
129 | ); |
130 | this.#mapLayersAdded = true; |
131 | } |
132 | |
133 | /** |
134 | get the defaultMapLayer |
135 | @type {MapLayer} |
136 | */ |
137 | |
138 | get defaultMapLayer ( ) { return this.#defaultMapLayer; } |
139 | } |
140 | |
141 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
142 | /** |
143 | The one and only one instance of MapLayersCollection class |
144 | @type {MapLayersCollection} |
145 | */ |
146 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
147 | |
148 | const theMapLayersCollection = new MapLayersCollection ( ); |
149 | |
150 | export default theMapLayersCollection; |
151 | |
152 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
153 |