text
stringlengths
12
786k
module Custom = struct type t = { dom : Js_browser . Element . t ; sync : ( Vdom . Custom . t -> bool ) bool ; dispose : ( unit -> unit ) unit ; } type ctx = { parent : Js_browser . Element . t ; send_event : ( Vdom . event -> unit ) unit ; after_redraw : ( ( unit -> unit ) unit -> unit ) unit ; } type handler = ctx -> Vdom . Custom . t -> t option let make ( ? dispose = ignore ) ignore ~ sync dom = { dom ; sync ; dispose } dispose let parent ctx = ctx . parent let send_event ctx = ctx . send_event let after_redraw ctx = ctx . after_redraw let rec find_handler ctx x = function | [ ] -> failwith " Vdom_blit : no custom element handler found " | hd :: tl -> begin match hd ctx x with | Some f -> f | None -> find_handler ctx x tl end let lookup ~ parent ~ process_custom ~ after_redraw elt handlers = let rec dom = lazy ( ( Lazy . force el ) el . dom ) dom and send_event e = process_custom ( Lazy . force dom ) dom e and el = lazy ( find_handler { parent ; send_event ; after_redraw } after_redraw elt handlers ) handlers in Lazy . force el end
let rec scroll_parent node = if node = Element . null then Document . body document else let overflow_y = Style . get ( Window . get_computed_style window node ) node " overflowY " in let is_scrollable = overflow_y <> " visible " && overflow_y <> " hidden " in if is_scrollable && Element . scroll_height node >= Element . client_height node then node else scroll_parent ( Element . parent_node node ) node
let scroll_to_make_visible child = let open Js_browser in let parent = scroll_parent child in let r_parent = Element . get_bounding_client_rect parent in let r_child = Element . get_bounding_client_rect child in let y1 = Rect . bottom r_parent and y2 = Rect . bottom r_child in if y2 > y1 then Element . set_scroll_top parent ( Element . scroll_top parent . + y2 . - y1 ) y1 else let y1 = Rect . top r_parent and y2 = Rect . top r_child in if y2 < y1 then Element . set_scroll_top parent ( Element . scroll_top parent . + y2 . - y1 ) y1
type ' msg ctrl = | BText of { vdom : ' msg vdom ; dom : Element . t } t | BElement of { vdom : ' msg vdom ; dom : Element . t ; children : ' msg ctrl list } list | BMap : { vdom : ' msg vdom ; dom : Element . t ; f : ( ' submsg -> ' msg ) ' msg ; child : ' submsg ctrl } ctrl -> ' msg ctrl | BMemo : { vdom : ' msg vdom ; dom : Element . t ; child : ' msg ctrl } ctrl -> ' msg ctrl | BCustom of { vdom : ' msg vdom ; elt : Custom . t } t
let get_dom = function | BText x -> x . dom | BElement x -> x . dom | BMap x -> x . dom | BMemo x -> x . dom | BCustom x -> x . elt . dom
let get_vdom = function | BText x -> x . vdom | BElement x -> x . vdom | BMap x -> x . vdom | BMemo x -> x . vdom | BCustom x -> x . vdom
let key_of_vdom = function | Text { key ; _ } _ | Element { key ; _ } _ | Map { key ; _ } _ | Memo { key ; _ } _ | Custom { key ; _ } _ -> key
let eval_prop = function | String x -> Ojs . string_to_js x | Int x -> Ojs . int_to_js x | Bool x -> Ojs . bool_to_js x | Float x -> Ojs . float_to_js x
let string_of_prop = function | String s -> s | Int x -> string_of_int x | Bool x -> string_of_bool x | Float x -> string_of_float x
let same_prop v1 v2 = v1 == v2 || match v1 , v2 with | String x1 , String x2 -> x1 = x2 | Int x1 , Int x2 -> x1 = x2 | Bool x1 , Bool x2 -> x1 = x2 | Float x1 , Float x2 -> x1 = x2 | _ -> false
let bmemo vdom child = BMemo { vdom ; dom = get_dom child ; child } child
let async f = ignore ( Window . set_timeout window f 0 ) 0
let custom_attribute dom = function | " scroll - to - show " -> async ( fun ( ) -> try scroll_to_make_visible dom with exn -> Printf . printf " scroll : % s \ n " %! ( Printexc . to_string exn ) exn ) ; true | " autofocus " -> async ( fun ( ) -> Element . focus dom ) dom ; true | " relative - dropdown " -> let style = Element . style dom in Style . set_position ( Element . style dom ) dom " absolute " ; let px = Printf . sprintf " % fpx " in async ( fun ( ) -> match Element . offset_parent dom with | None -> ( ) | Some offset_parent -> let parent = Element . parent_node dom in let rect = Element . get_bounding_client_rect parent in let offset_rect = Element . get_bounding_client_rect offset_parent in let top = Rect . top rect . - Rect . top offset_rect in let left = Rect . left rect . - Rect . left offset_rect in Style . set_top style ( px ( top . + float ( Element . offset_height parent ) parent ) parent ) parent ; Style . set_left style ( px left ) left ; Style . set_width style ( px ( float ( Element . offset_width parent ) parent ) parent ) parent ) ; true | _ -> false
let apply_attributes dom attributes = List . iter ( function | Property ( k , v ) v -> if not ( custom_attribute dom k ) k then Ojs . set ( Element . t_to_js dom ) dom k ( eval_prop v ) v | Style ( k , v ) v -> Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style ) " k ( Ojs . string_to_js v ) v | Attribute ( k , v ) v -> Element . set_attribute dom k v | _ -> ( ) ) attributes
type ctx = { process_custom : ( Element . t -> event -> unit ) unit ; custom_handlers : Custom . handler list ; after_redraw : ( ( unit -> unit ) unit -> unit ) unit ; }
let rec blit : ' msg . parent : _ -> ctx -> ' msg vdom -> ' msg ctrl = fun ~ parent ctx vdom -> match vdom with | Text { txt ; key = _ } _ -> BText { vdom ; dom = Document . create_text_node document txt } txt | Map { f ; child ; key = _ } _ -> let child = blit ~ parent ctx child in BMap { vdom ; dom = get_dom child ; f ; child } child | Memo { f ; arg ; key = _ } _ -> bmemo vdom ( blit ~ parent ctx ( f arg ) arg ) arg | Custom { elt ; attributes ; key = _ } _ -> let elt = try Custom . lookup ~ parent ~ process_custom : ctx . process_custom ~ after_redraw : ctx . after_redraw elt ctx . custom_handlers with exn -> Printf . printf " Error during vdom Custom % s lookup : % s \ n " %! ( Obj . Extension_constructor . name ( Obj . Extension_constructor . of_val elt ) elt ) elt ( Printexc . to_string exn ) exn ; raise exn in apply_attributes elt . dom attributes ; BCustom { vdom ; elt } elt | Element { ns ; tag ; children ; attributes ; key = _ } _ -> if debug then Printf . printf " create <% s >\ n " %! tag ; let dom = if ns = " " then Document . create_element document tag else Document . create_element_ns document ns tag in let children = List . map ( blit ~ parent : dom ctx ) ctx children in List . iter ( fun c -> Element . append_child dom ( get_dom c ) c ) c children ; apply_attributes dom attributes ; BElement { vdom ; dom ; children } children
let blit ctx vdom = try blit ctx vdom with exn -> Printf . printf " Error during vdom blit : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn
let sync_props to_string same set clear l1 l2 = let sort = List . sort ( fun ( k1 , _ ) _ ( k2 , _ ) _ -> compare ( k1 : string ) string k2 ) k2 in let l1 = sort l1 and l2 = sort l2 in let rec loop l1 l2 = match l1 , l2 with | [ ] , [ ] -> ( ) | ( k1 , v1 ) v1 :: tl1 , ( k2 , _ ) _ :: _ when k1 < k2 -> if debug then Printf . printf " Property % s unset % s =>\ n " %! k1 ( to_string v1 ) v1 ; clear k1 ; loop tl1 l2 | ( k1 , v1 ) v1 :: tl1 , [ ] -> if debug then Printf . printf " Property % s unset % s =>\ n " %! k1 ( to_string v1 ) v1 ; clear k1 ; loop tl1 [ ] | ( k1 , _ ) _ :: _ , ( k2 , v2 ) v2 :: tl2 when k2 < k1 -> if debug then Printf . printf " Property % s set => % s \ n " %! k2 ( to_string v2 ) v2 ; set k2 v2 ; loop l1 tl2 | [ ] , ( k2 , v2 ) v2 :: tl2 -> if debug then Printf . printf " Property % s set => % s \ n " %! k2 ( to_string v2 ) v2 ; set k2 v2 ; loop [ ] tl2 | ( _k1 , v1 ) v1 :: tl1 , ( k2 , v2 ) v2 :: tl2 -> if not ( same v1 v2 ) v2 then begin if debug then Printf . printf " Property % s changed % s => % s \ n " %! k2 ( to_string v1 ) v1 ( to_string v2 ) v2 ; set k2 v2 ; end ; loop tl1 tl2 in loop l1 l2
let rec choose f = function | [ ] -> [ ] | hd :: tl -> match f hd with | None -> choose f tl | Some x -> x :: choose f tl
let js_empty_string = Ojs . string_to_js " "
let sync_attributes dom a1 a2 = let props = function Property ( k , v ) v -> Some ( k , v ) v | Style _ | Handler _ | Attribute _ -> None in let set k v = match k , v with | " value " , String s when s = Element . value dom -> ( ) | _ -> if not ( custom_attribute dom k ) k then Ojs . set ( Element . t_to_js dom ) dom k ( eval_prop v ) v in let clear k = Ojs . set ( Element . t_to_js dom ) dom k Ojs . null in sync_props string_of_prop same_prop set clear ( choose props a1 ) a1 ( choose props a2 ) a2 ; let styles = function Style ( k , v ) v -> Some ( k , String v ) v | Property _ | Handler _ | Attribute _ -> None in let set k v = Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style ) " k ( eval_prop v ) v in let clear k = Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style ) " k js_empty_string in sync_props string_of_prop same_prop set clear ( choose styles a1 ) a1 ( choose styles a2 ) a2 ; let attrs = function Attribute ( k , v ) v -> Some ( k , v ) v | Style _ | Property _ | Handler _ -> None in let set k v = Element . set_attribute dom k v in let clear k = Element . remove_attribute dom k in sync_props Fun . id ( fun ( s1 : string ) string s2 -> s1 = s2 ) s2 set clear ( choose attrs a1 ) a1 ( choose attrs a2 ) a2
let rec dispose : type msg . msg ctrl -> unit = fun ctrl -> match ctrl with | BText _ -> ( ) | BCustom { elt ; _ } _ -> elt . dispose ( ) | BElement { children ; _ } _ -> List . iter dispose children | BMap { child ; _ } _ -> dispose child | BMemo { child ; _ } _ -> dispose child
let rec sync : type old_msg msg . ctx -> Element . t -> old_msg ctrl -> msg vdom -> msg ctrl = fun ctx parent old vdom -> match old , vdom with | _ when ( vdom : msg vdom ) vdom == ( Obj . magic ( get_vdom old : old_msg vdom ) vdom ) vdom -> ( Obj . magic ( old : old_msg ctrl ) ctrl : msg ctrl ) ctrl | BText { vdom = Text { txt = s1 ; key = _ } _ ; dom } dom , Text { txt = s2 ; key = _ } _ -> if s1 <> s2 then Element . set_node_value dom s2 ; BText { vdom ; dom } dom | BMap { child = c1 ; _ } _ , Map { f ; child = c2 ; key = _ } _ -> let child = sync ctx parent c1 c2 in BMap { vdom ; dom = get_dom child ; child ; f } f | BMemo { child = c1 ; vdom = Memo { f = f1 ; arg = a1 ; key = _ } _ ; _ } _ , Memo { f = f2 ; arg = a2 ; key = _ } _ -> if Obj . magic f1 == f2 && Obj . magic a1 == a2 then bmemo vdom ( Obj . magic ( c1 : old_msg ctrl ) ctrl : msg ctrl ) ctrl else bmemo vdom ( sync ctx parent c1 ( f2 a2 ) a2 ) a2 | BCustom { vdom = Custom { key = key1 ; elt = arg1 ; attributes = a1 } a1 ; elt } elt , Custom { key = key2 ; elt = arg2 ; attributes = a2 } a2 when key1 = key2 && ( arg1 == arg2 || elt . sync arg2 ) arg2 -> sync_attributes elt . dom a1 a2 ; BCustom { vdom ; elt } elt | BElement { vdom = Element e1 ; dom ; children } children , Element e2 when e1 . tag = e2 . tag && e1 . ns = e2 . ns && e1 . key = e2 . key -> let old_children = Array . of_list children in let new_children = Array . of_list e2 . children in let by_key = Hashtbl . create 8 in for i = Array . length old_children - 1 downto 0 do let k = key_of_vdom ( get_vdom old_children ( . i ) i ) i in Hashtbl . add by_key k i done ; let indices = Array . make ( Array . length new_children ) new_children ( - 1 ) 1 in for i = 0 to Array . length indices - 1 do let k = key_of_vdom new_children ( . i ) i in match Hashtbl . find by_key k with | exception Not_found -> ( ) | idx -> indices ( . i ) i <- idx ; Hashtbl . remove by_key k ; done ; Hashtbl . iter ( fun _ i -> if debug then Printf . printf " remove % i \ n " %! i ; let to_remove = old_children ( . i ) i in Element . remove_child dom ( get_dom to_remove ) to_remove ; dispose to_remove ) by_key ; let ctrls = ref [ ] in let next = ref ( Element . t_of_js Ojs . null ) null in for i = Array . length new_children - 1 downto 0 do let idx = indices ( . i ) i in if debug then Printf . printf " old = % i ; new = % i : " idx i ; let c = if idx < 0 then begin if debug then Printf . printf " create \ n " ; %! blit ~ parent ctx new_children ( . i ) i end else begin if debug then Printf . printf " sync & move \ n " ; %! sync ctx dom old_children ( . idx ) idx new_children ( . i ) i end in let move = idx < 0 || ( ( if i = Array . length new_children - 1 then idx <> Array . length old_children - 1 else indices ( . i + 1 ) 1 <> idx + 1 ) 1 && Element . next_sibling ( get_dom c ) c != ! next ) next in if move then begin if debug then Printf . printf " really move \ n " ; %! Element . insert_before dom ( get_dom c ) c ! next ; end ; next := get_dom c ; ctrls := c :: ! ctrls done ; let children = ! ctrls in sync_attributes dom e1 . attributes e2 . attributes ; BElement { vdom ; dom ; children } children | _ -> let x = blit ~ parent ctx vdom in Element . replace_child parent ( get_dom x ) x ( get_dom old ) old ; dispose old ; x
let sync ctx parent old vdom = try sync ctx parent old vdom with exn -> Printf . printf " Error during vdom sync : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn
type ' msg find = | NotFound | Found : { mapper : ( ' inner_msg -> ' msg ) ' msg ; inner : ' inner_msg ctrl ; parent : ' msg find } find -> ' msg find
let rec found : type inner_msg msg . ( inner_msg -> msg ) msg -> msg find -> inner_msg ctrl -> msg find = fun mapper parent -> function | BElement _ | BText _ | BCustom _ as inner -> Found { mapper ; inner ; parent } parent | BMap { f ; child ; _ } _ -> found ( fun x -> mapper ( f x ) x ) x parent child | BMemo { child ; _ } _ -> found mapper parent child
let rec vdom_of_dom : type msg . msg ctrl -> Element . t -> msg find = fun root dom -> match Ojs . option_of_js Element . t_of_js ( Element . t_to_js dom ) dom with | None -> NotFound | Some dom when dom == get_dom root -> found Fun . id NotFound root | Some dom -> begin match vdom_of_dom root ( Element . parent_node dom ) dom with | NotFound -> NotFound | Found { mapper ; inner = BElement { children ; _ } _ ; _ } _ as parent -> begin match List . find ( fun c -> get_dom c == dom ) dom children with | exception Not_found -> assert false | c -> found mapper parent c end | Found { mapper = _ ; inner = BCustom _ ; _ } _ -> NotFound | _ -> assert false end
let mouse_event e = { x = Event . client_x e ; y = Event . client_y e ; page_x = Event . page_x e ; page_y = Event . page_y e ; buttons = Event . buttons e ; alt_key = Event . alt_key e ; ctrl_key = Event . ctrl_key e ; shift_key = Event . shift_key e ; }
let key_event e = { which = Event . which e ; alt_key = Event . alt_key e ; ctrl_key = Event . ctrl_key e ; shift_key = Event . shift_key e ; }
type ( ' model , ' msg ) ' msg app = { dom : Js_browser . Element . t ; process : ( ' msg -> unit ) unit ; get : ( unit -> ' model ) ' model ; after_redraw : ( unit -> unit ) unit -> unit ; dispose : ( unit -> unit ) unit ; }
let dom x = x . dom
let process x = x . process
let get x = x . get ( )
let after_redraw x = x . after_redraw
type env = { cmds : Cmd . handler list ; customs : Custom . handler list ; }
let empty = { cmds = [ ] ; customs = [ ] }
let cmd h = { empty with cmds = [ h ] h } h
let custom h = { empty with customs = [ h ] h } h
let merge envs = { cmds = List . concat ( List . map ( fun e -> e . cmds ) cmds envs ) envs ; customs = List . concat ( List . map ( fun e -> e . customs ) customs envs ) envs ; }
let global = ref empty
let register e = global := merge [ e ; ! global ] global
let run ( type msg model ) model ( ? env = empty ) empty ? container ( { init = ( model0 , cmd0 ) cmd0 ; update ; view } view : ( model , msg ) msg Vdom . app ) app = let env = merge [ env ; ! global ] global in let container_created , container = match container with | None -> true , Document . create_element document " div " | Some container -> false , container in let post_redraw = ref [ ] in let after_redraw f = post_redraw := f :: ! post_redraw in let flush _ = let l = List . rev ! post_redraw in post_redraw := [ ] ; List . iter ( fun f -> f ( ) ) l in let process_custom_fwd = ref ( fun _ _ -> assert false ) false in let ctx = { process_custom = ( fun elt evt -> ! process_custom_fwd elt evt ) evt ; custom_handlers = env . customs ; after_redraw ; } in let view model = try view model with exn -> Printf . printf " Error during vdom view : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn in let x = blit ~ parent : container ctx ( view model0 ) model0 in Window . request_animation_frame window flush ; let model = ref model0 in let current = ref ( Some x ) x in let pending_redraw = ref false in let redraw _ = match ! current with | None -> ( ) | Some root -> pending_redraw := false ; let x = sync ctx container root ( view ! model ) model in current := Some x ; flush ( ) in let rec process msg = try let ( new_model : model ) model , ( cmd : msg Vdom . Cmd . t ) t = update ! model msg in model := new_model ; Cmd . run env . cmds process cmd ; if not ! pending_redraw then begin pending_redraw := true ; Window . request_animation_frame window redraw end with exn -> Printf . printf " Error during vdom process : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn in Element . append_child container ( get_dom x ) x ; let prev_value_attribute = " data - ocaml - vdom - prev - value " in let onevent evt = let ty = Event . type_ evt in try let tgt = Element . t_of_js ( Event . target evt ) evt in let rec apply_handler = function | [ ] -> None | hd :: tl -> let res = match ty , hd with | " input " , Handler ( Input f ) f -> Some ( f ( Element . value tgt ) tgt ) tgt | " blur " , Handler ( Change f ) f -> let curr_value = Element . value tgt in let changed = not ( Element . has_attribute tgt prev_value_attribute ) prev_value_attribute || Element . get_attribute tgt prev_value_attribute <> curr_value in if changed then Some ( f curr_value ) curr_value else None | " change " , Handler ( ChangeIndex f ) f -> Some ( f ( Element . selected_index tgt ) tgt ) tgt | " click " , Handler ( ChangeChecked f ) f -> Some ( f ( Element . checked tgt ) tgt ) tgt | " click " , Handler ( Click f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " dblclick " , Handler ( DblClick f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " blur " , Handler ( Blur msg ) msg -> Some msg | " focus " , Handler ( Focus msg ) msg -> Some msg | " mousemove " , Handler ( MouseMove f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " mousedown " , Handler ( MouseDown f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " keydown " , Handler ( KeyDown f ) f -> Some ( f ( key_event evt ) evt ) evt | " keydown " , Handler ( KeyDownCancel f ) f -> begin match f ( key_event evt ) evt with | None -> None | Some _ as r -> Event . prevent_default evt ; r end | " contextmenu " , Handler ( ContextMenu f ) f -> Event . prevent_default evt ; Some ( f ( mouse_event evt ) evt ) evt | _ -> None in match res with | Some _ -> res | None -> apply_handler tl in let rec propagate = function | Found { mapper ; inner = ( BElement { vdom = Element { attributes ; _ } _ ; _ } _ | BCustom { vdom = Custom { attributes ; _ } _ ; _ } _ ) ; parent ; } -> if ty = " focus " && List . exists ( function Handler ( Change _ ) _ -> true | _ -> false ) false attributes then Element . set_attribute tgt prev_value_attribute ( Element . value tgt ) tgt ; begin match apply_handler attributes with | None -> propagate parent | Some msg -> process ( mapper msg ) msg end | _ -> ( ) in Option . iter ( fun root -> propagate ( vdom_of_dom root tgt ) tgt ; ) ! current ; if ty = " input " || ty = " blur " then let f ( ) = Option . iter ( fun root -> match vdom_of_dom root tgt with | Found { mapper = _ ; inner = BElement { vdom = Element { attributes ; _ } _ ; _ } _ ; _ } _ -> List . iter ( function | Property ( " value " , String s2 ) s2 when s2 <> Element . value tgt -> Element . set_value tgt s2 | Property ( " checked " , Bool s2 ) s2 -> Element . set_checked tgt s2 | _ -> ( ) ) attributes | _ -> ( ) ) ! current in if ! pending_redraw then after_redraw f else f ( ) with exn -> Printf . printf " Error in event handler % S : % s \ n " %! ty ( Printexc . to_string exn ) exn in let process_custom tgt event = Option . iter ( fun root -> begin match vdom_of_dom root tgt with | Found { mapper ; inner = BCustom { vdom = Custom { attributes ; _ } _ ; _ } _ ; _ } _ -> let select_handler = function | Handler h -> event . ev h | _ -> None in let msgs = List . filter_map select_handler attributes in List . iter ( fun msg -> process ( mapper msg ) msg ) msg msgs | _ -> ( ) end ) ! current in process_custom_fwd := process_custom ; let listeners = List . map ( fun ( event , capture ) capture -> Element . add_cancellable_event_listener container event onevent capture ) [ Event . Click , false ; Event . Dblclick , false ; Event . Input , false ; Event . Change , false ; Event . Focus , true ; Event . Blur , true ; Event . Mousemove , true ; Event . Mousedown , true ; Event . Keydown , true ; Event . Contextmenu , true ; ] in Cmd . run env . cmds process cmd0 ; let dispose ( ) = Option . iter ( fun root -> current := None ; dispose root ; List . iter ( fun f -> f ( ) ) listeners ; if container_created then Element . remove container else Element . set_inner_HTML container " " ) ! current in { dom = container ; process ; get = ( fun ( ) -> ! model ) model ; after_redraw ; dispose } dispose
let dispose { dispose ; _ } _ = dispose ( )
let hello_world : Vdom . Node . t = Vdom . Node . text " hello world " !
let ( ) = Util . run_vdom hello_world ~ id " : hello_world "
let bulleted_list : Vdom . Node . t = let open Vdom . Node in div [ h3 [ text " Norwegian Pancakes " ] ; ul [ li [ text " 3 eggs " ] ; li [ text " 2 cups of milk " ] ; li [ text " 1 cup of flour " ] ] ] ; ;
let ( ) = Util . run_vdom bulleted_list ~ id " : bulleted_list "
let alert s = Js_of_ocaml . Dom_html . window ## alert ( Js_of_ocaml . Js . string s )
let input_placeholder : Vdom . Node . t = Vdom . Node . input ~ attr ( : Vdom . Attr . placeholder " placeholder text here " ) [ ] ; ;
let ( ) = Util . run_vdom input_placeholder ~ id " : input_placeholder "
let css_gen : Vdom . Node . t = Vdom . Node . span ~ attr ( : Vdom . Attr . style ( Css_gen . color ( ` Name " red " ) ) ) [ Vdom . Node . text " this text is red " ] ; ;
let ( ) = Util . run_vdom css_gen ~ id " : css_gen "
type mouse_event = Js_of_ocaml . Dom_html . mouseEvent Js_of_ocaml . Js . t
let clicky : Vdom . Node . t = Vdom . Node . button ~ attr : ( Vdom . Attr . on_click ( fun ( _evt : mouse_event ) -> alert " hello there " ; ! Ui_effect . Ignore ) ) [ Vdom . Node . text " click me " ! ] ; ;
let ( ) = Util . run_vdom clicky ~ id " : clicky_button "
let contains ~ pattern s = let rec loop i = if i < 0 then false else String . sub s i ( String . length pattern ) pattern = pattern || loop ( i - 1 ) 1 in loop ( String . length s - String . length pattern ) pattern
module SelectionList = struct open Vdom type ' a model = { list : ' a list ; cursor : int ; filter : string ; } type msg = [ ` Cursor of int | ` Filter of string | ` Nop ] Nop let view show msg select { list ; cursor ; filter } filter = let keep = contains ~ pattern : filter in let instrs = List . filter ( fun x -> keep ( show x ) x ) x list in let l = List . mapi ( fun i x -> let a = if i = cursor then [ style " background - color " " # E0E0E0 " ; scroll_to_show ] scroll_to_show else [ ] in div ~ a ( : onclick ( fun _ -> select x ) x :: class_ " link " :: a ) a [ text ( show x ) x ] ) instrs in div [ elt " input " [ ] ~ a : [ type_ " search " ; str_prop " placeholder " " Search . . . " ; class_ " searchbox " ; value filter ; autofocus ; oninput ( fun s -> msg ( ` Filter s ) s ) s ; onkeydown ( fun e -> match e . which with | 38 -> msg ( ` Cursor ( max ( cursor - 1 ) 1 0 ) 0 ) 0 | 40 -> msg ( ` Cursor ( min ( cursor + 1 ) 1 ( List . length instrs - 1 ) 1 ) 1 ) 1 | 33 -> msg ( ` Cursor ( max ( cursor - 15 ) 15 0 ) 0 ) 0 | 34 -> msg ( ` Cursor ( min ( cursor + 15 ) 15 ( List . length instrs - 1 ) 1 ) 1 ) 1 | 36 -> msg ( ` Cursor 0 ) 0 | 35 -> msg ( ` Cursor ( List . length instrs - 1 ) 1 ) 1 | 13 -> select ( List . nth instrs cursor ) cursor | _ -> msg ` Nop ) ] ; div ~ a [ : style " width " " 300px " ; style " margin - top " " 30px " ; style " overflow - y " " scroll " ; style " height " " 500px " ; ] l ] let update model = function | ` Filter filter -> { model with filter ; cursor = 0 } 0 | ` Cursor cursor -> { model with cursor } cursor | ` Nop -> model let init list = { list ; filter = " " ; cursor = 0 } 0 end
module Initializable = struct type ' a model = | Initializing | Ready of ' a type ( ' a , ' msg ) ' msg msg = [ ` Got of ' a | ` Internal of ' msg ] ' msg let internal msg = ` Internal msg let got x = ` Got x let update f model msg = match model , msg with | Initializing , ` Got x -> Vdom . return ( Ready x ) x | Ready model , ` Internal msg -> let m , a = f model msg in Vdom . return ( Ready m ) m ~ c [ : Vdom . Cmd . map internal a ] a | _ -> Vdom . return model let view f = function | Initializing -> Vdom . text " Please wait . . . " | Ready model -> Vdom . map internal ( f model ) model let init f = Vdom . return Initializing ~ c [ : Vdom . Cmd . map got f ] f let app ~ init : i ~ view : v ~ update : u ( ) = Vdom . app ~ init ( : init i ) i ~ update ( : update u ) u ~ view ( : view v ) v ( ) end
let [ @ inline ] min ( x : int ) y = if x < y then x else y ' a array -> int -> ' a array -> int -> int -> unit = " caml_array_blit "
module Make ( Resize : Vec_gen . ResizeType ) = struct type elt = Resize . t let null = Resize . null
let unsafe_blit = Bs_hash_stubs . int_unsafe_blit # endif
type t = { mutable arr : elt array ; mutable len : int ; }
let length d = d . len
let compact d = let d_arr = d . arr in if d . len <> Array . length d_arr then begin let newarr = unsafe_sub d_arr 0 d . len in d . arr <- newarr end
let singleton v = { len = 1 ; arr = [ | v ] | }
let empty ( ) = { len = 0 ; arr = [ ] ; || }
let is_empty d = d . len = 0
let reset d = d . len <- 0 ; d . arr <- [ ] ||
let to_list d = let rec loop ( d_arr : elt array ) idx accum = if idx < 0 then accum else loop d_arr ( idx - 1 ) ( Array . unsafe_get d_arr idx :: accum ) in loop d . arr ( d . len - 1 ) [ ]
let of_list lst = let arr = Array . of_list lst in { arr ; len = Array . length arr }
let to_array d = unsafe_sub d . arr 0 d . len
let of_array src = { len = Array . length src ; arr = Array . copy src ; }
let of_sub_array arr off len = { len = len ; arr = Array . sub arr off len }
let unsafe_internal_array v = v . arr
let copy src = let len = src . len in { len ; arr = unsafe_sub src . arr 0 len ; }
let reverse_in_place src = Ext_array . reverse_range src . arr 0 src . len
let sub ( src : t ) start len = let src_len = src . len in if len < 0 || start > src_len - len then invalid_arg " Vec . sub " else { len ; arr = unsafe_sub src . arr start len }
let iter d f = let arr = d . arr in for i = 0 to d . len - 1 do f ( Array . unsafe_get arr i ) done
let iteri d f = let arr = d . arr in for i = 0 to d . len - 1 do f i ( Array . unsafe_get arr i ) done
let iter_range d ~ from ~ to_ f = if from < 0 || to_ >= d . len then invalid_arg " Vec . iter_range " else let d_arr = d . arr in for i = from to to_ do f ( Array . unsafe_get d_arr i ) done
let iteri_range d ~ from ~ to_ f = if from < 0 || to_ >= d . len then invalid_arg " Vec . iteri_range " else let d_arr = d . arr in for i = from to to_ do f i ( Array . unsafe_get d_arr i ) done
let map_into_array f src = let src_len = src . len in let src_arr = src . arr in if src_len = 0 then [ ] || else let first_one = f ( Array . unsafe_get src_arr 0 ) in let arr = Array . make src_len first_one in for i = 1 to src_len - 1 do Array . unsafe_set arr i ( f ( Array . unsafe_get src_arr i ) ) done ; arr
let map_into_list f src = let src_len = src . len in let src_arr = src . arr in if src_len = 0 then [ ] else let acc = ref [ ] in for i = src_len - 1 downto 0 do acc := f ( Array . unsafe_get src_arr i ) :: ! acc done ; ! acc
let mapi f src = let len = src . len in if len = 0 then { len ; arr = [ | ] | } else let src_arr = src . arr in let arr = Array . make len ( Array . unsafe_get src_arr 0 ) in for i = 1 to len - 1 do Array . unsafe_set arr i ( f i ( Array . unsafe_get src_arr i ) ) done ; { len ; arr ; }
let fold_left f x a = let rec loop a_len ( a_arr : elt array ) idx x = if idx >= a_len then x else loop a_len a_arr ( idx + 1 ) ( f x ( Array . unsafe_get a_arr idx ) ) in loop a . len a . arr 0 x
let fold_right f a x = let rec loop ( a_arr : elt array ) idx x = if idx < 0 then x else loop a_arr ( idx - 1 ) ( f ( Array . unsafe_get a_arr idx ) x ) in loop a . arr ( a . len - 1 ) x
let filter f d = let new_d = copy d in let new_d_arr = new_d . arr in let d_arr = d . arr in let p = ref 0 in for i = 0 to d . len - 1 do let x = Array . unsafe_get d_arr i in if f x then begin Array . unsafe_set new_d_arr ! p x ; incr p ; end ; done ; new_d . len <- ! p ; new_d
let equal eq x y : bool = if x . len <> y . len then false else let rec aux x_arr y_arr i = if i < 0 then true else if eq ( Array . unsafe_get x_arr i ) ( Array . unsafe_get y_arr i ) then aux x_arr y_arr ( i - 1 ) else false in aux x . arr y . arr ( x . len - 1 )
let get d i = if i < 0 || i >= d . len then invalid_arg " Vec . get " else Array . unsafe_get d . arr i
let unsafe_get d i = Array . unsafe_get d . arr i
let last d = if d . len <= 0 then invalid_arg " Vec . last " else Array . unsafe_get d . arr ( d . len - 1 )
let capacity d = Array . length d . arr
let exists p d = let a = d . arr in let n = d . len in let rec loop i = if i = n then false else if p ( Array . unsafe_get a i ) then true else loop ( succ i ) in loop 0
let map f src = let src_len = src . len in if src_len = 0 then { len = 0 ; arr = [ ] } || ] } ) * else let src_arr = src . arr in let first = f ( Array . unsafe_get src_arr 0 ) in let arr = Array . make src_len first in for i = 1 to src_len - 1 do Array . unsafe_set arr i ( f ( Array . unsafe_get src_arr i ) ) done ; { len = src_len ; arr = arr ; }
let init len f = if len < 0 then invalid_arg " Vec . init " else if len = 0 then { len = 0 ; arr = [ ] || } else let first = f 0 in let arr = Array . make len first in for i = 1 to len - 1 do Array . unsafe_set arr i ( f i ) done ; { len ; arr } let make initsize : t = if initsize < 0 then invalid_arg " Vec . make " ; { len = 0 ; arr = Array . make initsize null ; } let reserve ( d : t ) s = let d_len = d . len in let d_arr = d . arr in if s < d_len || s < Array . length d_arr then ( ) else let new_capacity = min Sys . max_array_length s in let new_d_arr = Array . make new_capacity null in unsafe_blit d_arr 0 new_d_arr 0 d_len ; d . arr <- new_d_arr let push ( d : t ) v = let d_len = d . len in let d_arr = d . arr in let d_arr_len = Array . length d_arr in if d_arr_len = 0 then begin d . len <- 1 ; d . arr <- [ | v ] | end else begin if d_len = d_arr_len then begin if d_len >= Sys . max_array_length then failwith " exceeds max_array_length " ; let new_capacity = min Sys . max_array_length d_len * 2 in let new_d_arr = Array . make new_capacity null in d . arr <- new_d_arr ; unsafe_blit d_arr 0 new_d_arr 0 d_len ; end ; d . len <- d_len + 1 ; Array . unsafe_set d . arr d_len v end let delete ( d : t ) idx = let d_len = d . len in if idx < 0 || idx >= d_len then invalid_arg " Vec . delete " ; let arr = d . arr in unsafe_blit arr ( idx + 1 ) arr idx ( d_len - idx - 1 ) ; let idx = d_len - 1 in d . len <- idx ; Array . unsafe_set arr idx null let pop ( d : t ) = let idx = d . len - 1 in if idx < 0 then invalid_arg " Vec . pop " ; d . len <- idx ; Array . unsafe_set d . arr idx null # endif let get_last_and_pop ( d : t ) = let idx = d . len - 1 in if idx < 0 then invalid_arg " Vec . get_last_and_pop " ; let last = Array . unsafe_get d . arr idx in d . len <- idx ; Array . unsafe_set d . arr idx null # endif ; last let delete_range ( d : t ) idx len = let d_len = d . len in if len < 0 || idx < 0 || idx + len > d_len then invalid_arg " Vec . delete_range " ; let arr = d . arr in unsafe_blit arr ( idx + len ) arr idx ( d_len - idx - len ) ; d . len <- d_len - len ; for i = d_len - len to d_len - 1 do Array . unsafe_set arr i null done let get_and_delete_range ( d : t ) idx len : t = let d_len = d . len in if len < 0 || idx < 0 || idx + len > d_len then invalid_arg " Vec . get_and_delete_range " ; let arr = d . arr in let value = unsafe_sub arr idx len in unsafe_blit arr ( idx + len ) arr idx ( d_len - idx - len ) ; d . len <- d_len - len ; for i = d_len - len to d_len - 1 do Array . unsafe_set arr i null done ; { len = len ; arr = value } let clear ( d : t ) = for i = 0 to d . len - 1 do Array . unsafe_set d . arr i null done ; d . len <- 0 let inplace_filter f ( d : t ) : unit = let d_arr = d . arr in let d_len = d . len in let p = ref 0 in for i = 0 to d_len - 1 do let x = Array . unsafe_get d_arr i in if f x then begin let curr_p = ! p in ( if curr_p <> i then Array . unsafe_set d_arr curr_p x ) ; incr p end done ; let last = ! p in d . len <- last delete_range d last ( d_len - last ) let inplace_filter_from start f ( d : t ) : unit = if start < 0 then invalid_arg " Vec . inplace_filter_from " ; let d_arr = d . arr in let d_len = d . len in let p = ref start in for i = start to d_len - 1 do let x = Array . unsafe_get d_arr i in if f x then begin let curr_p = ! p in ( if curr_p <> i then Array . unsafe_set d_arr curr_p x ) ; incr p end done ; let last = ! p in d . len <- last delete_range d last ( d_len - last ) let inplace_filter_with f ~ cb_no acc ( d : t ) = let d_arr = d . arr in let p = ref 0 in let d_len = d . len in let acc = ref acc in for i = 0 to d_len - 1 do let x = Array . unsafe_get d_arr i in if f x then begin let curr_p = ! p in ( if curr_p <> i then Array . unsafe_set d_arr curr_p x ) ; incr p end else acc := cb_no x ! acc done ; let last = ! p in d . len <- last delete_range d last ( d_len - last ) ; ! acc end
type ( ' a , ' - p ) t = { mutable growth_rate : float ; mutable length : int ; mutable data : ' a array }
let make ? growth_rate ( : gr = default_growth_rate ) ? capacity ( : c = 0 ) ( ) = if gr <= 1 . then raise ( Invalid_argument " growth_rate <= 1 " ) else if c < 0 then raise ( Invalid_argument " capacity < 0 " ) else { growth_rate = gr ; length = 0 ; data = array_uninit c }
let as_read_only v = ( v :> ( ' a , [ ` R ] ) t )
let as_write_only v = ( v :> ( ' a , [ ` W ] ) t )
let set_growth_rate gr v = if gr <= 1 . then raise ( Invalid_argument " growth_rate <= 1 " ) else v . growth_rate <- gr