///| /// A tiny virtual DOM, pure data, generic over the message type `M` the handlers send. /// The app's `view` builds a tree; a platform layer (web/) turns it into real DOM and /// patches it by key. Because the tree is plain data, views are unit-tested by /// rendering them to HTML text (`to_html`). ///| /// What the user can do to an element. Pointer gestures are recognised by the gesture /// package; the platform layer reports them against the chain of elements that have /// handlers. pub(all) struct Handlers[M] { /// Pointer went down (acted on at once, e.g. grabbing a widget). down : M? /// A tap (down and up without a hold or a move). tap : M? /// A long press. press : M? /// Enter pressed in an input. enter : M? /// Escape pressed in an input. escape : M? /// An input's text changed. input : ((String) -> M)? } ///| pub fn[M] Handlers::none() -> Handlers[M] { { down: None, tap: None, press: None, enter: None, escape: None, input: None } } ///| pub fn[M] Handlers::is_empty(self : Handlers[M]) -> Bool { self.down is None && self.tap is None && self.press is None && self.enter is None && self.escape is None && self.input is None } ///| pub(all) enum Node[M] { Text(String) El(Element[M]) } ///| pub(all) struct Element[M] { tag : String /// Identity among siblings for the keyed diff (0: none). key : Int classes : Array[String] attrs : Array[(String, String)] /// DOM properties (not attributes), e.g. an input's `value`. props : Array[(String, String)] style : String handlers : Handlers[M] children : Array[Node[M]] } ///| /// An element builder: `el("div", classes=["a"], children=[...])`. pub fn[M] el( tag : String, key? : Int = 0, classes? : Array[String] = [], attrs? : Array[(String, String)] = [], props? : Array[(String, String)] = [], style? : String = "", handlers? : Handlers[M] = Handlers::none(), children? : Array[Node[M]] = [], ) -> Node[M] { El({ tag, key, classes, attrs, props, style, handlers, children }) } ///| pub fn[M] text(s : String) -> Node[M] { Text(s) } ///| /// `cls("a", on=false)` is the empty class, so callers can build lists with `filter`. pub fn cls_if(name : String, on : Bool) -> String { if on { name } else { "" } } ///| fn escape_html(s : String) -> String { let b = StringBuilder() for c in s { match c { '&' => b.write_string("&") '<' => b.write_string("<") '>' => b.write_string(">") '"' => b.write_string(""") _ => b.write_char(c) } } b.to_string() } ///| fn[M] handler_names(h : Handlers[M]) -> String { let names : Array[String] = [] if h.down is Some(_) { names.push("down") } if h.tap is Some(_) { names.push("tap") } if h.press is Some(_) { names.push("press") } if h.enter is Some(_) { names.push("enter") } if h.escape is Some(_) { names.push("escape") } if h.input is Some(_) { names.push("input") } names.join(" ") } ///| /// Render a node as HTML text, one element per line (children indented), for tests. /// Handlers show as an `on="tap press"` attribute. pub fn[M] to_html(node : Node[M], indent? : Int = 0) -> String { let pad = " ".repeat(indent) match node { Text(s) => pad + escape_html(s) El(e) => { let b = StringBuilder() b.write_string("\{pad}<\{e.tag}") let classes = e.classes.filter(fn(c) { c != "" }) if classes.length() > 0 { b.write_string(" class=\"\{classes.join(" ")}\"") } for kv in e.attrs { b.write_string(" \{kv.0}=\"\{escape_html(kv.1)}\"") } for kv in e.props { b.write_string(" .\{kv.0}=\"\{escape_html(kv.1)}\"") } if e.style != "" { b.write_string(" style=\"\{e.style}\"") } let on = handler_names(e.handlers) if on != "" { b.write_string(" on=\"\{on}\"") } b.write_string(">") if e.children.length() == 1 && e.children[0] is Text(s) { b.write_string("\{escape_html(s)}") } else if e.children.length() == 0 { b.write_string("") } else { for c in e.children { b.write_string("\n") b.write_string(to_html(c, indent=indent + 1)) } b.write_string("\n\{pad}") } b.to_string() } } }