-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
279 lines (233 loc) · 7.76 KB
/
main.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/** @type {HTMLInputElement} */
const input = /* @type {HTMLInputElement} */ (document.getElementById("input"));
const output = document.getElementById("output");
const queryForm = document.getElementById("query-form");
const querySuggest = document.getElementById("query-suggest");
const QUERY_HISTORY = "query_history";
const HISTORY_SHOW_COUNT = 20;
const HISTORY_SAVE_COUNT = 100;
let queryHistory = loadHistory();
/**
* Use <object> instead of <img>
*/
const OBJECT_IMAGE = false;
const HTML_ESCAPING = true;
queryForm.addEventListener("submit", e => {
e.preventDefault();
sendQuery();
});
document.addEventListener("keydown", e => {
if (e.key === "Enter") sendQuery();
else if (e.key === "Escape") hideSuggestions();
else if (e.key === "Tab") {
const suggestions = getSuggestions();
if (suggestions.length >= 1) {
e.preventDefault();
input.value = suggestions[0];
}
}
});
document.addEventListener("click", e => {
hideSuggestions();
});
input.addEventListener("keyup", e => {
if (e.altKey && "1234567890".includes(e.key)) {
e.preventDefault();
const suggest = getSuggestions()["1234567890".indexOf(e.key)];
if (typeof suggest !== "undefined") {
input.value = suggest;
}
}
else if (e.key === "Escape") return;
showSuggestions();
});
querySuggest.addEventListener("click", e => {
if (e.target instanceof HTMLLIElement) {
input.value = e.target.textContent;
showSuggestions();
input.focus();
}
});
window.addEventListener("hashchange", handleHash);
function handleHash () {
if (location.hash && !input.disabled) {
const searchParams = new URLSearchParams(location.hash.substr(1));
if (searchParams.has("q")) {
input.value = searchParams.get("q");
sendQuery();
}
}
}
handleHash();
input.focus();
function sendQuery () {
hideSuggestions();
output.innerHTML = "";
input.disabled = true;
const start = Date.now();
location.hash = "#q=" + encodeURIComponent(input.value);
document.title = "Query: " + input.value;
query(input.value)
.then(data => {
const duration = (Date.now() - start) / 1000;
saveHistory(input.value);
if (data.length === 2 && data[0][0] === "AST") {
output.innerHTML = renderAST(data[1][0]);
return;
}
output.innerHTML = renderTable({ rows: data.slice(), duration });
if (data.length >= 3 && data[0].length >= 2 &&
typeof data[1][0] === "number" && typeof data[1][1] === "number")
{
const footer = output.querySelector("tfoot td");
if (footer) {
const btn = document.createElement("button");
btn.className = "link";
btn.innerHTML = "Graph";
btn.addEventListener("click", () => {
footer.removeChild(btn);
output.appendChild(renderGraph(data.slice()));
});
footer.appendChild(btn);
}
}
})
.catch(e => {
output.innerHTML = `<p style="color: red; margin: 20px;">${e}</p>`;
})
.then(() => {
input.disabled = false;
});
}
/**
* @param {{ rows: any[][], duration: number }} options
*/
function renderTable({ rows, duration }) {
const headerRow = rows.shift();
if (!headerRow) {
return `<p style="font-weight: bold;">Empty Result Set</p><p>${duration} seconds</p>`;
}
return `<table>
<thead>
<tr>${headerRow.map(cell => `<th>${cell}</th>`).join("")}</tr>
</thead>
<tbody>
${rows.map(row => `
<tr>${row.map(cell => `<td>${formatCell({ cell })}</td>`).join("")}</tr>
`).join("")}
</tbody>
<tfoot><tr><td colspan="${headerRow.length}">${rows.length} rows, ${duration} seconds</td></tr></tfoot>
</table>`;
}
function formatCell ({ cell }) {
if (cell === null) {
return `<span class="null"></span>`;
}
if (cell instanceof Date) {
return formatDate(cell);
}
let str = String(cell);
if (HTML_ESCAPING && str.includes("<")) {
str = escapeHtml(str);
}
// Special Formatting for colour
if (/^#[0-9a-f]{6}$/i.test(str)) {
return `<div style="background-color: ${str}; height: 20px; width: 20px; margin: 0 auto;" title="${str}"></div>`;
}
// Special formatting for date
if (/^\d{4}-\d{2}-\d{2}/.test(str)) {
const d = new Date(str);
return formatDate(d);
}
if (!str.includes("<")) {
// Special formatting for urls and images
str = str.replace(/https?:\/\/[^,'>" &()[\]]+/g, url => {
let content = url;
if (/\.(jpe?g|gif|png|webp)$/i.test(url)) {
content = OBJECT_IMAGE ? `<object data="${url}" height="64"></object>` : `<img src="${url}" height="64" />`;
}
return `<a href="${url}" target="_blank">${content}</a>`;
});
// Special formatting for email addresses
str = str.replace(/[^@,\s]+@[^@,\s]+\.[^@,\s]+/g, email => `<a href="mailto:${email}">${email}</a>`);
}
return str;
}
function formatDate (d) {
if (d.getHours() === 0 && d.getMinutes() === 0) {
return d.toLocaleDateString(navigator.language);
}
return d.toLocaleString(navigator.language);
}
/**
*
* @param {any[][]} data
*/
function renderGraph (data) {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 300;
canvas.height = 300;
const headers = data.shift();
const X = data.map(r => r[0]);
const Y = data.map(r => r[1]);
const minX = Math.min(...X);
const maxX = Math.max(...X);
const minY = Math.min(...Y);
const maxY = Math.max(...Y);
const xScale = canvas.width / (maxX - minX);
const yScale = canvas.height / (maxY - minY);
const h = canvas.height;
ctx.strokeStyle = "#999";
ctx.strokeRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo((X[0] - minX) * xScale, h - (Y[0] - minY) * yScale);
for (let i = 1; i < data.length; i++) {
ctx.lineTo((X[i] - minX) * xScale, h - (Y[i] - minY) * yScale);
}
ctx.strokeStyle = "red";
ctx.stroke();
return canvas;
}
function renderAST (json) {
const ast = JSON.parse(json);
return renderNode(ast);
}
const NODE_TYPE_LIST = 8;
function renderNode (node) {
return `${node.id || (node.type === NODE_TYPE_LIST ? "LIST" : '')}${Array.isArray(node.children) ? `<ul>${node.children.map(c => `<li>${renderNode(c)}</li>`).join('')}</ul>` : ''}`;
}
function getSuggestions () {
return queryHistory.filter(q => q && q != input.value && q.startsWith(input.value)).slice(0, HISTORY_SHOW_COUNT);
}
function showSuggestions () {
querySuggest.innerHTML = getSuggestions().map(m => `<li>${m}</li>`).join("");
}
function hideSuggestions () {
querySuggest.innerHTML = "";
}
function saveHistory (value) {
queryHistory = queryHistory.filter(q => q !== value);
queryHistory.unshift(input.value);
queryHistory.length = HISTORY_SAVE_COUNT;
localStorage.setItem(QUERY_HISTORY, JSON.stringify(queryHistory));
}
function loadHistory () {
const saved = localStorage.getItem(QUERY_HISTORY);
if (saved) {
try {
return JSON.parse(saved);
} catch (e) { }
}
return [];
}
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}