This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathleafletMarkersHelpers.js
596 lines (517 loc) · 29.5 KB
/
leafletMarkersHelpers.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
angular.module('ui-leaflet').service('leafletMarkersHelpers', function ($rootScope, $timeout, leafletHelpers, leafletLogger, $compile, leafletGeoJsonHelpers, leafletWatchHelpers) {
var isDefined = leafletHelpers.isDefined,
defaultTo = leafletHelpers.defaultTo,
MarkerClusterPlugin = leafletHelpers.MarkerClusterPlugin,
AwesomeMarkersPlugin = leafletHelpers.AwesomeMarkersPlugin,
VectorMarkersPlugin = leafletHelpers.VectorMarkersPlugin,
MakiMarkersPlugin = leafletHelpers.MakiMarkersPlugin,
ExtraMarkersPlugin = leafletHelpers.ExtraMarkersPlugin,
DomMarkersPlugin = leafletHelpers.DomMarkersPlugin,
safeApply = leafletHelpers.safeApply,
Helpers = leafletHelpers,
isString = leafletHelpers.isString,
isNumber = leafletHelpers.isNumber,
isObject = leafletHelpers.isObject,
groups = {},
geoHlp = leafletGeoJsonHelpers,
errorHeader = leafletHelpers.errorHeader,
maybeWatch = leafletWatchHelpers.maybeWatch,
$log = leafletLogger;
var _string = function (marker) {
//this exists since JSON.stringify barfs on cyclic
var retStr = '';
['_icon', '_latlng', '_leaflet_id', '_map', '_shadow'].forEach(function (prop) {
retStr += prop + ': ' + defaultTo(marker[prop], 'undefined') + ' \n';
});
return '[leafletMarker] : \n' + retStr;
};
var _log = function (marker, useConsole) {
var logger = useConsole ? console : $log;
logger.debug(_string(marker));
};
var existDomContainer = function (groupName) {
return angular.element(groups[groupName]._map._container).parent().length > 0;
};
var createLeafletIcon = function (iconData) {
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'awesomeMarker') {
if (!AwesomeMarkersPlugin.isLoaded()) {
$log.error(errorHeader + ' The AwesomeMarkers Plugin is not loaded.');
}
return new L.AwesomeMarkers.icon(iconData);
}
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'vectorMarker') {
if (!VectorMarkersPlugin.isLoaded()) {
$log.error(errorHeader + ' The VectorMarkers Plugin is not loaded.');
}
return new L.VectorMarkers.icon(iconData);
}
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'makiMarker') {
if (!MakiMarkersPlugin.isLoaded()) {
$log.error(errorHeader + 'The MakiMarkers Plugin is not loaded.');
}
return new L.MakiMarkers.icon(iconData);
}
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'extraMarker') {
if (!ExtraMarkersPlugin.isLoaded()) {
$log.error(errorHeader + 'The ExtraMarkers Plugin is not loaded.');
}
return new L.ExtraMarkers.icon(iconData);
}
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'div') {
return new L.divIcon(iconData);
}
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'dom') {
if (!DomMarkersPlugin.isLoaded()) {
$log.error(errorHeader + 'The DomMarkers Plugin is not loaded.');
}
var markerScope = angular.isFunction(iconData.getMarkerScope) ? iconData.getMarkerScope().$new() : $rootScope,
template = $compile(iconData.template)(markerScope),
iconDataCopy = angular.copy(iconData);
iconDataCopy.ngElement = template;
iconDataCopy.element = template[0];
if(angular.isFunction(iconData.getMarkerScope))
iconDataCopy.scope = markerScope;
return new L.DomMarkers.icon(iconDataCopy);
}
// allow for any custom icon to be used... assumes the icon has already been initialized
if (isDefined(iconData) && isDefined(iconData.type) && iconData.type === 'icon') {
return iconData.icon;
}
var base64icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAApCAYAAADAk4LOAAAGmklEQVRYw7VXeUyTZxjvNnfELFuyIzOabermMZEeQC/OclkO49CpOHXOLJl/CAURu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6ALhDpVKv9qYeZA8eM3EhfPSCmpuD0AAAAASUVORK5CYII=";
if (!isDefined(iconData) || !isDefined(iconData.iconUrl)) {
return new L.Icon.Default({
iconUrl: base64icon,
shadowUrl: base64shadow,
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
}
return new L.Icon(iconData);
};
var _resetMarkerGroup = function (groupName) {
if (isDefined(groups[groupName])) {
delete groups[groupName];
}
};
var _resetMarkerGroups = function () {
groups = {};
};
var _resetUnusedMarkerGroups = function (){
for(var groupName in groups) {
if (!existDomContainer(groupName)) {
_resetMarkerGroup(groupName);
}
}
};
var _cleanDomIcon = function _cleanDomIcon(marker) {
if( marker.options.icon.options.ngElement) {
marker.options.icon.options.ngElement.remove();
}
if( marker.options.icon.options.scope) {
marker.options.icon.options.scope.$destroy();
}
};
var _deleteMarker = function (marker, map, layers) {
marker.closePopup();
// if it's a dom icon, clean it
if(marker.options.icon && marker.options.icon.options && marker.options.icon.options.type === 'dom') {
_cleanDomIcon(marker);
}
// There is no easy way to know if a marker is added to a layer, so we search for it
// if there are overlays
if (isDefined(layers) && isDefined(layers.overlays)) {
for (var key in layers.overlays) {
if (layers.overlays[key] instanceof L.LayerGroup || layers.overlays[key] instanceof L.FeatureGroup) {
if (layers.overlays[key].hasLayer(marker)) {
layers.overlays[key].removeLayer(marker);
return;
}
}
}
}
if (isDefined(groups)) {
for (var groupKey in groups) {
if (groups[groupKey].hasLayer(marker)) {
groups[groupKey].removeLayer(marker);
}
}
}
if (map.hasLayer(marker)) {
map.removeLayer(marker);
}
};
var adjustPopupPan = function(marker, map) {
var containerHeight = marker._popup._container.offsetHeight,
layerPos = new L.Point(marker._popup._containerLeft, -containerHeight - marker._popup._containerBottom),
containerPos = map.layerPointToContainerPoint(layerPos);
if (containerPos !== null) {
marker._popup._adjustPan();
}
};
var compilePopup = function(marker, markerScope) {
$compile(marker._popup._contentNode)(markerScope);
};
var updatePopup = function (marker, markerScope, map) {
//The innerText should be more than 1 once angular has compiled.
//We need to keep trying until angular has compiled before we _updateLayout and _updatePosition
//This should take care of any scenario , eg ngincludes, whatever.
//Is there a better way to check for this?
var innerText = marker._popup._contentNode.innerText || marker._popup._contentNode.textContent;
if (innerText.length < 1) {
$timeout(function () {
updatePopup(marker, markerScope, map);
});
}
//cause a reflow - this is also very important - if we don't do this then the widths are from before $compile
var reflow = marker._popup._contentNode.offsetWidth;
marker._popup._updateLayout();
marker._popup._updatePosition();
if (marker._popup.options.autoPan) {
adjustPopupPan(marker, map);
}
//using / returning reflow so jshint doesn't moan
return reflow;
};
var _manageOpenPopup = function (marker, markerData, map) {
// The marker may provide a scope returning function used to compile the message
// default to $rootScope otherwise
var markerScope = angular.isFunction(markerData.getMessageScope) ? markerData.getMessageScope() : $rootScope,
compileMessage = isDefined(markerData.compileMessage) ? markerData.compileMessage : true;
if (compileMessage) {
if (!isDefined(marker._popup) || !isDefined(marker._popup._contentNode)) {
$log.error(errorHeader + 'Popup is invalid or does not have any content.');
return false;
}
compilePopup(marker, markerScope);
updatePopup(marker, markerData, map);
}
};
var _manageOpenLabel = function (marker, markerData) {
var markerScope = angular.isFunction(markerData.getMessageScope) ? markerData.getMessageScope() : $rootScope,
labelScope = angular.isFunction(markerData.getLabelScope) ? markerData.getLabelScope() : markerScope,
compileMessage = isDefined(markerData.compileMessage) ? markerData.compileMessage : true;
if (Helpers.LabelPlugin.isLoaded() && isDefined(markerData.label)) {
if (isDefined(markerData.label.options) && markerData.label.options.noHide === true) {
marker.showLabel();
}
if (compileMessage && isDefined(marker.label)) {
$compile(marker.label._container)(labelScope);
}
}
};
var _updateMarker = function (markerData, oldMarkerData, marker, name, leafletScope, layers, map) {
if (!isDefined(oldMarkerData)) {
return;
}
// Update the lat-lng property (always present in marker properties)
if (!geoHlp.validateCoords(markerData)) {
$log.warn('There are problems with lat-lng data, please verify your marker model');
_deleteMarker(marker, map, layers);
return;
}
// watch is being initialized if old and new object is the same
var isInitializing = markerData === oldMarkerData;
// Update marker rotation
if (isDefined(markerData.iconAngle) && oldMarkerData.iconAngle !== markerData.iconAngle) {
marker.setIconAngle(markerData.iconAngle);
}
// It is possible that the layer has been removed or the layer marker does not exist
// Update the layer group if present or move it to the map if not
if (!isString(markerData.layer)) {
// There is no layer information, we move the marker to the map if it was in a layer group
if (isString(oldMarkerData.layer)) {
// Remove from the layer group that is supposed to be
if (isDefined(layers.overlays[oldMarkerData.layer]) && layers.overlays[oldMarkerData.layer].hasLayer(marker)) {
layers.overlays[oldMarkerData.layer].removeLayer(marker);
marker.closePopup();
}
// Test if it is not on the map and add it
if (!map.hasLayer(marker)) {
map.addLayer(marker);
}
}
}
if ((isNumber(markerData.opacity) || isNumber(parseFloat(markerData.opacity))) && markerData.opacity !== oldMarkerData.opacity) {
// There was a different opacity so we update it
marker.setOpacity(markerData.opacity);
}
if (isString(markerData.layer) && oldMarkerData.layer !== markerData.layer) {
// If it was on a layer group we have to remove it
if (isString(oldMarkerData.layer) && isDefined(layers.overlays[oldMarkerData.layer]) && layers.overlays[oldMarkerData.layer].hasLayer(marker)) {
layers.overlays[oldMarkerData.layer].removeLayer(marker);
}
marker.closePopup();
// Remove it from the map in case the new layer is hidden or there is an error in the new layer
if (map.hasLayer(marker)) {
map.removeLayer(marker);
}
// The markerData.layer is defined so we add the marker to the layer if it is different from the old data
if (!isDefined(layers.overlays[markerData.layer])) {
$log.error(errorHeader + 'You must use a name of an existing layer');
return;
}
// Is a group layer?
var layerGroup = layers.overlays[markerData.layer];
if (!(layerGroup instanceof L.LayerGroup || layerGroup instanceof L.FeatureGroup)) {
$log.error(errorHeader + 'A marker can only be added to a layer of type "group" or "featureGroup"');
return;
}
// The marker goes to a correct layer group, so first of all we add it
layerGroup.addLayer(marker);
// The marker is automatically added to the map depending on the visibility
// of the layer, so we only have to open the popup if the marker is in the map
if (map.hasLayer(marker) && markerData.focus === true) {
marker.openPopup();
}
}
// Update the draggable property
if (markerData.draggable !== true && oldMarkerData.draggable === true && (isDefined(marker.dragging))) {
marker.dragging.disable();
}
if (markerData.draggable === true && oldMarkerData.draggable !== true) {
// The markerData.draggable property must be true so we update if there wasn't a previous value or it wasn't true
if (marker.dragging) {
marker.dragging.enable();
} else {
if (L.Handler.MarkerDrag) {
marker.dragging = new L.Handler.MarkerDrag(marker);
marker.options.draggable = true;
marker.dragging.enable();
}
}
}
// Update the icon property
if (!isObject(markerData.icon)) {
// If there is no icon property or it's not an object
if (isObject(oldMarkerData.icon)) {
if(oldMarkerData.icon.type === 'dom') { // clean previous icon if it's a dom one
_cleanDomIcon(marker);
}
// If there was an icon before restore to the default
marker.setIcon(createLeafletIcon());
marker.closePopup();
marker.unbindPopup();
if (isString(markerData.message)) {
marker.bindPopup(markerData.message, markerData.popupOptions);
}
}
}
if (isObject(markerData.icon) && isObject(oldMarkerData.icon) && !angular.equals(markerData.icon, oldMarkerData.icon)) {
var dragG = false;
if (marker.dragging) {
dragG = marker.dragging.enabled();
}
if(oldMarkerData.icon.type === 'dom') { // clean previous icon if it's a dom one
_cleanDomIcon(marker);
}
marker.setIcon(createLeafletIcon(markerData.icon));
if (dragG) {
marker.dragging.enable();
}
marker.closePopup();
marker.unbindPopup();
if (isString(markerData.message)) {
marker.bindPopup(markerData.message, markerData.popupOptions);
// if marker has been already focused, reopen popup
if (map.hasLayer(marker) && markerData.focus === true) {
marker.openPopup();
}
}
}
// Update the Popup message property
if (!isString(markerData.message) && isString(oldMarkerData.message)) {
marker.closePopup();
marker.unbindPopup();
}
// Update the label content or bind a new label if the old one has been removed.
if (Helpers.LabelPlugin.isLoaded()) {
if (isDefined(markerData.label) && isDefined(markerData.label.message)) {
if ('label' in oldMarkerData && 'message' in oldMarkerData.label && !angular.equals(markerData.label.message, oldMarkerData.label.message)) {
marker.updateLabelContent(markerData.label.message);
} else if (!angular.isFunction(marker.getLabel) || angular.isFunction(marker.getLabel) && !isDefined(marker.getLabel())) {
marker.bindLabel(markerData.label.message, markerData.label.options);
_manageOpenLabel(marker, markerData);
} else {
_manageOpenLabel(marker, markerData);
}
} else if (!('label' in markerData && !('message' in markerData.label))) {
if (angular.isFunction(marker.unbindLabel)) {
marker.unbindLabel();
}
}
}
// There is some text in the popup, so we must show the text or update existing
if (isString(markerData.message) && !isString(oldMarkerData.message)) {
// There was no message before so we create it
marker.bindPopup(markerData.message, markerData.popupOptions);
}
if (isString(markerData.message) && isString(oldMarkerData.message) && markerData.message !== oldMarkerData.message) {
// There was a different previous message so we update it
marker.setPopupContent(markerData.message);
}
// Update the focus property
var updatedFocus = false;
if (markerData.focus !== true && oldMarkerData.focus === true) {
// If there was a focus property and was true we turn it off
marker.closePopup();
updatedFocus = true;
}
// The markerData.focus property must be true so we update if there wasn't a previous value or it wasn't true
if (markerData.focus === true && ( !isDefined(oldMarkerData.focus) || oldMarkerData.focus === false) || (isInitializing && markerData.focus === true)) {
// Reopen the popup when focus is still true
marker.openPopup();
updatedFocus = true;
}
// zIndexOffset adjustment
if (oldMarkerData.zIndexOffset !== markerData.zIndexOffset) {
marker.setZIndexOffset(markerData.zIndexOffset);
}
var markerLatLng = marker.getLatLng();
var isCluster = (isString(markerData.layer) && Helpers.MarkerClusterPlugin.is(layers.overlays[markerData.layer]));
// If the marker is in a cluster it has to be removed and added to the layer when the location is changed
if (isCluster) {
// The focus has changed even by a user click or programatically
if (updatedFocus) {
// We only have to update the location if it was changed programatically, because it was
// changed by a user drag the marker data has already been updated by the internal event
// listened by the directive
if ((markerData.lat !== oldMarkerData.lat) || (markerData.lng !== oldMarkerData.lng)) {
layers.overlays[markerData.layer].removeLayer(marker);
marker.setLatLng([markerData.lat, markerData.lng]);
layers.overlays[markerData.layer].addLayer(marker);
}
} else {
// The marker has possibly moved. It can be moved by a user drag (marker location and data are equal but old
// data is diferent) or programatically (marker location and data are diferent)
if ((markerLatLng.lat !== markerData.lat) || (markerLatLng.lng !== markerData.lng)) {
// The marker was moved by a user drag
layers.overlays[markerData.layer].removeLayer(marker);
marker.setLatLng([markerData.lat, markerData.lng]);
layers.overlays[markerData.layer].addLayer(marker);
} else if ((markerData.lat !== oldMarkerData.lat) || (markerData.lng !== oldMarkerData.lng)) {
// The marker was moved programatically
layers.overlays[markerData.layer].removeLayer(marker);
marker.setLatLng([markerData.lat, markerData.lng]);
layers.overlays[markerData.layer].addLayer(marker);
} else if (isObject(markerData.icon) && isObject(oldMarkerData.icon) && !angular.equals(markerData.icon, oldMarkerData.icon)) {
layers.overlays[markerData.layer].removeLayer(marker);
layers.overlays[markerData.layer].addLayer(marker);
}
}
} else if (markerLatLng.lat !== markerData.lat || markerLatLng.lng !== markerData.lng) {
marker.setLatLng([markerData.lat, markerData.lng]);
}
};
var _getLayerModels = function (models, layerName){
if (!isDefined(models))
return;
if (layerName)
return models[layerName];
return models;
};
var _getModelFromModels = function (models, id, layerName){
if(!isDefined(models))
return;
if(!id){
$log.error(errorHeader + 'marker id missing in getMarker');
return;
}
if(layerName)
return models[layerName][id];
return models[id];
};
return {
resetMarkerGroup: _resetMarkerGroup,
resetMarkerGroups: _resetMarkerGroups,
resetUnusedMarkerGroups: _resetUnusedMarkerGroups,
deleteMarker: _deleteMarker,
manageOpenPopup: _manageOpenPopup,
manageOpenLabel: _manageOpenLabel,
createMarker: function (markerData) {
if (!isDefined(markerData) || !geoHlp.validateCoords(markerData)) {
$log.error(errorHeader + 'The marker definition is not valid.');
return;
}
var coords = geoHlp.getCoords(markerData);
if (!isDefined(coords)) {
$log.error(errorHeader + 'Unable to get coordinates from markerData.');
return;
}
var markerOptions = {
icon: createLeafletIcon(markerData.icon),
title: isDefined(markerData.title) ? markerData.title : '',
draggable: isDefined(markerData.draggable) ? markerData.draggable : false,
clickable: isDefined(markerData.clickable) ? markerData.clickable : true,
riseOnHover: isDefined(markerData.riseOnHover) ? markerData.riseOnHover : false,
zIndexOffset: isDefined(markerData.zIndexOffset) ? markerData.zIndexOffset : 0,
iconAngle: isDefined(markerData.iconAngle) ? markerData.iconAngle : 0
};
// Add any other options not added above to markerOptions
for (var markerDatum in markerData) {
if (markerData.hasOwnProperty(markerDatum) && !markerOptions.hasOwnProperty(markerDatum)) {
markerOptions[markerDatum] = markerData[markerDatum];
}
}
var marker = new L.marker(coords, markerOptions);
if (!isString(markerData.message)) {
marker.unbindPopup();
}
return marker;
},
addMarkerToGroup: function (marker, groupName, groupOptions, map) {
if (!isString(groupName)) {
$log.error(errorHeader + 'The marker group you have specified is invalid.');
return;
}
if (!MarkerClusterPlugin.isLoaded()) {
$log.error(errorHeader + "The MarkerCluster plugin is not loaded.");
return;
}
if (!isDefined(groups[groupName])) {
groups[groupName] = new L.MarkerClusterGroup(groupOptions);
map.addLayer(groups[groupName]);
}
groups[groupName].addLayer(marker);
},
listenMarkerEvents: function (marker, markerData, leafletScope, watchType, map) {
marker.on("popupopen", function (/* event */) {
safeApply(leafletScope, function () {
if (isDefined(marker._popup) || isDefined(marker._popup._contentNode)) {
markerData.focus = true;
_manageOpenPopup(marker, markerData, map);//needed since markerData is now a copy
}
});
});
marker.on("popupclose", function (/* event */) {
safeApply(leafletScope, function () {
markerData.focus = false;
});
});
marker.on("add", function (/* event */) {
safeApply(leafletScope, function () {
if ('label' in markerData)
_manageOpenLabel(marker, markerData);
});
});
},
updateMarker: _updateMarker,
addMarkerWatcher: function (marker, name, leafletScope, layers, map, watchOptions) {
var markerWatchPath = Helpers.getObjectArrayPath("markers." + name);
maybeWatch(leafletScope, markerWatchPath, watchOptions, function(markerData, oldMarkerData, clearWatch){
if (!isDefined(markerData)) {
_deleteMarker(marker, map, layers);
clearWatch();
return;
}
_updateMarker(markerData, oldMarkerData, marker, name, leafletScope, layers, map);
});
},
string: _string,
log: _log,
getModelFromModels : _getModelFromModels,
getLayerModels : _getLayerModels
};
});