package alba
Alba compiler
Install
Dune Dependency
Authors
Maintainers
Sources
0.4.4.tar.gz
sha256=4817038301d3e45bac9edf7e6f2fc8bf0a6d78e76e02ad7ea33ef69bcc17df3b
md5=25234357587126685d64f16236167937
doc/src/alba.fmlib_js/browser.ml.html
Source file browser.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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
open Js_of_ocaml open Fmlib open Common (* Javascript interface *) type js_string = Js.js_string type js_any = Js.Unsafe.top let raise_js (message: string): 'a = Js.( raise_js_error (new%js error_constr (Js.string message))) class type primitive_number = object method valueOf_float: unit -> float Js.meth end class type event = object method stopPropagation: unit Js.meth method preventDefault: unit Js.meth end class type style = object method setProperty: js_string Js.t -> js_string Js.t -> unit Js.meth method removeProperty: js_string Js.t -> unit Js.meth end class type eventTarget = object method addEventListener: js_string Js.t -> ('a Js.t -> unit) Js.callback -> unit Js.meth (* Do not use on text nodes! *) method removeEventListener: js_string Js.t -> ('a Js.t -> unit) Js.callback -> unit Js.meth (* Do not use on text nodes! *) end class type node = object inherit eventTarget method nodeType: int Js.readonly_prop method parentNode: node Js.t Js.Opt.t Js.readonly_prop (* The following methods can be used only on elements *) method innerHTML: js_string Js.t Js.prop method firstChild: node Js.t Js.Opt.t Js.readonly_prop method lastChild: node Js.t Js.Opt.t Js.readonly_prop method nextSibling: node Js.t Js.Opt.t Js.readonly_prop method style: style Js.t Js.readonly_prop method setAttribute: js_string Js.t -> js_string Js.t -> unit Js.meth method removeAttribute: js_string Js.t -> unit Js.meth method appendChild: node Js.t -> unit Js.meth method removeChild: node Js.t -> unit Js.meth method replaceChild: node Js.t -> node Js.t -> unit Js.meth (* [parent##replaceChild newChild oldChild] *) method focus: unit -> unit Js.meth method tagName: js_string Js.t Js.readonly_prop end class type document = object inherit eventTarget method body: node Js.t Js.readonly_prop method _URL: js_string Js.t Js.readonly_prop method title: js_string Js.t Js.prop method createElement: js_string Js.t -> node Js.t Js.meth method createTextNode: js_string Js.t -> node Js.t Js.meth method getElementById: js_string Js.t -> node Js.t Js.Opt.t Js.meth method hasFocus: unit -> bool Js.t Js.meth method activeElement: node Js.t Js.Opt.t Js.readonly_prop end class type history = object method pushState: 'a Js.t Js.Opt.t -> js_string Js.t -> js_string Js.t -> unit Js.meth (* [pushState state title path] [title] is currently not used, [path] must not change origin. *) end class type window = object inherit eventTarget method history: history Js.t Js.readonly_prop method document: document Js.t Js.readonly_prop method requestAnimationFrame: (float -> unit) Js.callback -> unit Js.meth end class type xmlHttpRequest = object inherit eventTarget method open_: js_string Js.t -> js_string Js.t -> unit Js.meth method send: js_string Js.t -> unit Js.meth method readyState: int Js.readonly_prop (* 0: request not initialized 1: open has been called 2: send has been called 3: loading 4: complete *) method status: int Js.readonly_prop (* 200: Ok, 403: forbidden, 404: not found. *) method statusText: js_string Js.t Js.readonly_prop method responseText: js_string Js.t Js.readonly_prop end let get_time (): float = Js.Unsafe.global##.Date##now () let get_window (): window Js.t = Js.Unsafe.global let new_http_request (): xmlHttpRequest Js.t = let request = Js.Unsafe.global##._XMLHttpRequest in new%js request module Decoder = struct type path_element = | Name of string | Index of int type error = | Int | Bool | String | Field of string type problem = error * path_element list module Result = Monad.Result (struct type t = problem end) type 'a t = Common.Void.t Js.t -> 'a option let return (a: 'a): 'a t = fun _ -> Some a let typeof (js: 'a Js.t): string = Js.to_string (Js.typeof js) let float: float t = fun js -> if typeof js = "number" then Some ( (Js.Unsafe.coerce js)##valueOf_float () ) else None let int: int t = fun js -> Option.( float js >>= fun fl -> if Float.is_integer fl then Some (Float.to_int fl) else None ) let string: string t = fun js -> if Js.to_string (Js.typeof js) = "string" then Some ( Js.to_string (Js.Unsafe.coerce js) ) else None let bool: bool t = fun js -> if Js.to_string (Js.typeof js) = "boolean" then Some ( Js.to_bool (Js.Unsafe.coerce js) ) else None let field (name: string) (decode: 'a t): 'a t = fun obj -> Option.( Js.Optdef.to_option (Js.Unsafe.get obj (Js.string name)) >>= decode ) let map (f: 'a -> 'b) (decode: 'a t): 'b t = fun obj -> Option.( map f (decode obj) ) let (>>=) (d: 'a t) (f: 'a -> 'b t): 'b t = fun obj -> Option.( d obj >>= fun a -> f a obj ) end module Encoder = struct type t = Common.Void.t Js.t let string (s: string): t = Js.(Unsafe.coerce (string s)) let bool (b: bool): t = Js.(Unsafe.coerce (bool b)) let object_ (_: (string * t) list): t = assert false (* nyi *) end module Make (App: Web_application.WEB_APPLICATION with type 'a decoder = 'a Decoder.t and type encoder = Encoder.t) = struct module Vdom = App.Dom module Command = App.Command module Subscription = App.Subscription type handler = (Void.t Js.t -> unit) Js.callback module Tree = struct module Attributes = struct type 'msg t = { mutable styles: string String_map.t; mutable attributes: string String_map.t; mutable properties: Encoder.t String_map.t; mutable handlers: handler String_map.t; } let make (make_handler: 'msg Decoder.t -> handler) (attributes: 'msg App.Attribute.t list) : 'msg t = let module VA = App.Attribute in List.fold_left (fun attributes attr -> match attr with | VA.Style (name, value) -> {attributes with styles = String_map.add name value attributes.styles} | VA.Attribute (name, value) -> {attributes with attributes = String_map.add name value attributes.attributes} | VA.Property (name, value) -> {attributes with properties = String_map.add name value attributes.properties} | VA.On (name, decode) -> {attributes with handlers = String_map.add name (make_handler decode) attributes.handlers} ) { styles = String_map.empty; attributes = String_map.empty; properties = String_map.empty; handlers = String_map.empty; } attributes let set_style (node: node Js.t) (name: string) (value: string) : unit = node##.style##setProperty (Js.string name) (Js.string value) let remove_style (node: node Js.t) (name: string) (_: string) : unit = node##.style##removeProperty (Js.string name) let set_property (node: node Js.t) (name: string) (value: Encoder.t) : unit = Js.Unsafe.set node (Js.string name) value let remove_property (node: node Js.t) (name: string) (_: Encoder.t) : unit = Js.Unsafe.delete node (Js.string name) let set_attribute (node: node Js.t) (name: string) (value: string) : unit = node##setAttribute (Js.string name) (Js.string value) let remove_attribute (node: node Js.t) (name: string) (_: string) : unit = node##removeAttribute (Js.string name) let add_handler (node: node Js.t) (name: string) (value: handler) : unit = node##addEventListener (Js.string name) value let remove_handler (node: node Js.t) (name: string) (value: handler) : unit = node##removeEventListener (Js.string name) value let update_style (node: node Js.t) (name: string) (new_value: string) (value: string) = if new_value <> value then set_style node name new_value let update_property (node: node Js.t) (name: string) (new_value: Encoder.t) (_: Encoder.t) = set_property node name new_value let update_attribute (node: node Js.t) (name: string) (new_value: string) (value: string) = if new_value <> value then set_attribute node name new_value let update_handler (node: node Js.t) (name: string) (new_value: handler) (value: handler) = remove_handler node name value; add_handler node name new_value let add (attributes: 'msg t) (node: node Js.t): unit = (* add [attributes] to [node] *) String_map.fold (fun name value _ -> set_style node name value) attributes.styles (); String_map.fold (fun name value _ -> set_property node name value) attributes.properties (); String_map.fold (fun name value _ -> set_attribute node name value) attributes.attributes (); String_map.fold (fun name value _ -> add_handler node name value) attributes.handlers () let update (new_attributes: 'msg t) (attributes: 'msg t) (node: node Js.t) : unit = let rec update news olds add upd rem = match news, olds with | [], [] -> () | (name_new, value_new) :: news1, (name, value) :: olds1 -> let cmp = String.compare name_new name in if cmp = -1 then ( add name_new value_new; update news1 olds add upd rem ) else if cmp = 0 then ( upd name value_new value; update news1 olds1 add upd rem ) else ( rem name value; update news olds1 add upd rem ) | [], _ -> List.iter (fun (name, value) -> rem name value) olds | _, [] -> List.iter (fun (name, value) -> add name value) news in update (String_map.bindings new_attributes.styles) (String_map.bindings attributes.styles) (set_style node) (update_style node) (remove_style node); update (String_map.bindings new_attributes.properties) (String_map.bindings attributes.properties) (set_property node) (update_property node) (remove_property node); update (String_map.bindings new_attributes.attributes) (String_map.bindings attributes.attributes) (set_attribute node) (update_attribute node) (remove_attribute node); update (String_map.bindings new_attributes.handlers) (String_map.bindings attributes.handlers) (add_handler node) (update_handler node) (remove_handler node) end (* Attributes *) (* Start Tree main part *) type 'msg t = { node: node Js.t; data: 'msg t0 } and 'msg t0 = | Text of string | Node of string * 'msg Attributes.t ref * 'msg t list ref let node (tree: 'msg t): node Js.t = tree.node let data (tree: 'msg t): 'msg t0 = tree.data let make_text (text: string) (node: node Js.t): 'msg t = { node; data = Text text } let fill_node (tag: string) (nd: node Js.t) (attributes: 'msg Attributes.t) (children: 'msg t list) : 'msg t = ignore ( List.fold_left (fun () child -> nd##appendChild (node child)) () children ); Attributes.add attributes nd; { node = nd; data = Node (tag, ref attributes, ref children) } let rec update (vdom: 'msg Vdom.t) (tree: 'msg t) (make_tree: 'msg Vdom.t -> 'msg t) (make_handler: 'msg Decoder.t -> handler) : 'msg t option = let update vdom tree = update vdom tree make_tree make_handler in match vdom, (data tree) with | Vdom.Text text, Text old_text when text = old_text -> None | Vdom.Node (tag, attributes, children), Node (old_tag, old_attributes, old_children) when tag = old_tag -> (* update attributes *) ( let open Attributes in let attributes = make make_handler attributes in update attributes !old_attributes (node tree); old_attributes := attributes ); old_children := ( let rec update_cs parent children old_children = match children, old_children with | [], [] -> [] | [], _ -> (* vdom has no more children, but actual dom still has. Remove the remaining children in the actual dom. *) let parent = node tree in List.iter (fun child -> parent##removeChild (node child)) old_children; [] | _, [] -> (* vdom has more children than the actual dom. *) let children_rev = let parent = node tree in List.fold_left (fun cs child -> let child = make_tree child in parent##appendChild (node child); child :: cs) [] children in List.rev children_rev | child :: children, old_child :: old_children -> ( match update child old_child with | None -> old_child :: update_cs parent children old_children | Some child -> (node parent)##replaceChild (node child) (node old_child); child :: update_cs parent children old_children ) in update_cs tree children !old_children ); None | _, _ -> Some (make_tree vdom) end (* Tree *) type ('model,'msg) t = { window: window Js.t; root: node Js.t; view: 'model -> 'msg Vdom.t; update: 'msg -> 'model -> 'model * 'msg Command.t; subscription: 'model -> 'msg Subscription.t; mutable model: 'model; mutable dirty: bool; mutable tree: 'msg Tree.t option; } let make (window: window Js.t) (root: node Js.t) (model: 'model) (view: 'model -> 'msg Vdom.t) (update: 'msg -> 'model -> 'model * 'msg Command.t) (subscription: 'model -> 'msg Subscription.t) : ('model,'msg) t = { window; root; view; update; subscription; model; dirty = true; tree = None; } let view (state: ('model, 'msg) t): 'msg Vdom.t = state.view state.model let rec update (message: 'msg) (state: ('model, 'msg) t): unit = let model, command = state.update message state.model in state.dirty <- true; state.model <- model; do_command state command and do_command (state: ('model,'msg) t) (command: 'msg Command.t): unit = let rec perform command = match command with | Command.None -> () | Command.Batch lst -> List.iter perform lst | Command.Http (type_, url, data, handler) -> Printf.printf "http request <%s> <%s>\n" type_ url; let request = new_http_request () in request##open_ (Js.string type_) (Js.string url); request##addEventListener (Js.string "readystatechange") (Js.wrap_callback (fun _ -> if request##.readyState = 4 then ( let status = request##.status in let result = if 200 <= status && status < 300 then Ok (Js.to_string request##.responseText) else Error status in update (handler result) state ) ) ); request##send (Js.string data) in perform command let make_event_handler (state: ('model, 'msg) t) (decode: 'msg Decoder.t) : ('a Js.t -> unit) Js.callback = Js.wrap_callback (fun event -> match decode event with | None -> Printf.printf "cannot decode event\n" | Some message -> update message state ) let make_tree (state: ('model, 'msg) t) (vdom: 'msg Vdom.t) : 'msg Tree.t = let open Vdom in let doc = state.window##.document in let rec make vdom = match vdom with | Text str -> Tree.make_text str (doc##createTextNode (Js.string str)) | Node (tag, attributes, children) -> let node = doc##createElement (Js.string tag) and attributes = Tree.Attributes.make (make_event_handler state) attributes in Tree.fill_node tag node attributes (List.map make children) in make vdom let remove_children (node: node Js.t): unit = let rec remove () = match Js.Opt.to_option node##.firstChild with | None -> () | Some child -> node##removeChild child; remove () in remove () let update_dom (state: ('model, 'msg) t): unit = match state.tree with | None -> (* No tree available. Build the tree and attach it to the root node. *) let tree = make_tree state (view state) and root = state.root in remove_children root; root##appendChild (Tree.node tree); state.tree <- Some tree; state.dirty <- false | Some tree -> (* Tree available. Update the tree or build a new one. *) match Tree.update (view state) tree (make_tree state) (make_event_handler state) with | None -> (* Tree has been updated. *) state.dirty <- false | Some new_tree -> (* Tree has been built newly. *) state.root##replaceChild (Tree.node new_tree) (Tree.node tree); state.tree <- Some new_tree; state.dirty <- false let rec animate (state: ('model, 'msg) t) (_: 'a): unit = if state.dirty then update_dom state else (); state.window##requestAnimationFrame (Js.wrap_callback (animate state)) let sandbox (model: 'model) (view: 'model -> 'msg Vdom.t) (update: 'msg -> 'model -> 'model) : unit = let window = get_window () in window##addEventListener (Js.string "load") (Js.wrap_callback (fun _ -> let state = make window window##.document##.body model view (fun msg model -> update msg model, Command.None) (fun _ -> Subscription.None) in state.window##requestAnimationFrame (Js.wrap_callback (animate state)) ) ) let element (decode: 'a Decoder.t) (init: 'a -> 'model * 'msg App.Command.t) (view: 'model -> 'msg Vdom.t) (update: 'msg -> 'model -> 'model * 'msg App.Command.t) (subscription: 'model -> 'msg App.Subscription.t) : unit = let decode_init: (string*'a) Decoder.t = Decoder.( field "node" string >>= fun name -> map (fun data -> name,data) (field "data" decode) ) in Js.export "Alba" (object%js method init (js: Void.t Js.t): unit = match decode_init js with | None -> raise_js "Cannot decode init" | Some (root_name, (data: 'a)) -> let window = get_window () in let root = match Js.Opt.to_option (window##.document##getElementById (Js.string root_name)) with | None -> raise_js ("Cannot find node " ^ root_name) | Some root -> root in let model, command = init data in let state = make window root model view update subscription in state.window##requestAnimationFrame (Js.wrap_callback (animate state)); do_command state command end); () end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>