-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJavaScript.lean
More file actions
71 lines (57 loc) · 2.22 KB
/
JavaScript.lean
File metadata and controls
71 lines (57 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import N2O.Network.Default
import NITRO.Elements
def action (x : String) : Result :=
let term := λ b => Term.tuple
[ Term.atom "io",
Term.binary b,
Term.binary ByteArray.empty ];
match Put.run (Put.unicode x) with
| Sum.ok bytes =>
match writeTerm (term bytes) with
| Sum.ok v => Result.reply (Msg.binary v)
| Sum.fail s => Result.error s
| Sum.fail s => Result.error s
variable (α : Type) [BERT α]
def update (target : String) (elem : Elem α) : Result :=
let (html, js) := render elem;
action $ "qi('" ++ target ++ "').outerHTML='" ++ html ++ "';" ++ js
def updateText (target text : String) : Result :=
action $ "qi('" ++ target ++ "').innerText='" ++ text ++ "';"
def insertTagTop (tag target : String) (elem : Elem α) : Result :=
let (html, js) := render elem;
action $
"qi('" ++ target ++ "').insertBefore(" ++
"(function(){ var div = qn('" ++ tag ++ "'); div.innerHTML = '" ++
html ++ "'; return div.firstChild; })()," ++
"qi('" ++ target ++ "').firstChild);" ++ js
def insertTagBottom (tag target : String) (elem : Elem α) : Result :=
let (html, js) := render elem;
action $
"(function(){ var div = qn('" ++ tag ++
"'); div.innerHTML = '" ++ html ++
"';qi('" ++ target ++ "').appendChild(div.firstChild); })();" ++ js
def insertTop := insertTagTop α "div"
def insertBottom := insertTagBottom α "div"
def insertAdjacent (position target : String) (elem : Elem α) :=
let (html, js) := render elem;
action $
"qi('" ++ target ++ "').insertAdjacentHTML('" ++
position ++ "', '" ++ html ++ "');" ++ js
def insertBefore := insertAdjacent α "beforebegin"
def insertAfter := insertAdjacent α "afterend"
def clear (target : String) :=
action $
"(function(){var x = qi('" ++ target ++
"'); while (x.firstChild) x.removeChild(x.firstChild);})();"
def remove (target : String) :=
action $
"(function(){var x=qi('" ++ target ++
"'); x && x.parentNode.removeChild(x);})();"
def redirect (url : String) :=
action $ "(function(){document.location = '" ++ url ++ "';})();"
def display (elem status : String) :=
action $
"(function(){var x = qi('" ++ elem ++
"'); if (x) x.style.display = '" ++ status ++ "';})();"
def show' (elem : String) := display elem "block"
def hide' (elem : String) := display elem "none"