@@ -4,7 +4,7 @@ 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
9
import { OSM , XYZ , TileWMS , ImageWMS } from 'ol/source'
10
10
import { Style , Fill , Stroke , Circle } from 'ol/style'
@@ -38,6 +38,9 @@ import { position } from 'ol-ext/control/control'
38
38
import { ResizeObserver } from '@juggle/resize-observer'
39
39
import VectorSource from 'ol/source/Vector'
40
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'
41
44
42
45
interface GttClientOption {
43
46
target : HTMLDivElement | null
@@ -56,6 +59,16 @@ interface FilterOption {
56
59
distance : boolean
57
60
}
58
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
+
59
72
export class GttClient {
60
73
readonly map : Map
61
74
maps : Array < Map >
@@ -148,27 +161,47 @@ export class GttClient {
148
161
const layers = JSON . parse ( this . contents . layers ) as [ LayerObject ]
149
162
layers . forEach ( ( layer ) => {
150
163
const s = layer . type . split ( '.' )
151
- const tileSource = getTileSource ( s [ 1 ] , s [ 2 ] )
152
- if ( tileSource ) {
153
- 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 ) ( {
168
+ visible : false ,
169
+ source : new ( tileLayerSource . source ) ( layer . options )
170
+ } )
171
+
172
+ l . set ( 'lid' , layer . id )
173
+ l . set ( 'title' , layer . name )
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 ) ( {
154
188
visible : false ,
155
- source : new ( tileSource ) ( layer . options )
156
- } )
189
+ source : new ( imageLayerSource . source ) ( layer . options as ImageWMSOptions )
190
+ } )
157
191
158
192
l . set ( 'lid' , layer . id )
159
193
l . set ( 'title' , layer . name )
160
194
l . set ( 'baseLayer' , layer . baselayer )
161
195
if ( layer . baselayer ) {
162
196
l . on ( 'change:visible' , e => {
163
- const target = e . target as Tile < XYZ >
197
+ const target = e . target as Image < ImageSource >
164
198
if ( target . getVisible ( ) ) {
165
199
const lid = target . get ( 'lid' )
166
200
document . cookie = `_redmine_gtt_basemap=${ lid } ;path=/`
167
201
}
168
202
} )
169
203
}
170
204
this . layerArray . push ( l )
171
- //this.map.addLayer(l)
172
205
}
173
206
} , this )
174
207
@@ -1228,16 +1261,16 @@ export class GttClient {
1228
1261
}
1229
1262
}
1230
1263
1231
- const getTileSource = ( source : string , class_name : string ) : any => {
1264
+ const getLayerSource = ( source : string , class_name : string ) : TileLayerSource | ImageLayerSource | undefined => {
1232
1265
if ( source === 'source' ) {
1233
1266
if ( class_name === 'OSM' ) {
1234
- return OSM
1267
+ return { layer : Tile , source : OSM }
1235
1268
} else if ( class_name === 'XYZ' ) {
1236
- return XYZ
1269
+ return { layer : Tile , source : XYZ }
1237
1270
} else if ( class_name === 'TileWMS' ) {
1238
- return TileWMS
1271
+ return { layer : Tile , source : TileWMS }
1239
1272
} else if ( class_name === 'ImageWMS' ) {
1240
- return ImageWMS
1273
+ return { layer : Image , source : ImageWMS }
1241
1274
}
1242
1275
}
1243
1276
return undefined
0 commit comments