Commit 7f53d531639967a18bbe6a1d298eb0e8d95cd9b4
Committed by
GitHub
1 parent
76300e40
[3.0] Fixed polygon functionality (#2747)
* Add default setting and title for group * Fix polygon: updateColor, functions tooltip and color polygon, update coordinate polygon * Improvement code * Refactoring code
Showing
4 changed files
with
47 additions
and
37 deletions
... | ... | @@ -14,7 +14,7 @@ |
14 | 14 | /// limitations under the License. |
15 | 15 | /// |
16 | 16 | |
17 | -import L, { LatLngBounds, LatLngTuple, markerClusterGroup, MarkerClusterGroupOptions, FeatureGroup, LayerGroup } from 'leaflet'; | |
17 | +import L, { FeatureGroup, LatLngBounds, LatLngTuple, markerClusterGroup, MarkerClusterGroupOptions } from 'leaflet'; | |
18 | 18 | |
19 | 19 | import 'leaflet-providers'; |
20 | 20 | import 'leaflet.markercluster/dist/leaflet.markercluster'; |
... | ... | @@ -32,8 +32,7 @@ import { BehaviorSubject, Observable } from 'rxjs'; |
32 | 32 | import { filter } from 'rxjs/operators'; |
33 | 33 | import { Polyline } from './polyline'; |
34 | 34 | import { Polygon } from './polygon'; |
35 | -import { DatasourceData } from '@app/shared/models/widget.models'; | |
36 | -import { safeExecute, createTooltip } from '@home/components/widget/lib/maps/maps-utils'; | |
35 | +import { createTooltip, safeExecute } from '@home/components/widget/lib/maps/maps-utils'; | |
37 | 36 | |
38 | 37 | export default abstract class LeafletMap { |
39 | 38 | |
... | ... | @@ -379,13 +378,13 @@ export default abstract class LeafletMap { |
379 | 378 | |
380 | 379 | // Polygon |
381 | 380 | |
382 | - updatePolygons(polyData: DatasourceData[]) { | |
383 | - polyData.forEach((data: DatasourceData) => { | |
384 | - if (data.data.length && data.dataKey.name === this.options.polygonKeyName) { | |
385 | - if (typeof (data?.data[0][1]) === 'string') { | |
386 | - data.data = JSON.parse(data.data[0][1]) as LatLngTuple[]; | |
381 | + updatePolygons(polyData: FormattedData[]) { | |
382 | + polyData.forEach((data: FormattedData) => { | |
383 | + if (data.hasOwnProperty(this.options.polygonKeyName)) { | |
384 | + if (typeof (data[this.options.polygonKeyName]) === 'string') { | |
385 | + data[this.options.polygonKeyName] = JSON.parse(data[this.options.polygonKeyName]) as LatLngTuple[]; | |
387 | 386 | } |
388 | - if (this.polygons.get(data.datasource.entityName)) { | |
387 | + if (this.polygons.get(data.$datasource.entityName)) { | |
389 | 388 | this.updatePolygon(data, polyData, this.options); |
390 | 389 | } |
391 | 390 | else { |
... | ... | @@ -395,16 +394,16 @@ export default abstract class LeafletMap { |
395 | 394 | }); |
396 | 395 | } |
397 | 396 | |
398 | - createPolygon(polyData: DatasourceData, dataSources: DatasourceData[], settings: PolygonSettings) { | |
397 | + createPolygon(polyData: FormattedData, dataSources: FormattedData[], settings: PolygonSettings) { | |
399 | 398 | this.ready$.subscribe(() => { |
400 | 399 | const polygon = new Polygon(this.map, polyData, dataSources, settings); |
401 | 400 | const bounds = polygon.leafletPoly.getBounds(); |
402 | 401 | this.fitBounds(bounds); |
403 | - this.polygons.set(polyData.datasource.entityName, polygon); | |
402 | + this.polygons.set(polyData.$datasource.entityName, polygon); | |
404 | 403 | }); |
405 | 404 | } |
406 | 405 | |
407 | - updatePolygon(polyData: DatasourceData, dataSources: DatasourceData[], settings: PolygonSettings) { | |
406 | + updatePolygon(polyData: FormattedData, dataSources: FormattedData[], settings: PolygonSettings) { | |
408 | 407 | this.ready$.subscribe(() => { |
409 | 408 | const poly = this.polygons.get(polyData.datasource.entityName); |
410 | 409 | poly.updatePolygon(polyData.data, dataSources, settings); | ... | ... |
... | ... | @@ -217,6 +217,7 @@ export class MapWidgetController implements MapWidgetInterface { |
217 | 217 | tooltipFunction: parseFunction(settings.tooltipFunction, functionParams), |
218 | 218 | colorFunction: parseFunction(settings.colorFunction, functionParams), |
219 | 219 | polygonColorFunction: parseFunction(settings.polygonColorFunction, functionParams), |
220 | + polygonTooltipFunction: parseFunction(settings.polygonTooltipFunction, functionParams), | |
220 | 221 | markerImageFunction: parseFunction(settings.markerImageFunction, ['data', 'images', 'dsData', 'dsIndex']), |
221 | 222 | labelColor: this.ctx.widgetConfig.color, |
222 | 223 | polygonKeyName: settings.polKeyName ? settings.polKeyName : settings.polygonKeyName, |
... | ... | @@ -236,7 +237,7 @@ export class MapWidgetController implements MapWidgetInterface { |
236 | 237 | if (this.drawRoutes) |
237 | 238 | this.map.updatePolylines(parseArray(this.data)); |
238 | 239 | if (this.settings.showPolygon) { |
239 | - this.map.updatePolygons(this.data); | |
240 | + this.map.updatePolygons(parseData(this.data)); | |
240 | 241 | } |
241 | 242 | if (this.settings.draggableMarker) { |
242 | 243 | this.map.setDataSources(parseData(this.data)); | ... | ... |
... | ... | @@ -16,8 +16,7 @@ |
16 | 16 | |
17 | 17 | import L, { LatLngExpression, LatLngTuple, LeafletMouseEvent } from 'leaflet'; |
18 | 18 | import { createTooltip, parseWithTranslation, safeExecute } from './maps-utils'; |
19 | -import { PolygonSettings } from './map-models'; | |
20 | -import { DatasourceData } from '@app/shared/models/widget.models'; | |
19 | +import { FormattedData, PolygonSettings } from './map-models'; | |
21 | 20 | |
22 | 21 | export class Polygon { |
23 | 22 | |
... | ... | @@ -26,19 +25,22 @@ export class Polygon { |
26 | 25 | data; |
27 | 26 | dataSources; |
28 | 27 | |
29 | - constructor(public map, polyData: DatasourceData, dataSources, private settings: PolygonSettings) { | |
30 | - this.leafletPoly = L.polygon(polyData.data, { | |
31 | - fill: true, | |
32 | - fillColor: settings.polygonColor, | |
33 | - color: settings.polygonStrokeColor, | |
34 | - weight: settings.polygonStrokeWeight, | |
35 | - fillOpacity: settings.polygonOpacity, | |
36 | - opacity: settings.polygonStrokeOpacity | |
37 | - }).addTo(this.map); | |
28 | + constructor(public map, polyData: FormattedData, dataSources, private settings: PolygonSettings) { | |
38 | 29 | this.dataSources = dataSources; |
39 | 30 | this.data = polyData; |
31 | + const polygonColor = this.getPolygonColor(settings); | |
32 | + | |
33 | + this.leafletPoly = L.polygon(polyData[this.settings.polygonKeyName], { | |
34 | + fill: true, | |
35 | + fillColor: polygonColor, | |
36 | + color: settings.polygonStrokeColor, | |
37 | + weight: settings.polygonStrokeWeight, | |
38 | + fillOpacity: settings.polygonOpacity, | |
39 | + opacity: settings.polygonStrokeOpacity | |
40 | + }).addTo(this.map); | |
41 | + | |
40 | 42 | if (settings.showPolygonTooltip) { |
41 | - this.tooltip = createTooltip(this.leafletPoly, settings, polyData.datasource); | |
43 | + this.tooltip = createTooltip(this.leafletPoly, settings, polyData.$datasource); | |
42 | 44 | this.updateTooltip(polyData); |
43 | 45 | } |
44 | 46 | if (settings.polygonClick) { |
... | ... | @@ -52,17 +54,17 @@ export class Polygon { |
52 | 54 | } |
53 | 55 | } |
54 | 56 | |
55 | - updateTooltip(data: DatasourceData) { | |
57 | + updateTooltip(data: FormattedData) { | |
56 | 58 | const pattern = this.settings.usePolygonTooltipFunction ? |
57 | 59 | safeExecute(this.settings.polygonTooltipFunction, [this.data, this.dataSources, this.data.dsIndex]) : |
58 | 60 | this.settings.polygonTooltipPattern; |
59 | 61 | this.tooltip.setContent(parseWithTranslation.parseTemplate(pattern, data, true)); |
60 | 62 | } |
61 | 63 | |
62 | - updatePolygon(data: LatLngTuple[], dataSources: DatasourceData[], settings: PolygonSettings) { | |
64 | + updatePolygon(data: LatLngTuple[], dataSources: FormattedData[], settings: PolygonSettings) { | |
63 | 65 | this.data = data; |
64 | 66 | this.dataSources = dataSources; |
65 | - this.leafletPoly.setLatLngs(data); | |
67 | + this.leafletPoly.setLatLngs(data[this.settings.polygonKeyName]); | |
66 | 68 | if (settings.showPolygonTooltip) |
67 | 69 | this.updateTooltip(this.data); |
68 | 70 | this.updatePolygonColor(settings); |
... | ... | @@ -73,11 +75,10 @@ export class Polygon { |
73 | 75 | } |
74 | 76 | |
75 | 77 | updatePolygonColor(settings: PolygonSettings) { |
76 | - const color = settings.usePolygonColorFunction ? | |
77 | - safeExecute(settings.polygonColorFunction, [this.data, this.dataSources, this.data.dsIndex]) : settings.polygonColor; | |
78 | + const polygonColor = this.getPolygonColor(settings); | |
78 | 79 | const style: L.PathOptions = { |
79 | 80 | fill: true, |
80 | - fillColor: color, | |
81 | + fillColor: polygonColor, | |
81 | 82 | color: settings.polygonStrokeColor, |
82 | 83 | weight: settings.polygonStrokeWeight, |
83 | 84 | fillOpacity: settings.polygonOpacity, |
... | ... | @@ -94,4 +95,12 @@ export class Polygon { |
94 | 95 | this.leafletPoly.setLatLngs(latLngs); |
95 | 96 | this.leafletPoly.redraw(); |
96 | 97 | } |
98 | + | |
99 | + private getPolygonColor(settings: PolygonSettings): string | null { | |
100 | + if (settings.usePolygonColorFunction) { | |
101 | + return safeExecute(settings.polygonColorFunction, [this.data, this.dataSources, this.data.dsIndex]); | |
102 | + } else { | |
103 | + return settings.polygonColor; | |
104 | + } | |
105 | + } | |
97 | 106 | } | ... | ... |
... | ... | @@ -22,7 +22,8 @@ export const googleMapSettingsSchema = |
22 | 22 | properties: { |
23 | 23 | gmApiKey: { |
24 | 24 | title: 'Google Maps API Key', |
25 | - type: 'string' | |
25 | + type: 'string', | |
26 | + default: 'AIzaSyDoEx2kaGz3PxwbI9T7ccTSg5xjdw8Nw8Q' | |
26 | 27 | }, |
27 | 28 | gmDefaultMapType: { |
28 | 29 | title: 'Default map type', |
... | ... | @@ -30,8 +31,7 @@ export const googleMapSettingsSchema = |
30 | 31 | default: 'roadmap' |
31 | 32 | } |
32 | 33 | }, |
33 | - required: [ | |
34 | - ] | |
34 | + required: [] | |
35 | 35 | }, |
36 | 36 | form: [ |
37 | 37 | 'gmApiKey', |
... | ... | @@ -69,7 +69,8 @@ export const tencentMapSettingsSchema = |
69 | 69 | properties: { |
70 | 70 | tmApiKey: { |
71 | 71 | title: 'Tencent Maps API Key', |
72 | - type: 'string' | |
72 | + type: 'string', | |
73 | + default: '84d6d83e0e51e481e50454ccbe8986b' | |
73 | 74 | }, |
74 | 75 | tmDefaultMapType: { |
75 | 76 | title: 'Default map type', |
... | ... | @@ -77,8 +78,7 @@ export const tencentMapSettingsSchema = |
77 | 78 | default: 'roadmap' |
78 | 79 | } |
79 | 80 | }, |
80 | - required: [ | |
81 | - ] | |
81 | + required: [] | |
82 | 82 | }, |
83 | 83 | form: [ |
84 | 84 | 'tmApiKey', |
... | ... | @@ -117,6 +117,7 @@ export const hereMapSettingsSchema = |
117 | 117 | }, |
118 | 118 | credentials: { |
119 | 119 | type: 'object', |
120 | + title: 'Credentials', | |
120 | 121 | properties: { |
121 | 122 | app_id: { |
122 | 123 | title: 'HERE app id', | ... | ... |