forked from angular-ui/ui-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojson.js
135 lines (117 loc) · 5.33 KB
/
geojson.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
angular.module('ui-leaflet')
.directive('geojson', function ($timeout, leafletLogger, leafletData, leafletHelpers,
leafletWatchHelpers, leafletDirectiveControlsHelpers,leafletIterators, leafletGeoJsonEvents) {
var _maybeWatch = leafletWatchHelpers.maybeWatch,
_defaultWatchOptions = leafletHelpers.watchOptions,
_extendDirectiveControls = leafletDirectiveControlsHelpers.extend,
hlp = leafletHelpers,
$it = leafletIterators,
modelChangedFromDirective = false;
// $log = leafletLogger;
return {
restrict: "A",
scope: false,
replace: false,
require: 'leaflet',
link: function(scope, element, attrs, controller) {
var isDefined = leafletHelpers.isDefined,
leafletScope = controller.getLeafletScope(),
leafletGeoJSON = {},
_hasSetLeafletData = false;
controller.getMap().then(function(map) {
var watchOptions;
if(leafletScope.watchOptions && leafletScope.watchOptions.geojson) {
watchOptions = leafletScope.watchOptions.geojson;
}
else {
watchOptions = _defaultWatchOptions;
}
var _hookUpEvents = function(geojson, maybeName){
var onEachFeature;
if (angular.isFunction(geojson.onEachFeature)) {
onEachFeature = geojson.onEachFeature;
} else {
onEachFeature = function(feature, layer) {
if (leafletHelpers.LabelPlugin.isLoaded() && isDefined(feature.properties.description)) {
layer.bindLabel(feature.properties.description);
}
leafletGeoJsonEvents.bindEvents(attrs.id, layer, null, feature,
leafletScope, maybeName,
{resetStyleOnMouseout: geojson.resetStyleOnMouseout,
mapId: attrs.id});
};
}
return onEachFeature;
};
var isNested = (hlp.isDefined(attrs.geojsonNested) &&
hlp.isTruthy(attrs.geojsonNested));
var _clean = function(){
if(!leafletGeoJSON)
return;
var _remove = function(lObject) {
if (isDefined(lObject) && map.hasLayer(lObject)) {
map.removeLayer(lObject);
}
};
if(isNested) {
$it.each(leafletGeoJSON, function(lObject) {
_remove(lObject);
});
return;
}
_remove(leafletGeoJSON);
};
var _addGeojson = function(geojson, maybeName){
if (!(isDefined(geojson) && isDefined(geojson.data))) {
return;
}
var onEachFeature = _hookUpEvents(geojson, maybeName);
if (!isDefined(geojson.options)) {
modelChangedFromDirective = true;
geojson.options = {
style: geojson.style,
filter: geojson.filter,
onEachFeature: onEachFeature,
pointToLayer: geojson.pointToLayer
};
$timeout(function(){
modelChangedFromDirective = false;
}, hlp.watchTrapDelayMilliSec);
}
var lObject = L.geoJson(geojson.data, geojson.options);
if(maybeName && hlp.isString(maybeName)){
leafletGeoJSON[maybeName] = lObject;
}
else{
leafletGeoJSON = lObject;
}
lObject.addTo(map);
if(!_hasSetLeafletData){//only do this once and play with the same ref forever
_hasSetLeafletData = true;
leafletData.setGeoJSON(leafletGeoJSON, attrs.id);
}
};
var _create = function(model){
_clean();
if(isNested) {
if(!model || !Object.keys(model).length)
return;
$it.each(model, function(m, name) {
//name could be layerName and or groupName
//for now it is not tied to a layer
_addGeojson(m,name);
});
return;
}
_addGeojson(model);
};
_extendDirectiveControls(attrs.id, 'geojson', _create, _clean);
_maybeWatch(leafletScope,'geojson', watchOptions, function(geojson){
if(modelChangedFromDirective)
return;
_create(geojson);
});
});
}
};
});