@@ -778,38 +778,69 @@ function toggleRoutes(event) {
778778 if ( event && isCtrlClick ( event ) ) editStyle ( "routes" ) ;
779779 } else {
780780 if ( event && isCtrlClick ( event ) ) return editStyle ( "routes" ) ;
781- routes . selectAll ( "path " ) . remove ( ) ;
781+ routes . selectAll ( "#roads, #trails, #searoutes, #airroutes " ) . html ( "" ) ;
782782 turnButtonOff ( "toggleRoutes" ) ;
783783 }
784784}
785785
786786function drawRoutes ( ) {
787787 TIME && console . time ( "drawRoutes" ) ;
788- const routePaths = { } ;
788+ const typedPaths = { } ;
789789
790790 for ( const route of pack . routes ) {
791791 const { i, group, points} = route ;
792792 if ( ! points || points . length < 2 ) continue ;
793- if ( ! routePaths [ group ] ) routePaths [ group ] = [ ] ;
794- const typeAttr = route . type ? ` data-type="${ route . type } "` : "" ;
795- routePaths [ group ] . push ( `<path id="route${ i } "${ typeAttr } d="${ Routes . getPath ( route ) } "/>` ) ;
793+ const type = route . type || "" ;
794+ const key = type ? `${ group } /${ type } ` : group ;
795+ if ( ! typedPaths [ key ] ) typedPaths [ key ] = { group, type, paths : [ ] } ;
796+ typedPaths [ key ] . paths . push ( `<path id="route${ i } " d="${ Routes . getPath ( route ) } "/>` ) ;
796797 }
797798
798- routes . selectAll ( "path" ) . remove ( ) ;
799- for ( const group in routePaths ) {
800- routes . select ( "#" + group ) . html ( routePaths [ group ] . join ( "" ) ) ;
799+ // clear all content from route group elements (sub-groups and paths)
800+ routes . selectAll ( "#roads, #trails, #searoutes, #airroutes" ) . html ( "" ) ;
801+
802+ for ( const key in typedPaths ) {
803+ const { group, type, paths} = typedPaths [ key ] ;
804+ const groupEl = routes . select ( "#" + group ) ;
805+ if ( groupEl . empty ( ) ) continue ;
806+ if ( type ) {
807+ const subGroup = groupEl . append ( "g" ) . attr ( "id" , type ) ;
808+ applyRouteTypeStyle ( subGroup . node ( ) , type ) ;
809+ subGroup . html ( paths . join ( "" ) ) ;
810+ } else {
811+ groupEl . html ( groupEl . html ( ) + paths . join ( "" ) ) ;
812+ }
801813 }
802814
803815 TIME && console . timeEnd ( "drawRoutes" ) ;
804816}
805817
806818function drawRoute ( route ) {
807- const el = routes
808- . select ( "#" + route . group )
809- . append ( "path" )
810- . attr ( "d" , Routes . getPath ( route ) )
811- . attr ( "id" , "route" + route . i ) ;
812- if ( route . type ) el . attr ( "data-type" , route . type ) ;
819+ const groupEl = routes . select ( "#" + route . group ) ;
820+ if ( groupEl . empty ( ) ) return ;
821+ if ( route . type ) {
822+ let subGroup = groupEl . select ( "#" + route . type ) ;
823+ if ( subGroup . empty ( ) ) {
824+ subGroup = groupEl . append ( "g" ) . attr ( "id" , route . type ) ;
825+ applyRouteTypeStyle ( subGroup . node ( ) , route . type ) ;
826+ }
827+ subGroup . append ( "path" ) . attr ( "d" , Routes . getPath ( route ) ) . attr ( "id" , "route" + route . i ) ;
828+ } else {
829+ groupEl . append ( "path" ) . attr ( "d" , Routes . getPath ( route ) ) . attr ( "id" , "route" + route . i ) ;
830+ }
831+ }
832+
833+ function applyRouteTypeStyle ( el , type ) {
834+ const typeStyle = style . routes [ type ] ;
835+ if ( ! typeStyle ) return ;
836+ for ( const attr in typeStyle ) {
837+ const value = typeStyle [ attr ] ;
838+ if ( value === null || value === "null" ) {
839+ el . removeAttribute ( attr ) ;
840+ } else if ( value !== undefined ) {
841+ el . setAttribute ( attr , value ) ;
842+ }
843+ }
813844}
814845
815846function toggleMilitary ( event ) {
0 commit comments