Skip to content

Commit 1d2052d

Browse files
committed
refactor : some refactor of editor.ml
1 parent a61bc99 commit 1d2052d

File tree

6 files changed

+62
-126
lines changed

6 files changed

+62
-126
lines changed

src/app/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
learnocaml_repository
2222
learnocaml_data
2323
learnocaml_api
24+
grading_jsoo
2425
ocplib_i18n)
2526
)
2627

src/app/learnocaml_common.ml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,3 +930,43 @@ let setup_prelude_pane ace prelude =
930930
(fun _ -> state := not !state ; update () ; true) ;
931931
Manip.appendChildren prelude_pane
932932
[ prelude_title ; prelude_container ]
933+
934+
module Grade_exercise = struct
935+
936+
let get_grade =
937+
let get_worker = get_worker_code "learnocaml-grader-worker.js" in
938+
fun ?callback ?timeout exercise ->
939+
get_worker () >>= fun worker_js_file ->
940+
Grading_jsoo.get_grade ~worker_js_file ?callback ?timeout exercise
941+
942+
let display_report exo report =
943+
let score, _failed = Report.result report in
944+
let report_button = find_component "learnocaml-exo-button-report" in
945+
Manip.removeClass report_button "success" ;
946+
Manip.removeClass report_button "failure" ;
947+
Manip.removeClass report_button "partial" ;
948+
let grade =
949+
let max = Learnocaml_exercise.(access File.max_score exo) in
950+
if max = 0 then 999 else score * 100 / max
951+
in
952+
if grade >= 100 then begin
953+
Manip.addClass report_button "success" ;
954+
Manip.replaceChildren report_button
955+
Tyxml_js.Html5.[ pcdata [%i"Report"] ]
956+
end else if grade = 0 then begin
957+
Manip.addClass report_button "failure" ;
958+
Manip.replaceChildren report_button
959+
Tyxml_js.Html5.[ pcdata [%i"Report"] ]
960+
end else begin
961+
Manip.addClass report_button "partial" ;
962+
let pct = Format.asprintf "%2d%%" grade in
963+
Manip.replaceChildren report_button
964+
Tyxml_js.Html5.[ pcdata [%i"Report"] ;
965+
span ~a: [ a_class [ "score" ] ] [ pcdata pct ]]
966+
end ;
967+
let report_container = find_component "learnocaml-exo-tab-report" in
968+
Manip.setInnerHtml report_container
969+
(Format.asprintf "%a" Report.(output_html ~bare: true) report) ;
970+
grade
971+
972+
end

src/app/learnocaml_common.mli

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,14 @@ val typecheck :
215215
val set_nickname_div : unit -> unit
216216

217217
val setup_prelude_pane : 'a Ace.editor -> string -> unit
218+
219+
module Grade_exercise : sig
220+
val get_grade :
221+
?callback:(string -> unit) ->
222+
?timeout:float ->
223+
Learnocaml_exercise.t ->
224+
(string -> (Learnocaml_report.t * string * string * string) Lwt.t) Lwt.t
225+
val display_report :
226+
Learnocaml_exercise.t -> Learnocaml_data.Report.t -> int
227+
end
228+

src/app/learnocaml_exercise_main.ml

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ open Js_utils
1010
open Lwt.Infix
1111
open Learnocaml_common
1212
open Learnocaml_data
13-
13+
open Grade_exercise
1414
module H = Tyxml_js.Html
1515

1616
let init_tabs, select_tab =
@@ -29,42 +29,6 @@ let check_if_need_refresh () =
2929
let contents = [ H.p [H.pcdata (String.trim message) ] ] in
3030
confirm ~title ~ok_label ~cancel_label contents refresh
3131

32-
let get_grade =
33-
let get_worker = get_worker_code "learnocaml-grader-worker.js" in
34-
fun ?callback ?timeout exercise ->
35-
get_worker () >>= fun worker_js_file ->
36-
Grading_jsoo.get_grade ~worker_js_file ?callback ?timeout exercise
37-
38-
let display_report exo report =
39-
let score, _failed = Report.result report in
40-
let report_button = find_component "learnocaml-exo-button-report" in
41-
Manip.removeClass report_button "success" ;
42-
Manip.removeClass report_button "failure" ;
43-
Manip.removeClass report_button "partial" ;
44-
let grade =
45-
let max = Learnocaml_exercise.(access File.max_score exo) in
46-
if max = 0 then 999 else score * 100 / max
47-
in
48-
if grade >= 100 then begin
49-
Manip.addClass report_button "success" ;
50-
Manip.replaceChildren report_button
51-
Tyxml_js.Html5.[ pcdata [%i"Report"] ]
52-
end else if grade = 0 then begin
53-
Manip.addClass report_button "failure" ;
54-
Manip.replaceChildren report_button
55-
Tyxml_js.Html5.[ pcdata [%i"Report"] ]
56-
end else begin
57-
Manip.addClass report_button "partial" ;
58-
let pct = Format.asprintf "%2d%%" grade in
59-
Manip.replaceChildren report_button
60-
Tyxml_js.Html5.[ pcdata [%i"Report"] ;
61-
span ~a: [ a_class [ "score" ] ] [ pcdata pct ]]
62-
end ;
63-
let report_container = find_component "learnocaml-exo-tab-report" in
64-
Manip.setInnerHtml report_container
65-
(Format.asprintf "%a" Report.(output_html ~bare: true) report) ;
66-
grade
67-
6832
let display_descr ex_meta =
6933
let open Tyxml_js.Html5 in
7034
let open Learnocaml_data.Exercise in

