-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
429 lines (396 loc) · 11.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import Style from "ol/style/Style.js"
import Fill from "ol/style/Fill.js"
import Stroke from "ol/style/Stroke.js"
import Text from "ol/style/Text.js"
import Icon from "ol/style/Icon.js"
import RegularShape from "ol/style/RegularShape.js"
import CircleStyle from "ol/style/Circle.js"
/**
* @typedef StyleLike
* @type {object}
* @property {FillLike} fill
* @property {StrokeLike} stroke
* @property {TextLike} text
* @property {IconLike | CircleStyleLike | RegularShapeLike} image
*/
/**
* @typedef FillLike
* @type {object}
* @property {string} color
*/
/**
* @typedef StrokeLike
* @type {object}
* @property {string} color
* @property {'butt'|'round'|'square'} lineCap
* @property {'bevel'|'round'|'miter'} lineJoin
* @property {number[]} lineDash
* @property {number} lineDashOffset
* @property {number} miterLimit
* @property {number} width
*/
/**
* @typedef TextLike
* @type {object}
* @property {FillLike} backgroundFill
* @property {StrokeLike} backgroundStroke
* @property {FillLike} fill
* @property {StrokeLike} stroke
* @property {string} font
* @property {number} maxAngle
* @property {number} offsetX
* @property {number} offsetY
* @property {boolean} overflow
* @property {'point'|'line'} placement
* @property {number} scale
* @property {number} rotation
* @property {boolean} rotateWithView
* @property {string} text
* @property {string} textAlign
* @property {string} textBaseline
* @property {string} justify
* @property {number[]} padding
*/
/**
* @typedef IconLike
* @type {object}
* @property {[number,number]} anchor
* @property {string} color
* @property {[number,number]} displacement
* @property {number} opacity
* @property {number} scale
* @property {number} rotation
* @property {boolean} rotateWithView
* @property {[width:number, height:number]} size
* @property {string} src
* @property {'declutter'|'obstacle'|'none'|undefined} declutterMode
* @property {'Icon'} _type
*/
/**
* @typedef RegularShapeLike
* @type {object}
* @property {FillLike} fill
* @property {StrokeLike} stroke
* @property {number} points
* @property {number} radius
* @property {number} radius2
* @property {number} angle
* @property {[number,number]} displacement
* @property {number} rotation
* @property {boolean} rotateWithView
* @property {number} scale
* @property {'declutter'|'obstacle'|'none'|undefined} declutterMode
* @property {'RegularShape'} _type
*/
/**
* @typedef CircleStyleLike
* @type {object}
* @property {FillLike} fill
* @property {StrokeLike} stroke
* @property {number} radius
* @property {[number,number]} displacement
* @property {number} scale
* @property {number} rotation
* @property {boolean} rotateWithView
* @property {'declutter'|'obstacle'|'none'|undefined} declutterMode
* @property {'CircleStyle'} _type
*/
/**
* Mark the _type on IconLike|RegularShapeLike|CircleStyleLike object
* to identify the class when deserializing.
*/
const IMAGE_TYPE_ENUM = {
Icon: 'Icon',
CircleStyle: 'CircleStyle',
RegularShape: 'RegularShape'
}
/**
* serialize ol.style.Fill instance into FillLike object.
* @param olFill {Fill}
* @return {FillLike}
*/
export function serializeFill(olFill) {
if (olFill instanceof Fill) {
if (olFill.getColor()) {
return {
color: olFill.getColor()
}
}
}
}
/**
* construct ol.style.Fill instance use FillLike object.
* @param fillLike {FillLike}
* @return {Fill}
*/
export function constructFill(fillLike) {
if (fillLike?.color) return tryConstruct(Fill, fillLike)
}
/**
* serialize ol.style.Stroke instance into StrokeLike object.
* @param olStroke {Stroke}
* @return {StrokeLike}
*/
export function serializeStroke(olStroke) {
if (olStroke instanceof Stroke) {
return trimEmptyProperty({
color: olStroke.getColor(),
lineCap: olStroke.getLineCap(),
lineDash: olStroke.getLineDash(),
lineDashOffset: olStroke.getLineDashOffset(),
lineJoin: olStroke.getLineJoin(),
miterLimit: olStroke.getMiterLimit(),
width: olStroke.getWidth()
})
}
}
/**
* construct ol.style.Stroke instance use StrokeLike object.
* @param strokeLike {StrokeLike}
* @return {Stroke}
*/
export function constructStroke(strokeLike) {
if (strokeLike) return tryConstruct(Stroke, strokeLike)
}
/**
* serialize ol.style.Text instance into TextLike object.
* @param olText {Text}
* @return {TextLike}
*/
export function serializeText(olText) {
if (olText instanceof Text) {
return trimEmptyProperty({
backgroundFill: serializeFill(olText.getBackgroundFill()),
backgroundStroke: serializeStroke(olText.getBackgroundStroke()),
fill: serializeFill(olText.getFill()),
stroke: serializeStroke(olText.getStroke()),
font: olText.getFont(),
maxAngle: olText.getMaxAngle(),
offsetX: olText.getOffsetX(),
offsetY: olText.getOffsetY(),
overflow: olText.getOverflow(),
placement: olText.getPlacement(),
scale: olText.getScale(),
rotation: olText.getRotation(),
rotateWithView: olText.getRotateWithView(),
text: olText.getText(),
textAlign: olText.getTextAlign(),
textBaseline: olText.getTextBaseline(),
justify: olText.getJustify(),
padding: olText.getPadding()
})
}
}
/**
* construct ol.style.Text instance use TextLike object.
* @param textLike {TextLike}
* @return {Text}
*/
export function constructText(textLike) {
if (!textLike) return undefined
const {
backgroundFill,
backgroundStroke,
fill,
stroke,
...props
} = textLike
return tryConstruct(Text, {
...props,
backgroundFill: constructFill(backgroundFill),
backgroundStroke: constructStroke(backgroundStroke),
fill: constructFill(fill),
stroke: constructStroke(stroke)
})
}
/**
* serialize ol.style.Icon instance into IconLike object.
* @param olIcon {Icon}
* @return {IconLike}
*/
export function serializeIcon(olIcon) {
if (olIcon instanceof Icon) {
if (!olIcon.getSrc()) return undefined
return trimEmptyProperty({
_type: IMAGE_TYPE_ENUM.Icon,
anchor: olIcon.getAnchor(),
color: olIcon.getColor(),
displacement: olIcon.getDisplacement(),
opacity: olIcon.getOpacity(),
scale: olIcon.getScale(),
rotateWithView: olIcon.getRotateWithView(),
rotation: olIcon.getRotation(),
size: olIcon.getSize(),
src: olIcon.getSrc(),
declutterMode: olIcon.getDeclutterMode()
})
}
}
/**
* construct ol.style.Icon instance use IconLike object.
* @param iconLike {IconLike}
* @return {Icon}
*/
export function constructIcon(iconLike) {
if (!iconLike) return undefined
if (!iconLike.src) return undefined
return tryConstruct(Icon, iconLike)
}
/**
* serialize ol.style.RegularShape instance into RegularShapeLike object.
* @param regularShape {RegularShape}
* @return {RegularShapeLike}
*/
export function serialRegularShape(regularShape) {
if (regularShape instanceof RegularShape) {
return trimEmptyProperty({
_type: IMAGE_TYPE_ENUM.RegularShape,
//
fill: serializeFill(regularShape.getFill()),
stroke: serializeStroke(regularShape.getStroke()),
//
points: regularShape.getPoints(),
radius: regularShape.getRadius(),
radius2: regularShape.getRadius2(),
angle: regularShape.getAngle(),
displacement: regularShape.getDisplacement(),
rotation: regularShape.getRotation(),
rotateWithView: regularShape.getRotateWithView(),
scale: regularShape.getScale(),
declutterMode: regularShape.getDeclutterMode()
})
}
}
/**
* construct ol.style.RegularShape instance use RegularShapeLike object.
* @param regularShapeLike {RegularShapeLike}
* @return {RegularShape}
*/
export function constructRegularShape(regularShapeLike) {
if (!regularShapeLike) return undefined
const { fill, stroke, ...props } = regularShapeLike
return tryConstruct(RegularShape, {
...props,
fill: constructFill(fill),
stroke: constructStroke(stroke)
})
}
/**
* serialize ol.style.CircleStyle instance into CircleStyleLike object.
* @param circleStyle {CircleStyle}
* @return {CircleStyleLike}
*/
export function serialCircleShape(circleStyle) {
if (circleStyle instanceof CircleStyle) {
return trimEmptyProperty({
_type: IMAGE_TYPE_ENUM.CircleStyle,
//
fill: serializeFill(circleStyle.getFill()),
stroke: serializeStroke(circleStyle.getStroke()),
//
radius: circleStyle.getRadius(),
displacement: circleStyle.getDisplacement(),
scale: circleStyle.getScale(),
rotation: circleStyle.getRotation(),
rotateWithView: circleStyle.getRotateWithView(),
declutterMode: circleStyle.getDeclutterMode()
})
}
}
/**
* construct ol.style.CircleStyle instance use CircleStyleLike object.
* @param circleStyleLike {CircleStyleLike}
* @return {CircleStyle}
*/
export function constructCircleStyle(circleStyleLike) {
if (!circleStyleLike) return undefined
const { fill, stroke, ...props } = circleStyleLike
return tryConstruct(CircleStyle, {
...props,
fill: constructFill(fill),
stroke: constructStroke(stroke)
})
}
/**
* serialize ol.style.Image sub class instance into ImageLike object.
* CircleStyle is RegularShape's sub class, handle CircleStyle first.
* @param olImage {Icon|RegularShape|CircleStyle}
* @return {IconLike|RegularShapeLike|CircleStyleLike}
*/
export function serializeImage(olImage) {
if (olImage instanceof Icon) {
return serializeIcon(olImage)
}
if (olImage instanceof CircleStyle) {
return serialCircleShape(olImage)
}
if (olImage instanceof RegularShape) {
return serialRegularShape(olImage)
}
}
/**
* construct ol.style.Image sub class instance use ImageLike object.
* @param imageLike {IconLike|RegularShapeLike|CircleStyleLike}
* @return {Icon|RegularShape|CircleStyle}
*/
export function constructImage(imageLike) {
if (!imageLike) return undefined
switch (imageLike._type) {
case IMAGE_TYPE_ENUM.Icon:
return constructIcon(imageLike)
case IMAGE_TYPE_ENUM.CircleStyle:
return constructCircleStyle(imageLike)
case IMAGE_TYPE_ENUM.RegularShape:
return constructRegularShape(imageLike)
default:
return constructIcon(imageLike)
}
}
/**
* serialize ol.style.Style instance into StyleLike object.
* @param olStyle {Style}
* @return {StyleLike}
*/
export function serializeStyle(olStyle) {
if (olStyle instanceof Style) {
return trimEmptyProperty({
image: serializeImage(olStyle.getImage()),
fill: serializeFill(olStyle.getFill()),
stroke: serializeStroke(olStyle.getStroke()),
text: serializeText(olStyle.getText())
})
}
}
/**
* construct ol.style.Style instance use StyleLike object.
* return undefined on construction failure.
* @param styleLike {StyleLike}
* @return {Style}
*/
export function constructStyle(styleLike) {
if (!styleLike) return undefined
const { image, fill, stroke, text } = styleLike
return tryConstruct(Style, {
fill: constructFill(fill),
stroke: constructStroke(stroke),
text: constructText(text),
image: constructImage(image),
})
}
/**
* remove the null/undefined-valued 1 depth property from an object.
*/
function trimEmptyProperty(obj) {
return obj && Object.keys(obj)
.filter((key) => obj[key] !== null && obj[key] !== undefined)
.reduce((acc, key) => ({ ...acc, [key]: obj[key] }), {})
}
/**
* ignore the error and return undefined when construct failed.
*/
function tryConstruct(constructor, options) {
try {
return options ? new constructor(trimEmptyProperty(options)) : new constructor()
} catch (e) {
}
}