@@ -24,6 +24,8 @@ open Js_of_ocaml
24
24
open Editor_lib
25
25
open Dom_html
26
26
open Test_spec
27
+
28
+
27
29
(* ----------------------------------------------------------------------*)
28
30
29
31
let init_tabs, select_tab =
@@ -65,6 +67,22 @@ let set_string_translations () =
65
67
Manip. setInnerHtml (find_component id) text)
66
68
translations
67
69
70
+ let activate_before_unload () :unit =
71
+ Js.Unsafe. js_expr
72
+ " window.onbeforeunload = function() {return 'You have unsaved changes!';}"
73
+
74
+ let unable_before_unload () :unit =
75
+ Js.Unsafe. js_expr " window.onbeforeunload = null"
76
+
77
+ let onchange ace_list =
78
+ let add_change_listener ace =
79
+ Ace. on
80
+ ace
81
+ " change"
82
+ (fun _ -> activate_before_unload () ;) in
83
+ List. iter (fun ace -> add_change_listener ace) ace_list
84
+
85
+
68
86
let () =
69
87
run_async_with_log @@ fun () ->
70
88
(* set_string_translations ();*)
@@ -157,7 +175,6 @@ let () =
157
175
(Tyxml_js.To_dom. of_div editor_prelude) in
158
176
let ace_prel = Ocaml_mode. get_editor editor_prel in
159
177
let contents= get_prelude id in
160
-
161
178
Ace. set_contents ace_prel contents ;
162
179
Ace. set_font_size ace_prel 18 ;
163
180
@@ -384,6 +401,7 @@ let () =
384
401
end ;
385
402
386
403
let recovering () =
404
+ unable_before_unload () ;
387
405
let solution = Ace. get_contents ace in
388
406
let descr = Ace. get_contents ace_quest in
389
407
let template = Ace. get_contents ace_temp in
@@ -435,41 +453,14 @@ let () =
435
453
(* let toolbar_button2 = button2 ~container: exo_toolbar ~theme: "light" in*)
436
454
begin toolbar_button
437
455
~icon: " left" [% i" Metadata" ] @@ fun () ->
438
- recovering () ;
439
456
Dom_html. window##.location##assign
440
457
(Js. string (" new_exercise.html#id=" ^ id ^ " &action=open" ));
441
458
Lwt. return ()
442
459
end;
443
460
begin toolbar_button
444
461
~icon: " list" [% i" Exercises" ] @@ fun () ->
445
- let _aborted, abort_message =
446
- let t, _u = Lwt. task () in
447
- let btn_cancel = Tyxml_js.Html5. (button [ pcdata [% i" Cancel" ] ]) in
448
- Manip.Ev. onclick btn_cancel ( fun _ ->
449
- hide_loading ~id: " learnocaml-exo-loading" () ; true ) ;
450
- let btn_yes = Tyxml_js.Html5. (button [ pcdata [% i" Yes" ] ]) in
451
- Manip.Ev. onclick btn_yes (fun _ ->
452
- recovering () ;
453
462
Dom_html. window##.location##assign
454
- (Js. string " index.html#activity=editor" ) ; true ) ;
455
- let btn_no = Tyxml_js.Html5. (button [ pcdata [% i" No" ] ]) in
456
- Manip.Ev. onclick btn_no (fun _ ->
457
- Dom_html. window##.location##assign
458
- (Js. string " index.html#activity=editor" ) ; true );
459
- let div =
460
- Tyxml_js.Html5. (div ~a: [ a_class [ " dialog" ] ]
461
- [ pcdata [% i" Do you want to save before closing?\n " ] ;
462
- btn_yes ;
463
- pcdata " " ;
464
- btn_no ;
465
- pcdata " " ;
466
- btn_cancel ]) in
467
- Manip.SetCss. opacity div (Some " 0" ) ;
468
- t, div in
469
- Manip. replaceChildren messages
470
- Tyxml_js.Html5. [ li [ pcdata " " ] ] ;
471
- show_load " learnocaml-exo-loading" [ abort_message ] ;
472
- Manip.SetCss. opacity abort_message (Some " 1" ) ;
463
+ (Js. string " index.html#activity=editor" );
473
464
Lwt. return ()
474
465
end ;
475
466
@@ -544,7 +535,7 @@ let () =
544
535
recovering () ;
545
536
grade ()
546
537
end ;
547
- Window. onunload ( fun _ev -> recovering () ; true ) ;
538
+ onchange [ace_temp; ace_t; ace_prep; ace_prel; ace_quest; ace ] ;
548
539
(* ---- return -------------------------------------------------------- *)
549
540
(* toplevel_launch >>= fun _ -> should be unnecessary? *)
550
541
(* typecheck false >>= fun () -> *)
0 commit comments