11/*!
2- * angular-leaflet-directive 0.8.5 2015-06-29
2+ * angular-leaflet-directive 0.8.5 2015-07-03
33* angular-leaflet-directive - An AngularJS directive to easily interact with Leaflet maps
44* git: https://github.com/tombatossals/angular-leaflet-directive
55*/
@@ -225,10 +225,12 @@ angular.module("leaflet-directive").factory('leafletBoundsHelpers', ["$log", "le
225225 } ;
226226} ] ) ;
227227
228- angular . module ( "leaflet-directive" ) . factory ( 'leafletControlHelpers' , [ "$rootScope" , "$log" , "leafletHelpers" , "leafletMapDefaults" , function ( $rootScope , $log , leafletHelpers , leafletMapDefaults ) {
228+ angular . module ( "leaflet-directive" ) . factory ( 'leafletControlHelpers' , [ "$rootScope" , "$log" , "leafletHelpers" , "leafletLayerHelpers" , " leafletMapDefaults", function ( $rootScope , $log , leafletHelpers , leafletLayerHelpers , leafletMapDefaults ) {
229229 var isDefined = leafletHelpers . isDefined ;
230230 var isObject = leafletHelpers . isObject ;
231+ var createLayer = leafletLayerHelpers . createLayer ;
231232 var _controls = { } ;
233+ var errorHeader = leafletHelpers . errorHeader + ' [Controls] ' ;
232234
233235 var _controlLayersMustBeVisible = function ( baselayers , overlays , mapId ) {
234236 var defaults = leafletMapDefaults . getDefaults ( mapId ) ;
@@ -279,9 +281,107 @@ angular.module("leaflet-directive").factory('leafletControlHelpers', ["$rootScop
279281 return control ;
280282 } ;
281283
284+ var controlTypes = {
285+ draw : {
286+ isPluginLoaded : function ( ) {
287+ if ( ! angular . isDefined ( L . Control . Draw ) ) {
288+ $log . error ( errorHeader + ' Draw plugin is not loaded.' ) ;
289+ return false ;
290+ }
291+ return true ;
292+ } ,
293+ checkValidParams : function ( /* params */ ) {
294+ return true ;
295+ } ,
296+ createControl : function ( params ) {
297+ return new L . Control . Draw ( params ) ;
298+ }
299+ } ,
300+ scale : {
301+ isPluginLoaded : function ( ) {
302+ return true ;
303+ } ,
304+ checkValidParams : function ( /* params */ ) {
305+ return true ;
306+ } ,
307+ createControl : function ( params ) {
308+ return new L . control . scale ( params ) ;
309+ }
310+ } ,
311+ fullscreen : {
312+ isPluginLoaded : function ( ) {
313+ if ( ! angular . isDefined ( L . Control . Fullscreen ) ) {
314+ $log . error ( errorHeader + ' Fullscreen plugin is not loaded.' ) ;
315+ return false ;
316+ }
317+ return true ;
318+ } ,
319+ checkValidParams : function ( /* params */ ) {
320+ return true ;
321+ } ,
322+ createControl : function ( params ) {
323+ return new L . Control . Fullscreen ( params ) ;
324+ }
325+ } ,
326+ search : {
327+ isPluginLoaded : function ( ) {
328+ if ( ! angular . isDefined ( L . Control . Search ) ) {
329+ $log . error ( errorHeader + ' Search plugin is not loaded.' ) ;
330+ return false ;
331+ }
332+ return true ;
333+ } ,
334+ checkValidParams : function ( /* params */ ) {
335+ return true ;
336+ } ,
337+ createControl : function ( params ) {
338+ return new L . Control . Search ( params ) ;
339+ }
340+ } ,
341+ minimap : {
342+ isPluginLoaded : function ( ) {
343+ if ( ! angular . isDefined ( L . Control . MiniMap ) ) {
344+ $log . error ( errorHeader + ' Minimap plugin is not loaded.' ) ;
345+ return false ;
346+ }
347+
348+ return true ;
349+ } ,
350+ checkValidParams : function ( params ) {
351+ if ( ! isDefined ( params . layer ) ) {
352+ $log . warn ( errorHeader + ' minimap "layer" option should be defined.' ) ;
353+ return false ;
354+ }
355+ return true ;
356+ } ,
357+ createControl : function ( params ) {
358+ var layer = createLayer ( params . layer ) ;
359+
360+ if ( ! isDefined ( layer ) ) {
361+ $log . warn ( errorHeader + ' minimap control "layer" could not be created.' ) ;
362+ return ;
363+ }
364+
365+ return new L . Control . MiniMap ( layer , params ) ;
366+ }
367+ }
368+ } ;
369+
282370 return {
283371 layersControlMustBeVisible : _controlLayersMustBeVisible ,
284372
373+ isValidControlType : function ( type ) {
374+ return Object . keys ( controlTypes ) . indexOf ( type ) !== - 1 ;
375+ } ,
376+
377+ createControl : function ( type , params ) {
378+ if ( ! controlTypes [ type ] . checkValidParams ( params ) ) {
379+ return ;
380+ }
381+
382+ return controlTypes [ type ] . createControl ( params ) ;
383+ } ,
384+
285385 updateLayersControl : function ( map , mapId , loaded , baselayers , overlays , leafletLayers ) {
286386 var i ;
287387 var _layersControl = _controls [ mapId ] ;
@@ -712,24 +812,6 @@ angular.module("leaflet-directive").factory('leafletHelpers', ["$q", "$log", fun
712812 d [ id ] . resolvedDefer = true ;
713813 } ,
714814
715- FullScreenControlPlugin : {
716- isLoaded : function ( ) {
717- return angular . isDefined ( L . Control . Fullscreen ) ;
718- }
719- } ,
720-
721- SearchControlPlugin : {
722- isLoaded : function ( ) {
723- return angular . isDefined ( L . Control . Search ) ;
724- }
725- } ,
726-
727- MiniMapControlPlugin : {
728- isLoaded : function ( ) {
729- return angular . isDefined ( L . Control . MiniMap ) ;
730- }
731- } ,
732-
733815 AwesomeMarkersPlugin : {
734816 isLoaded : function ( ) {
735817 return angular . isDefined ( L . AwesomeMarkers ) && angular . isDefined ( L . AwesomeMarkers . Icon ) ;
@@ -2994,7 +3076,7 @@ angular.module("leaflet-directive").directive('center',
29943076 } ;
29953077} ] ) ;
29963078
2997- angular . module ( "leaflet-directive" ) . directive ( 'controls' , [ "$log" , "leafletHelpers" , "leafletLayerHelpers " , function ( $log , leafletHelpers , leafletLayerHelpers ) {
3079+ angular . module ( "leaflet-directive" ) . directive ( 'controls' , [ "$log" , "leafletHelpers" , "leafletControlHelpers " , function ( $log , leafletHelpers , leafletControlHelpers ) {
29983080 return {
29993081 restrict : "A" ,
30003082 scope : false ,
@@ -3006,81 +3088,49 @@ angular.module("leaflet-directive").directive('controls', ["$log", "leafletHelpe
30063088 return ;
30073089 }
30083090
3009- var isDefined = leafletHelpers . isDefined ,
3010- createLayer = leafletLayerHelpers . createLayer ,
3011- leafletScope = controller . getLeafletScope ( ) ,
3012- controls = leafletScope . controls ,
3013- errorHeader = leafletHelpers . errorHeader + ' [Controls] ' ;
3091+ var createControl = leafletControlHelpers . createControl ;
3092+ var isValidControlType = leafletControlHelpers . isValidControlType ;
3093+ var leafletScope = controller . getLeafletScope ( ) ;
3094+ var isDefined = leafletHelpers . isDefined ;
3095+ var leafletControls = { } ;
3096+ var errorHeader = leafletHelpers . errorHeader + ' [Controls] ' ;
30143097
30153098 controller . getMap ( ) . then ( function ( map ) {
3016- if ( isDefined ( L . Control . Draw ) && isDefined ( controls . draw ) ) {
30173099
3018- if ( ! isDefined ( controls . edit ) ) {
3019- controls . edit = { featureGroup : new L . FeatureGroup ( ) } ;
3020- map . addLayer ( controls . edit . featureGroup ) ;
3021- }
3100+ leafletScope . $watchCollection ( 'controls' , function ( newControls ) {
30223101
3023- var drawControl = new L . Control . Draw ( controls ) ;
3024- map . addControl ( drawControl ) ;
3025- }
3102+ // Delete controls from the array
3103+ for ( var name in leafletControls ) {
3104+ if ( ! isDefined ( newControls [ name ] ) ) {
3105+ if ( map . hasControl ( leafletControls [ name ] ) ) {
3106+ map . removeControl ( leafletControls [ name ] ) ;
3107+ }
3108+ delete leafletControls [ name ] ;
3109+ }
3110+ }
30263111
3027- if ( isDefined ( controls . scale ) ) {
3028- var scaleControl = new L . control . scale ( controls . scale ) ;
3029- map . addControl ( scaleControl ) ;
3030- }
3112+ for ( var newName in newControls ) {
3113+ var control ;
30313114
3032- if ( isDefined ( controls . fullscreen ) ) {
3033- if ( leafletHelpers . FullScreenControlPlugin . isLoaded ( ) ) {
3034- var fullscreenControl = new L . Control . Fullscreen ( controls . fullscreen ) ;
3035- map . addControl ( fullscreenControl ) ;
3036- } else {
3037- $log . error ( errorHeader + ' Fullscreen plugin is not loaded.' ) ;
3038- }
3039- }
3115+ var controlType = isDefined ( newControls [ newName ] . type ) ? newControls [ newName ] . type : newName ;
30403116
3041- if ( isDefined ( controls . search ) ) {
3042- if ( leafletHelpers . SearchControlPlugin . isLoaded ( ) ) {
3043- var searchControl = new L . Control . Search ( controls . search ) ;
3044- map . addControl ( searchControl ) ;
3045- } else {
3046- $log . error ( errorHeader + ' Search plugin is not loaded.' ) ;
3047- }
3048- }
3117+ if ( ! isValidControlType ( controlType ) ) {
3118+ $log . error ( errorHeader + ' Invalid control type: ' + controlType + '.' ) ;
3119+ return ;
3120+ }
30493121
3050- if ( isDefined ( controls . minimap ) ) {
3051- if ( leafletHelpers . MiniMapControlPlugin . isLoaded ( ) ) {
3052- if ( isDefined ( controls . minimap . layer ) ) {
3053- var layer = createLayer ( controls . minimap . layer ) ;
3054- delete controls . minimap . layer ;
3055-
3056- if ( isDefined ( layer ) ) {
3057- if ( isDefined ( leafletScope . center ) ) {
3058- var moveend = function ( /* event */ ) {
3059- var minimapControl = new L . Control . MiniMap ( layer , controls . minimap ) ;
3060- map . addControl ( minimapControl ) ;
3061- map . off ( 'moveend' , moveend ) ;
3062- } ;
3063- map . on ( 'moveend' , moveend ) ;
3064- } else {
3065- var minimapControl = new L . Control . MiniMap ( layer , controls . minimap ) ;
3066- map . addControl ( minimapControl ) ;
3067- }
3068- } else {
3069- $log . warn ( errorHeader + ' Layer could not be created.' ) ;
3070- }
3122+ if ( controlType !== 'custom' ) {
3123+ control = createControl ( controlType , newControls [ newName ] ) ;
30713124 } else {
3072- $log . warn ( errorHeader + ' Layer option should be defined.' ) ;
3125+ control = newControls [ newName ] ;
30733126 }
3074- } else {
3075- $log . error ( errorHeader + ' Minimap plugin is not loaded.' ) ;
3076- }
3077- }
3127+ map . addControl ( control ) ;
30783128
3079- if ( isDefined ( controls . custom ) ) {
3080- for ( var i in controls . custom ) {
3081- map . addControl ( controls . custom [ i ] ) ;
3129+ leafletControls [ newName ] = control ;
30823130 }
3083- }
3131+
3132+ } ) ;
3133+
30843134 } ) ;
30853135 }
30863136 } ;
@@ -4363,7 +4413,7 @@ angular.module("leaflet-directive").directive('tiles', ["$log", "leafletData", "
43634413 leafletScope = controller . getLeafletScope ( ) ,
43644414 tiles = leafletScope . tiles ;
43654415
4366- if ( ! isDefined ( tiles ) && ! isDefined ( tiles . url ) ) {
4416+ if ( ! isDefined ( tiles ) || ! isDefined ( tiles . url ) ) {
43674417 $log . warn ( "[AngularJS - Leaflet] The 'tiles' definition doesn't have the 'url' property." ) ;
43684418 return ;
43694419 }
0 commit comments