Skip to content

Commit f222b39

Browse files
committed
issue allenhwkim#157, added watcher for shapes, did refactoring
1 parent 40ab9f5 commit f222b39

File tree

15 files changed

+310
-289
lines changed

15 files changed

+310
-289
lines changed

app/scripts/directives/bicycling-layer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ ngMap.directive('bicyclingLayer', ['Attr2Options', function(Attr2Options) {
4040
var layer = getLayer(options, events);
4141
mapController.addObject('bicyclingLayers', layer);
4242
parser.observeAttrSetObj(orgAttrs, attrs, layer); //observers
43+
element.bind('$destroy', function() {
44+
mapController.deleteObject('biclclingLayers', layer);
45+
});
4346
}
4447
}; // return
4548
}]);

app/scripts/directives/cloud-layer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ ngMap.directive('cloudLayer', ['Attr2Options', function(Attr2Options) {
3939
var layer = getLayer(options, events);
4040
mapController.addObject('cloudLayers', layer);
4141
parser.observeAttrSetObj(orgAttrs, attrs, layer); //observers
42+
element.bind('$destroy', function() {
43+
mapController.deleteObject('cloudLayers', layer);
44+
});
4245
}
4346
}; // return
4447
}]);

app/scripts/directives/drawing-manager.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Example:
1111
*
1212
* <map zoom="13" center="37.774546, -122.433523" map-type-id="SATELLITE">
13-
* <drawing-manager on-overlaycomplete="onMapOverlayCompleted()" position="ControlPosition.TOP_CENTER" drawingModes="POLYGON,CIRCLE" drawingControl="true" circleOptions="fillColor: '#FFFF00';fillOpacity: 1;strokeWeight: 5;clickable: false;zIndex: 1;editable: true;" ></drawing-manager>
13+
* <drawing-manager on-overlaycomplete="onMapOverlayCompleted()" position="ControlPosition.TOP_CENTER" drawingModes="POLYGON,CIRCLE" drawingControl="true" circleOptions="fillColor: '#FFFF00';fillOpacity: 1;strokeWeight: 5;clickable: false;zIndex: 1;editable: true;" ></drawing-manager>
1414
* </map>
1515
*
1616
* TODO: Add remove button.
@@ -19,45 +19,45 @@
1919
*/
2020
/*jshint -W089*/
2121
ngMap.directive('drawingManager', ['Attr2Options', function(Attr2Options) {
22-
var parser = Attr2Options;
22+
var parser = Attr2Options;
2323

24-
return {
25-
restrict: 'E',
26-
require: '^map',
24+
return {
25+
restrict: 'E',
26+
require: '^map',
2727

28-
link: function(scope, element, attrs, mapController) {
29-
var orgAttrs = parser.orgAttributes(element);
30-
var filtered = parser.filter(attrs);
31-
var options = parser.getOptions(filtered);
32-
var controlOptions = parser.getControlOptions(filtered);
33-
var events = parser.getEvents(scope, filtered);
28+
link: function(scope, element, attrs, mapController) {
29+
var orgAttrs = parser.orgAttributes(element);
30+
var filtered = parser.filter(attrs);
31+
var options = parser.getOptions(filtered);
32+
var controlOptions = parser.getControlOptions(filtered);
33+
var events = parser.getEvents(scope, filtered);
3434

35-
console.log("filtered", filtered, "options", options, 'controlOptions', controlOptions, 'events', events);
35+
console.log("filtered", filtered, "options", options, 'controlOptions', controlOptions, 'events', events);
3636

37-
/**
38-
* set options
39-
*/
40-
var drawingManager = new google.maps.drawing.DrawingManager({
41-
drawingMode: options.drawingmode,
42-
drawingControl: options.drawingcontrol,
43-
drawingControlOptions: controlOptions.drawingControlOptions,
44-
circleOptions:options.circleoptions,
45-
markerOptions:options.markeroptions,
46-
polygonOptions:options.polygonoptions,
47-
polylineOptions:options.polylineoptions,
48-
rectangleOptions:options.rectangleoptions
49-
});
37+
/**
38+
* set options
39+
*/
40+
var drawingManager = new google.maps.drawing.DrawingManager({
41+
drawingMode: options.drawingmode,
42+
drawingControl: options.drawingcontrol,
43+
drawingControlOptions: controlOptions.drawingControlOptions,
44+
circleOptions:options.circleoptions,
45+
markerOptions:options.markeroptions,
46+
polygonOptions:options.polygonoptions,
47+
polylineOptions:options.polylineoptions,
48+
rectangleOptions:options.rectangleoptions
49+
});
5050

5151

52-
/**
53-
* set events
54-
*/
55-
var events = parser.getEvents(scope, filtered);
56-
for (var eventName in events) {
57-
google.maps.event.addListener(drawingManager, eventName, events[eventName]);
58-
}
52+
/**
53+
* set events
54+
*/
55+
var events = parser.getEvents(scope, filtered);
56+
for (var eventName in events) {
57+
google.maps.event.addListener(drawingManager, eventName, events[eventName]);
58+
}
5959

60-
mapController.addObject('mapDrawingManager', drawingManager);
61-
}
62-
}; // return
60+
mapController.addObject('mapDrawingManager', drawingManager);
61+
}
62+
}; // return
6363
}]);

