Skip to content

Commit

Permalink
Template: simplifying demo's code
Browse files Browse the repository at this point in the history
Removing first class modules to make the code easier to read for beginners
Making the code more canonical
  • Loading branch information
balat committed Feb 18, 2024
1 parent 8327946 commit 32b423b
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 349 deletions.
21 changes: 1 addition & 20 deletions template.distillery/demo.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,5 @@ let%shared handler myid_o () () =
; p [%i18n Demo.intro_3] ]

let%shared () =
let registerDemo (module D : Demo_tools.Page) =
%%%MODULE_NAME%%%_base.App.register ~service:D.service
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = D.page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class [D.page_class]] myid_o p )
in
List.iter registerDemo Demo_tools.demos;
%%%MODULE_NAME%%%_base.App.register ~service:%%%MODULE_NAME%%%_services.demo_service
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo
(%%%MODULE_NAME%%%_page.Opt.connected_page handler)

(* [detail_page_handler] is not registered in [Demo_tools] because we
- don't want to show detail pages in the menu. *)
let%shared () =
let detail_page_handler myid_o page () =
%%%MODULE_NAME%%%_container.page
~a:[a_class ["os-page-demo-transition"]]
myid_o
(Demo_pagetransition.make_detail_page page ())
in
%%%MODULE_NAME%%%_base.App.register ~service:Demo_pagetransition.detail_page_service
(%%%MODULE_NAME%%%_page.Opt.connected_page detail_page_handler)
24 changes: 10 additions & 14 deletions template.distillery/demo_cache.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
(* Eliom_cscache demo *)
open Eliom_content.Html.F]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-cache"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.cache]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-cache"

(* Page for this demo *)
let%shared page () =
Lwt.return
Expand All @@ -29,3 +15,13 @@ let%shared page () =
~os_user_proxy:[code [txt "Os_user_proxy"]]]
; p [%i18n Demo.cache_3 ~eliom_cscache:[code [txt "Eliom_cscache"]]]
; p [%i18n Demo.cache_4 ~eliom_cscache:[code [txt "Eliom_cscache"]]] ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_cache
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-cache"]] myid_o p )
24 changes: 10 additions & 14 deletions template.distillery/demo_calendar.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
(* Calendar demo *)
open Eliom_content.Html.D]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-calendar"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* A reactive value containing the currently selected date *)
(* NOTE: in this example, we define a shared signal on the server side. Its
original value can only be read when the server generates the first page
Expand Down Expand Up @@ -45,12 +37,6 @@ let%server date_as_string () : string Eliom_shared.React.S.t =
let%rpc date_reactive () : string Eliom_shared.React.S.t Lwt.t =
Lwt.return @@ date_as_string ()

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.calendar]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-calendar"

(* Page for this demo *)
let%shared page () =
let calendar =
Expand All @@ -62,3 +48,13 @@ let%shared page () =
; p [%i18n Demo.this_page_show_calendar]
; div ~a:[a_class ["os-calendar"]] [calendar]
; p [Eliom_content.Html.R.txt dr] ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_calendar
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-calendar"]] myid_o p )
24 changes: 10 additions & 14 deletions template.distillery/demo_carousel1.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ open Eliom_content.Html]

[%%shared open Eliom_content.Html.F]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-carousel1"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.carousel_1]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-carousel1"

