@@ -2009,7 +2009,28 @@ angular.module('ui-leaflet')
20092009 } ;
20102010} ] ) ;
20112011
2012- angular . module ( 'ui-leaflet' ) . factory ( 'leafletLegendHelpers' , function ( ) {
2012+ angular . module ( "ui-leaflet" ) . factory ( 'leafletLegendHelpers' , [ "$http" , "$q" , "$log" , "leafletHelpers" , function ( $http , $q , $log , leafletHelpers ) {
2013+ var requestQueue = { } ,
2014+ isDefined = leafletHelpers . isDefined ;
2015+
2016+ var _execNext = function ( mapId ) {
2017+ var queue = requestQueue [ mapId ] ;
2018+ var task = queue [ 0 ] ;
2019+ $http ( task . c ) . then ( function ( data ) {
2020+ queue . shift ( ) ;
2021+ task . d . resolve ( data ) ;
2022+ if ( queue . length > 0 ) {
2023+ _execNext ( mapId ) ;
2024+ }
2025+ } , function ( err ) {
2026+ queue . shift ( ) ;
2027+ task . d . reject ( err ) ;
2028+ if ( queue . length > 0 ) {
2029+ _execNext ( mapId ) ;
2030+ }
2031+ } ) ;
2032+ } ;
2033+
20132034 var _updateLegend = function ( div , legendData , type , url ) {
20142035 div . innerHTML = '' ;
20152036 if ( legendData . error ) {
@@ -2069,9 +2090,20 @@ angular.module('ui-leaflet').factory('leafletLegendHelpers', function () {
20692090 return {
20702091 getOnAddLegend : _getOnAddLegend ,
20712092 getOnAddArrayLegend : _getOnAddArrayLegend ,
2072- updateLegend : _updateLegend
2093+ updateLegend : _updateLegend ,
2094+ addLegendURL : function ( mapId , config ) {
2095+ var d = $q . defer ( ) ;
2096+ if ( ! isDefined ( requestQueue [ mapId ] ) ) {
2097+ requestQueue [ mapId ] = [ ] ;
2098+ }
2099+ requestQueue [ mapId ] . push ( { c :config , d :d } ) ;
2100+ if ( requestQueue [ mapId ] . length === 1 ) {
2101+ _execNext ( mapId ) ;
2102+ }
2103+ return d . promise ;
2104+ }
20732105 } ;
2074- } ) ;
2106+ } ] ) ;
20752107
20762108angular . module ( 'ui-leaflet' ) . factory ( 'leafletMapDefaults' , [ "$q" , "leafletHelpers" , function ( $q , leafletHelpers ) {
20772109 function _getDefaults ( ) {
@@ -4228,17 +4260,20 @@ angular.module('ui-leaflet').directive('layers', ["leafletLogger", "$q", "leafle
42284260 } ;
42294261} ] ) ;
42304262
4231- angular . module ( 'ui-leaflet' ) . directive ( 'legend' , [ "leafletLogger" , "$http" , "leafletHelpers" , "leafletLegendHelpers" , function ( leafletLogger , $http , leafletHelpers , leafletLegendHelpers ) {
4232- var $log = leafletLogger ;
4263+ angular . module ( "ui-leaflet" ) . directive ( 'legend' , [ "leafletLogger" , "$http" , "$timeout" , "leafletHelpers" , "leafletLegendHelpers" , function ( leafletLogger , $http , $timeout , leafletHelpers , leafletLegendHelpers ) {
4264+ var $log = leafletLogger ,
4265+ errorHeader = leafletHelpers . errorHeader + ' [Legend] ' ;
42334266 return {
42344267 restrict : "A" ,
42354268 scope : false ,
42364269 replace : false ,
42374270 require : 'leaflet' ,
4271+ transclude : false ,
42384272
42394273 link : function ( scope , element , attrs , controller ) {
42404274
42414275 var isArray = leafletHelpers . isArray ,
4276+ isString = leafletHelpers . isString ,
42424277 isDefined = leafletHelpers . isDefined ,
42434278 isFunction = leafletHelpers . isFunction ,
42444279 leafletScope = controller . getLeafletScope ( ) ,
@@ -4252,23 +4287,34 @@ angular.module('ui-leaflet').directive('legend', ["leafletLogger", "$http", "lea
42524287 leafletScope . $watch ( 'legend' , function ( newLegend ) {
42534288
42544289 if ( isDefined ( newLegend ) ) {
4255-
42564290 legendClass = newLegend . legendClass ? newLegend . legendClass : "legend" ;
4257-
42584291 position = newLegend . position || 'bottomright' ;
4259-
42604292 // default to arcgis
42614293 type = newLegend . type || 'arcgis' ;
42624294 }
4263-
42644295 } , true ) ;
42654296
4266- controller . getMap ( ) . then ( function ( map ) {
4297+ var createLegend = function ( map , legendData , newURL ) {
4298+ if ( legendData && legendData . layers && legendData . layers . length > 0 ) {
4299+ if ( isDefined ( leafletLegend ) ) {
4300+ leafletLegendHelpers . updateLegend ( leafletLegend . getContainer ( ) , legendData , type , newURL ) ;
4301+ } else {
4302+ leafletLegend = L . control ( {
4303+ position : position
4304+ } ) ;
4305+ leafletLegend . onAdd = leafletLegendHelpers . getOnAddLegend ( legendData , legendClass , type , newURL ) ;
4306+ leafletLegend . addTo ( map ) ;
4307+ }
42674308
4268- leafletScope . $watch ( 'legend' , function ( newLegend ) {
4309+ if ( isDefined ( legend . loadedData ) && isFunction ( legend . loadedData ) ) {
4310+ legend . loadedData ( ) ;
4311+ }
4312+ }
4313+ } ;
42694314
4315+ controller . getMap ( ) . then ( function ( map ) {
4316+ leafletScope . $watch ( 'legend' , function ( newLegend ) {
42704317 if ( ! isDefined ( newLegend ) ) {
4271-
42724318 if ( isDefined ( leafletLegend ) ) {
42734319 leafletLegend . removeFrom ( map ) ;
42744320 leafletLegend = null ;
@@ -4278,16 +4324,12 @@ angular.module('ui-leaflet').directive('legend', ["leafletLogger", "$http", "lea
42784324 }
42794325
42804326 if ( ! isDefined ( newLegend . url ) && ( type === 'arcgis' ) && ( ! isArray ( newLegend . colors ) || ! isArray ( newLegend . labels ) || newLegend . colors . length !== newLegend . labels . length ) ) {
4281-
4282- $log . warn ( "[AngularJS - Leaflet] legend.colors and legend.labels must be set." ) ;
4283-
4327+ $log . warn ( errorHeader + " legend.colors and legend.labels must be set." ) ;
42844328 return ;
42854329 }
42864330
42874331 if ( isDefined ( newLegend . url ) ) {
4288-
4289- $log . info ( "[AngularJS - Leaflet] loading legend service." ) ;
4290-
4332+ $log . info ( errorHeader + " loading legend service." ) ;
42914333 return ;
42924334 }
42934335
@@ -4299,44 +4341,65 @@ angular.module('ui-leaflet').directive('legend', ["leafletLogger", "$http", "lea
42994341 leafletLegend = L . control ( {
43004342 position : position
43014343 } ) ;
4344+
43024345 if ( type === 'arcgis' ) {
43034346 leafletLegend . onAdd = leafletLegendHelpers . getOnAddArrayLegend ( newLegend , legendClass ) ;
43044347 }
43054348 leafletLegend . addTo ( map ) ;
4306-
43074349 } ) ;
43084350
43094351 leafletScope . $watch ( 'legend.url' , function ( newURL ) {
4310-
43114352 if ( ! isDefined ( newURL ) ) {
43124353 return ;
43134354 }
4314- $http . get ( newURL )
4315- . success ( function ( legendData ) {
43164355
4317- if ( isDefined ( leafletLegend ) ) {
4356+ if ( ! isArray ( newURL ) && ! isString ( newURL ) ) {
4357+ $log . warn ( errorHeader + " legend.url must be an array or string." ) ;
4358+ return ;
4359+ }
43184360
4319- leafletLegendHelpers . updateLegend ( leafletLegend . getContainer ( ) , legendData , type , newURL ) ;
4361+ var urls = isString ( newURL ) ? [ newURL ] : newURL ;
43204362
4363+ var legendData ;
4364+ var onResult = function ( idx , url ) {
4365+ return function ( ld ) {
4366+ if ( isDefined ( ld . data . error ) ) {
4367+ $log . warn ( errorHeader + 'Error loadin legend from: ' + url , ld . data . error . message ) ;
43214368 } else {
4322-
4323- leafletLegend = L . control ( {
4324- position : position
4325- } ) ;
4326- leafletLegend . onAdd = leafletLegendHelpers . getOnAddLegend ( legendData , legendClass , type , newURL ) ;
4327- leafletLegend . addTo ( map ) ;
4369+ if ( legendData && legendData . layers && legendData . layers . length > 0 ) {
4370+ legendData . layers = legendData . layers . concat ( ld . data . layers ) ;
4371+ } else {
4372+ legendData = ld . data ;
4373+ }
43284374 }
43294375
4330- if ( isDefined ( legend . loadedData ) && isFunction ( legend . loadedData ) ) {
4331- legend . loadedData ( ) ;
4376+ if ( idx === urls . length - 1 ) {
4377+ createLegend ( map , legendData , newURL ) ;
43324378 }
4333- } )
4334- . error ( function ( ) {
4335- $log . warn ( '[AngularJS - Leaflet] legend.url not loaded.' ) ;
4336- } ) ;
4379+ } ;
4380+ } ;
4381+ var onError = function ( err ) {
4382+ $log . warn ( errorHeader + ' legend.url not loaded.' , err ) ;
4383+ } ;
4384+
4385+ for ( var i = 0 ; i < urls . length ; i ++ ) {
4386+ leafletLegendHelpers . addLegendURL ( attrs . id , {
4387+ url : urls [ i ] ,
4388+ method : 'GET'
4389+ } ) . then ( onResult ( i ) ) . catch ( onError ) ;
4390+ }
43374391 } ) ;
43384392
4393+ leafletScope . $watch ( 'legend.legendData' , function ( legendData ) {
4394+ $log . debug ( 'legendData' , legendData ) ;
4395+ if ( isDefined ( leafletScope . legend . url ) || ! isDefined ( legendData ) ) {
4396+ return ;
4397+ }
4398+
4399+ createLegend ( map , legendData ) ;
4400+ } , true ) ;
43394401 } ) ;
4402+
43404403 }
43414404 } ;
43424405 } ] ) ;
0 commit comments