Skip to content

Commit 380a7f2

Browse files
author
Allen Kim
committed
Merge branch 'map-pool'
2 parents f01dd4a + 6e2e32d commit 380a7f2

File tree

9 files changed

+4688
-91
lines changed

9 files changed

+4688
-91
lines changed

controllers/map-controller.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
var Attr2MapOptions;
88

99
var __MapController = function(
10-
$scope, $element, $attrs, $parse, _Attr2MapOptions_, NgMap
10+
$scope, $element, $attrs, $parse, _Attr2MapOptions_, NgMap, NgMapPool
1111
) {
1212
Attr2MapOptions = _Attr2MapOptions_;
1313
var vm = this;
1414

1515
vm.mapOptions; /** @memberof __MapController */
1616
vm.mapEvents; /** @memberof __MapController */
17-
vm.ngMapDiv; /** @memberof __MapController */
1817

1918
/**
2019
* Add an object to the collection of group
@@ -55,7 +54,10 @@
5554
if (obj.map) {
5655
var objs = obj.map[groupName];
5756
for (var name in objs) {
58-
objs[name] === obj && (delete objs[name]);
57+
if (objs[name] === obj) {
58+
google.maps.event.clearInstanceListeners(obj);
59+
delete objs[name];
60+
}
5961
}
6062

6163
/* delete from map */
@@ -107,7 +109,7 @@
107109
* @param {String} group name of group e.g., markers
108110
*/
109111
vm.objectChanged = function(group) {
110-
if (
112+
if ( vm.map &&
111113
(group == 'markers' || group == 'customMarkers') &&
112114
vm.map.zoomToIncludeMarkers == 'auto'
113115
) {
@@ -125,11 +127,11 @@
125127
*/
126128
vm.initializeMap = function() {
127129
var mapOptions = vm.mapOptions,
128-
mapEvents = vm.mapEvents,
129-
ngMapDiv = vm.ngMapDiv;
130+
mapEvents = vm.mapEvents;
130131

131132
var lazyInitMap = vm.map; //prepared for lazy init
132-
vm.map = new google.maps.Map(ngMapDiv, {});
133+
vm.map = NgMapPool.getMapInstance($element[0]);
134+
NgMap.setStyle($element[0]);
133135

134136
// set objects for lazyInit
135137
if (lazyInitMap) {
@@ -209,10 +211,6 @@
209211
vm.mapOptions = mapOptions;
210212
vm.mapEvents = mapEvents;
211213

212-
// create html <div> for map
213-
vm.ngMapDiv = NgMap.getNgMapDiv($element[0]);
214-
$element.append(vm.ngMapDiv);
215-
216214
if (options.lazyInit) { // allows controlled initialization
217215
vm.map = {id: $attrs.id}; //set empty, not real, map
218216
NgMap.addMap(vm);
@@ -221,12 +219,13 @@
221219
}
222220

223221
$element.bind('$destroy', function() {
222+
NgMapPool.returnMapInstance(vm.map);
224223
NgMap.deleteMap(vm);
225224
});
226225
}; // __MapController
227226

228227
__MapController.$inject = [
229-
'$scope', '$element', '$attrs', '$parse', 'Attr2MapOptions', 'NgMap'
228+
'$scope', '$element', '$attrs', '$parse', 'Attr2MapOptions', 'NgMap', 'NgMapPool'
230229
];
231230
angular.module('ngMap').controller('__MapController', __MapController);
232231
})();

services/ng-map-pool.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
(function() {
2+
'use strict';
3+
var mapInstances = [];
4+
var $window, $document;
5+
6+
var add = function(el) {
7+
var mapDiv = $document.createElement("div");
8+
mapDiv.style.width = "100%";
9+
mapDiv.style.height = "100%";
10+
el.appendChild(mapDiv);
11+
var map = new $window.google.maps.Map(mapDiv, {});
12+
mapInstances.push(map);
13+
return map;
14+
};
15+
16+
var find = function(el) { //jshint ignore:line
17+
var notInUseMap;
18+
for (var i=0; i<mapInstances.length; i++) {
19+
var map = mapInstances[i];
20+
if (!map.inUse) {
21+
var mapDiv = map.getDiv();
22+
el.appendChild(mapDiv);
23+
notInUseMap = map;
24+
break;
25+
}
26+
}
27+
return notInUseMap;
28+
};
29+
30+
var getMapInstance = function(el) {
31+
var map = find(el);
32+
if (!map) {
33+
map = add(el);
34+
}
35+
map.inUse = true;
36+
return map;
37+
};
38+
39+
var returnMapInstance = function(map) {
40+
map.inUse = false;
41+
};
42+
43+
var NgMapPool = function(_$document_, _$window_) {
44+
$document = _$document_[0], $window = _$window_;
45+
46+
return {
47+
mapInstances: mapInstances,
48+
getMapInstance: getMapInstance,
49+
returnMapInstance: returnMapInstance
50+
};
51+
};
52+
NgMapPool.$inject = [ '$document', '$window' ];
53+
54+
angular.module('ngMap').factory('NgMapPool', NgMapPool);
55+
56+
})();

services/ng-map.js

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
var mapControllers = {};
1313

14+
var getStyle = function(el, styleProp) {
15+
var y;
16+
if (el.currentStyle) {
17+
y = el.currentStyle[styleProp];
18+
} else if ($window.getComputedStyle) {
19+
y = $document.defaultView.
20+
getComputedStyle(el, null).
21+
getPropertyValue(styleProp);
22+
}
23+
return y;
24+
};
25+
1426
/**
1527
* @memberof NgMap
1628
* @function initMap
@@ -62,8 +74,10 @@
6274
* @returns promise
6375
*/
6476
var addMap = function(mapCtrl) {
65-
var len = Object.keys(mapControllers).length;
66-
mapControllers[mapCtrl.map.id || len] = mapCtrl;
77+
if (mapCtrl.map) {
78+
var len = Object.keys(mapControllers).length;
79+
mapControllers[mapCtrl.map.id || len] = mapCtrl;
80+
}
6781
};
6882

6983
/**
@@ -73,62 +87,9 @@
7387
*/
7488
var deleteMap = function(mapCtrl) {
7589
var len = Object.keys(mapControllers).length - 1;
76-
delete mapControllers[mapCtrl.map.id || len];
77-
};
78-
79-
/**
80-
* @memberof NgMap
81-
* @function getStyle
82-
* @param {HTMLElemnet} el html element
83-
* @param {String} styleProp style property name e.g. 'display'
84-
* @returns value of property
85-
*/
86-
var getStyle = function(el, styleProp) {
87-
var y;
88-
if (el.currentStyle) {
89-
y = el.currentStyle[styleProp];
90-
} else if ($window.getComputedStyle) {
91-
y = $document.defaultView.
92-
getComputedStyle(el, null).
93-
getPropertyValue(styleProp);
94-
}
95-
return y;
96-
};
97-
98-
/**
99-
* @memberof NgMap
100-
* @function getNgMapDiv
101-
* @param {HTMLElemnet} el html element
102-
* @returns map DIV elemnt
103-
* @desc
104-
* create a new `div` inside map tag, so that it does not touch map element
105-
* and disable drag event for the elmement
106-
*/
107-
var getNgMapDiv = function(ngMapEl) {
108-
var el = $document.createElement("div");
109-
var defaultStyle = ngMapEl.getAttribute('default-style');
110-
el.style.width = "100%";
111-
el.style.height = "100%";
112-
113-
//if style is not given to the map element, set display and height
114-
if (defaultStyle == "true") {
115-
ngMapEl.style.display = 'block';
116-
ngMapEl.style.height = '300px';
117-
} else {
118-
if (getStyle(ngMapEl, 'display') != "block") {
119-
ngMapEl.style.display = 'block';
120-
}
121-
if (getStyle(ngMapEl, 'height').match(/^(0|auto)/)) {
122-
ngMapEl.style.height = '300px';
123-
}
124-
}
125-
126-
// disable drag event
127-
el.addEventListener('dragstart', function (event) {
128-
event.preventDefault();
129-
return false;
130-
});
131-
return el;
90+
var mapId = mapCtrl.map.id || len;
91+
delete mapCtrl.map;
92+
delete mapControllers[mapId];
13293
};
13394

13495
/**
@@ -193,9 +154,30 @@
193154
};
194155
};
195156

157+
/**
158+
* @memberof NgMap
159+
* @function getMap
160+
* @param {Hash} options optional, e.g., {id: 'foo, timeout: 5000}
161+
* @returns promise
162+
*/
163+
var setStyle = function(el) {
164+
//if style is not given to the map element, set display and height
165+
var defaultStyle = el.getAttribute('default-style');
166+
if (defaultStyle == "true") {
167+
el.style.display = 'block';
168+
el.style.height = '300px';
169+
} else {
170+
if (getStyle(el, 'display') != "block") {
171+
el.style.display = 'block';
172+
}
173+
if (getStyle(el, 'height').match(/^(0|auto)/)) {
174+
el.style.height = '300px';
175+
}
176+
}
177+
};
178+
196179
angular.module('ngMap').provider('NgMap', function() {
197180
var defaultOptions = {};
198-
var useTinfoilShielding = false;
199181

200182
/**
201183
* @memberof NgMap
@@ -233,8 +215,7 @@
233215
deleteMap: deleteMap,
234216
getMap: getMap,
235217
initMap: initMap,
236-
getStyle: getStyle,
237-
getNgMapDiv: getNgMapDiv,
218+
setStyle: setStyle,
238219
getGeoLocation: getGeoLocation,
239220
observeAndSet: observeAndSet
240221
};

spec/services/ng-map-pool-spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
describe('NgMapPool', function() {
2+
'use strict';
3+
var scope, $window, NgMapPool;
4+
5+
beforeEach(module('ngMap', function($provide) { //jshint ignore:line
6+
}));
7+
8+
beforeEach(inject(function ($rootScope, _NgMapPool_, _$window_) {
9+
scope = $rootScope;
10+
NgMapPool = _NgMapPool_, $window = _$window_;
11+
$window.google = {
12+
maps: {
13+
Map: function() {
14+
this.getDiv = function() {};
15+
}
16+
}
17+
};
18+
}));
19+
20+
describe("#getMapInstance #returnMapInstance", function() {
21+
it('it should create a new map instance and return', function() {
22+
var el = {
23+
style: {},
24+
appendChild: function(){},
25+
getAttribute: function() {return "true";},
26+
currentStyle: {}
27+
};
28+
var map1, map2, map3;
29+
expect(NgMapPool.mapInstances.length).toEqual(0);
30+
map1 = NgMapPool.getMapInstance(el);
31+
expect(NgMapPool.mapInstances.length).toEqual(1);
32+
expect(map1.inUse).toEqual(true);
33+
map2 = NgMapPool.getMapInstance(el);
34+
expect(NgMapPool.mapInstances.length).toEqual(2);
35+
expect(map2.inUse).toEqual(true);
36+
map3 = NgMapPool.getMapInstance(el);
37+
expect(NgMapPool.mapInstances.length).toEqual(3);
38+
expect(map3.inUse).toEqual(true);
39+
40+
NgMapPool.returnMapInstance(map1);
41+
NgMapPool.returnMapInstance(map2);
42+
expect(NgMapPool.mapInstances.length).toEqual(3);
43+
expect(map1.inUse).toEqual(false);
44+
expect(map2.inUse).toEqual(false);
45+
expect(map3.inUse).toEqual(true);
46+
47+
var map4 = NgMapPool.getMapInstance(el);
48+
var map5 = NgMapPool.getMapInstance(el);
49+
expect(map1).toEqual(map4);
50+
expect(map2).toEqual(map5);
51+
});
52+
});
53+
54+
});

0 commit comments

Comments
 (0)