-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpx-map-behavior-layer-geojson.es6.js
354 lines (314 loc) · 13.3 KB
/
px-map-behavior-layer-geojson.es6.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
/**
* @license
* Copyright (c) 2018, General Electric
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';
/****************************************************************************
* BEHAVIORS
****************************************************************************/
/* Ensures the behavior namespace is created */
window.PxMapBehavior = (window.PxMapBehavior || {});
/**
*
* @polymerBehavior PxMapBehavior.GeoJSONLayer
*/
PxMapBehavior.GeoJSONLayerImpl = {
properties: {
/**
* An object formatted as a GeoJSON FeatureCollection with one or more Features.
* Each feature can be formatted as any valid GeoJSON geometry type: Point,
* LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon,
* or GeometryCollection. See the [GeoJSON spec](http://geojson.org/geojson-spec.html)
* for guidance on generating valid GeoJSON.
*
* Each feature should contain a `properties` object that can hold metadata
* about the feature. Optionally, the feature's `properties.style` can be
* set to an object that will be used to style the feature when it is drawn.
* Styles set in a feature's `properties.style` will override the styles
* set in the `featureStyle` attribute. See the `featureStyle` attribute
* documentation for a list of available style options.
*
* @type {Object}
*/
data: {
type: Object,
observer: 'shouldUpdateInst'
},
/**
* An object with settings that will be used to style each feature when
* it is added to the map. The following options are available:
*
* - {Boolean} `stroke`: [default=true] Set to false to disable borders on polygons/circles
* - {String} `color`: [default=$primary-blue] Color for polygon/circle borders
* - {Number} `weight`: [default=2] Weight for polygon/circle borders in pixels
* - {Number} `opacity`: [default=1.0] Opacity for polygon/circle borders
* - {Boolean} `fill`: [default=true] Set to false to disable filling polygons/circles
* - {String} `fillColor`: [default=$dv-light-blue] Color for polygon/circle fill
* - {Number} `fillOpacity`: [default=0.4] Opacity for polygon/circle fill
* - {String} `fillRule`: [default='evenodd'] Defines how the [inside of a shape](https://developer.mozilla.org/docs/Web/SVG/Attribute/fill-rule) is determined
* - {String} `lineCap`: [default='round'] Defines the [shape to be used](https://developer.mozilla.org/docs/Web/SVG/Attribute/stroke-linecap) at the end of the stroke
* - {String} `lineJoin`: [default='round'] Defines the [shape to be used](https://developer.mozilla.org/docs/Web/SVG/Attribute/stroke-linejoin) at the corner of a stroke
* - {String} `dashArray`: [default=null] Defines the stroke [dash pattern](https://developer.mozilla.org/docs/Web/SVG/Attribute/stroke-dasharray)
* - {String} `dashOffset`: [default=null] Defines the [distance into the dash to start the dash](https://developer.mozilla.org/docs/Web/SVG/Attribute/stroke-dashoffset)
*
* Note that styles can also be added to each feature individually (see
* the `data` attribute documentation). Styles defined on each feature will
* override the `featureStyle`.
*
* @type {Object}
*/
featureStyle: {
type: Object,
observer: 'shouldUpdateInst'
},
/**
* If set, a popup containing a feature's properties will be opened when
* the user taps that feature. The following properties will be filtered
* out: the property key `style` and its value; any properties with the
* value "unset" as a string.
*
* Note: A data-style popup will be used. This popup currently cannot be
* configured and will use its default configuration.
*
* @type {Boolean}
*/
showFeatureProperties: {
type: Boolean,
value: false,
observer: 'shouldUpdateInst'
}
},
/**
* Forces the GeoJSON layer to deeply check the `data` attribute for differences
* in the data from the last draw, and make any necessary updates. Call this
* method if you are passing an object by reference to `data` and making deep
* updates that don't trigger property observers.
*
* @return {undef}
*/
update() {
if (!this.elementInst) return;
this.shouldUpdateInst();
},
canAddInst() {
return this.data && typeof this.data === 'object' && Object.keys(this.data).length;
},
// extends the layer `addInst` method to harvest and fire events
addInst(parent) {
// Bind custom events. Events will be unbound automatically.
const addedFn = this._handleFeatureAdded.bind(this);
const removedFn = this._handleFeatureRemoved.bind(this);
this.bindEvents({
'layeradd' : addedFn,
'layerremove' : removedFn
});
// If any layers already added before events bound, manually fire layer
// added events to attach listeners/notify the world the layer is added
if (this.elementInst.getLayers().length !== 0) {
this.elementInst.eachLayer((layer) => {
this.elementInst.fire('layeradd', { layer: layer });
});
}
// Now call layer's add
PxMapBehavior.LayerImpl.addInst.call(this, parent);
},
createInst(options) {
const styleAttributeProperties = this.getInstOptions().featureStyle;
const geojsonLayer = L.geoJson(options.data, {
pointToLayer: (feature, latlng) => {
const featureProperties = feature.properties.style || {};
const attributeProperties = options.featureStyle;
const style = this._getStyle(feature, featureProperties, attributeProperties);
return new L.CircleMarker(latlng, style);
},
onEachFeature: (feature, layer) => {
if (!this.showFeatureProperties) return;
this._bindPopup(feature, layer);
},
style: (feature) => {
const featureProperties = feature.properties.style || {};
return this._getStyle(featureProperties, styleAttributeProperties);
}
});
return geojsonLayer;
},
_getStyle(featureProperties, attributeProperties) {
return {
radius: featureProperties.radius || attributeProperties.radius || 5,
color: featureProperties.color || attributeProperties.color || '#3E87E8', //primary-blue,
fillColor: featureProperties.fillColor || attributeProperties.fillColor || '#88BDE6', //$dv-light-blue
weight: featureProperties.weight || attributeProperties.weight || 2,
opacity: featureProperties.opacity || attributeProperties.opacity || 1,
fillOpacity: featureProperties.fillOpacity || attributeProperties.fillOpacity || 0.4
};
},
_bindFeaturePopups() {
if (!this.elementInst) return;
this.elementInst.eachLayer((layer) => this._bindPopup(layer.feature, layer));
},
_bindPopup(feature, layer) {
// Filter keys to remove info that should not be displayed in a popup.
// If no keys remain, do not bind a popup.
const popupDataKeys = Object.keys(feature.properties).filter(key => feature.properties.hasOwnProperty(key) && feature.properties[key] !== 'unset' && key !== 'style');
if (!popupDataKeys.length) return;
const popupData = popupDataKeys.reduce((accum, key) => {
accum[key] = feature.properties[key];
return accum;
}, {});
const popup = new PxMap.DataPopup({
title: 'Feature Properties',
data: popupData,
autoPanPadding: [1,1]
});
layer.bindPopup(popup);
},
_unbindFeaturePopups() {
if (!this.elementInst) return;
this.elementInst.eachLayer((layer) => this._unbindPopup(layer));
},
_unbindPopup(layer) {
if (typeof layer.getPopup() !== 'undefined') {
layer.unbindPopup();
}
},
/*
* Update the instance if the new data is not the same as the old OR if the
* new style is not the same as the old. (Stringifying is needed here to be
* able to do a deep equality check).
*/
updateInst(lastOptions, nextOptions) {
if (!Object.keys(nextOptions.data).length) {
this.elementInst.clearLayers();
}
else if (Object.keys(nextOptions.data).length && (lastOptions.dataHash !== nextOptions.dataHash || lastOptions.featureStyleHash !== nextOptions.featureStyleHash)) {
const styleAttributeProperties = this.getInstOptions().featureStyle;
this.elementInst.clearLayers();
this.elementInst.options.style = (feature) => {
const featureProperties = feature.properties.style || {};
return this._getStyle(featureProperties, styleAttributeProperties);
};
this.elementInst.addData(nextOptions.data);
if (nextOptions.showFeatureProperties) {
this._bindFeaturePopups();
}
}
else if (lastOptions.showFeatureProperties !== nextOptions.showFeatureProperties) {
if (nextOptions.showFeatureProperties) this._bindFeaturePopups();
if (!nextOptions.showFeatureProperties) this._unbindFeaturePopups();
}
},
getInstOptions() {
return {
data: this.data || {},
dataHash: JSON.stringify(this.data || {}),
featureStyle: this.featureStyle || {},
featureStyleHash: JSON.stringify(this.featureStyle || {}),
showFeatureProperties: this.showFeatureProperties
};
},
_handleFeatureAdded(evt) {
if (!evt || !evt.layer) return;
// Bind layer click events
evt.layer.on('click', this._handleFeatureTapped.bind(this));
evt.layer.on('popupopen', this._handleFeaturePopupOpened.bind(this));
evt.layer.on('popupclose', this._handleFeaturePopupClosed.bind(this));
const detail = {};
if (evt.layer && evt.layer.feature) {
detail.feature = evt.layer.feature;
}
this.fire('px-map-layer-geojson-feature-added', detail);
},
/**
* Fired when a feature is attached the map. Note that every feature is
* added/removed when any part of the `data` attribute is updated.
*
* * {Object} detail - Contains the event details
* * {Object|undefined} detail.feature - Object containing the feature's GeoJSON source
*
* @event px-map-layer-geojson-feature-added
*/
_handleFeatureRemoved(evt) {
if (!evt || !evt.layer) return;
// Unbind layer click events
evt.layer.off();
const detail = {};
if (evt.layer && evt.layer.feature) {
detail.feature = evt.layer.feature;
}
this.fire('px-map-layer-geojson-feature-removed', detail);
},
/**
* Fired when a feature is removed from the map. Note that every feature is
* added/removed when any part of the `data` attribute is updated.
*
* * {Object} detail - Contains the event details
* * {Object|undefined} detail.feature - Object containing the feature's GeoJSON source
*
* @event px-map-layer-geojson-feature-added
*/
_handleFeatureTapped(evt) {
const detail = {};
if (evt.target && evt.target.feature) {
detail.feature = evt.target.feature;
}
this.fire('px-map-layer-geojson-feature-tapped', detail);
},
/**
* Fired when a feature is tapped by the user.
*
* * {Object} detail - Contains the event details
* * {Object|undefined} detail.feature - Object containing the feature's GeoJSON source
*
* @event px-map-layer-geojson-feature-tapped
*/
_handleFeaturePopupOpened(evt) {
const detail = {};
if (evt.target && evt.target.feature) {
detail.feature = evt.target.feature;
}
this.fire('px-map-layer-geojson-feature-popup-opened', detail);
},
/**
* Fired when a feature's popup is opened by the user.
*
* * {Object} detail - Contains the event details
* * {Object|undefined} detail.feature - Object containing the feature's GeoJSON source
*
* @event px-map-layer-geojson-feature-popup-opened
*/
_handleFeaturePopupClosed(evt) {
const detail = {};
if (evt.target && evt.target.feature) {
detail.feature = evt.target.feature;
}
this.fire('px-map-layer-geojson-feature-popup-closed', detail);
}
/**
* Fired when a feature's popup is closed by the user.
*
* * {Object} detail - Contains the event details
* * {Object|undefined} detail.feature - Object containing the feature's GeoJSON source
*
* @event px-map-layer-geojson-feature-popup-closed
*/
};
/* Bind GeoJSONLayer behavior */
/** @polymerBehavior */
PxMapBehavior.GeoJSONLayer = [
PxMapBehavior.Layer,
PxMapBehavior.GeoJSONLayerImpl
];
})();