aboutsummaryrefslogtreecommitdiff
path: root/editor/actions
diff options
context:
space:
mode:
Diffstat (limited to 'editor/actions')
-rwxr-xr-xeditor/actions/actions.ml128
-rwxr-xr-xeditor/actions/add_page.ml11
-rwxr-xr-xeditor/actions/delete_page.ml16
-rwxr-xr-xeditor/actions/dune13
-rwxr-xr-xeditor/actions/event.ml8
5 files changed, 176 insertions, 0 deletions
diff --git a/editor/actions/actions.ml b/editor/actions/actions.ml
new file mode 100755
index 0000000..e8b4d71
--- /dev/null
+++ b/editor/actions/actions.ml
@@ -0,0 +1,128 @@
+open StdLabels
+open Js_of_ocaml
+open Brr
+open Brr_note
+
+module Event = Event
+
+type button_actions =
+ { delete : Event.t Note.event
+ ; redirect : Jstr.t option Note.event
+ ; add: Event.t Note.event
+ }
+
+let populate_menu
+ : Forms.Events.event option Note.E.send -> button_actions option
+ = fun sender ->
+ match Blog.Sidebar.get () with
+ | None -> None
+ | Some element ->
+ let () = Blog.Sidebar.clean element in
+
+ let delete_button = El.button
+ ~at:At.[ class' (Jstr.v "action-button") ]
+ [ El.i []
+ ~at:At.[ class' (Jstr.v "fa")
+ ; class' (Jstr.v "fa-2x")
+ ; class' (Jstr.v "fa-trash") ] ]
+
+ and home_button = El.button
+ ~at:At.[ class' (Jstr.v "action-button") ]
+ [ El.i []
+ ~at:At.[ class' (Jstr.v "fa")
+ ; class' (Jstr.v "fa-2x")
+ ; class' (Jstr.v "fa-home") ] ]
+
+ and add_button = El.button
+ ~at:At.[ class' (Jstr.v "action-button") ]
+ [ El.i []
+ ~at:At.[ class' (Jstr.v "fa")
+ ; class' (Jstr.v "fa-2x")
+ ; class' (Jstr.v "fa-plus") ] ]
+
+ in
+
+ let delete_event =
+ Evr.on_el
+ Ev.click
+ (fun _ -> Event.E
+ ( sender
+ , (module Delete_page: Event.Handler with type t = Delete_page.t)) )
+ delete_button
+
+ and add_event =
+ Evr.on_el
+ Ev.click
+ (fun _ -> Event.E
+ ( sender
+ , (module Add_page: Event.Handler with type t = Add_page.t)) )
+ add_button in
+
+ let stored_pages = State.Storage.get_ids () in
+ let pages =
+ List.map
+ stored_pages
+ ~f:(fun id ->
+
+ let name_opt = (State.Storage.load (Some id))##.title in
+ let name = Js.Opt.get
+ name_opt
+ (fun () -> id) in
+
+ let target = Jstr.v "#" in
+ El.li
+ [ El.a
+ ~at:[At.href target]
+ [ El.txt name ] ]
+ ) in
+
+ (* Wait for a click on an existing page in order to sent the associated
+ event.
+
+ We compose the resulting event with both :
+ - the home button
+ - the list for all the pages presents in the sidebar *)
+ let redirect_event = Note.E.select
+ (( Evr.on_el
+ Ev.click
+ (fun _ -> None)
+ home_button
+ ) :: (
+ List.map2 stored_pages pages
+ ~f:(fun name el ->
+ Evr.on_el
+ Ev.click
+ (fun _ -> Some name)
+ el ))) in
+
+ let childs =
+ [ home_button
+ ; add_button
+ ; El.button
+ ~at:At.[class' (Jstr.v "action-button")]
+ [ El.i
+ []
+ ~at:At.[ class' (Jstr.v "fa")
+ ; class' (Jstr.v "fa-2x")
+ ; class' (Jstr.v "fa-download") ]
+ ]
+ ; delete_button
+ ; El.button
+ ~at:At.[class' (Jstr.v "action-button")]
+ [ El.i
+ []
+ ~at:At.[ class' (Jstr.v "fa")
+ ; class' (Jstr.v "fa-2x")
+ ; class' (Jstr.v "fa-cog") ]
+ ]
+ ; El.hr ()
+ ; El.ul
+ pages
+ ] in
+
+ let () = El.append_children element childs in
+ Some
+ { delete = delete_event
+ ; redirect = redirect_event
+ ; add = add_event
+ }
diff --git a/editor/actions/add_page.ml b/editor/actions/add_page.ml
new file mode 100755
index 0000000..b817573
--- /dev/null
+++ b/editor/actions/add_page.ml
@@ -0,0 +1,11 @@
+type t = Forms.Events.event option Note.E.send
+
+let apply
+ : t -> State.t -> State.t
+ = fun close_sender state ->
+ let title = Jstr.v "Nouvelle page" in
+ let popup = Forms.Ui.popup
+ ~title
+ ~form:(Some (Forms.Add_page.create ()))
+ close_sender in
+ { state with window = popup::state.window}
diff --git a/editor/actions/delete_page.ml b/editor/actions/delete_page.ml
new file mode 100755
index 0000000..cc15693
--- /dev/null
+++ b/editor/actions/delete_page.ml
@@ -0,0 +1,16 @@
+type t = Forms.Events.event option Note.E.send
+
+let apply
+ : t -> State.t -> State.t
+ = fun close_sender state ->
+ begin match state.page_id with
+ | None -> state
+ | Some page_id ->
+ let title = Jstr.v "Confirmation" in
+ let popup = Forms.Ui.popup
+ ~title
+ ~form:(Some (Forms.Delete_page.create page_id))
+ close_sender in
+ { state with window = popup::state.window}
+ end
+
diff --git a/editor/actions/dune b/editor/actions/dune
new file mode 100755
index 0000000..5d269c4
--- /dev/null
+++ b/editor/actions/dune
@@ -0,0 +1,13 @@
+(library
+ (name actions)
+ (libraries
+ brr
+ brr.note
+ elements
+ blog
+ js_lib
+ forms
+ state
+ )
+ (preprocess (pps js_of_ocaml-ppx))
+ )
diff --git a/editor/actions/event.ml b/editor/actions/event.ml
new file mode 100755
index 0000000..5e30587
--- /dev/null
+++ b/editor/actions/event.ml
@@ -0,0 +1,8 @@
+module type Handler = sig
+ type t
+
+ val apply: t -> State.t -> State.t
+
+end
+
+type t = E : 'a * (module Handler with type t = 'a) -> t