text
stringlengths
0
601k
type ' msg t = | CommentNode of string | Text of string | Node of string * string * string * string * ' msg properties * ' msg t list | LazyGen of string * ( unit -> ' msg t ) * ' msg t ref | Tagger of ( ' msg applicationCallbacks ref -> ' msg applicationCallbacks ref ) * ' msg t
let noNode = CommentNode " "
let comment s = CommentNode s
let text s = Text s
let fullnode namespace tagName key unique props vdoms = Node ( namespace , tagName , key , unique , props , vdoms )
let node ( ? namespace " " ) = tagName ( ? key " " ) = ( ? unique " " ) = props vdoms = fullnode namespace tagName key unique props vdoms
let lazyGen key fn = LazyGen ( key , fn , ref noNode )
let prop key value = RawProp ( key , value )
let onCB name key cb = Event ( name , EventHandlerCallback ( key , cb ) , ref None )
let onMsg name msg = Event ( name , EventHandlerMsg msg , ref None )
let attribute namespace key value = Attribute ( namespace , key , value )
let data key value = Data ( key , value )
let style key value = Style [ ( key , value ) ]
let styles s = Style s
let rec renderToHtmlString = function | CommentNode s -> " <!-- " ^ s ^ " " --> | Text s -> s | Node ( namespace , tagName , _key , _unique , props , vdoms ) -> let renderProp = function | NoProp -> " " | RawProp ( k , v ) -> String . concat " " [ " " ; k ; " " " ; =\ v ; " " " ] \ | Attribute ( _namespace , k , v ) ->...
let emptyEventHandler : Web . Node . event_cb = fun [ @ bs ] _ev -> ( )
let emptyEventCB = fun _ev -> None
let eventHandler callbacks cb : Web . Node . event_cb = fun [ @ bs ] ev -> match ! cb ev with | None -> ( ) | Some msg -> ! callbacks . enqueue msg
let eventHandler_GetCB = function | EventHandlerCallback ( _ , cb ) -> cb | EventHandlerMsg msg -> fun _ev -> Some msg
let compareEventHandlerTypes left = function | EventHandlerCallback ( cb , _ ) -> ( match left with | EventHandlerCallback ( lcb , _ ) when cb = lcb -> true | _ -> false ) | EventHandlerMsg msg -> ( match left with | EventHandlerMsg lmsg when msg = lmsg -> true | _ -> false )
let eventHandler_Register callbacks elem name handlerType = let cb = ref ( eventHandler_GetCB handlerType ) in let handler = eventHandler callbacks cb in let ( ) = Web . Node . addEventListener elem name handler false in Some { handler ; cb }
let eventHandler_Unregister elem name = function | None -> None | Some cache -> let ( ) = Web . Node . removeEventListener elem name cache . handler false in None
let eventHandler_Mutate callbacks elem ( oldName : string ) ( newName : string ) oldHandlerType newHandlerType oldCache newCache = match ! oldCache with | None -> newCache := eventHandler_Register callbacks elem newName newHandlerType | Some oldcache -> if oldName = newName then let ( ) = newCache := ! oldCache in if c...
let patchVNodesOnElems_PropertiesApply_Add callbacks elem _idx = function | NoProp -> ( ) | RawProp ( k , v ) -> Web . Node . setProp elem k v | Attribute ( namespace , k , v ) -> Web . Node . setAttributeNsOptional elem namespace k v | Data ( k , v ) -> Js . log ( " TODO : Add Data Unhandled " , k , v ) ; failwith " T...
let patchVNodesOnElems_PropertiesApply_Remove _callbacks elem _idx = function | NoProp -> ( ) | RawProp ( k , _v ) -> Web . Node . setProp elem k Js . Undefined . empty | Attribute ( namespace , k , _v ) -> Web . Node . removeAttributeNsOptional elem namespace k | Data ( k , v ) -> Js . log ( " TODO : Remove Data Unhan...
let patchVNodesOnElems_PropertiesApply_RemoveAdd callbacks elem idx oldProp newProp = let ( ) = patchVNodesOnElems_PropertiesApply_Remove callbacks elem idx oldProp in let ( ) = patchVNodesOnElems_PropertiesApply_Add callbacks elem idx newProp in ( )
let patchVNodesOnElems_PropertiesApply_Mutate _callbacks elem _idx oldProp = function | NoProp as _newProp -> failwith " This should never be called as all entries through NoProp are gated . " | RawProp ( k , v ) as _newProp -> Web . Node . setProp elem k v | Attribute ( namespace , k , v ) as _newProp -> Web . Node . ...
let rec patchVNodesOnElems_PropertiesApply callbacks elem idx oldProperties newProperties = match [ @ ocaml . warning " - 4 " ] oldProperties , newProperties with | [ ] , [ ] -> true | [ ] , _newProp :: _newRest -> false | _oldProp :: _oldRest , [ ] -> false | NoProp :: oldRest , NoProp :: newRest -> patchVNodesOnElems...
let patchVNodesOnElems_Properties callbacks elem oldProperties newProperties = patchVNodesOnElems_PropertiesApply callbacks elem 0 oldProperties newProperties
let genEmptyProps length = let rec aux lst = function | 0 -> lst | len -> aux ( noProp :: lst ) ( len - 1 ) in aux [ ] length
let mapEmptyProps props = List . map ( fun _ -> noProp ) props
let rec patchVNodesOnElems_ReplaceNode callbacks elem elems idx = function [ @ ocaml . warning " - 4 " ] | ( Node ( newNamespace , newTagName , _newKey , _newUnique , newProperties , newChildren ) ) -> let oldChild = elems . ( idx ) in let newChild = Web . Document . createElementNsOptional newNamespace newTagName in l...
let patchVNodesIntoElement callbacks elem oldVNodes newVNodes = let elems = Web . Node . childNodes elem in let ( ) = patchVNodesOnElems callbacks elem elems 0 oldVNodes newVNodes in newVNodes
let patchVNodeIntoElement callbacks elem oldVNode newVNode = patchVNodesIntoElement callbacks elem [ oldVNode ] [ newVNode ]
let wrapCallbacks func callbacks = ref { enqueue = ( fun msg -> ! callbacks . enqueue ( func msg ) ) }
let map : ( ' a -> ' b ) -> ' a t -> ' b t = fun func vdom -> let tagger callbacks = ref { enqueue = ( fun msg -> ! callbacks . enqueue ( func msg ) ) } in Tagger ( Obj . magic tagger , Obj . magic vdom )
module Cmd = struct type ' msg ctx = { send_msg : ( ' msg -> unit ) unit ; } let send_msg ctx = ctx . send_msg type handler = { f : ' msg . ' msg ctx -> ' msg Vdom . Cmd . t -> bool } bool let rec run : type t . handler list -> ( t -> unit ) unit -> t Cmd . t -> unit = fun h p -> function | Cmd . Echo msg -> p msg | Cm...
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 ->...
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...
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 p...
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...
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 = ...
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 ...
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 ; ...
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 Pr...
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...
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 = T...
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...
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 d...
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...
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 -> kee...
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...
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