@@ -4,9 +4,9 @@ import { Map, Feature, View, Geolocation } from 'ol'
4
4
import 'ol-ext/filter/Base'
5
5
import { Geometry , Point } from 'ol/geom'
6
6
import { GeoJSON , WKT } from 'ol/format'
7
- import { Layer , Tile } from 'ol/layer'
7
+ import { Layer , Tile , Image } from 'ol/layer'
8
8
import VectorLayer from 'ol/layer/Vector'
9
- import { OSM , XYZ } from 'ol/source'
9
+ import { OSM , XYZ , TileWMS , ImageWMS } from 'ol/source'
10
10
import { Style , Fill , Stroke , Circle } from 'ol/style'
11
11
import { OrderFunction } from 'ol/render'
12
12
import {
@@ -32,11 +32,15 @@ import Toggle from 'ol-ext/control/Toggle'
32
32
import Button from 'ol-ext/control/Button'
33
33
import TextButton from 'ol-ext/control/TextButton'
34
34
import LayerPopup from 'ol-ext/control/LayerPopup'
35
+ import LayerSwitcher from 'ol-ext/control/LayerSwitcher'
35
36
import Popup from 'ol-ext/overlay/Popup'
36
37
import { position } from 'ol-ext/control/control'
37
38
import { ResizeObserver } from '@juggle/resize-observer'
38
39
import VectorSource from 'ol/source/Vector'
39
40
import { FeatureLike } from 'ol/Feature'
41
+ import TileSource from 'ol/source/Tile'
42
+ import ImageSource from 'ol/source/Image'
43
+ import { Options as ImageWMSOptions } from 'ol/source/ImageWMS'
40
44
41
45
interface GttClientOption {
42
46
target : HTMLDivElement | null
@@ -46,6 +50,7 @@ interface LayerObject {
46
50
type : string
47
51
id : number
48
52
name : string
53
+ baselayer : boolean
49
54
options : object
50
55
}
51
56
@@ -54,6 +59,16 @@ interface FilterOption {
54
59
distance : boolean
55
60
}
56
61
62
+ interface TileLayerSource {
63
+ layer : typeof Tile
64
+ source : typeof OSM | typeof XYZ
65
+ }
66
+
67
+ interface ImageLayerSource {
68
+ layer : typeof Image
69
+ source : typeof ImageWMS
70
+ }
71
+
57
72
export class GttClient {
58
73
readonly map : Map
59
74
maps : Array < Map >
@@ -146,26 +161,70 @@ export class GttClient {
146
161
const layers = JSON . parse ( this . contents . layers ) as [ LayerObject ]
147
162
layers . forEach ( ( layer ) => {
148
163
const s = layer . type . split ( '.' )
149
- const tileSource = getTileSource ( s [ 1 ] , s [ 2 ] )
150
- if ( tileSource ) {
151
- const l = new Tile ( {
164
+ const layerSource = getLayerSource ( s [ 1 ] , s [ 2 ] )
165
+ const tileLayerSource = layerSource as TileLayerSource
166
+ if ( tileLayerSource ) {
167
+ const l = new ( tileLayerSource . layer ) ( {
152
168
visible : false ,
153
- source : new ( tileSource ) ( layer . options )
169
+ source : new ( tileLayerSource . source ) ( layer . options )
154
170
} )
171
+
155
172
l . set ( 'lid' , layer . id )
156
173
l . set ( 'title' , layer . name )
157
- l . set ( 'baseLayer' , true )
158
- l . on ( 'change:visible' , e => {
159
- const target = e . target as Tile < XYZ >
160
- if ( target . getVisible ( ) ) {
161
- const lid = target . get ( 'lid' )
162
- document . cookie = `_redmine_gtt_basemap=${ lid } ;path=/`
163
- }
174
+ l . set ( 'baseLayer' , layer . baselayer )
175
+ if ( layer . baselayer ) {
176
+ l . on ( 'change:visible' , e => {
177
+ const target = e . target as Tile < TileSource >
178
+ if ( target . getVisible ( ) ) {
179
+ const lid = target . get ( 'lid' )
180
+ document . cookie = `_redmine_gtt_basemap=${ lid } ;path=/`
181
+ }
182
+ } )
183
+ }
184
+ this . layerArray . push ( l )
185
+ } else if ( layerSource as ImageLayerSource ) {
186
+ const imageLayerSource = layerSource as ImageLayerSource
187
+ const l = new ( imageLayerSource . layer ) ( {
188
+ visible : false ,
189
+ source : new ( imageLayerSource . source ) ( layer . options as ImageWMSOptions )
164
190
} )
191
+
192
+ l . set ( 'lid' , layer . id )
193
+ l . set ( 'title' , layer . name )
194
+ l . set ( 'baseLayer' , layer . baselayer )
195
+ if ( layer . baselayer ) {
196
+ l . on ( 'change:visible' , e => {
197
+ const target = e . target as Image < ImageSource >
198
+ if ( target . getVisible ( ) ) {
199
+ const lid = target . get ( 'lid' )
200
+ document . cookie = `_redmine_gtt_basemap=${ lid } ;path=/`
201
+ }
202
+ } )
203
+ }
165
204
this . layerArray . push ( l )
166
- this . map . addLayer ( l )
167
205
}
168
206
} , this )
207
+
208
+ /**
209
+ * Ordering the Layers for the LayerSwitcher Control.
210
+ * BaseLayers are added first.
211
+ */
212
+ this . layerArray . forEach ( ( l :Layer ) => {
213
+ if ( l . get ( "baseLayer" ) ) {
214
+ this . map . addLayer ( l )
215
+ }
216
+ }
217
+ )
218
+
219
+ var containsOverlay = false ;
220
+
221
+ this . layerArray . forEach ( ( l :Layer ) => {
222
+ if ( ! l . get ( "baseLayer" ) ) {
223
+ this . map . addLayer ( l )
224
+ containsOverlay = true
225
+ }
226
+ }
227
+ )
169
228
}
170
229
171
230
this . setBasemap ( )
@@ -331,7 +390,15 @@ export class GttClient {
331
390
} )
332
391
333
392
// Add LayerSwitcher Image Toolbar
334
- this . map . addControl ( new LayerPopup ( ) )
393
+ if ( containsOverlay ) {
394
+ this . map . addControl ( new LayerSwitcher ( {
395
+ reordering : false
396
+ } ) )
397
+ }
398
+ else {
399
+ this . map . addControl ( new LayerPopup ( ) )
400
+ }
401
+
335
402
336
403
// Because Redmine filter functions are applied later, the Window onload
337
404
// event provides a workaround to have filters loaded before executing
@@ -1194,12 +1261,16 @@ export class GttClient {
1194
1261
}
1195
1262
}
1196
1263
1197
- const getTileSource = ( source : string , class_name : string ) : any => {
1264
+ const getLayerSource = ( source : string , class_name : string ) : TileLayerSource | ImageLayerSource | undefined => {
1198
1265
if ( source === 'source' ) {
1199
1266
if ( class_name === 'OSM' ) {
1200
- return OSM
1267
+ return { layer : Tile , source : OSM }
1201
1268
} else if ( class_name === 'XYZ' ) {
1202
- return XYZ
1269
+ return { layer : Tile , source : XYZ }
1270
+ } else if ( class_name === 'TileWMS' ) {
1271
+ return { layer : Tile , source : TileWMS }
1272
+ } else if ( class_name === 'ImageWMS' ) {
1273
+ return { layer : Image , source : ImageWMS }
1203
1274
}
1204
1275
}
1205
1276
return undefined
0 commit comments