@@ -14,6 +14,14 @@ use plotly::layout::{AxisType};
1414use yew:: prelude:: * ;
1515use serde:: Deserialize ;
1616
17+ use wasm_bindgen_futures:: spawn_local;
18+ use web_sys:: HtmlElement ;
19+ use web_sys:: { Blob , BlobPropertyBag , Url } ;
20+ use wasm_bindgen:: JsValue ;
21+ use js_sys:: Array ;
22+ use web_sys:: wasm_bindgen:: JsCast ;
23+
24+
1725#[ derive( Debug , Serialize , Deserialize ) ]
1826struct ReactionData {
1927 #[ serde( rename = "energy" ) ]
@@ -181,6 +189,41 @@ fn convert_string(entry: &Entry) -> String {
181189 output
182190}
183191
192+ async fn download_xs_cache ( selected_indexes : HashSet < usize > ) {
193+ let cache = generate_cache ( & selected_indexes) . await ;
194+
195+ // Convert the cache data to JSON
196+ let json_data = serde_json:: to_string ( & cache) . unwrap ( ) ;
197+
198+ // Create a Blob from the JSON data
199+ let mut blob_options = BlobPropertyBag :: new ( ) ;
200+ blob_options. set_type ( "application/json" ) ;
201+
202+ let blob = Blob :: new_with_str_sequence_and_options (
203+ & Array :: of1 ( & JsValue :: from_str ( & json_data) ) ,
204+ & blob_options,
205+ ) . unwrap ( ) ;
206+
207+ // Create a URL for the Blob
208+ let url = Url :: create_object_url_with_blob ( & blob) . unwrap ( ) ;
209+
210+ // Create a hidden anchor element to trigger the download
211+ let document = web_sys:: window ( ) . unwrap ( ) . document ( ) . unwrap ( ) ;
212+ let a = document. create_element ( "a" ) . unwrap ( ) ;
213+ a. set_attribute ( "href" , & url) . unwrap ( ) ;
214+ a. set_attribute ( "download" , "xs_cache.json" ) . unwrap ( ) ;
215+ a. set_attribute ( "style" , "display: none;" ) . unwrap ( ) ;
216+ document. body ( ) . unwrap ( ) . append_child ( & a) . unwrap ( ) ;
217+
218+ // Trigger the download
219+
220+ let a: HtmlElement = a. dyn_into :: < HtmlElement > ( ) . unwrap ( ) ;
221+ a. click ( ) ;
222+
223+ // Clean up by revoking the object URL
224+ Url :: revoke_object_url ( & url) . unwrap ( ) ;
225+ document. body ( ) . unwrap ( ) . remove_child ( & a) . unwrap ( ) ;
226+ }
184227
185228#[ function_component( Home ) ]
186229pub fn home ( ) -> Html {
@@ -381,6 +424,17 @@ pub fn home() -> Html {
381424 } )
382425 } ;
383426
427+
428+ let onclick_download = {
429+ let selected_indexes = selected_indexes. clone ( ) ;
430+ Callback :: from ( move |_| {
431+ let selected_indexes = selected_indexes. current ( ) . clone ( ) ;
432+ spawn_local ( async move {
433+ download_xs_cache ( selected_indexes) . await ;
434+ } ) ;
435+ } )
436+ } ;
437+
384438 // let pagination_options = yew_custom_components::pagination::Options::default()
385439 // .show_prev_next(true)
386440 // .show_first_last(true)
@@ -491,6 +545,15 @@ pub fn home() -> Html {
491545 >
492546 { if * is_y_log { "Switch Y to Linear Scale" } else { "Switch Y to Log Scale" } }
493547 </button>
548+
549+ <button
550+ class="btn btn-success mt-2"
551+ onclick={ onclick_download}
552+ >
553+ <i class="fas fa-download mr-2" ></i>
554+ { " Download Cross Section Data" }
555+ </button>
556+
494557 </div>
495558
496559 <div class="d-flex mb-2" >
0 commit comments