(* Bind arrow keys *)
let%shared bind_keys
(change : ([`Goto of int | `Next | `Prev] -> unit) Eliom_client_value.t)
Expand Down Expand Up @@ -78,3 +64,13 @@ let%shared page () =
~a:[a_class ["demo-carousel1"]]
[div ~a:[a_class ["demo-carousel1-box"]] [carousel; prev; next; bullets]]
]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_carousel1
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-carousel1"]] myid_o p )
24 changes: 10 additions & 14 deletions template.distillery/demo_carousel2.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ let%shared lorem_ipsum =
"Amicitiam autem adhibendam esse censent, quia sit ex eo genere, quae prosunt. Hoc loco tenere se Triarius non potuit. Facile est hoc cernere in primis puerorum aetatulis. Sed in rebus apertissimis nimium longi sumus. Utrum igitur tibi litteram videor an totas paginas commovere? Quid de Platone aut de Democrito loquar?"
] ]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-carousel2"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.carousel_2]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-carousel2"

(* Page for this demo *)
let%shared page () =
let make_page name =
Expand Down Expand Up @@ -93,3 +79,13 @@ let%shared page () =
; div
~a:[a_class ["demo-carousel2"]]
[div ~a:[a_class ["demo-carousel2-box"]] [tabs; carousel]] ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_carousel2
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-carousel2"]] myid_o p )
24 changes: 10 additions & 14 deletions template.distillery/demo_carousel3.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ open Eliom_content.Html]

[%%shared open Eliom_content.Html.F]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-carousel3"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.carousel_wheel]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-carousel3"

(* Page for this demo *)
let%shared page () =
let carousel_pages =
Expand Down Expand Up @@ -86,3 +72,13 @@ let%shared page () =
~change ~pos
~vis_elts:(Eliom_shared.React.S.const 1)
~length [] ] ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_carousel3
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-carousel3"]] myid_o p )
24 changes: 10 additions & 14 deletions template.distillery/demo_i18n.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
(* Ocsigen_i18n demo *)
open Eliom_content.Html.F]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-i18n"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.internationalization ~capitalize:true]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-i18n"

(* Page for this demo *)
let%shared page () =
(* Syntax [%i18n key] or [%i18n Module.key] inserts
Expand All @@ -43,3 +29,13 @@ let%shared page () =
Demo.internationalization_4
~f:[code [txt "assets/%%%PROJECT_NAME%%%_Demo_i18n.tsv"]]
~demo_prefix:[code [txt "demo_"]]] ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_i18n
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-i18n"]] myid_o p )
24 changes: 10 additions & 14 deletions template.distillery/demo_links.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
(* Static files demo *)
open Eliom_content.Html.F]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-static-files"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.links_and_static_files]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-links"

(* An example of external service: *)
let%server ocsigen_service =
Eliom_service.extern ~prefix:"http://ocsigen.org" ~path:[]
Expand Down Expand Up @@ -70,3 +56,13 @@ let%shared page () =
~service:(Eliom_service.static_dir ())
["images"; "ocsigen.png"])
() ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_links
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-links"]] myid_o p )
25 changes: 12 additions & 13 deletions template.distillery/demo_notif.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@
(* Notification demo *)
open Js_of_ocaml_lwt]

(* Service for this demo *)
let%server service =
Eliom_service.create ~path:(Eliom_service.Path ["demo-notif"])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

(* Make service available on the client *)
let%client service = ~%service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.notification]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-notif"
[%%shared open Eliom_content]
[%%shared open Html.D]

(* Instantiate function Os_notif.Simple for each kind of notification
you need.
Expand Down Expand Up @@ -105,3 +94,13 @@ let%shared page () =
; txt [%i18n Demo.S.fill_input_form_send_message] ])
; make_form [%i18n Demo.S.send_message]
[%client (notify : string -> unit Lwt.t)] ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_notif
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page ~a:[a_class ["os-page-demo-notif"]] myid_o p )
47 changes: 24 additions & 23 deletions template.distillery/demo_pagetransition.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,16 @@ open Eliom_content]
[%%shared open Html.D]
[%%client open Js_of_ocaml_lwt]

(* Service for this demo *)
let%server service =
Eliom_service.create
~path:(Eliom_service.Path ["demo-page-transition"; ""])
~meth:(Eliom_service.Get Eliom_parameter.unit) ()

let%server detail_page_service =
Eliom_service.create
~path:(Eliom_service.Path ["demo-page-transition"; "detail"; ""])
~meth:(Eliom_service.Get (Eliom_parameter.int "page"))
()

(* Make service available on the client *)
let%client service = ~%service
let%client detail_page_service = ~%detail_page_service

(* Name for demo menu *)
let%shared name () = [%i18n Demo.S.pagetransition]

(* Class for the page containing this demo (for internal use) *)
let%shared page_class = "os-page-demo-transition"

let%shared create_item index =
let open F in
li
~a:
[ a_class
["demo-list-item"; Printf.sprintf "demo-list-item-%d" (index mod 5)]
]
[a ~service:detail_page_service [txt (Printf.sprintf "list%d" index)] index]
[ a ~service:Demo_services.detail_page
[txt (Printf.sprintf "list%d" index)]
index ]

let%shared page () =
let l =
Expand Down Expand Up @@ -93,3 +73,24 @@ let%shared make_detail_page page () =
([%i18n Demo.pagetransition_detail_page]
@ [txt (Printf.sprintf " %d" page)])
; back_button ]

(* Service registration is done on both sides (shared section),
so that pages can be generated from the server
(first request, crawling, search engines ...)
or the client (subsequent link clicks, or mobile app ...). *)
let%shared () =
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.demo_pagetransition
( %%%MODULE_NAME%%%_page.Opt.connected_page @@ fun myid_o () () ->
let%lwt p = page () in
%%%MODULE_NAME%%%_container.page
~a:[a_class ["os-page-demo-pagetransition"]]
myid_o p )

let%shared () =
let detail_page_handler myid_o page () =
%%%MODULE_NAME%%%_container.page
~a:[a_class ["os-page-demo-transition"]]
myid_o (make_detail_page page ())
in
%%%MODULE_NAME%%%_base.App.register ~service:Demo_services.detail_page
(%%%MODULE_NAME%%%_page.Opt.connected_page detail_page_handler)
Loading

0 comments on commit 32b423b

Please sign in to comment.