app/scripts/directives/info-window.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ ngMap.directive('infoWindow', ['Attr2Options', '$compile', '$timeout', function(
113113

114114
mapController.addObject('infoWindows', infoWindow);
115115
parser.observeAttrSetObj(orgAttrs, attrs, infoWindow); /* observers */
116+
element.bind('$destroy', function() {
117+
mapController.deleteObject('infoWindows', infoWindow);
118+
});
116119

117120
// show InfoWindow when initialized
118121
if (infoWindow.visible) {

app/scripts/directives/kml-layer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ ngMap.directive('kmlLayer', ['Attr2Options', function(Attr2Options) {
4949
var kmlLayer = getKmlLayer(options, events);
5050
mapController.addObject('kmlLayers', kmlLayer);
5151
parser.observeAttrSetObj(orgAttrs, attrs, kmlLayer); //observers
52+
element.bind('$destroy', function() {
53+
mapController.deleteObject('kmlLayers', kmlLayer);
54+
});
5255
}
5356
}; // return
5457
}]);

app/scripts/directives/map_controller.js

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,62 @@
88
* @property {MarkerClusterer} markerClusterer MarkerClusterer initiated within `map` directive
99
*/
1010
/*jshint -W089*/
11+
/* global google, ngMap */
1112
ngMap.MapController = function() {
13+
'use strict';
1214

1315
this.map = null;
14-
this._objects = [];
16+
this._objects = []; /* temporary collection of map objects */
1517

1618
/**
17-
* Add a marker to map and $scope.markers
19+
* Add an object to the collection of group
1820
* @memberof MapController
19-
* @name addMarker
20-
* @param {Marker} marker google map marker
21+
* @name addObject
22+
* @param groupName the name of collection that object belongs to
23+
* @param obj an object to add into a collection, i.e. marker, shape
2124
*/
22-
this.addMarker = function(marker) {
25+
this.addObject = function(groupName, obj) {
2326
/**
24-
* marker and shape are initialized before map is initialized
25-
* so, collect _objects then will init. those when map is initialized
27+
* objects, i.e. markers and shapes, are initialized before map is initialized
28+
* so, we collect those objects, then, we will add to map when map is initialized
2629
* However the case as in ng-repeat, we can directly add to map
2730
*/
28-
if (this.map) {
29-
this.map.markers = this.map.markers || {};
30-
marker.setMap(this.map);
31-
if (marker.centered) {
32-
this.map.setCenter(marker.position);
33-
}
34-
var len = Object.keys(this.map.markers).length;
35-
this.map.markers[marker.id || len] = marker;
36-
} else {
37-
this._objects.push(marker);
38-
}
39-
};
40-
41-
/**
42-
* Add a shape to map and $scope.shapes
43-
* @memberof MapController
44-
* @name addShape
45-
* @param {Shape} shape google map shape
46-
*/
47-
this.addShape = function(shape) {
48-
if (this.map) {
49-
this.map.shapes = this.map.shapes || {};
50-
shape.setMap(this.map);
51-
var len = Object.keys(this.map.shapes).length;
52-
this.map.shapes[shape.id || len] = shape;
53-
} else {
54-
this._objects.push(shape);
55-
}
56-
};
57-
58-
this.addObject = function(groupName, obj) {
5931
if (this.map) {
6032
this.map[groupName] = this.map[groupName] || {};
6133
var len = Object.keys(this.map[groupName]).length;
6234
this.map[groupName][obj.id || len] = obj;
6335
if (groupName != "infoWindows" && obj.setMap) { //infoWindow.setMap works like infoWindow.open
6436
obj.setMap(this.map);
6537
}
38+
if (obj.centered && obj.position) {
39+
this.map.setCenter(obj.position);
40+
}
6641
} else {
6742
obj.groupName = groupName;
6843
this._objects.push(obj);
6944
}
7045
}
7146

7247
/**
73-
* Add a shape to map and $scope.shapes
48+
* Delete an object from the collection and remove from map
49+
* @memgerof MapController
50+
* @name deleteFromMap
51+
* @param {Array} objs the collection of objects. i.e., map.markers
52+
* @param {Object} obj the object to be removed. i.e., marker
53+
*/
54+
this.deleteObject = function(groupName, obj) {
55+
/* delete from map */
56+
obj.map && obj.setMap(null);
57+
58+
/* delete from group */
59+
var objs = obj.map[groupName];
60+
for (var name in objs) {
61+
objs[name] === obj && (delete objs[name]);
62+
}
63+
};
64+
65+
/**
66+
* Add collected objects to map
7467
* @memberof MapController
7568
* @name addShape
7669
* @param {Shape} shape google map shape
@@ -79,13 +72,13 @@ ngMap.MapController = function() {
7972
for (var i=0; i<objects.length; i++) {
8073
var obj=objects[i];
8174
if (obj instanceof google.maps.Marker) {
82-
this.addMarker(obj);
75+
this.addObject('markers', obj);
8376
} else if (obj instanceof google.maps.Circle ||
8477
obj instanceof google.maps.Polygon ||
8578
obj instanceof google.maps.Polyline ||
8679
obj instanceof google.maps.Rectangle ||
8780
obj instanceof google.maps.GroundOverlay) {
88-
this.addShape(obj);
81+
this.addObject('shapes', obj);
8982
} else {
9083
this.addObject(obj.groupName, obj);
9184
}

app/scripts/directives/marker.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
* <marker position="the cn tower" on-click="myfunc()"></div>
3434
* </map>
3535
*/
36+
/* global google, ngMap */
3637
ngMap.directive('marker', ['Attr2Options', function(Attr2Options) {
38+
'use strict';
3739
var parser = Attr2Options;
3840

3941
var getMarker = function(options, events) {
@@ -89,28 +91,16 @@ ngMap.directive('marker', ['Attr2Options', function(Attr2Options) {
8991
var markerEvents = parser.getEvents(scope, filtered);
9092
console.log('marker options', markerOptions, 'events', markerEvents);
9193

92-
/**
93-
* set event to clean up removed marker
94-
* useful with ng-repeat
95-
*/
96-
element.bind('$destroy', function() {
97-
var markers = marker.map.markers;
98-
for (var name in markers) {
99-
if (markers[name] == marker) {
100-
delete markers[name];
101-
}
102-
}
103-
marker.setMap(null);
104-
});
105-
10694
var marker = getMarker(markerOptions, markerEvents);
107-
mapController.addMarker(marker);
95+
mapController.addObject('markers', marker);
10896

10997
/**
11098
* set observers
11199
*/
112100
parser.observeAttrSetObj(orgAttrs, attrs, marker); /* observers */
113-
101+
element.bind('$destroy', function() {
102+
mapController.deleteObject('markers', marker);
103+
});
114104
} //link
115105
}; // return
116106
}]);//

app/scripts/directives/shape.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ ngMap.directive('shape', ['Attr2Options', function(Attr2Options) {
141141
var shapeEvents = parser.getEvents(scope, filtered);
142142

143143
var shape = getShape(shapeOptions, shapeEvents);
144-
mapController.addShape(shape);
144+
mapController.addObject('shapes', shape);
145145

146146
/**
147147
* set observers
148148
*/
149149
parser.observeAttrSetObj(orgAttrs, attrs, shape);
150+
element.bind('$destroy', function() {
151+
mapController.deleteObject('shapes', shape);
152+
});
150153
}
151154
}; // return
152155
}]);

app/scripts/directives/traffic-layer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ ngMap.directive('trafficLayer', ['Attr2Options', function(Attr2Options) {
3939
var layer = getLayer(options, events);
4040
mapController.addObject('trafficLayers', layer);
4141
parser.observeAttrSetObj(orgAttrs, attrs, layer); //observers
42+
element.bind('$destroy', function() {
43+
mapController.deleteObject('trafficLayers', layer);
44+
});
4245
}
4346
}; // return
4447
}]);

app/scripts/directives/transit-layer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ ngMap.directive('transitLayer', ['Attr2Options', function(Attr2Options) {
3939
var layer = getLayer(options, events);
4040
mapController.addObject('transitLayers', layer);
4141
parser.observeAttrSetObj(orgAttrs, attrs, layer); //observers
42+
element.bind('$destroy', function() {
43+
mapController.deleteObject('transitLayers', layer);
44+
});
4245
}
4346
}; // return
4447
}]);

0 commit comments

Comments
 (0)