@@ -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 ( ) {
@@ -4206,17 +4238,20 @@ angular.module('ui-leaflet').directive('layers', ["leafletLogger", "$q", "leafle
42064238 } ;
42074239} ] ) ;
42084240
4209- angular . module ( 'ui-leaflet' ) . directive ( 'legend' , [ "leafletLogger" , "$http" , "leafletHelpers" , "leafletLegendHelpers" , function ( leafletLogger , $http , leafletHelpers , leafletLegendHelpers ) {
4210- var $log = leafletLogger ;
4241+ angular . module ( "ui-leaflet" ) . directive ( 'legend' , [ "leafletLogger" , "$http" , "$timeout" , "leafletHelpers" , "leafletLegendHelpers" , function ( leafletLogger , $http , $timeout , leafletHelpers , leafletLegendHelpers ) {
4242+ var $log = leafletLogger ,
4243+ errorHeader = leafletHelpers . errorHeader + ' [Legend] ' ;
42114244 return {
42124245 restrict : "A" ,
42134246 scope : false ,
42144247 replace : false ,
42154248 require : 'leaflet' ,
4249+ transclude : false ,
42164250
42174251 link : function ( scope , element , attrs , controller ) {
42184252
42194253 var isArray = leafletHelpers . isArray ,
4254+ isString = leafletHelpers . isString ,
42204255 isDefined = leafletHelpers . isDefined ,
42214256 isFunction = leafletHelpers . isFunction ,
42224257 leafletScope = controller . getLeafletScope ( ) ,
@@ -4230,23 +4265,34 @@ angular.module('ui-leaflet').directive('legend', ["leafletLogger", "$http", "lea
42304265 leafletScope . $watch ( 'legend' , function ( newLegend ) {
42314266
42324267 if ( isDefined ( newLegend ) ) {
4233-
42344268 legendClass = newLegend . legendClass ? newLegend . legendClass : "legend" ;
4235-
42364269 position = newLegend . position || 'bottomright' ;
4237-
42384270 // default to arcgis
42394271 type = newLegend . type || 'arcgis' ;
42404272 }
4241-
42424273 } , true ) ;
42434274
4244- controller . getMap ( ) . then ( function ( map ) {
4275+ var createLegend = function ( map , legendData , newURL ) {
4276+ if ( legendData && legendData . layers && legendData . layers . length > 0 ) {
4277+ if ( isDefined ( leafletLegend ) ) {
4278+ leafletLegendHelpers . updateLegend ( leafletLegend . getContainer ( ) , legendData , type , newURL ) ;
4279+ } else {
4280+ leafletLegend = L . control ( {
4281+ position : position
4282+ } ) ;
4283+ leafletLegend . onAdd = leafletLegendHelpers . getOnAddLegend ( legendData , legendClass , type , newURL ) ;
4284+ leafletLegend . addTo ( map ) ;
4285+ }
42454286
4246- leafletScope . $watch ( 'legend' , function ( newLegend ) {
4287+ if ( isDefined ( legend . loadedData ) && isFunction ( legend . loadedData ) ) {
4288+ legend . loadedData ( ) ;
4289+ }
4290+ }
4291+ } ;
42474292
4293+ controller . getMap ( ) . then ( function ( map ) {
4294+ leafletScope . $watch ( 'legend' , function ( newLegend ) {
42484295 if ( ! isDefined ( newLegend ) ) {
4249-
42504296 if ( isDefined ( leafletLegend ) ) {
42514297 leafletLegend . removeFrom ( map ) ;
42524298 leafletLegend = null ;
@@ -4256,16 +4302,12 @@ angular.module('ui-leaflet').directive('legend', ["leafletLogger", "$http", "lea
42564302 }
42574303
42584304 if ( ! isDefined ( newLegend . url ) && ( type === 'arcgis' ) && ( ! isArray ( newLegend . colors ) || ! isArray ( newLegend . labels ) || newLegend . colors . length !== newLegend . labels . length ) ) {
4259-
4260- $log . warn ( "[AngularJS - Leaflet] legend.colors and legend.labels must be set." ) ;
4261-
4305+ $log . warn ( errorHeader + " legend.colors and legend.labels must be set." ) ;
42624306 return ;
42634307 }
42644308
42654309 if ( isDefined ( newLegend . url ) ) {
4266-
4267- $log . info ( "[AngularJS - Leaflet] loading legend service." ) ;
4268-
4310+ $log . info ( errorHeader + " loading legend service." ) ;
42694311 return ;
42704312 }
42714313
@@ -4277,44 +4319,65 @@ angular.module('ui-leaflet').directive('legend', ["leafletLogger", "$http", "lea
42774319 leafletLegend = L . control ( {
42784320 position : position
42794321 } ) ;
4322+
42804323 if ( type === 'arcgis' ) {
42814324 leafletLegend . onAdd = leafletLegendHelpers . getOnAddArrayLegend ( newLegend , legendClass ) ;
42824325 }
42834326 leafletLegend . addTo ( map ) ;
4284-
42854327 } ) ;
42864328
42874329 leafletScope . $watch ( 'legend.url' , function ( newURL ) {
4288-
42894330 if ( ! isDefined ( newURL ) ) {
42904331 return ;
42914332 }
4292- $http . get ( newURL )
4293- . success ( function ( legendData ) {
42944333
4295- if ( isDefined ( leafletLegend ) ) {
4334+ if ( ! isArray ( newURL ) && ! isString ( newURL ) ) {
4335+ $log . warn ( errorHeader + " legend.url must be an array or string." ) ;
4336+ return ;
4337+ }
42964338
4297- leafletLegendHelpers . updateLegend ( leafletLegend . getContainer ( ) , legendData , type , newURL ) ;
4339+ var urls = isString ( newURL ) ? [ newURL ] : newURL ;
42984340
4341+ var legendData ;
4342+ var onResult = function ( idx , url ) {
4343+ return function ( ld ) {
4344+ if ( isDefined ( ld . data . error ) ) {
4345+ $log . warn ( errorHeader + 'Error loadin legend from: ' + url , ld . data . error . message ) ;
42994346 } else {
4300-
4301- leafletLegend = L . control ( {
4302- position : position
4303- } ) ;
4304- leafletLegend . onAdd = leafletLegendHelpers . getOnAddLegend ( legendData , legendClass , type , newURL ) ;
4305- leafletLegend . addTo ( map ) ;
4347+ if ( legendData && legendData . layers && legendData . layers . length > 0 ) {
4348+ legendData . layers = legendData . layers . concat ( ld . data . layers ) ;
4349+ } else {
4350+ legendData = ld . data ;
4351+ }
43064352 }
43074353
4308- if ( isDefined ( legend . loadedData ) && isFunction ( legend . loadedData ) ) {
4309- legend . loadedData ( ) ;
4354+ if ( idx === urls . length - 1 ) {
4355+ createLegend ( map , legendData , newURL ) ;
43104356 }
4311- } )
4312- . error ( function ( ) {
4313- $log . warn ( '[AngularJS - Leaflet] legend.url not loaded.' ) ;
4314- } ) ;
4357+ } ;
4358+ } ;
4359+ var onError = function ( err ) {
4360+ $log . warn ( errorHeader + ' legend.url not loaded.' , err ) ;
4361+ } ;
4362+
4363+ for ( var i = 0 ; i < urls . length ; i ++ ) {
4364+ leafletLegendHelpers . addLegendURL ( attrs . id , {
4365+ url : urls [ i ] ,
4366+ method : 'GET'
4367+ } ) . then ( onResult ( i ) ) . catch ( onError ) ;
4368+ }
43154369 } ) ;
43164370
4371+ leafletScope . $watch ( 'legend.legendData' , function ( legendData ) {
4372+ $log . debug ( 'legendData' , legendData ) ;
4373+ if ( isDefined ( leafletScope . legend . url ) || ! isDefined ( legendData ) ) {
4374+ return ;
4375+ }
4376+
4377+ createLegend ( map , legendData ) ;
4378+ } , true ) ;
43174379 } ) ;
4380+
43184381 }
43194382 } ;
43204383 } ] ) ;
0 commit comments