From 52c341fd8d2f92b66fb6ab5f74c3e27e8fec253f Mon Sep 17 00:00:00 2001 From: Andrew Sullivan Date: Wed, 24 Dec 2014 18:43:27 -0700 Subject: [PATCH] More refactoring --- dist/angular-mapbox.js | 11 ++++------- dist/angular-mapbox.min.js | 2 +- examples/index.html | 2 +- src/directives/mapbox.directive.js | 3 --- src/directives/marker.directive.js | 5 ++--- src/mapbox.service.js | 3 ++- 6 files changed, 10 insertions(+), 16 deletions(-) diff --git a/dist/angular-mapbox.js b/dist/angular-mapbox.js index b3892a7..dbe31f2 100644 --- a/dist/angular-mapbox.js +++ b/dist/angular-mapbox.js @@ -28,7 +28,8 @@ addMapInstance: addMapInstance, getMarkers: getMarkers, addMarker: addMarker, - fitMapToMarkers: fitMapToMarkers + fitMapToMarkers: fitMapToMarkers, + getOptionsForMap: getOptionsForMap }; return service; @@ -155,9 +156,6 @@ scope.map.setView([attrs.lat, attrs.lng], zoom); } - // TODO: refactor this option into mapService - scope.isClusteringMarkers = attrs.clusterMarkers !== undefined; - if(attrs.onReposition) { scope.map.on('dragend', function() { scope[attrs.onReposition](scope.map.getBounds()); @@ -254,7 +252,7 @@ marker.bindPopup(popupContent); } - if(controller.$scope.isClusteringMarkers && opts.excludeFromClustering !== true) { + if(mapboxService.getOptionsForMap(map).clusterMarkers && opts.excludeFromClustering !== true) { controller.$scope.clusterGroup.addLayer(marker); } else { marker.addTo(map); @@ -267,7 +265,6 @@ } mapboxService.addMarker(marker); - //mapboxService.fitMapToMarkers(map); // TODO: debounce this return marker; }; @@ -324,7 +321,7 @@ } element.bind('$destroy', function() { - if(controller.$scope.isClusteringMarkers) { + if(mapboxService.getOptionsForMap(map).clusterMarkers) { controller.$scope.clusterGroup.removeLayer(marker); } else { map.removeLayer(marker); diff --git a/dist/angular-mapbox.min.js b/dist/angular-mapbox.min.js index 1e77760..4220456 100644 --- a/dist/angular-mapbox.min.js +++ b/dist/angular-mapbox.min.js @@ -1 +1 @@ -!function(){"use strict";angular.module("angular-mapbox",[])}(),function(){"use strict";function mapboxService(){function init(opts){if(!opts.accessToken)throw"MissingMapboxApiToken";L.mapbox.accessToken=opts.accessToken}function addMapInstance(map,mapOptions){mapOptions=mapOptions||{},_mapInstances.push(map),_mapOptions.push(mapOptions),_markers.push([])}function getMapInstances(){return _mapInstances}function addMarker(marker){var map=getMapInstances()[0];_markers[0].push(marker);var opts=getOptionsForMap(map);opts.scaleToFit&&fitMapToMarkers(map)}function debounce(func,wait,immediate){var timeout;return function(){var context=this,args=arguments,later=function(){timeout=null,immediate||func.apply(context,args)},callNow=immediate&&!timeout;clearTimeout(timeout),timeout=setTimeout(later,wait),callNow&&func.apply(context,args)}}function getMarkers(){return _markers[0]}function getOptionsForMap(){return _mapOptions[0]}var _mapInstances=[],_markers=[],_mapOptions=[],fitMapToMarkers=debounce(function(){var map=_mapInstances[0],group=new L.featureGroup(getMarkers());map.fitBounds(group.getBounds())},0),service={init:init,getMapInstances:getMapInstances,addMapInstance:addMapInstance,getMarkers:getMarkers,addMarker:addMarker,fitMapToMarkers:fitMapToMarkers};return service}angular.module("angular-mapbox").service("mapboxService",mapboxService)}(),function(){"use strict";angular.module("angular-mapbox").directive("featureLayer",function(){return{restrict:"E",require:"^mapbox",link:function(scope,element,attrs,controller){attrs.data?controller.getMap().then(function(map){var geojsonObject=scope.$eval(attrs.data),featureLayer=L.mapbox.featureLayer(geojsonObject).addTo(map);controller.$scope.featureLayers.push(featureLayer)}):attrs.url&&controller.getMap().then(function(map){var featureLayer=L.mapbox.featureLayer().addTo(map);featureLayer.loadURL(attrs.url),controller.$scope.featureLayers.push(featureLayer)})}}})}(),function(){"use strict";angular.module("angular-mapbox").directive("mapbox",["$compile","$q","mapboxService",function($compile,$q,mapboxService){var _mapboxMap;return{restrict:"E",transclude:!0,scope:!0,replace:!0,link:function(scope,element,attrs){scope.map=L.mapbox.map(element[0],attrs.mapId),_mapboxMap.resolve(scope.map);var mapOptions={clusterMarkers:void 0!==attrs.clusterMarkers,scaleToFit:void 0!==attrs.scaleToFit};mapboxService.addMapInstance(scope.map,mapOptions);var mapWidth=attrs.width||500,mapHeight=attrs.height||500;element.css("width",mapWidth+"px"),element.css("height",mapHeight+"px");var zoom=attrs.zoom||12;attrs.lat&&attrs.lng&&scope.map.setView([attrs.lat,attrs.lng],zoom),scope.isClusteringMarkers=void 0!==attrs.clusterMarkers,attrs.onReposition&&scope.map.on("dragend",function(){scope[attrs.onReposition](scope.map.getBounds())}),attrs.onZoom&&scope.map.on("zoomend",function(){scope[attrs.onZoom](scope.map.getBounds())})},template:'
',controller:["$scope","mapboxService",function($scope,mapboxService){$scope.markers=mapboxService.getMarkers(),$scope.featureLayers=[],_mapboxMap=$q.defer(),$scope.getMap=this.getMap=function(){return _mapboxMap.promise},L.MarkerClusterGroup&&($scope.clusterGroup=new L.MarkerClusterGroup,this.getMap().then(function(map){map.addLayer($scope.clusterGroup)})),this.$scope=$scope}]}}])}(),function(){"use strict";angular.module("angular-mapbox").directive("marker",["$compile","mapboxService",function($compile,mapboxService){var _colors={navy:"#001f3f",blue:"#0074d9",aqua:"#7fdbff",teal:"#39cccc",olive:"#3d9970",green:"#2ecc40",lime:"#01ff70",yellow:"#ffdc00",orange:"#ff851b",red:"#ff4136",fuchsia:"#f012be",purple:"#b10dc9",maroon:"#85144b",white:"white",silver:"#dddddd",gray:"#aaaaaa",black:"#111111"};return{restrict:"E",require:"^mapbox",transclude:!0,scope:!0,link:function(scope,element,attrs,controller,transclude){function setStyleOptions(attrs,defaultOpts){var opts=defaultOpts||{};return attrs.size&&(opts["marker-size"]=attrs.size),attrs.color&&(opts["marker-color"]="#"===attrs.color[0]?attrs.color:_colors[attrs.color]||attrs.color),attrs.icon&&(opts["marker-symbol"]=attrs.icon),opts}var marker,opts={draggable:void 0!==attrs.draggable},style=setStyleOptions(attrs),addMarker=function(map,latlng,popupContent,opts,style){opts=opts||{};var marker=L.mapbox.marker.style({properties:style},latlng);return popupContent&&popupContent.length>0&&marker.bindPopup(popupContent),controller.$scope.isClusteringMarkers&&opts.excludeFromClustering!==!0?controller.$scope.clusterGroup.addLayer(marker):marker.addTo(map),opts.draggable&&marker.dragging.enable(),mapboxService.addMarker(marker),marker},addCurrentLocation=function(map,popupContent,opts,style){style=setStyleOptions(style,{"marker-color":"#000","marker-symbol":"star"}),opts.excludeFromClustering=!0,map.on("locationfound",function(e){marker=addMarker(map,[e.latlng.lat,e.latlng.lng],null,opts,style)}),map.locate()};controller.getMap().then(function(map){map.on("popupopen",function(){var popup=angular.element(document.getElementsByClassName("leaflet-popup-content"));$compile(popup)(scope),scope.$$phase||scope.$digest()}),setTimeout(function(){for(var popupHTML="",transcluded=transclude(scope,function(){}),i=0;i',controller:["$scope","mapboxService",function($scope,mapboxService){$scope.markers=mapboxService.getMarkers(),$scope.featureLayers=[],_mapboxMap=$q.defer(),$scope.getMap=this.getMap=function(){return _mapboxMap.promise},L.MarkerClusterGroup&&($scope.clusterGroup=new L.MarkerClusterGroup,this.getMap().then(function(map){map.addLayer($scope.clusterGroup)})),this.$scope=$scope}]}}])}(),function(){"use strict";angular.module("angular-mapbox").directive("marker",["$compile","mapboxService",function($compile,mapboxService){var _colors={navy:"#001f3f",blue:"#0074d9",aqua:"#7fdbff",teal:"#39cccc",olive:"#3d9970",green:"#2ecc40",lime:"#01ff70",yellow:"#ffdc00",orange:"#ff851b",red:"#ff4136",fuchsia:"#f012be",purple:"#b10dc9",maroon:"#85144b",white:"white",silver:"#dddddd",gray:"#aaaaaa",black:"#111111"};return{restrict:"E",require:"^mapbox",transclude:!0,scope:!0,link:function(scope,element,attrs,controller,transclude){function setStyleOptions(attrs,defaultOpts){var opts=defaultOpts||{};return attrs.size&&(opts["marker-size"]=attrs.size),attrs.color&&(opts["marker-color"]="#"===attrs.color[0]?attrs.color:_colors[attrs.color]||attrs.color),attrs.icon&&(opts["marker-symbol"]=attrs.icon),opts}var marker,opts={draggable:void 0!==attrs.draggable},style=setStyleOptions(attrs),addMarker=function(map,latlng,popupContent,opts,style){opts=opts||{};var marker=L.mapbox.marker.style({properties:style},latlng);return popupContent&&popupContent.length>0&&marker.bindPopup(popupContent),mapboxService.getOptionsForMap(map).clusterMarkers&&opts.excludeFromClustering!==!0?controller.$scope.clusterGroup.addLayer(marker):marker.addTo(map),opts.draggable&&marker.dragging.enable(),mapboxService.addMarker(marker),marker},addCurrentLocation=function(map,popupContent,opts,style){style=setStyleOptions(style,{"marker-color":"#000","marker-symbol":"star"}),opts.excludeFromClustering=!0,map.on("locationfound",function(e){marker=addMarker(map,[e.latlng.lat,e.latlng.lng],null,opts,style)}),map.locate()};controller.getMap().then(function(map){map.on("popupopen",function(){var popup=angular.element(document.getElementsByClassName("leaflet-popup-content"));$compile(popup)(scope),scope.$$phase||scope.$digest()}),setTimeout(function(){for(var popupHTML="",transcluded=transclude(scope,function(){}),i=0;i {{ status }} -