-
Notifications
You must be signed in to change notification settings - Fork 20
/
angular-ymaps.js
231 lines (228 loc) · 7.94 KB
/
angular-ymaps.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
/*global angular*/
angular.module('ymaps', [])
.factory('$script', ['$q', '$rootScope', function ($q, $rootScope) {
"use strict";
//классический кроссбраузерный способ подключить внешний скрипт
function loadScript(path, callback) {
var el = document.createElement("script");
el.onload = el.onreadystatechange = function () {
if (el.readyState && el.readyState !== "complete" &&
el.readyState !== "loaded") {
return;
}
// если все загрузилось, то снимаем обработчик и выбрасываем callback
el.onload = el.onreadystatechange = null;
if(angular.isFunction(callback)) {
callback();
}
};
el.async = true;
el.src = path;
document.getElementsByTagName('body')[0].appendChild(el);
}
var loadHistory = [], //кэш загруженных файлов
pendingPromises = {}; //обещания на текущие загруки
return function(url) {
var deferred = $q.defer();
if(loadHistory.indexOf(url) !== -1) {
deferred.resolve();
}
else if(pendingPromises[url]) {
return pendingPromises[url];
} else {
loadScript(url, function() {
delete pendingPromises[url];
loadHistory.push(url);
//обязательно использовать `$apply`, чтобы сообщить
//angular о том, что что-то произошло
$rootScope.$apply(function() {
deferred.resolve();
});
});
pendingPromises[url] = deferred.promise;
}
return deferred.promise;
};
}])
.factory('ymapsLoader', ['$window', '$timeout', '$script', 'ymapsConfig', function($window, $timeout, $script, ymapsConfig) {
"use strict";
var scriptPromise;
return {
ready: function(callback) {
if(!scriptPromise) {
scriptPromise = $script(ymapsConfig.apiUrl).then(function() {
return $window.ymaps;
});
}
scriptPromise.then(function(ymaps) {
ymaps.ready(function() {
$timeout(function() {callback(ymaps);});
});
});
}
};
}])
.constant('ymapsConfig', {
apiUrl: '//api-maps.yandex.ru/2.1/?load=package.standard,package.clusters&mode=release&lang=ru-RU&ns=ymaps',
mapBehaviors: ['default'],
mapControls: ['default'],
markerOptions: {
preset: 'islands#darkgreenIcon'
},
clusterOptions: {
preset: 'islands#darkGreenClusterIcons'
},
fitMarkers: true,
fitMarkersZoomMargin: 40,
clusterize: false
})
//brought from underscore http://underscorejs.org/#debounce
.value('debounce', function (func, wait) {
"use strict";
var timeout = null;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
})
.controller('YmapController', ['$scope', '$element', 'ymapsLoader', 'ymapsConfig', 'debounce', function ($scope, $element, ymapsLoader, config, debounce) {
"use strict";
function initAutoFit(map, collection, ymaps) {
collection.events.add('boundschange', debounce(function () {
if(collection.getLength() > 0) {
var maxZoomBefore = map.options.get('maxZoom');
map.options.set('maxZoom', $scope.zoom);
map.setBounds(collection.getBounds(), {
checkZoomRange: true,
zoomMargin: config.fitMarkersZoomMargin
}).then(function () {
map.options.set('maxZoom', maxZoomBefore);
//we need to manually update zoom, because of http://clubs.ya.ru/mapsapi/replies.xml?item_no=59735
map.setZoom(map.getZoom());
});
}
}, 100));
}
var self = this;
ymapsLoader.ready(function(ymaps) {
self.addMarker = function(coordinates, properties, options) {
var placeMark = new ymaps.Placemark(coordinates, properties, options);
$scope.markers.add(placeMark);
return placeMark;
};
self.removeMarker = function (marker) {
$scope.markers.remove(marker);
};
self.map = new ymaps.Map($element[0], {
center : $scope.center || [0, 0],
zoom : $scope.zoom || 0,
behaviors: config.mapBehaviors,
controls: config.mapControls
});
var collection = new ymaps.GeoObjectCollection({}, config.markerOptions);
if(config.clusterize) {
$scope.markers = new ymaps.Clusterer(config.clusterOptions);
collection.add($scope.markers);
} else {
$scope.markers = collection;
}
self.map.geoObjects.add(collection);
if(config.fitMarkers) {
initAutoFit(self.map, collection, ymaps);
}
var updatingBounds, moving;
$scope.$watch('center', function(newVal) {
if(updatingBounds) {
return;
}
moving = true;
self.map.panTo(newVal).always(function() {
moving = false;
});
}, true);
$scope.$watch('zoom', function(zoom) {
if(updatingBounds) {
return;
}
self.map.setZoom(zoom, {checkZoomRange: true});
});
self.map.events.add('boundschange', function(event) {
if(moving) {
return;
}
//noinspection JSUnusedAssignment
updatingBounds = true;
$scope.$apply(function() {
$scope.center = event.get('newCenter');
$scope.zoom = event.get('newZoom');
});
updatingBounds = false;
});
});
}])
.directive('yandexMap', ['ymapsLoader', function (ymapsLoader) {
"use strict";
return {
restrict: 'EA',
terminal: true,
transclude: true,
scope: {
center: '=',
zoom: '='
},
link: function($scope, element, attrs, ctrl, transcludeFn) {
ymapsLoader.ready(function() {
transcludeFn(function( copy ) {
element.append(copy);
});
});
},
controller: 'YmapController'
};
}])
.directive('ymapMarker', function () {
"use strict";
return {
restrict: "EA",
require : '^yandexMap',
scope : {
coordinates: '=',
index: '=',
properties: '=',
options: '='
},
link: function ($scope, elm, attr, mapCtrl) {
var marker;
function pickMarker() {
var coord = [
parseFloat($scope.coordinates[0]),
parseFloat($scope.coordinates[1])
];
if (marker) {
mapCtrl.removeMarker(marker);
}
marker = mapCtrl.addMarker(coord, angular.extend({iconContent: $scope.index}, $scope.properties), $scope.options);
}
$scope.$watch("index", function (newVal) {
if (marker) {
marker.properties.set('iconContent', newVal);
}
});
$scope.$watch("coordinates", function (newVal) {
if (newVal) {
pickMarker();
}
}, true);
$scope.$on('$destroy', function () {
if (marker) {
mapCtrl.removeMarker(marker);
}
});
}
};
});