-
Notifications
You must be signed in to change notification settings - Fork 2
/
repl.html
129 lines (105 loc) · 3.89 KB
/
repl.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>au</title>
</head>
<body>
expr:
<input class="expr" style="width: 300px;" type="text">
<button class="btn-fire">calc</button>
<h1 class="result"></h1>
<h2 class="msg"></h2>
<h3>Variables:</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody class="vars"></tbody>
</table>
<script src="./au.js"></script>
<script>
(function (global) {
function qs(selector) {
return document.querySelector(selector);
}
function remove(tbody, tr, name, value) {
if (tbody.children.length > 1 && name.value.trim().length === 0 && value.value.trim().length === 0) {
tr.remove();
}
}
function add(tb) {
var lc = tb.lastElementChild;
var name = lc.firstElementChild.firstElementChild;
var value = lc.lastElementChild.lastElementChild;
if (name.value.trim().length !== 0 || value.value.trim().length !== 0) {
generateVar(tb);
}
}
function generateVar(tbody) {
var tb = document.createElement('tbody');
var tr;
var name;
var value;
tb.innerHTML = '<tr> \
<td><input type="text" class="var-name"></td> \
<td><input type="text" class="var-value"></td> \
</tr>';
tr = tb.children[0];
tbody.appendChild(tr);
name = tr.querySelector('.var-name');
value = tr.querySelector('.var-value');
name.addEventListener('keyup', function (e) {
remove(tbody, tr, name, value);
add(tbody);
});
value.addEventListener('keyup', function (e) {
remove(tbody, tr, name, value);
add(tbody);
});
}
function createVarsObject(tb) {
var obj = {};
Array.prototype.slice.call(tb.children).forEach(function (tr) {
var name = tr.firstElementChild.firstElementChild.value.trim();
var value = tr.lastElementChild.lastElementChild.value.trim();
if (name.length > 0 && value.length > 0) {
obj[name] = value;
}
});
return obj;
}
document.addEventListener('DOMContentLoaded', function () {
var expr = qs('.expr');
var result = qs('.result');
var fire = qs('.btn-fire');
var msg = qs('.msg');
var tb = qs('.vars');
function figure() {
var input = expr.value;
msg.textContent = '';
result.textContent = '';
if (typeof input === 'string') {
try {
result.textContent = '= ' + au(input, createVarsObject(tb));
}
catch (m) {
msg.textContent = m;
}
}
}
expr.addEventListener('keydown', function (e) {
e.keyCode === 13 ? figure() : null;
});
fire.addEventListener('click', figure);
generateVar(tb);
});
})(this);
</script>
</body>
</html>