/
angulargmMapController.js
301 lines (226 loc) · 8.67 KB
/
angulargmMapController.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
describe('angulargmMapController', function() {
var scope, elm;
var mapCtrl, mapCntr;
beforeEach(function() {
module('AngularGM');
});
beforeEach(inject(function($rootScope, $controller, angulargmContainer) {
// set up scope
scope = $rootScope.$new();
scope.gmMapOptions = function() {
return {
center: new google.maps.LatLng(2, 3),
zoom: 1,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
};
scope.gmMapId = function() {
return 'test';
};
// set up element
elm = angular.element('<div gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds" gm-map-options="mapOptions">' +
'<div id="test"></div>' +
'</div');
mapCtrl = $controller('angulargmMapController', {$scope: scope, $element: elm});
mapCntr = angulargmContainer;
}));
afterEach(inject(function(angulargmContainer) {
if (scope && scope.$destroy) {
scope.$destroy();
}
angulargmContainer.removeMap('test');
}));
it('constructs the map using the provided map options', function() {
expect(mapCtrl.dragging).toBeFalsy();
expect(mapCtrl.center).toEqual(new google.maps.LatLng(2, 3));
expect(mapCtrl.zoom).toEqual(1);
var map = mapCntr.getMap(scope.gmMapId());
expect(mapCtrl.bounds).toEqual(map.getBounds());
expect(mapCtrl.mapTypeId).toEqual(google.maps.MapTypeId.TERRAIN);
});
it('constructs the map using defaults when there are no options', inject(function($rootScope, $controller, angulargmDefaults) {
scope = $rootScope.$new();
scope.gmMapOptions = function() { };
scope.gmMapId = function() {
return 'test2';
};
var elm2 = angular.element('<div gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds">' +
'<div id="test2"></div>' +
'</div');
mapCtrl = $controller('angulargmMapController', {$scope: scope, $element: elm2});
expect(mapCtrl.center).toEqual(angulargmDefaults.mapOptions.center);
expect(mapCtrl.zoom).toEqual(angulargmDefaults.mapOptions.zoom);
}));
it('resets map on controller re-instantiation', inject(function($rootScope, $controller) {
var map = mapCntr.getMap(scope.gmMapId());
var scope2 = $rootScope.$new();
var gmMapOptions = scope.gmMapOptions();
var gmMapId = scope.gmMapId();
scope2.gmMapOptions = function() { return gmMapOptions };
scope2.gmMapId = function() { return gmMapId };
// move map
var newCenter = new google.maps.LatLng(gmMapOptions.center.lat() + 5,
gmMapOptions.center.lng() + 5);
map.setCenter(newCenter);
map.setZoom(gmMapOptions.zoom + 2);
map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
expect(mapCtrl.center).toEqual(newCenter); // sanity check--we actually changed something
// destroy scope
scope.$destroy();
// re-instantiate controller
mapCtrl = $controller('angulargmMapController', {$scope: scope2, $element: elm});
expect(mapCtrl.center).toEqual(gmMapOptions.center);
expect(mapCtrl.zoom).toEqual(gmMapOptions.zoom);
expect(map.getMapTypeId()).toEqual(gmMapOptions.mapTypeId);
}));
it('removes markers on scope destroy', function() {
var mapId = scope.gmMapId();
scope.$destroy();
numMarkers = 0;
mapCtrl.forEachMarker(function(marker) {
numMarkers++;
});
expect(numMarkers).toEqual(0);
});
it('adds listeners to the map', function() {
var called = false;
mapCtrl.addMapListener('center_changed', function() {
called = true;
});
google.maps.event.trigger(mapCntr.getMap(scope.gmMapId()), 'center_changed');
expect(called).toBeTruthy();
});
it('adds one time listeners to the map', function() {
var callCount = 0;
mapCtrl.addMapListenerOnce('center_changed', function() {
callCount++;
});
google.maps.event.trigger(mapCntr.getMap(scope.gmMapId()), 'center_changed');
google.maps.event.trigger(mapCntr.getMap(scope.gmMapId()), 'center_changed');
expect(callCount).toEqual(1);
});
it('adds generic listeners', function() {
var called = false;
var object = {};
mapCtrl.addListener(object, 'event', function() {
called = true;
});
google.maps.event.trigger(object, 'event');
expect(called).toBeTruthy();
});
it('adds generic one time listeners', function() {
var callCount = 0;
var object = {};
mapCtrl.addListenerOnce(object, 'event', function() {
callCount++;
});
google.maps.event.trigger(object, 'event');
google.maps.event.trigger(object, 'event');
expect(callCount).toEqual(1);
});
it('triggers map events', function() {
var callCount = 0;
mapCtrl.addMapListener('event', function() {
callCount++;
});
mapCtrl.mapTrigger('event');
expect(callCount).toEqual(1);
});
it('triggers generic events', function() {
var callCount = 0;
var object = {};
mapCtrl.addListener(object, 'event', function() {
callCount++;
});
mapCtrl.trigger(object, 'event');
expect(callCount).toEqual(1);
});
describe('marker functions', function() {
var position, positionSame, positionVeryClose, position2;
var markerOptions, markerOptionsSame, markerOptionsVeryClose, markerOptions2;
beforeEach(function() {
position = new google.maps.LatLng(1, 2);
positionSame = new google.maps.LatLng(1.0004, 2.0004);
positionVeryClose = new google.maps.LatLng(1.0005, 2.0005);
position2 = new google.maps.LatLng(3, 4);
scope = 'scope';
markerOptions = {
position: position
};
markerOptionsSame = {
position: positionSame
};
markerOptionsVeryClose = {
position: positionVeryClose
};
markerOptions2 = {
position: position2
};
mapCtrl.addMarker(scope, markerOptions);
});
describe('addMarker', function() {
it('adds new markers to the map', function() {
added = mapCtrl.addMarker(scope, markerOptions2);
expect(added).toBeTruthy();
});
it('does not add markers already on the map', function() {
var added = mapCtrl.addMarker(scope, markerOptions);
expect(added).toBeFalsy();
});
it('adds markers which differ by at least 0.0005', function() {
var added = mapCtrl.addMarker(scope, markerOptionsVeryClose);
expect(added).toBeTruthy();
});
it('does not add markers which differ less than 0.0005', function() {
var added = mapCtrl.addMarker(scope, markerOptionsSame);
expect(added).toBeFalsy();
});
});
describe('getMarker', function() {
it('retrieves markers that are on the map', function() {
var marker = mapCtrl.getMarker(scope, position.lat(), position.lng());
expect(marker.getPosition()).toEqual(markerOptions.position);
});
it('returns null for marker not on the map', function() {
var marker = mapCtrl.getMarker(scope, position2.lat(), position2.lng());
expect(marker).toBeNull();
});
it('retrives markers given a lat and lng that are within 0.0005', function() {
var marker = mapCtrl.getMarker(scope, positionSame.lat(), positionSame.lng());
expect(marker.getPosition()).toEqual(markerOptions.position);
});
it('does not retrieve marker given lat and lng more than 0.0005 away', function() {
var marker = mapCtrl.getMarker(scope, positionVeryClose.lat(), positionVeryClose.lng());
expect(marker).toBeNull();
});
});
describe('removeMarker', function() {
it('removes markers from the map', function() {
var removed = mapCtrl.removeMarker(scope, position.lat(), position.lng());
expect(removed).toBeTruthy();
expect(mapCtrl.getMarker(scope, position.lat(), position.lng())).toBeNull();
});
it('does not remove markers not on the map', function() {
var removed = mapCtrl.removeMarker(scope, position2.lat(), position2.lng());
expect(removed).toBeFalsy();
expect(mapCtrl.getMarker(scope, position.lat(), position.lng())).not.toBeNull();
});
});
it('can apply a function to each marker', function() {
markers = [];
mapCtrl.forEachMarker(function(marker) {
markers.push(marker);
});
expect(markers.length).toEqual(1);
expect(markers[0].getPosition()).toEqual(markerOptions.position);
});
it('does not apply a function to removed markers', function() {
mapCtrl.removeMarker(scope, 1, 2);
var called = false;
mapCtrl.forEachMarker(function(marker) {
called = true;
});
expect(called).toBeFalsy();
});
});
});