From fe2cced55e1b44dbae57e55fe0f459c85e7369cb Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 7 Feb 2022 16:21:26 +0100 Subject: Application unification --- editor/actions/actions.ml | 47 +++++++++++++++++++++++++------------------ editor/actions/add_page.ml | 44 +++++++++++++++++++++++++++++++++------- editor/actions/delete_page.ml | 37 +++++++++++++++++++++++----------- editor/actions/event.ml | 1 + editor/actions/load_page.ml | 12 +++++++++++ 5 files changed, 102 insertions(+), 39 deletions(-) create mode 100755 editor/actions/load_page.ml (limited to 'editor/actions') diff --git a/editor/actions/actions.ml b/editor/actions/actions.ml index e8b4d71..b150279 100755 --- a/editor/actions/actions.ml +++ b/editor/actions/actions.ml @@ -7,16 +7,17 @@ module Event = Event type button_actions = { delete : Event.t Note.event - ; redirect : Jstr.t option Note.event + ; redirect : Event.t Note.event ; add: Event.t Note.event } let populate_menu - : Forms.Events.event option Note.E.send -> button_actions option - = fun sender -> + : unit -> button_actions option + = fun () -> match Blog.Sidebar.get () with | None -> None | Some element -> + let () = Blog.Sidebar.clean element in let delete_button = El.button @@ -39,24 +40,27 @@ let populate_menu ~at:At.[ class' (Jstr.v "fa") ; class' (Jstr.v "fa-2x") ; class' (Jstr.v "fa-plus") ] ] - in + (* We are waiting for event inside another event ( form validation inside + popup creation. + + Note.E.join is used here in order to get only te popup validation. *) 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 + Note.E.join ( + Evr.on_el + Ev.click + (fun _ -> Delete_page.create ()) + delete_button) + (* Event on popup creation *) 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 + Note.E.join ( + Evr.on_el + Ev.click + (fun _ -> Add_page.create ()) + add_button) in + let stored_pages = State.Storage.get_ids () in let pages = @@ -73,8 +77,7 @@ let populate_menu El.li [ El.a ~at:[At.href target] - [ El.txt name ] ] - ) in + [ El.txt name ] ]) in (* Wait for a click on an existing page in order to sent the associated event. @@ -82,17 +85,21 @@ let populate_menu We compose the resulting event with both : - the home button - the list for all the pages presents in the sidebar *) + + let redirect_handler = + (module Load_page.M : Event.Handler with type t = Load_page.M.t ) in + let redirect_event = Note.E.select (( Evr.on_el Ev.click - (fun _ -> None) + (fun _ -> Event.E (None, redirect_handler)) home_button ) :: ( List.map2 stored_pages pages ~f:(fun name el -> Evr.on_el Ev.click - (fun _ -> Some name) + (fun _ -> Event.E ((Some name), redirect_handler)) el ))) in let childs = diff --git a/editor/actions/add_page.ml b/editor/actions/add_page.ml index b817573..3ada726 100755 --- a/editor/actions/add_page.ml +++ b/editor/actions/add_page.ml @@ -1,11 +1,41 @@ -type t = Forms.Events.event option Note.E.send +module Js = Js_of_ocaml.Js -let apply - : t -> State.t -> State.t - = fun close_sender state -> +module M = struct + + type t = Forms.Add_page.t + + let key_of_title + : Jstr.t -> Jstr.t + = fun title -> + title + + let apply + : t -> State.t -> State.t + = fun {title} state -> + let page_id = key_of_title title in + let new_date = (new%js Js.date_now)##getTime in + let content_obj = object%js + val content = Js.null + val title = Js.some title + val date = Js.some new_date + end in + State.load_page (Some page_id) state content_obj +end + +(** Create a new element *) +let create + : unit -> Event.t Note.event + = fun () -> let title = Jstr.v "Nouvelle page" in - let popup = Forms.Ui.popup + let ev = Forms.Ui.popup ~title ~form:(Some (Forms.Add_page.create ())) - close_sender in - { state with window = popup::state.window} + in + Note.E.map + (fun v -> Event.E + (v + , (module M : Event.Handler with type t = M.t ))) + (* Option.on_some trigger the event only when the pop up is validated. + Closing the popup doesn't do anything. + *) + (Note.E.Option.on_some ev) diff --git a/editor/actions/delete_page.ml b/editor/actions/delete_page.ml index cc15693..5c625bd 100755 --- a/editor/actions/delete_page.ml +++ b/editor/actions/delete_page.ml @@ -1,16 +1,29 @@ -type t = Forms.Events.event option Note.E.send +module M = struct -let apply - : t -> State.t -> State.t - = fun close_sender state -> - begin match state.page_id with + type t = unit + + let apply + : t -> State.t -> State.t + = fun () state -> + 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 + State.Storage.delete (fun () -> Some page_id); + let json = State.Storage.load None in + State.load_page None state json + +end +let create + : unit -> Event.t Note.event + = fun () -> + let title = Jstr.v "Confirmation" in + let ev = Forms.Ui.popup + ~title + ~form:(Some (Forms.Delete_page.create () )) + in + Note.E.map + (fun v -> Event.E + ( v + , (module M : Event.Handler with type t = M.t ))) + (Note.E.Option.on_some ev) diff --git a/editor/actions/event.ml b/editor/actions/event.ml index 5e30587..21e3d3a 100755 --- a/editor/actions/event.ml +++ b/editor/actions/event.ml @@ -1,4 +1,5 @@ module type Handler = sig + type t val apply: t -> State.t -> State.t diff --git a/editor/actions/load_page.ml b/editor/actions/load_page.ml new file mode 100755 index 0000000..eb4afac --- /dev/null +++ b/editor/actions/load_page.ml @@ -0,0 +1,12 @@ +module M = struct + + type t = Jstr.t option + + let apply + : t -> State.t -> State.t + = fun page_id state -> + let json = State.Storage.load page_id in + State.load_page page_id state json + +end + -- cgit v1.2.3