-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
196 lines (163 loc) · 6.05 KB
/
index.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
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
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="/lib/prism/prism.css">
<style>
* {
box-sizing: border-box;
margin: 0 0 0 0;
}
.top {
position: absolute;
top: 0; right: 0; left: 0; bottom: 200px;
}
.bottom {
position: absolute;
bottom: 0;
height: 200px; width: 100%;
}
.column {
padding: 10px;
overflow: auto;
float: left;
}
.left {
height: 200px; width: 30%;
}
.middle {
height: 200px; width: 25%;
}
.right {
height: 200px; width: 45%;
}
</style>
<script src='/lib/prism/prism.js' data-manual></script>
1<script src='/lib/moment/moment.js'></script>
<script src="/lib/chart/chart.js"></script>
<script src='/lib/chart-moment-adapter/adapter.js'></script>
<script type="module">
import Yace from './lib/yace/index.js'
const sql_highlighter = (value) =>
Prism.highlight( value, Prism.languages.sql, "sql" );
const js_highlighter = (value) =>
Prism.highlight( value, Prism.languages.javascript, "javascript" );
window.sql = new Yace( '#sql', {
styles: {
overflow: "auto"
},
highlighter: sql_highlighter,
lineNumbers: true
} )
window.sql.textarea.spellcheck = false
window.code = new Yace( '#code', {
styles: {
overflow: "auto"
},
highlighter: js_highlighter,
lineNumbers: true
} )
window.code.textarea.spellcheck = false
</script>
<script src='/lib/sql/sql-wasm.js'></script>
<script>initSqlJs( { locateFile: filename => `/lib/sql/${filename}` } ).then( function( SQL ) {
var xhr = new XMLHttpRequest()
xhr.open( 'GET', 'database.sqlite', true )
xhr.responseType = 'arraybuffer'
// on successful sqlite database load
xhr.onload = e => {
var uInt8Array = new Uint8Array( xhr.response )
// global for database access
window.db = new SQL.Database( uInt8Array )
// global for location data
var locations = db.exec( 'SELECT * FROM location' )
window.locations = {}
locations[0].values.forEach( row => {
window.locations[ row[0] ] = { id: row[0], name: row[1], source: row[2], colour: row[3] }
} )
}
xhr.send()
// globals for SQL results after a query
window.result = ''
window.values = ''
} )</script>
</head>
<body onLoad="load( 'demo' )">
<div class="top" style="background-color: darkblue">
<div id="load" style="color: white; position: absolute; cursor: pointer" onClick="load()">⇱</div>
<div id="save" style="color: white; float: right; cursor: pointer" onClick="save()">⇲</div>
<canvas id="chart""></canvas>
</div>
<div class="bottom">
<div class="left column" style="background-color: orange">
<div id="sql" onKeyDown="if( event.keyCode == 13 && event.ctrlKey ) query( sql.value );">
</div>
</div>
<div class="middle column" style="background-color: darkgreen">
<div id="data">
</div>
<tt id="rowcount" style="font-size: 10px; color: white; position: absolute; margin: -10px; top: 10px">
</tt>
</div>
<div class="right column" style="background-color: purple;">
<div id="code" onKeyDown="if( event.keyCode == 13 && event.ctrlKey ) graph( code.value );">
</div>
<input id="auto" type="checkbox" style="font-size: 10px; color: white; position: absolute; margin: -10px; top: 10px">
</input>
</div>
</div>
<script>
function query( sql ) {
let rowcount = document.getElementById( 'rowcount' )
rowcount.innerHTML = 'running...';
setTimeout( () => {
try {
window.result = db.exec( sql );
window.values = window.result[0].values;
// display up to 10 rows of returned data
var data_slice = JSON.stringify( window.values.slice( 0, 10 ) );
var row_count = window.values.length;
if ( row_count > 10 ) data_slice = data_slice.replace(/.$/,",...]")
document.getElementById( 'data' ).innerHTML = Prism.highlight( data_slice, Prism.languages.json, 'json' );
rowcount.innerHTML = window.values.length;
// if auto-execute code is selected then re-run graph()
if ( document.getElementById( 'auto' ).checked )
graph( code.value )
}
catch ( e ) { document.getElementById( 'rowcount' ).innerHTML = ''; alert( e ) }
}, 10 );
}
function graph( code ) {
try {
if( window.hasOwnProperty( 'chart' ) ) window.chart.destroy();
eval( code );
}
catch( e ) { alert( 'Error: ' + e + ' :' + e.lineNumber + '' ) }
}
function load( filename = '' ) {
if ( filename == '' )
var filename = prompt( 'Load :' )
if ( filename != '' && filename != null ) {
if ( !filename.startsWith( 'http' ) )
filename = '/load.lua?filename=' + filename
fetch( filename )
.then( response => { if ( response.status != 200 ) { throw 'HTTP Error : ' + response.status } else return response.text() } )
.then( data => {
apart = data.split( "\n\n----\n\n" )
window.sql.update( { value: apart[0] } )
window.code.update( { value: apart[1] } )
} )
.catch( error => alert( error ) )
}
}
function save() {
var filename = prompt( 'Save filename: ', '' )
if ( filename != '' && filename != null ) {
var to_save = window.sql.value + "\n\n----\n\n" + window.code.value
var xhr = new XMLHttpRequest();
xhr.open( 'PUT', '/save.lua?filename=' + filename, true )
xhr.send( to_save )
}
}
</script>
</body>
</html>