Skip to content

Commit

Permalink
Prefix URLs in the front-end
Browse files Browse the repository at this point in the history
Signed-off-by: Alban Gruin <alban.gruin@univ-tlse3.fr>
  • Loading branch information
agrn committed Jun 29, 2020
1 parent 1b25b04 commit 22f7f8f
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/app/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
(wrapped false)
(flags :standard -warn-error -9-27-32)
(modules Learnocaml_local_storage
Learnocaml_config
Server_caller
Learnocaml_common)
(preprocess
(per_module ((pps js_of_ocaml.ppx)
Learnocaml_config
Learnocaml_local_storage
Server_caller)
((pps ppx_ocplib_i18n js_of_ocaml.ppx)
Expand Down
14 changes: 8 additions & 6 deletions src/app/learnocaml_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
open Js_utils
open Lwt.Infix
open Learnocaml_data
open Learnocaml_config

module H = Tyxml_js.Html

Expand Down Expand Up @@ -171,7 +172,7 @@ let show_loading ?(id = "ocp-loading-layer") contents f =
Manip.(removeClass elt "loaded") ;
Manip.(addClass elt "loading") ;
let chamo_src =
"/icons/tryocaml_loading_" ^ string_of_int (Random.int 9 + 1) ^ ".gif" in
api_server ^ "/icons/tryocaml_loading_" ^ string_of_int (Random.int 9 + 1) ^ ".gif" in
Manip.replaceChildren elt
H.[
div ~a: [ a_id "chamo" ] [ img ~alt: "loading" ~src: chamo_src () ] ;
Expand Down Expand Up @@ -286,7 +287,7 @@ let button ~container ~theme ?group ?state ~icon lbl cb =
| Some group -> group in
let button =
H.(button [
img ~alt:"" ~src:("/icons/icon_" ^ icon ^ "_" ^ theme ^ ".svg") () ;
img ~alt:"" ~src:(api_server ^ "/icons/icon_" ^ icon ^ "_" ^ theme ^ ".svg") () ;
pcdata " " ;
span ~a:[ a_class [ "label" ] ] [ pcdata lbl ]
]) in
Expand Down Expand Up @@ -537,13 +538,13 @@ let stars_div stars =
let num = 5 * int_of_float (stars *. 2.) in
let num = max (min num 40) 0 in
let alt = Format.asprintf [%if"difficulty: %d / 40"] num in
let src = Format.asprintf "/icons/stars_%02d.svg" num in
let src = Format.asprintf "%s/icons/stars_%02d.svg" api_server num in
H.img ~alt ~src ()
]
let exercise_text ex_meta exo =
let mathjax_url =
"/js/mathjax/MathJax.js?delayStartupUntil=configured"
api_server ^ "/js/mathjax/MathJax.js?delayStartupUntil=configured"
in
let mathjax_config =
"MathJax.Hub.Config({\n\
Expand Down Expand Up @@ -578,7 +579,7 @@ let exercise_text ex_meta exo =
<html><head>\
<title>%s - exercise text</title>\
<meta charset='UTF-8'>\
<link rel='stylesheet' href='/css/learnocaml_standalone_description.css'>\
<link rel='stylesheet' href='%s/css/learnocaml_standalone_description.css'>\
<script type='text/x-mathjax-config'>%s</script>
<script type='text/javascript' src='%s'></script>\
</head>\
Expand All @@ -588,6 +589,7 @@ let exercise_text ex_meta exo =
<script type='text/javascript'>MathJax.Hub.Configured()</script>\
</html>"
ex_meta.Exercise.Meta.title
api_server
mathjax_config
mathjax_url
descr
Expand Down Expand Up @@ -1023,7 +1025,7 @@ module Display_exercise =
let num = 5 * int_of_float (ex_meta.Meta.stars *. 2.) in
let num = max (min num 40) 0 in
let alt = Format.asprintf [%if"difficulty: %d / 40"] num in
let src = Format.asprintf "/icons/stars_%02d.svg" num in
let src = Format.asprintf "%s/icons/stars_%02d.svg" api_server num in
img ~alt ~src ()
in
div ~a:[ a_class [ "stars" ] ] [
Expand Down
20 changes: 20 additions & 0 deletions src/app/learnocaml_config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(* This file is part of Learn-OCaml
*
* Copyright (C) 2020 Alban Gruin.
*
* Learn-OCaml is distributed under the terms of the MIT license. See the
* included LICENSE file for details. *)

class type learnocaml_config = object
method enableTryocaml: bool Js.optdef_prop
method enableLessons: bool Js.optdef_prop
method enableExercises: bool Js.optdef_prop
method enableToplevel: bool Js.optdef_prop
method enablePlayground: bool Js.optdef_prop
method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop
method txtNickname: Js.js_string Js.t Js.optdef_prop
method root: Js.js_string Js.t Js.optdef_prop
end

let config : learnocaml_config Js.t = Js.Unsafe.js_expr "learnocaml_config"
let api_server = Js.(to_string (Optdef.get config##.root (fun () -> string "")))
24 changes: 24 additions & 0 deletions src/app/learnocaml_config.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(* This file is part of Learn-OCaml
*
* Copyright (C) 2020 Alban Gruin.
*
* Learn-OCaml is distributed under the terms of the MIT license. See the
* included LICENSE file for details. *)

(* This is not transpiled to learnocaml-static.js, but is an interface
to the values stored in this file. It is "statically linked" with
learnocaml-common.ml. *)

class type learnocaml_config = object
method enableTryocaml: bool Js.optdef_prop
method enableLessons: bool Js.optdef_prop
method enableExercises: bool Js.optdef_prop
method enableToplevel: bool Js.optdef_prop
method enablePlayground: bool Js.optdef_prop
method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop
method txtNickname: Js.js_string Js.t Js.optdef_prop
method root: Js.js_string Js.t Js.optdef_prop
end

val config : learnocaml_config Js.t
val api_server : string
19 changes: 4 additions & 15 deletions src/app/learnocaml_index_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open Js_utils
open Lwt
open Learnocaml_data
open Learnocaml_common
open Learnocaml_config

module H = Tyxml_js.Html5

Expand Down Expand Up @@ -63,7 +64,7 @@ let show_loading msg = show_loading ~id:El.loading_id H.[ul [li [pcdata msg]]]
let get_url token dynamic_url static_url id =
match token with
| Some _ -> dynamic_url ^ Url.urlencode id ^ "/"
| None -> static_url ^ Url.urlencode id
| None -> api_server ^ "/" ^ static_url ^ Url.urlencode id

let exercises_tab token _ _ () =
show_loading [%i"Loading exercises"] @@ fun () ->
Expand Down Expand Up @@ -111,7 +112,7 @@ let exercises_tab token _ _ () =
| Some pct when pct >= 100 -> [ "stats" ; "success" ]
| Some _ -> [ "stats" ; "partial" ])
pct_signal in
a ~a:[ a_href (get_url token "/exercises/" "/exercise.html#id=" exercise_id) ;
a ~a:[ a_href (get_url token "/exercises/" "exercise.html#id=" exercise_id) ;
a_class [ "exercise" ] ] [
div ~a:[ a_class [ "descr" ] ] (
h1 [ pcdata title ] ::
Expand Down Expand Up @@ -163,7 +164,7 @@ let playground_tab token _ _ () =
let open Tyxml_js.Html5 in
let title = pmeta.Playground.Meta.title in
let short_description = pmeta.Playground.Meta.short_description in
a ~a:[ a_href (get_url token "/playground/" "/playground.html#id=" id) ;
a ~a:[ a_href (get_url token "/playground/" "playground.html#id=" id) ;
a_class [ "exercise" ] ] [
div ~a:[ a_class [ "descr" ] ] (
h1 [ pcdata title ] ::
Expand Down Expand Up @@ -606,18 +607,6 @@ let init_sync_token button_group =
Lwt.return (Some token))
(fun _ -> Lwt.return None)

class type learnocaml_config = object
method enableTryocaml: bool Js.optdef_prop
method enableLessons: bool Js.optdef_prop
method enableExercises: bool Js.optdef_prop
method enableToplevel: bool Js.optdef_prop
method enablePlayground: bool Js.optdef_prop
method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop
method txtNickname: Js.js_string Js.t Js.optdef_prop
end

let config : learnocaml_config Js.t = Js.Unsafe.js_expr "learnocaml_config"

let set_string_translations () =
let configured v s = Js.Optdef.case v (fun () -> s) Js.to_string in
let translations = [
Expand Down
5 changes: 2 additions & 3 deletions src/app/server_caller.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ let () =
(Printexc.to_string e))
| _ -> None

let urlpath =
let api_server = "" in
fun p -> String.concat "/" (api_server::p)
let urlpath p =
String.concat "/" (Learnocaml_config.api_server :: p)

let request req =
let do_req = function
Expand Down
1 change: 1 addition & 0 deletions static/description.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
<script language="JavaScript" src="/js/learnocaml-description.js" defer></script>

</head>
Expand Down
3 changes: 2 additions & 1 deletion static/exercise.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
<script language="JavaScript" src="/js/learnocaml-exercise.js" defer></script>
</head>
<body>
Expand All @@ -32,7 +33,7 @@
</div>
<script language="JavaScript">
var n = Math.floor (Math.random () * 8.99) + 1;
document.getElementById('chamo-img').src = '/icons/tryocaml_loading_' + n + '.gif';
document.getElementById('chamo-img').src = learnocaml_config.root + '/icons/tryocaml_loading_' + n + '.gif';
</script>
<!-- Anything below could be recreated dynamically, but IDs must be kept. -->
<div id="learnocaml-exo-toolbar">
Expand Down
1 change: 1 addition & 0 deletions static/partition-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
<script language="JavaScript" src="/js/learnocaml-partition-view.js" defer></script>
</head>
<body>
Expand Down
3 changes: 2 additions & 1 deletion static/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
<script language="JavaScript" src="/js/learnocaml-playground.js" defer></script>
</head>
<body>
Expand All @@ -31,7 +32,7 @@
</div>
<script language="JavaScript">
var n = Math.floor (Math.random () * 8.99) + 1;
document.getElementById('chamo-img').src = '/icons/tryocaml_loading_' + n + '.gif';
document.getElementById('chamo-img').src = learnocaml_config.root + '/icons/tryocaml_loading_' + n + '.gif';
</script>
<!-- Anything below could be recreated dynamically, but IDs must be kept. -->
<div id="learnocaml-exo-toolbar">
Expand Down
1 change: 1 addition & 0 deletions static/student-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
<script language="JavaScript" src="/js/learnocaml-student-view.js" defer></script>
</head>
<body>
Expand Down

0 comments on commit 22f7f8f

Please sign in to comment.