@@ -16,16 +16,31 @@ function remove_google_maps_pin( biome_site_id ) {
1616//
1717// TODO: Replace this with an actual JS library that handles quotes, commas,
1818// etc. Shouldn't need to hand-roll this
19+
20+
1921function convert_to_csv ( json_data ) {
20- columns = Object . keys ( json_data [ 'site_1_data' ] [ 'Grass' ] ) ;
2122 json2csvParser = json2csv . Parser ;
22- parser = new json2csvParser ( columns , flatten = true ) ;
2323 flat_data = [ ] ;
2424 for ( site in json_data ) {
2525 for ( ecosystem in json_data [ site ] ) {
26- flat_data . push ( json_data [ site ] [ ecosystem ] ) ;
26+ cleaned = json_data [ site ] [ ecosystem ]
27+ for ( field in cleaned ) {
28+ cleaned [ field ] = json_data [ site ] [ ecosystem ] [ field ] . join ( ", " )
29+ }
30+ flat_data . push ( cleaned ) ;
2731 }
2832 }
33+ columns = flat_data . reduce ( function ( x , y ) {
34+ keys = Object . keys ( y )
35+ for ( columns in keys ) {
36+ if ( x . indexOf ( keys [ columns ] ) === - 1 ) {
37+ x . push ( keys [ columns ] )
38+ }
39+ }
40+ return x
41+ } , [ ] )
42+
43+ parser = new json2csvParser ( columns , flatten = true ) ;
2944 str = parser . parse ( flat_data )
3045 var element = document . createElement ( 'a' ) ;
3146 element . setAttribute ( 'href' , 'data:text/csv;charset=utf-8,' + encodeURIComponent ( str ) ) ;
@@ -137,36 +152,33 @@ function populate_html_from_latlng( lat, lng ) {
137152
138153 // Tag the site w the given lat and lng
139154 $ ( 'div.well:not(.inactive_site)' ) . find ( '.site_latlng' ) . text ( "( " + lat . toFixed ( 2 ) + ", " + lng . toFixed ( 2 ) + " )" ) ;
140-
141- if ( data . native_eco != null ) {
142- $ . each ( data . native_eco , function ( k , v ) {
143- $ ( 'div.well:not(.inactive_site)' ) . find ( '.native_biomes' ) . find ( '.biome_list' ) . append (
144- '<div class="biome_match checkbox">' +
145- '<label class="biome-match"><input type="checkbox">' + k . replace ( / _ / g, " " ) + '</input></label>' +
146- '<a class="edit_icon_link action-link" data-toggle="modal" data-target="#ecosystem_popup" href="#ecosystem_popup">' +
147- '<i class="glyphicon glyphicon-list-alt" rel="tooltip" title="Edit"></i>' +
148- '</a>' +
149- '<a class="biome_pdf_link action-link"href="' +
150- '/ecosystem_fact_sheets/' + v . code + '.pdf' + //pdf file name
151- '" download><img class="download-pdf-icon" src="/assets/pdf_24x24.png"></a>' +
152- '</div>'
155+ function biome_div ( type ) {
156+ return function make_element ( k , v ) {
157+ element = '<div class="biome_match checkbox">' +
158+ '<label class="biome-match"><input type="checkbox">' + k . replace ( / _ / g, " " ) + '</input></label>'
159+ if ( v [ 'in_synmap' ] [ 0 ] ) {
160+ element = element . concat ( '<span class="synmap-flag" style="color: red; font-size: 20px;">*</span>' )
161+ $ ( '#synmap-key' ) . css ( "display" , "inline" )
162+ }
163+ element = element . concat (
164+ '<a class="edit_icon_link action-link" data-toggle="modal" data-target="#ecosystem_popup" href="#ecosystem_popup">' +
165+ '<i class="glyphicon glyphicon-list-alt" rel="tooltip" title="Edit"></i>' +
166+ '</a>' +
167+ '<a class="biome_pdf_link action-link"href="' +
168+ '/ecosystem_fact_sheets/' + v . code + '.pdf' + //pdf file name
169+ '" download><img class="download-pdf-icon" src="/assets/pdf_24x24.png"></a>' +
170+ '</div>' )
171+ $ ( 'div.well:not(.inactive_site)' ) . find ( type ) . find ( '.biome_list' ) . append (
172+ element
153173 ) . parent ( ) . css ( "height" , "auto" ) ;
154174 // Could add delegate option here to show / hide the EDIT icon on checking the checkbox
155- } ) ;
175+ } ;
176+ } ;
177+ if ( data . native_eco != null ) {
178+ $ . each ( data . native_eco , biome_div ( '.native_biomes' ) ) ;
156179 } ;
157-
158180 if ( data . agroecosystem_eco != null ) {
159- $ . each ( data . agroecosystem_eco , function ( k , v ) {
160- $ ( 'div.well:not(.inactive_site)' ) . find ( '.agroecosystem_biomes' ) . find ( '.biome_list' ) . append (
161- '<div class="biome_match checkbox">' +
162- '<label class="biome-match"><input type="checkbox">' + k . replace ( / _ / g, " " ) + '</input></label>' +
163- '<a class="edit_icon_link action-link" data-toggle="modal" href="#ecosystem_popup">' +
164- '<i class="glyphicon glyphicon-list-alt inline-block" rel="tooltip" title="Edit" />' +
165- '</a>' +
166- '</div>'
167- ) . parent ( ) . css ( "height" , "auto" ) ;
168- // Could add delegate option here to show / hide the EDIT icon on checking the checkbox
169- } ) ;
181+ $ . each ( data . agroecosystem_eco , biome_div ( '.agroecosystem_biomes' ) ) ;
170182 } ;
171183 } ) ;
172184} ;
@@ -448,7 +460,7 @@ $(document).ready(function() {
448460 var biome_group = $ ( this ) ;
449461 var ecosystem_to_include = get_selected_ecosystems_name_and_type ( $ ( this ) ) ;
450462 var current_biomes_json = $ . parseJSON ( biome_group . find ( '.json_saved' ) . text ( ) ) ;
451- console . log ( "current_biomes_json: " + JSON . stringify ( current_biomes_json ) ) ;
463+ // console.log("current_biomes_json: " + JSON.stringify(current_biomes_json));
452464
453465 var loc = biome_group . find ( ".biome_site_header .site_latlng" ) . text ( ) ;
454466 var lat_lng_matcher = / \( * ( [ \d \. - ] + ) * , * ( [ \d \. - ] + ) * \) / ;
@@ -467,7 +479,7 @@ $(document).ready(function() {
467479
468480 $ . each ( ecosystem_to_include , function ( i , v ) {
469481 var input_ecosystem_json = current_biomes_json [ v [ 0 ] + "_eco" ] [ v [ 1 ] . replace ( / / g, "_" ) ] ;
470- console . log ( input_ecosystem_json ) ;
482+ // console.log( input_ecosystem_json );
471483 var biome_name_string = v [ 1 ] . replace ( / / g, "_" )
472484
473485 ghgvcR_input [ biome_group_string ] [ "ecosystems" ] [ biome_name_string ] = input_ecosystem_json ;
@@ -513,12 +525,12 @@ $(document).ready(function() {
513525 mi_svg = mi_svg . replace ( re , "glyph-mi" )
514526 co2_svg = atob ( data . plots . co2 [ 0 ] ) ;
515527 mi_svg = mi_svg . replace ( re , "glyph-co2" )
516-
528+
517529
518530 // Place SVGs on the map
519531 $ ( "#mi_container" ) . html ( mi_svg ) ;
520532 $ ( "#co2_container" ) . html ( co2_svg ) ;
521-
533+
522534 // make the returned data available so it can be exported to a csv
523535 window . json_data = data . results ;
524536 // Add the message about the black dots
0 commit comments