/*----------------------------------------------------------------------------*/ /* */ /* Copyright (c) 2026 Rexx Language Association. All rights reserved. */ /* */ /* This program and the accompanying materials are made available under */ /* the terms of the Common Public License v1.0 which accompanies this */ /* distribution. A copy is also available at the following address: */ /* https://www.oorexx.org/license.html */ /* */ /*----------------------------------------------------------------------------*/ /* * dom.cls: ADDRESS DOM, the page (HTML) from classic Rexx (ooRexx/WASM in the * browser). Written in Rexx on top of the interpreter's .CommandHandler (a * command environment written in Rexx) and js.cls (the page's DOM as .JSObjects); commands are * split by commandwords.cls, as FETCH's. * * ::requires "dom.cls" * address dom * NEW "Hello" -- a panel, where the rest goes * FIELD name '"Your name"' -- in quotes: it has a blank * BUTTON ok '"Say hello"' OK -- a click is the event OK * CREATE answer p * WAIT INTO 'ev' -- until something happens * name VALUE INTO 'who' -- the field's value * answer TEXT FROM msg -- msg: a variable * * Commands: words separated by blanks, case does not matter; a word in * quotes ("..." or '...', the quote doubled inside) may hold blanks. Where * a value is expected, FROM name takes it from the variable name. A value a * command gives goes to the variable named by INTO name, anywhere after the * command's name, as the most natural ooRexx value (a string, a .JSObject for * an element, a .DomEvent); without INTO it is written to WITH OUTPUT, or to * the console. (The words INTO and FROM are keywords only unquoted.) * * Where: everything happens inside the current ROOT (selectors only find * what is in it). The page that runs ooRexx chooses the first one (run * option dom: {root: "#app"}; the page's body by default; none at all in the * playground); NEW makes a panel (a small floating window, in a Shadow DOM: * the page's styles do not reach it, its own do not leak) and makes it the * root; ROOT PAGE goes back to the whole page. * * Which element: like XEDIT, there is a CURRENT ELEMENT, and commands act on * it. LOCATE moves it, UP and TOP too; NEW and ROOT put it at the root; * CREATE, FIELD, BUTTON, TABLE and LIST add to it without moving it (so a * panel is filled without going up and down). For a one-off target, the * target goes first; it does not move the current element: * * out TEXT "hola" an element's nickname (CREATE gives them) * "#total TEXT 42" or a CSS selector * '"#form input" VALUE ""' in quotes if it has blanks * * A target alone is a LOCATE, as in XEDIT: out alone is LOCATE out. The * first word is a target when it is in quotes or is not a command, so a * nickname cannot be a command's name (CREATE text p is refused) and a * selector that is one goes in quotes ('"table" CLASS wide'). A nickname is * looked up first, then a selector. * * Writing the commands: Rexx needs no quotes around them, but it evaluates * whatever is not quoted. So quote the names of variables (after INTO and * FROM: WAIT INTO 'ev', as from the second time on ev has a value), a word * that is also a variable you do not mean (a nickname, or a command: with a * variable text, TEXT is its value), what Rexx would upper-case (values, * CSS selectors and classes; FROM avoids the question for values), and words * with characters Rexx would compute (font-size, "-"). Or quote it all. * * NEW [title] [AS nickname] [INTO el] a panel (a nickname for ROOT) * ROOT PAGE | ref the root: the whole page, or an * element (a panel by its nickname) * LOCATE ref [INTO el] the current element (RC 2: none) * ref [INTO el] the same: a target alone * UP, TOP to the parent (RC 2 at the root); * to the root * CLOSE the panel of the element goes away * CREATE nickname tag [text] [INTO el] a new element, the target's last * child ("-": no nickname) * REMOVE, CLEAR the element; its children * TEXT [value] [INTO var] the text: set, or get * VALUE [value] [INTO var] a field's value (a checkbox: 1/0) * ATTR name [value] [INTO var] an attribute * STYLE property [value] [INTO var] a CSS property (CSS names: * background-color); get: as shown * CLASS [+a] [-b] [c] [INTO var] add (+ or nothing), remove (-) * classes; get: all of them * SHOW, HIDE, ENABLE, DISABLE, FOCUS * FIELD nickname [label] [type] a labelled input; type TEXT (the * default), PASSWORD, NUMBER, EMAIL, * DATE, CHECKBOX or TEXTAREA * BUTTON nickname text [NAME] a button; with NAME, its clicks * are the event NAME (ON click NAME) * TABLE [HEADER] [FROM var] a table (on a table element: * its rows replaced) from an Array * of Arrays, or of lines with tabs * between columns, or a stem, or * the WITH INPUT lines; HEADER: the * first row is the header * LIST [FROM var] a list (ul), the same way * CSS [rules] CSS rules for the root's panel * (or the page): a value, or the * WITH INPUT lines; the panel's own * look comes first, so a CLASS can * be given a look * ON event NAME [LATEST] [PREVENT] the element's event (click, input, * [STOP] change, submit, keydown, ...; ENTER: * Enter in a field) is the event NAME * for WAIT; LATEST keeps only the * newest waiting (mousemove), PREVENT * and STOP: preventDefault and * stopPropagation (a submit always * prevents) * OFF event no more * WAIT [seconds] [INTO ev] the next event, a .DomEvent: name, * target (the element's nickname, in * upper case, or .nil), value, key, * x, y, element * and jsEvent (.JSObjects); a * panel's close button is the event * CLOSE. RC 1 when time is over * * In the panels, a built-in style makes headings, paragraphs, fields, * buttons, tables and lists look right with no CSS at all, light or dark * as the system; class "row" puts what is in an element side by side. * * Events are not callbacks: as with XEDIT's READ or ISPF's DISPLAY, the * program builds, WAITs, looks at the event and acts. WAIT takes the * events of .js~handler too, and sends them their message (as .js~runEventLoop * would) while it waits for its own, so both kinds of handlers coexist. * * RC: 0 when all went well. Positive, and the ERROR condition, when the * command was right but the outcome is not the expected one: 2 LOCATE found * nothing, UP at the root. (WAIT's 1, time over, raises nothing.) * Negative, and the FAILURE condition, when the program is wrong: -1 no page * (the page did not grant it: js: {page: true}, or not in a browser); -2 no * root or no current element (NEW, or ROOT PAGE, first); -3 an unknown * command or wrong arguments; -4 a target or reference that names no element; * -5 a JavaScript error. The reason goes to WITH ERROR or the console. * * In the browser only: the commands work on the page's DOM through the * page realm, which the page that runs ooRexx must grant (js: {page: true}). * Panels are made with a small JavaScript function (resource panel.js * below), built with the page's Function constructor. */ .CommandHandler~register("DOM", .DomHandler~new) ::requires "commandwords.cls" ::requires "js.cls" ::class DomHandler public subclass CommandHandler ::method init expose ready doc root current nicks bindings pending panels makePanel ready = .false root = .nil; current = .nil nicks = .directory~new -- NICKNAME -> element bindings = .array~new -- .DomBinding for ON pending = .queue~new -- events of ours dispatched by someone else panels = 0 makePanel = .nil -- the page, once: .false (and a message) if there is none ::method setup private expose ready doc root current use strict arg context if ready then return .true do doc = .js~page~document catch syntax .CommandIO~error(context, "DOM: there is no page (the page must grant it: js: {page: true})") return .false end ready = .true which = value("ORX_DOM_ROOT", , "ENVIRONMENT") if which == "none" then return .true if which \== "" then root = doc~querySelector(which) if root == .nil then root = doc~body current = root return .true ::method command expose root current use strict arg command, context if \self~setup(context) then return context~raiseFailure(-1) words = .CommandWords~new(command) if words~error \== .nil then return self~bad(context, words~error) if words~items = 0 then return self~bad(context, "a command is needed") -- a target first: a word in quotes, or one that is not a command target = .nil if words~quoted(1) | \self~isCommand(words[1]) then do if root == .nil then return self~noRoot(context) ref = words~take target = self~find(ref, context) if target == "" then return context~raiseFailure(-3) -- a bad selector if target == .nil then do -- a target alone is a LOCATE: not found is RC 2, as LOCATE's if words~items = 0 | (words~items = 2 & words~isKeyword(1, "INTO")) then return context~raiseError(2) .CommandIO~error(context, "DOM: not a command, and no element:" ref) return context~raiseFailure(-4) end end if target \== .nil then do -- INTO may follow a target alone into = words~takeInto verb = words~take end else do verb = words~take into = words~takeInto end if verb == .nil then do -- a target alone: LOCATE it current = target if into \== .nil then return self~give(context, into, target) return 0 end if \self~isCommand(verb) then return self~bad(context, "unknown command" verb) verb = verb~upper if target \== .nil, "NEW ROOT LOCATE UP TOP WAIT CSS"~caselessWordPos(verb) > 0 then return self~bad(context, verb "takes no target") do select case verb when "NEW" then return self~new(words, into, context) when "ROOT" then return self~setRoot(words, into, context) when "LOCATE" then return self~locate(words, into, context) when "TOP" then return self~top(words, into, context) when "WAIT" then return self~wait(words, into, context) when "CSS" then return self~css(words, into, context) otherwise nop end if target == .nil then target = current if target == .nil then return self~noRoot(context) select case verb when "UP" then return self~up(words, into, context) when "CLOSE" then return self~close(target, words, into, context) when "CREATE" then return self~create(target, words, into, context) when "REMOVE" then return self~remove(target, words, into, context) when "CLEAR" then return self~clear(target, words, into, context) when "TEXT" then return self~property(target, "textContent", words, into, context) when "VALUE" then return self~value(target, words, into, context) when "ATTR" then return self~attr(target, words, into, context) when "STYLE" then return self~style(target, words, into, context) when "CLASS" then return self~classes(target, words, into, context) when "SHOW" then return self~flag(target, "hidden", .false, words, into, context) when "HIDE" then return self~flag(target, "hidden", .true, words, into, context) when "ENABLE" then return self~flag(target, "disabled", .false, words, into, context) when "DISABLE" then return self~flag(target, "disabled", .true, words, into, context) when "FOCUS" then return self~focus(target, words, into, context) when "FIELD" then return self~field(target, words, into, context) when "BUTTON" then return self~button(target, words, into, context) when "TABLE" then return self~table(target, words, into, context) when "LIST" then return self~list(target, words, into, context) when "ON" then return self~on(target, words, into, context) when "OFF" then return self~off(target, words, into, context) otherwise return self~bad(context, "unknown command" verb) end catch e = syntax reason = e~message parse var reason "JavaScript error: " js if js \== "" then reason = js .CommandIO~error(context, "DOM" verb":" reason) return context~raiseFailure(-5) end -- ---- helpers --------------------------------------------------------------- -- the commands, for telling them from targets and keeping them out of nicknames ::method isCommand private use strict arg word return ("NEW ROOT LOCATE UP TOP CLOSE CREATE REMOVE CLEAR TEXT VALUE ATTR STYLE", "CLASS SHOW HIDE ENABLE DISABLE FOCUS FIELD BUTTON TABLE LIST CSS ON OFF WAIT")~caselessWordPos(word) > 0 -- a nickname that is a command name could not be a target: refused ::method badNick private use strict arg nick, context, verb if \self~isCommand(nick) then return .false .CommandIO~error(context, "DOM:" verb": a command name cannot be a nickname:" nick) return .true -- the caller raises FAILURE ::method bad private use strict arg context, why .CommandIO~error(context, "DOM:" why) return context~raiseFailure(-3) ::method noRoot private use strict arg context .CommandIO~error(context, "DOM: no root and no current element: NEW, or ROOT PAGE, first") return context~raiseFailure(-2) -- no more words expected ::method noMore private use strict arg words, context, usage if words~items = 0 then return .true .CommandIO~error(context, "DOM: unexpected" words~rest~makeString("L", " ")"; usage:" usage) return .false -- a value for INTO var, or to the output ::method give private use strict arg context, into, value if into == .nil then do .CommandIO~output(context, value~string) return 0 end if into~right(1) == "." then do -- a stem: lines, as EXECIO if value~isA(.String) then lines = value~makeArray("0a"x) else lines = value~makeArray context~dropVar(into) do i = 1 to lines~items context~setVar(into || i, lines[i]) end context~setVar(into || "0", lines~items) return 0 end context~setVar(into, value) return 0 -- an element by nickname, or by selector in the root; .nil: none; "": a bad -- selector (with a message) ::method find private expose root nicks use strict arg ref, context el = nicks[ref~upper] if el \== .nil then do if el~isConnected then return el nicks~remove(ref~upper) -- it was removed meanwhile end if root == .nil then return .nil do return root~querySelector(ref) catch syntax .CommandIO~error(context, "DOM: not a nickname, and not a CSS selector:" ref) return "" end -- the nickname of an element, or .nil ::method nickOf private expose nicks use strict arg el do with index name item e over nicks if e == el then return name end return .nil -- the root of the element's panel or page: where its CSS goes ::method styleFor private expose doc use strict arg el node = el~getRootNode if node \== doc then return node~querySelector("style.user") style = doc~getElementById("orx-dom-css") if style == .nil then do style = doc~createElement("style") style~id = "orx-dom-css" doc~head~appendChild(style) end return style -- the panel (host element) an element is in, or .nil ::method panelOf private expose doc use strict arg el node = el~getRootNode if node == doc then return .nil if node~js.hasProperty("host") then return node~host return .nil -- a value from a word or FROM var: .nil (and a message) when the variable has none ::method takeValue private use strict arg words, context fromVar = words~isKeyword(1, "FROM") value = words~takeValue(context) if value == .nil & fromVar then .CommandIO~error(context, "DOM: FROM: the variable has no value") return value -- ---- root and navigation --------------------------------------------------- ::method new private expose doc root current nicks panels makePanel use strict arg words, into, context title = "" nick = .nil if words~items > 0 & \words~isKeyword(1, "AS") then do title = self~takeValue(words, context) if title == .nil then return context~raiseFailure(-3) end if words~isKeyword(1, "AS") then do words~take nick = words~take if nick == .nil then return self~bad(context, "NEW: AS needs a nickname") if self~badNick(nick, context, "NEW") then return context~raiseFailure(-3) nick = nick~upper end if \self~noMore(words, context, "NEW [title] [AS nickname] [INTO el]") then return context~raiseFailure(-3) if makePanel == .nil then do makePanel = .js~page~Function~new("doc", "title", "closeFn", "css", "n", , .resources["PANEL.JS"]~makeString("L", "0a"x)) end binding = .DomBinding~new(self, "CLOSE", nick, .nil, "close") handler = .js~handler(binding, "FIRE") binding~handler = handler panels += 1 made = makePanel~call(.nil, doc, title, handler, , .resources["PANEL.CSS"]~makeString("L", "0a"x), .js~num(panels - 1)) body = made["body"] binding~element = made["host"] if nick \== .nil then nicks[nick~upper] = body root = body; current = body if into \== .nil then return self~give(context, into, body) return 0 ::method setRoot private expose doc root current use strict arg words, into, context ref = words~take if ref == .nil | words~items > 0 | into \== .nil then return self~bad(context, "ROOT PAGE | ref") if ref~upper == "PAGE" & \words~quoted(1) then el = doc~body else do el = self~find(ref, context) if el == "" then return context~raiseFailure(-3) if el == .nil then do .CommandIO~error(context, "DOM: no element" ref) return context~raiseFailure(-4) end end root = el; current = el return 0 ::method locate private expose root current use strict arg words, into, context ref = words~take if ref == .nil | words~items > 0 then return self~bad(context, "LOCATE ref [INTO el]") if root == .nil then return self~noRoot(context) el = self~find(ref, context) if el == "" then return context~raiseFailure(-3) if el == .nil then return context~raiseError(2) current = el if into \== .nil then return self~give(context, into, el) return 0 ::method up private expose root current use strict arg words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "UP takes no arguments") if current == root then return context~raiseError(2) current = current~parentElement return 0 ::method top private expose root current use strict arg words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "TOP takes no arguments") if root == .nil then return self~noRoot(context) current = root return 0 ::method close private expose root current use strict arg target, words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "CLOSE takes no arguments") host = self~panelOf(target) if host == .nil then return self~bad(context, "CLOSE: the element is not in a panel") if root \== .nil then if self~panelOf(root) == host then do root = .nil; current = .nil end host~remove return 0 -- ---- elements --------------------------------------------------------------- ::method create private expose doc nicks use strict arg target, words, into, context nick = words~take; tag = words~take if nick == .nil | tag == .nil then return self~bad(context, "CREATE nickname tag [text] [INTO el]") if nick \== "-", self~badNick(nick, context, "CREATE") then return context~raiseFailure(-3) text = .nil if words~items > 0 then do text = self~takeValue(words, context) if text == .nil then return context~raiseFailure(-3) end if \self~noMore(words, context, "CREATE nickname tag [text] [INTO el]") then return context~raiseFailure(-3) el = doc~createElement(tag) if text \== .nil then el["textContent"] = text target~appendChild(el) if nick \== "-" then nicks[nick~upper] = el if into \== .nil then return self~give(context, into, el) return 0 ::method remove private expose root current use strict arg target, words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "REMOVE takes no arguments") if target == root then return self~bad(context, "REMOVE: not the root (CLOSE closes a panel)") if target~contains(current) then current = target~parentElement target~remove return 0 ::method clear private expose current use strict arg target, words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "CLEAR takes no arguments") if target \== current & target~contains(current) then current = target target~replaceChildren return 0 -- TEXT: textContent, set or got ::method property private use strict arg target, name, words, into, context if words~items = 0 then return self~give(context, into, target[name]) if into \== .nil then return self~bad(context, "a value, or INTO, not both") value = self~takeValue(words, context) if value == .nil then return context~raiseFailure(-3) if \self~noMore(words, context, "a single value (in quotes if it has blanks)") then return context~raiseFailure(-3) target[name] = value return 0 -- VALUE: a field's value; a checkbox's or radio's is its checked state, 1/0 ::method value private use strict arg target, words, into, context check = .false if \missing(target["type"]) then check = wordPos(target["type"], "checkbox radio") > 0 if words~items = 0 then do if check then return self~give(context, into, target["checked"]) value = target["value"] if missing(value) then value = "" return self~give(context, into, value) end if into \== .nil then return self~bad(context, "a value, or INTO, not both") value = self~takeValue(words, context) if value == .nil then return context~raiseFailure(-3) if \self~noMore(words, context, "VALUE [value] [INTO var]") then return context~raiseFailure(-3) if check then target["checked"] = .js~bool(value) else target["value"] = value return 0 ::method attr private use strict arg target, words, into, context name = words~take if name == .nil then return self~bad(context, "ATTR name [value] [INTO var]") if words~items = 0 then do value = target~getAttribute(name) if value == .nil then value = "" return self~give(context, into, value) end if into \== .nil then return self~bad(context, "a value, or INTO, not both") value = self~takeValue(words, context) if value == .nil then return context~raiseFailure(-3) if \self~noMore(words, context, "ATTR name [value] [INTO var]") then return context~raiseFailure(-3) target~setAttribute(name, value) return 0 ::method style private use strict arg target, words, into, context name = words~take if name == .nil then return self~bad(context, "STYLE property [value] [INTO var]") if words~items = 0 then return self~give(context, into, .js~page~getComputedStyle(target)~getPropertyValue(name)) if into \== .nil then return self~bad(context, "a value, or INTO, not both") value = self~takeValue(words, context) if value == .nil then return context~raiseFailure(-3) if \self~noMore(words, context, "STYLE property [value] [INTO var]") then return context~raiseFailure(-3) target~style~setProperty(name, value) return 0 ::method classes private use strict arg target, words, into, context if words~items = 0 then return self~give(context, into, target["className"]) if into \== .nil then return self~bad(context, "CLASS changes, or INTO, not both") list = target~classList do word over words~rest select case word~left(1) when "-" then list~remove(word~substr(2)) when "+" then list~add(word~substr(2)) otherwise list~add(word) end end return 0 ::method flag private use strict arg target, name, on, words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "no arguments") target[name] = .js~bool(on) return 0 ::method focus private use strict arg target, words, into, context if words~items > 0 | into \== .nil then return self~bad(context, "FOCUS takes no arguments") target~focus return 0 -- ---- higher level ---------------------------------------------------------- ::method field private expose doc nicks current use strict arg target, words, into, context usage = "FIELD nickname [label] [type] [INTO el]" nick = words~take if nick == .nil then return self~bad(context, usage) if self~badNick(nick, context, "FIELD") then return context~raiseFailure(-3) label = .nil; type = "TEXT" types = "TEXT PASSWORD NUMBER EMAIL DATE CHECKBOX TEXTAREA" if words~items > 0 then do label = self~takeValue(words, context) if label == .nil then return context~raiseFailure(-3) end if words~items > 0 then do type = words~take~upper if wordPos(type, types) = 0 then return self~bad(context, "FIELD: type one of" types) end if \self~noMore(words, context, usage) then return context~raiseFailure(-3) box = doc~createElement("div") box~className = "field" if type == "TEXTAREA" then input = doc~createElement("textarea") else do input = doc~createElement("input") input~type = type~lower end id = "orx-f-"nick~lower~changeStr(" ", "-") input~id = id if label \== .nil then do lab = doc~createElement("label") lab["textContent"] = label lab~htmlFor = id end if type == "CHECKBOX" then do box~classList~add("check") box~appendChild(input) if label \== .nil then box~appendChild(lab) end else do if label \== .nil then box~appendChild(lab) box~appendChild(input) end target~appendChild(box) nicks[nick~upper] = input if into \== .nil then return self~give(context, into, input) return 0 ::method button private expose doc nicks use strict arg target, words, into, context usage = "BUTTON nickname text [NAME] [INTO el]" nick = words~take if nick == .nil | words~items = 0 then return self~bad(context, usage) if self~badNick(nick, context, "BUTTON") then return context~raiseFailure(-3) text = self~takeValue(words, context) if text == .nil then return context~raiseFailure(-3) name = words~take if \self~noMore(words, context, usage) then return context~raiseFailure(-3) el = doc~createElement("button") el~type = "button" el["textContent"] = text target~appendChild(el) if nick \== "-" then nicks[nick~upper] = el if name \== .nil then self~listen(el, "click", name, "") if into \== .nil then return self~give(context, into, el) return 0 -- rows for TABLE / LIST: from FROM var or WITH INPUT; .nil (with a message) if none ::method rows private use strict arg words, context, cells if words~items > 0 then do if \words~isKeyword(1, "FROM") | words~items < 2 then signal wrong words~take name = words~take if context~hasVar(name) then data = context[name] else data = .nil if data == .nil then do .CommandIO~error(context, "DOM: FROM" name": the variable has no value") return .nil end if words~items > 0 then signal wrong end else if context~isRedirected("Input") then data = .CommandIO~lines(context) else do .CommandIO~error(context, "DOM: rows are needed: FROM var, or WITH INPUT") return .nil end if data~isA(.Stem) then do lines = .array~new do i = 1 to data[0] lines~append(data[i]) end data = lines end else if data~isA(.String) then data = data~makeArray("0a"x) else data = data~makeArray rows = .array~new do row over data if \cells then rows~append(row~string) else if row~isA(.String) then rows~append(row~makeArray("09"x)) else rows~append(row~makeArray) end return rows wrong: .CommandIO~error(context, "DOM: FROM var, or the WITH INPUT lines") return .nil ::method table private expose doc use strict arg target, words, into, context header = .false if words~isKeyword(1, "HEADER") then do words~take; header = .true end rows = self~rows(words, context, .true) if rows == .nil then return context~raiseFailure(-3) if target~tagName == "TABLE" then do table = target table~replaceChildren end else do table = doc~createElement("table") target~appendChild(table) end first = 1 if header & rows~items > 0 then do head = table~createTHead~insertRow do cell over rows[1] th = doc~createElement("th") th["textContent"] = cell~string head~appendChild(th) end first = 2 end body = table~createTBody do i = first to rows~items tr = body~insertRow do cell over rows[i] td = tr~insertCell td["textContent"] = cell~string end end if into \== .nil then return self~give(context, into, table) return 0 ::method list private expose doc use strict arg target, words, into, context rows = self~rows(words, context, .false) if rows == .nil then return context~raiseFailure(-3) if wordPos(target~tagName, "UL OL") > 0 then do list = target list~replaceChildren end else do list = doc~createElement("ul") target~appendChild(list) end do row over rows li = doc~createElement("li") li["textContent"] = row list~appendChild(li) end if into \== .nil then return self~give(context, into, list) return 0 ::method css private expose root current use strict arg words, into, context if into \== .nil then return self~bad(context, "CSS [rules]") if root == .nil then return self~noRoot(context) if words~items > 0 then do rules = self~takeValue(words, context) if rules == .nil then return context~raiseFailure(-3) if \self~noMore(words, context, "CSS [rules] (in quotes, or FROM var)") then return context~raiseFailure(-3) end else if context~isRedirected("Input") then rules = .CommandIO~lines(context)~makeString("L", "0a"x) else return self~bad(context, "CSS: rules are needed (a value, or WITH INPUT)") style = self~styleFor(root) style["textContent"] = style["textContent"] || "0a"x || rules return 0 -- ---- events ---------------------------------------------------------------- ::method on private use strict arg target, words, into, context usage = "ON event NAME [LATEST] [PREVENT] [STOP]" type = words~take; name = words~take if type == .nil | name == .nil | into \== .nil then return self~bad(context, usage) options = "" do word over words~rest select case word~upper when "LATEST" then options ||= " latest" when "PREVENT" then options ||= " preventDefault" when "STOP" then options ||= " stopPropagation" otherwise return self~bad(context, usage) end end self~listen(target, type~lower, name, options) return 0 -- (a new ON for the same element and event replaces the old one) ::method listen private expose bindings use strict arg el, type, name, options self~unlisten(el, type) jsType = type if type == "enter" then jsType = "keydown" if type == "submit" then options ||= " preventDefault" binding = .DomBinding~new(self, name~upper, self~nickOf(el), el, type) binding~handler = .js~handler(binding, "FIRE", options) el~addEventListener(jsType, binding~handler) bindings~append(binding) ::method unlisten private expose bindings use strict arg el, type do i = bindings~items to 1 by -1 b = bindings[i] if b~type == type, b~element == el then do jsType = type if type == "enter" then jsType = "keydown" el~removeEventListener(jsType, b~handler) b~handler~release bindings~delete(i) end end ::method off private use strict arg target, words, into, context type = words~take if type == .nil | words~items > 0 | into \== .nil then return self~bad(context, "OFF event") self~unlisten(target, type~lower) return 0 -- an event of ours dispatched by someone else's loop: kept for WAIT ::method queueEvent expose pending use strict arg binding, arguments pending~queue(.array~of(binding, arguments)) ::method wait private expose pending use strict arg words, into, context seconds = -1 if words~items > 0 then do seconds = words~take if \seconds~dataType("N") then return self~bad(context, "WAIT [seconds] [INTO ev]") if seconds < 0 then return self~bad(context, "WAIT [seconds] [INTO ev]") end if words~items > 0 then return self~bad(context, "WAIT [seconds] [INTO ev]") start = .dateTime~new do forever if pending~items > 0 then do item = pending~pull binding = item[1]; arguments = item[2] end else do if seconds < 0 then e = .js~nextEvent else do left = seconds - (.dateTime~new - start)~totalSeconds if left <= 0 then e = .nil else e = .js~nextEvent(left) end if e == .nil then do if into \== .nil then context~dropVar(into) return 1 end binding = e~target arguments = e~arguments if \binding~isA(.DomBinding) then do -- another kind of handler e~dispatch iterate end if binding~owner \== self then do e~dispatch -- another DOM handler's iterate end end jsEvent = .nil if arguments~items > 0 then jsEvent = arguments[1] if binding~type == "enter" then do if missing(jsEvent) then iterate if jsEvent["key"] \== "Enter" then iterate end if binding~type == "close" then self~closed(binding) ev = .DomEvent~new(binding~name, binding~nick, binding~element, jsEvent) if into == .nil then return self~give(context, into, ev~name) return self~give(context, into, ev) end -- a panel was closed by its close button: forget it ::method closed private expose root current use strict arg binding host = binding~element if root \== .nil then if self~panelOf(root) == host then do root = .nil; current = .nil end binding~handler~release -- the panel's look ::resource panel.css :host { all: initial; --bg: #ffffff; --ink: #1d2330; --muted: #5d6577; --line: #d5dae3; --accent: #2b5797; --on-accent: #ffffff; --zebra: #f3f5f9; --field: #ffffff; } @media (prefers-color-scheme: dark) { :host { --bg: #1c2028; --ink: #e6e9ef; --muted: #9aa3b5; --line: #3a4150; --accent: #7aa7e8; --on-accent: #0f1420; --zebra: #232833; --field: #14171d; } } [hidden] { display: none !important; } .frame { font: 14px/1.45 "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 28px rgba(0, 0, 0, .22); min-width: 240px; max-width: min(640px, 92vw); max-height: 80vh; display: flex; flex-direction: column; overflow: hidden; } .bar { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px; background: var(--zebra); border-bottom: 1px solid var(--line); cursor: move; user-select: none; touch-action: none; } .title { flex: 1; font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .bar .close { border: 0; background: transparent; color: var(--muted); font-size: 18px; line-height: 1; padding: 2px 6px; border-radius: 4px; cursor: pointer; } .bar .close:hover { background: var(--line); color: var(--ink); filter: none; } .body { padding: 12px 14px 14px; overflow: auto; display: flex; flex-direction: column; align-items: flex-start; gap: 10px; } .body > :is(div, p, table, ul, ol, form, .field, textarea) { align-self: stretch; } .body > :is(p, h1, h2, h3, h4, table, ul, ol):empty { display: none; } h1, h2, h3, h4 { margin: 0; line-height: 1.25; } h1 { font-size: 20px; } h2 { font-size: 17px; } h3 { font-size: 15px; } h4 { font-size: 14px; } p { margin: 0; } .row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; } .field { display: flex; flex-direction: column; gap: 3px; } .field label { font-size: 12px; font-weight: 500; color: var(--muted); } .field.check { flex-direction: row; align-items: center; gap: 8px; } .field.check label { font-size: 14px; color: var(--ink); } input, select, textarea { font: inherit; color: var(--ink); background: var(--field); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; min-width: 0; } input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); } textarea { min-height: 4.5em; resize: vertical; } :is(input, select, textarea):focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: 0; border-color: var(--accent); } button { font: inherit; font-weight: 500; color: var(--on-accent); background: var(--accent); border: 1px solid var(--accent); border-radius: 6px; padding: 6px 14px; cursor: pointer; } button:hover { filter: brightness(1.08); } button:disabled { opacity: .5; cursor: default; filter: none; } table { border-collapse: collapse; font-variant-numeric: tabular-nums; } th, td { text-align: left; padding: 5px 10px; border-bottom: 1px solid var(--line); } th { font-size: 12px; font-weight: 600; letter-spacing: .03em; color: var(--muted); } tbody tr:nth-child(even) { background: var(--zebra); } ul, ol { margin: 0; padding-left: 20px; } li + li { margin-top: 2px; } ::END -- the panel: a host element with a Shadow DOM; returns {host, body} ::resource panel.js const host = doc.createElement('div'); host.className = 'orx-dom-panel'; host.style.cssText = 'position:fixed;z-index:1000;top:' + (72 + 28 * (n % 8)) + 'px;right:' + (24 + 28 * (n % 8)) + 'px'; const shadow = host.attachShadow({ mode: 'open' }); const base = doc.createElement('style'); base.textContent = css; const user = doc.createElement('style'); user.className = 'user'; const frame = doc.createElement('div'); frame.className = 'frame'; const bar = doc.createElement('div'); bar.className = 'bar'; const name = doc.createElement('span'); name.className = 'title'; name.textContent = title; const x = doc.createElement('button'); x.className = 'close'; x.type = 'button'; x.title = 'Close'; x.textContent = '×'; const body = doc.createElement('div'); body.className = 'body'; bar.append(name, x); frame.append(bar, body); shadow.append(base, user, frame); // closed at once, even when the run is over; then the program hears CLOSE x.addEventListener('click', () => { host.remove(); closeFn(); }); // moved by its title bar, kept reachable let drag = null; const front = () => { for (const p of doc.querySelectorAll('.orx-dom-panel')) p.style.zIndex = 1000; host.style.zIndex = 1001; }; host.addEventListener('pointerdown', front); bar.addEventListener('pointerdown', (e) => { if (e.button !== 0 || e.target === x) return; const r = host.getBoundingClientRect(); drag = { dx: e.clientX - r.left, dy: e.clientY - r.top }; host.style.right = 'auto'; host.style.left = r.left + 'px'; host.style.top = r.top + 'px'; bar.setPointerCapture(e.pointerId); }); bar.addEventListener('pointermove', (e) => { if (!drag) return; const w = doc.defaultView; host.style.left = Math.max(60 - host.offsetWidth, Math.min(w.innerWidth - 60, e.clientX - drag.dx)) + 'px'; host.style.top = Math.max(0, Math.min(w.innerHeight - 30, e.clientY - drag.dy)) + 'px'; }); const up = () => { drag = null; }; bar.addEventListener('pointerup', up); bar.addEventListener('pointercancel', up); doc.body.append(host); front(); return { host: host, body: body }; ::END /** What ON (or a panel's close button) set up: the handler's target. */ ::class DomBinding public ::attribute owner get ::attribute name get ::attribute nick get ::attribute element ::attribute type get ::attribute handler ::method init expose owner name nick element type use strict arg owner, name, nick, element, type -- only when someone else's event loop takes the event: WAIT will see it ::method fire expose owner owner~queueEvent(self, arg(1, "A")) /** An event, as WAIT INTO ev gives it. */ ::class DomEvent public ::attribute name get -- the NAME given to ON (CLOSE: a panel closed) ::attribute target get -- the element's nickname, or .nil ::attribute element get -- the element (a .JSObject) ::attribute jsEvent get -- JavaScript's event (a .JSObject), or .nil ::method init expose name target element jsEvent use strict arg name, target, element, jsEvent ::method value -- the element's value now (a checkbox: 1/0) expose element if element == .nil then return "" type = element["type"] if \missing(type) then if wordPos(type, "checkbox radio") > 0 then return element["checked"] value = element["value"] if missing(value) then return "" return value ::method key -- the key, for keyboard events expose jsEvent if missing(jsEvent) then return "" key = jsEvent["key"] if missing(key) then return "" return key ::method x -- the pointer, relative to the element expose jsEvent if missing(jsEvent) then return "" x = jsEvent["offsetX"] if missing(x) then return "" return x ::method y expose jsEvent if missing(jsEvent) then return "" y = jsEvent["offsetY"] if missing(y) then return "" return y ::method string expose name target if target == .nil then return "a DomEvent ("name")" return "a DomEvent ("name "on" target")" -- a JavaScript value that is not there: null (.nil) or undefined ::routine missing use arg v return v == .nil | v == .js~undefined