src/app/learnocaml_exercise_main.mli

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/editor/editor.ml

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,19 @@
1818
open Js_utils
1919
open Lwt.Infix
2020
open Learnocaml_common
21+
open Grade_exercise
2122
open Learnocaml_data
2223
open Js_of_ocaml
2324
open Editor_lib
2425
open Dom_html
2526
open Test_spec
26-
27-
let display_report exo report =
28-
let score, _failed = Report.result report in
29-
let report_button = find_component "learnocaml-exo-button-report" in
30-
Manip.removeClass report_button "success" ;
31-
Manip.removeClass report_button "failure" ;
32-
Manip.removeClass report_button "partial" ;
33-
let grade =
34-
let max = Learnocaml_exercise.(access File.max_score exo) in
35-
if max = 0 then 999 else score * 100 / max
36-
in
37-
if grade >= 100 then begin
38-
Manip.addClass report_button "success" ;
39-
Manip.replaceChildren report_button
40-
Tyxml_js.Html5.[ pcdata [%i"Report"] ]
41-
end else if grade = 0 then begin
42-
Manip.addClass report_button "failure" ;
43-
Manip.replaceChildren report_button
44-
Tyxml_js.Html5.[ pcdata [%i"Report"] ]
45-
end else begin
46-
Manip.addClass report_button "partial" ;
47-
let pct = Format.asprintf "%2d%%" grade in
48-
Manip.replaceChildren report_button
49-
Tyxml_js.Html5.[ pcdata [%i"Report"] ;
50-
span ~a: [ a_class [ "score" ] ] [ pcdata pct ]]
51-
end ;
52-
let report_container = find_component "learnocaml-exo-tab-report" in
53-
Manip.setInnerHtml report_container
54-
(Format.asprintf "%a" Report.(output_html ~bare: true) report) ;
55-
grade
56-
57-
58-
let get_grade =
59-
let get_worker = get_worker_code "learnocaml-grader-worker.js" in
60-
fun ?callback ?timeout exercise ->
61-
get_worker () >>= fun worker_js_file ->
62-
Grading_jsoo.get_grade ~worker_js_file ?callback ?timeout exercise
63-
6427
(*----------------------------------------------------------------------*)
6528

6629
let init_tabs, select_tab =
67-
let names = [ "toplevel" ; "report" ; "editor" ; "template" ; "test" ;
68-
"question" ; "prelude" ; "prepare" ] in
69-
let current = ref "question" in
70-
let select_tab name =
71-
set_arg "tab" name ;
72-
Manip.removeClass
73-
(find_component ("learnocaml-exo-button-" ^ !current))
74-
"front-tab" ;
75-
Manip.removeClass
76-
(find_component ("learnocaml-exo-tab-" ^ !current))
77-
"front-tab" ;
78-
Manip.enable
79-
(find_component ("learnocaml-exo-button-" ^ !current)) ;
80-
Manip.addClass
81-
(find_component ("learnocaml-exo-button-" ^ name))
82-
"front-tab" ;
83-
Manip.addClass
84-
(find_component ("learnocaml-exo-tab-" ^ name))
85-
"front-tab" ;
86-
Manip.disable
87-
(find_component ("learnocaml-exo-button-" ^ name)) ;
88-
current := name in
89-
let init_tabs () =
90-
current := begin try
91-
let requested = arg "tab" in
92-
if List.mem requested names then requested else "question"
93-
with Not_found -> "question"
94-
end ;
95-
List.iter
96-
(fun name ->
97-
Manip.removeClass
98-
(find_component ("learnocaml-exo-button-" ^ name))
99-
"front-tab" ;
100-
Manip.removeClass
101-
(find_component ("learnocaml-exo-tab-" ^ name))
102-
"front-tab" ;
103-
Manip.Ev.onclick
104-
(find_component ("learnocaml-exo-button-" ^ name))
105-
(fun _ -> select_tab name ; true))
106-
names ;
107-
select_tab !current in
108-
init_tabs, select_tab
109-
30+
mk_tab_handlers "question" [ "toplevel" ; "report" ; "editor" ; "template" ; "test" ;
31+
"prelude" ; "prepare" ]
32+
33+
11034
let set_string_translations () =
11135
let translations = [
11236
"txt_preparing", [%i"Preparing the environment"];
@@ -142,13 +66,7 @@ let set_string_translations () =
14266
translations
14367

14468
let () =
145-
Lwt.async_exception_hook := begin function
146-
| Failure message -> fatal message
147-
| Server_caller.Cannot_fetch message -> fatal message
148-
| exn -> fatal (Printexc.to_string exn)
149-
end ;
150-
(match Js_utils.get_lang() with Some l -> Ocplib_i18n.set_lang l | None -> ());
151-
Lwt.async @@ fun () ->
69+
run_async_with_log @@ fun () ->
15270
(*set_string_translations ();*)
15371
Learnocaml_local_storage.init () ;
15472

@@ -600,6 +518,9 @@ let () =
600518
Lwt.return_unit
601519
end;
602520
*)
521+
522+
(* TODO : factorize somehow this with
523+
src/app/learnocaml_exercise_main grade to learnocaml_common *)
603524
let messages = Tyxml_js.Html5.ul [] in
604525
let callback text =
605526
Manip.appendChild messages Tyxml_js.Html5.(li [ pcdata text ]) in

0 commit comments

Comments
 (0)