0 | module Rhone.JS.ElemRef
22 | data ElemRef : (t : Type) -> Type where
24 | -> (0 tpe : ElementType tag t)
28 | Class : {tag : String}
29 | -> (0 tpe : ElementType tag t)
33 | Body : ElemRef HTMLElement
35 | Document : ElemRef Document
37 | Window : ElemRef Window
42 | data ById : ElemRef t -> Type where
43 | IsById : {0 tpe : _} -> {0 id : _} -> ById (Id tpe id)
48 | data ByClass : ElemRef t -> Type where
49 | IsByClass : {0 tpe : _} -> {0 id : _} -> ByClass (Class tpe id)
54 | ref : (r : ElemRef t) -> {auto 0 _ : ById r} -> Attribute ev
60 | idRef : (r : ElemRef t) -> {auto 0 _ : ById r} -> List Declaration -> Rule n
61 | idRef (Id _ i) = id i
65 | classRef : (r : ElemRef t) -> {auto 0 _ : ByClass r} -> List Declaration -> Rule n
66 | classRef (Class _ i) = class i
77 | strictGetElementById : SafeCast t => (tag,id : String) -> JSIO t
78 | strictGetElementById tag id = do
79 | Nothing <- castElementById t id | Just t => pure t
80 | liftJSIO $
throwError $
81 | Caught "Control.Monad.Dom.Interface.strictGetElementById: Could not find \{tag} with id \{id}"
88 | strictGetHTMLElementById : (tag,id : String) -> JSIO HTMLElement
89 | strictGetHTMLElementById = strictGetElementById
96 | getElementByRef : SafeCast t => ElemRef t -> JSIO t
97 | getElementByRef (Id {tag} _ id) = strictGetElementById tag id
98 | getElementByRef (Class _ class) = getElementByClass class
99 | getElementByRef Body = body
100 | getElementByRef Document = document
101 | getElementByRef Window = window
104 | err = "Control.Monad.Dom.Interface.castElementByRef"
111 | castElementByRef : SafeCast t2 => ElemRef t -> JSIO t2
112 | castElementByRef (Id {tag} _ id) = strictGetElementById tag id
113 | castElementByRef (Class _ class) = getElementByClass class
114 | castElementByRef Body = body >>= tryCast err
115 | castElementByRef Document = document >>= tryCast err
116 | castElementByRef Window = window >>= tryCast err