package bonsai
A library for building dynamic webapps, using Js_of_ocaml
Install
Dune Dependency
Authors
Maintainers
Sources
v0.15.1.tar.gz
sha256=0c4a714146073f421f1a6179561f836b45d8dc012c743207d3481ea63bef74bf
doc/src/bonsai.web_ui_form/view.ml.html
Source file view.ml
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
open! Core open Bonsai_web module Attr = Vdom.Attr module Node = Vdom.Node module Style = [%css.raw {| .clear_fieldset_styles { border: 0; margin: 0; padding: 0; } |}] module Error_details = struct type t = { error : Error.t ; on_mouse_over : (unit Ui_effect.t[@sexp.opaque]) ; on_mouse_out : (unit Ui_effect.t[@sexp.opaque]) ; on_click : (unit Ui_effect.t[@sexp.opaque]) ; is_viewing : bool ; is_toggled : bool } [@@deriving sexp_of] end module Row = struct type t = { label : (Node.t option[@sexp.opaque]) ; tooltip : (Node.t option[@sexp.opaque]) ; form : (Node.t[@sexp.opaque]) ; id : string ; error : Error_details.t option } [@@deriving sexp_of] end type t = | Empty | Row of Row.t | List of t list | Group of { label : (Node.t option[@sexp.opaque]) ; tooltip : (Node.t option[@sexp.opaque]) ; view : t } | Header_group of { label : (Node.t option[@sexp.opaque]) ; tooltip : (Node.t option[@sexp.opaque]) ; header_view : t ; view : t } [@@deriving sexp_of] let suggest_error e1 = function | Row row -> let error = match row.error with (* Keep the inner error if one exists *) | Some e2 -> Some e2 | None -> Some e1 in Row { row with error } | other -> other ;; let rec set_label label t = match t with | Empty -> Empty | List [] -> List [] (* [set_label] on a list will traverse the list and attach it to the head *) | List (hd :: tl) -> List (set_label label hd :: tl) | Row row -> Row { row with label = Some label } | Group group -> Group { group with label = Some label } | Header_group group -> Header_group { group with label = Some label } | Submit_button _ -> t ;; let rec set_tooltip tooltip t = match t with | Empty -> Empty | List [] -> List [] (* [set_tooltip] on a list will traverse the list and attach it to the head *) | List (hd :: tl) -> List (set_tooltip tooltip hd :: tl) | Row row -> Row { row with tooltip = Some tooltip } | Group group -> Group { group with tooltip = Some tooltip } | Header_group group -> Header_group { group with tooltip = Some tooltip } | Submit_button _ -> t ;; let group_list t = match t with | List _ -> Group { view = t; label = None; tooltip = None } | _ -> t ;; let rec suggest_label label t = match t with | Empty -> Empty | List [] -> List [] (* [suggest_label] on a list will traverse the list and attach it to the head *) | List (hd :: tl) -> List (suggest_label label hd :: tl) (* If it already has a label, keep it *) | Row { label = Some _; _ } | Group { label = Some _; _ } | Header_group { label = Some _; _ } -> t | Row ({ label = None; _ } as row) -> Row { row with label = Some label } | Group ({ label = None; _ } as group) -> Group { group with label = Some label } | Header_group ({ label = None; _ } as group) -> Header_group { group with label = Some label } | Submit_button _ -> t ;; let group label view = Group { label = Some label; tooltip = None; view } let of_vdom ~id form = Row { label = None; tooltip = None; form; id; error = None } let concat a b = match a, b with | Empty, x | x, Empty -> x | List a, List b -> List (a @ b) | a, List b -> List (a :: b) | List a, b -> List (List.append a [ b ]) | a, b -> List [ a; b ] ;; let rec view_error (e : Error.Internal_repr.t) : Node.t list = let bold text = Node.strong [ Node.text text ] in let pre text = Node.pre [ Node.text text ] in let view_sexp sexp = sexp |> Sexp.to_string_hum |> pre in let div contents = Node.div [ contents ] in match e with | Could_not_construct sexp -> [ div (bold "could not construct"); div (view_sexp sexp) ] | String s -> [ div (Node.text s) ] | Exn e -> [ div (pre (Exn.to_string e)) ] | Sexp s -> [ div (view_sexp s) ] | Tag_sexp (string, sexp, Some there) -> [ div (bold string) ; div (view_sexp sexp) ; div (pre (Source_code_position.to_string there)) ] | Tag_sexp (string, sexp, None) -> [ div (bold string); div (view_sexp sexp) ] | Tag_t (string, error) -> div (bold string) :: view_error error | Tag_arg (string, sexp, error) -> div (bold string) :: div (view_sexp sexp) :: view_error error | Of_list (Some truncate_after, errors) -> errors |> Fn.flip List.take truncate_after |> List.bind ~f:view_error | Of_list (None, errors) -> errors |> List.bind ~f:view_error | With_backtrace (error, backtrace) -> List.append (view_error error) [ div (pre backtrace) ] ;; let view_error error = view_error (Error.Internal_repr.of_info error) let view_error_details { Error_details.error; on_mouse_over; on_mouse_out; on_click; is_viewing; is_toggled } = let flag = Node.div ~attr: (Attr.many_without_merge [ Attr.on_mouseover (fun _ -> on_mouse_over) ; Attr.on_mouseout (fun _ -> on_mouse_out) ; Attr.on_click (fun _ -> on_click) ; Attr.style Css_gen.( (if is_toggled then color (`Name "black") else color (`Hex "#f54646")) @> font_size (`Em_float 1.2) @> Css_gen.create ~field:"cursor" ~value:"pointer") ]) [ Node.text "⚠" ] in let contents = if not is_viewing then Node.none else Node.div ~attr: (Attr.style Css_gen.( (if is_toggled then border ~width:(`Px 1) ~color:(`Name "black") ~style:`Solid () else border ~width:(`Px 1) ~color:(`Name "red") ~style:`Solid ()) @> position ~top:(`Px 0) ~left:(`Em 2) `Absolute @> padding ~left:(`Em 1) ~right:(`Em 1) () @> border_radius (`Px 3) @> background_color (`Name "pink"))) (view_error error) in let result = Node.div ~attr: (Attr.many_without_merge [ Attr.style (Css_gen.position `Relative); Attr.class_ "bonsai-forms-error" ]) [ flag; contents ] in result ;; module Tooltip = struct module Css = [%css.raw {| .container { position: relative; display: inline-block; } .label { cursor: pointer; color: blue; } .text { visibility: hidden; width: 300px; background-color: azure; color: black; text-align: center; border-radius: 3px; padding: 0.5em 1em 0.5em 1em; border: 1px solid darkblue; position: absolute; z-index: 1; bottom: 100%; left: 50%; margin-left: -150px; cursor: text; } .container:hover .text { visibility: visible; } .checkbox:checked ~ .text { visibility: visible; } .checkbox:checked ~ .span { color: black; } .checkbox { position: absolute; opacity: 0%; cursor: pointer; } |}] let view inner = Node.div ~attr:(Attr.class_ Css.container) [ Node.label ~attr:(Attr.class_ Css.label) [ Node.input ~attr:Attr.(type_ "checkbox" @ class_ Css.checkbox) [] ; Node.span ~attr:(Attr.class_ Css.span) [ Node.text "ⓘ" ] ; Node.div ~attr:(Attr.class_ Css.text) [ inner ] ] ] ;; end let rec to_vdom ~depth = let depth_td ~extra_attrs = let attr = Attr.(style (Css_gen.padding_left (`Em depth)) @ extra_attrs) in Node.td ~attr in function | Empty -> [] | Group { label; tooltip; view } -> let rest = to_vdom view ~depth:(depth + 1) in let header_is_inhabited = Option.is_some label || Option.is_some tooltip in if header_is_inhabited then ( let label = match label with | Some label -> depth_td ~extra_attrs:Attr.(style (Css_gen.font_weight `Bold) @ colspan 2) [ label ] | None -> Node.None in let tooltip = match tooltip with | Some tooltip -> Node.td [ Tooltip.view tooltip ] | None -> Node.None in Node.tr [ label; tooltip ] :: rest) else rest | Header_group { label; tooltip; view; header_view } -> let rest = to_vdom view ~depth:(depth + 1) in let header_view = let colspan = if Option.is_some label then Attr.empty else Attr.colspan 2 in Node.td ~attr:colspan (to_vdom_plain header_view) in let label = match label with | Some label -> depth_td ~extra_attrs:(Attr.style (Css_gen.font_weight `Bold)) [ label ] | None -> Node.None in let tooltip = match tooltip with | Some tooltip -> Node.td [ Tooltip.view tooltip ] | None -> Node.None in Node.tr [ label; header_view; tooltip ] :: rest | Submit_button _ as btn -> let = to_vdom_plain btn in [ Node.tr [ depth_td ~extra_attrs:Attr.(colspan 2) button ] ] | Row { label; tooltip; id; form; error } -> let label = match label with | Some label -> (* <label> nodes can be clicked on to focus the input element contained inside. By setting display:block, even the whitespace to the right of the label is clickable, meaning that mis-clicking on particularly small labels is less likely. *) Node.label ~attr: (Attr.many_without_merge [ Attr.for_ id; Attr.style (Css_gen.display `Block) ]) [ label ] | _ -> Node.text "" in let tooltip = match tooltip with | Some tooltip -> Tooltip.view tooltip | None -> Node.text "" in let error = match error with | None -> Node.text "" | Some e -> view_error_details e in let label_attrs = Attr.style Css_gen.( padding_right (`Em 1) @> text_align `Left @> font_weight `Bold @> user_select `None) in [ (* This key prevents inputs of different "kinds" from clobbering each other *) Node.tr ~key:id [ depth_td ~extra_attrs:label_attrs [ label ] ; Node.td [ form ] ; Node.td [ Node.div ~attr:(Attr.style (Css_gen.flex_container ~direction:`Row ())) [ tooltip; error ] ] ] ] | List l -> List.concat_map l ~f:(to_vdom ~depth) (* If the form is just a single row, return the view for it without wrapping *) and to_vdom_plain = function | Empty -> [] | Header_group { label = _; tooltip = _; header_view; view } -> to_vdom_plain header_view @ to_vdom_plain view | Group { label = _; tooltip = _; view } -> to_vdom_plain view | Row { label = _; tooltip = _; id = _; form; error = _ } -> [ form ] | List l -> List.concat_map l ~f:to_vdom_plain | Submit_button { on_submit; text } -> (match on_submit with | Some event -> let event = Vdom.Effect.(Many [ event; Prevent_default; Stop_propagation ]) in [ Node.button ~attr:(Attr.on_click (fun _ -> event)) [ Vdom.Node.text text ] ] | None -> [ Node.button ~attr:Attr.disabled [ Vdom.Node.text text ] ]) ;; type submission_options = { on_submit : unit Ui_effect.t option ; handle_enter : bool ; button_text : string option } type editable = [ `Yes_always | `Currently_yes | `Currently_no ] let with_fieldset ~currently_editable view = let disabled_ = if currently_editable then Vdom.Attr.empty else Vdom.Attr.disabled in Vdom.Node.fieldset ~attr:Vdom.Attr.(disabled_ @ class_ Style.clear_fieldset_styles) [ view ] ;; let to_vdom ?on_submit ?(editable = `Yes_always) view = let view = match on_submit with | Some { on_submit; button_text = Some ; handle_enter = _ } -> let = Submit_button { text = button_text; on_submit } in concat view button | _ -> view in let inner_table = Node.table [ Node.tbody (to_vdom view ~depth:0) ] in let inner_table = match editable with | `Yes_always -> inner_table | `Currently_yes -> with_fieldset ~currently_editable:true inner_table | `Currently_no -> with_fieldset ~currently_editable:false inner_table in match on_submit with | Some { on_submit; handle_enter = true; _ } -> let always_use = [ Vdom.Effect.Prevent_default; Vdom.Effect.Stop_propagation ] in let event = match on_submit with | None -> Vdom.Effect.Many always_use | Some event -> Vdom.Effect.Many (event :: always_use) in Node.create "form" ~attr:(Vdom.Attr.on_submit (fun _ -> event)) [ inner_table ] | _ -> inner_table ;;
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>