aboutsummaryrefslogtreecommitdiff
path: root/editor/actions.ml
blob: f7633e11bb2c50dcd9a5aa5570309d57eac248f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
open StdLabels
open Js_of_ocaml
open Brr
open Brr_note

type button_actions =
  { delete : unit Note.event
  ; redirect : Jstr.t option Note.event
  ; add: unit Note.event
  }

let populate_menu () =
  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
        Evr.unit
        delete_button
    and add_event =
      Evr.on_el
        Ev.click
        Evr.unit
        add_button in

    let stored_pages = Storage.get_ids () in
    let pages =
      List.map
        stored_pages
        ~f:(fun id ->

            let name_opt = (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
      }