-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.js
245 lines (214 loc) · 7.12 KB
/
gui.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
const execBtn = document.getElementById("execute");
const outputElm = document.getElementById('output');
const errorElm = document.getElementById('error');
const commandsElm = document.getElementById('commands');
const dbFileElm = document.getElementById('dbfile');
const savedbElm = document.getElementById('savedb');
const querySection = document.getElementById('query');
const uploadSection = document.getElementById('dropzone');
const promptArea = document.getElementById('prompt-area');
const artificialLimit = 5000;
// Start the worker in which sql.js will run
const worker = new Worker("worker.sql-wasm.js");
worker.onerror = error;
// Open a database
worker.postMessage({ action: 'open' });
// Connect to the HTML element we 'print' to
function print(text) {
outputElm.innerHTML = text.replace(/\n/g, '<br>');
}
function error(e) {
console.log(e);
errorElm.classList.remove('visually-hidden');
errorElm.classList.add('alert', 'alert-danger');
errorElm.textContent = e.message;
}
function noerror() {
errorElm.classList.add('visually-hidden');
errorElm.classList.remove('alert', 'alert-danger');
}
function showResults(results, maxRows) {
tic();
outputElm.innerHTML = "";
for (let i = 0; i < results.length; i++) {
let table = tableCreate(results[i].columns, results[i].values, maxRows);
outputElm.appendChild(table);
}
toc("Displaying results");
}
function askUserIfToShowEverything(results) {
promptArea.classList.remove('visually-hidden');
promptArea.innerHTML = `
<div class="group-question">
<p>השאילתה מחזירה הרבה תוצאות. להציג את כולן?</p>
<div class="grouped-buttons">
<button class="btn btn-success">לא</button>
<button data-get-all="true" class="btn btn-danger">כן</button>
</div>
</div>
`;
const buttons = Array.from(promptArea.querySelectorAll('button'))
buttons.forEach((b) => {
b.addEventListener('click', () => {
const getAll = (b.dataset.getAll !== undefined);
const maxToShow = getAll ? Infinity : artificialLimit;
if (getAll) {
promptArea.innerHTML = "<p>זה יקח קצת זמן...</p>";
}
showResults(results, maxToShow);
promptArea.classList.add('visually-hidden');
});
});
}
// Run a command in the database
function execute(commands) {
tic();
worker.onmessage = function (event) {
var results = event.data.results;
toc("Executing SQL");
if (!results) {
error({message: event.data.error});
return;
}
if (results[0].values.length > artificialLimit) {
askUserIfToShowEverything(results);
} else {
showResults(results, Infinity);
}
}
worker.postMessage({ action: 'exec', sql: commands });
outputElm.textContent = "Fetching results...";
}
// Create an HTML table
var tableCreate = function () {
function valconcat(vals, tagName) {
if (vals.length === 0) return '';
var open = '<' + tagName + '>', close = '</' + tagName + '>';
return open + vals.join(close + open) + close;
}
return function (columns, values, maxRows) {
maxRows = Math.min(maxRows, values.length);
values = values.slice(0, maxRows);
var tbl = document.createElement('table');
tbl.classList.add('table', 'table-hover')
var html = '<thead>' + valconcat(columns, 'th') + '</thead>';
var rows = values.map(function (v) { return valconcat(v, 'td'); });
html += '<tbody>' + valconcat(rows, 'tr') + '</tbody>';
tbl.innerHTML = html;
return tbl;
}
}();
// Execute the commands when the button is clicked
function execEditorContents() {
noerror()
execute(editor.getValue() + ';');
}
execBtn.addEventListener("click", execEditorContents, true);
// Performance measurement functions
var tictime;
if (!window.performance || !performance.now) { window.performance = { now: Date.now } }
function tic() { tictime = performance.now() }
function toc(msg) {
var dt = performance.now() - tictime;
console.log((msg || 'toc') + ": " + dt + "ms");
}
// Add syntax highlihjting to the textarea
var editor = CodeMirror.fromTextArea(commandsElm, {
mode: 'text/x-sql',
viewportMargin: Infinity,
autofocus: true,
indentWithTabs: true,
smartIndent: true,
lineNumbers: true,
matchBrackets: true,
autofocus: true,
extraKeys: {
"Ctrl-Enter": execEditorContents,
"Ctrl-S": savedb,
}
});
// Load a db from a file
var dbFile = new FileReader();
var defaultQuery = "SELECT `name`, `sql`\n FROM `sqlite_master`\n WHERE type='table';"
dbFile.onload = function () {
worker.onmessage = function () {
toc("Loading database from file");
// Show the schema of the loaded database
editor.setValue(localStorage.getItem('lastQuery') || defaultQuery);
execEditorContents();
uploadSection.classList.add('visually-hidden');
querySection.classList.remove('visually-hidden');
noerror();
};
tic();
try {
worker.postMessage({ action: 'open', buffer: dbFile.result }, [dbFile.result]);
}
catch (exception) {
worker.postMessage({ action: 'open', buffer: dbFile.result });
}
}
function init() {
let dropzoneElement = new Dropzone("#demo-upload");
dropzoneElement.uploadFiles = function(files) {
var self = this,
minSteps = 6,
maxSteps = 60,
timeBetweenSteps = 1,
bytesPerStep = 1000000;
for (var i = 0; i < files.length; i++) {
var file = files[i];
totalSteps = Math.round(Math.min(maxSteps, Math.max(minSteps, file.size / bytesPerStep)));
for (var step = 0; step < totalSteps; step++) {
var duration = timeBetweenSteps * (step + 1);
setTimeout(function(file, totalSteps, step) {
return function() {
file.upload = {
progress: 100 * (step + 1) / totalSteps,
total: file.size,
bytesSent: (step + 1) * file.size / totalSteps
};
self.emit('uploadprogress', file, file.upload.progress, file.upload.bytesSent);
if (file.upload.progress == 100) {
file.status = Dropzone.SUCCESS;
self.emit("success", file, 'success', null);
self.emit("complete", file);
self.processQueue();
}
};
}(file, totalSteps, step), duration);
}
dbFile.readAsArrayBuffer(files[0]);
}
}
function saveQuery(e) {
if (e.getValue()) {
localStorage.setItem('lastQuery', e.getValue());
}
}
editor.setValue(localStorage.getItem('lastQuery') || defaultQuery);
editor.on("changes", saveQuery);
}
Dropzone.autoDiscover = false;
document.addEventListener("DOMContentLoaded", init);
// Save the db to a file
function savedb() {
worker.onmessage = function (event) {
toc("Exporting the database");
var arraybuff = event.data.buffer;
var blob = new Blob([arraybuff]);
var a = document.createElement("a");
document.body.appendChild(a);
a.href = window.URL.createObjectURL(blob);
a.download = "sql.db";
a.onclick = function () {
setTimeout(function () {
window.URL.revokeObjectURL(a.href);
}, 1500);
};
a.click();
};
tic();
worker.postMessage({ action: 'export' });
}
savedbElm.addEventListener("click", savedb, true);