forked from SimplyEdit/SimplyStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
276 lines (270 loc) · 8.31 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
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
<!doctype html>
<meta charset="utf-8">
<link rel="stylesheet" href="/assets/css/style.css">
<style>
:root {
--ds-primary: var(--ds-simplystore);
--ds-primary-contrast: var(--ds-simplystore-contrast);
--ds-primary-gradient: var(--ds-simplystore-gradient);
--ds-primary-gradient-bump: var(--ds-simplystore-gradient-bump);
}
#databrowser {
border: 0;
margin: 0;
padding: 0;
width: 50%;
height: 80vh;
}
form.Query {
width: 49%;
float: left;
height: calc(100vh - 65px);
margin-right: 1%;
margin-top: 65px;
}
.CodeMirror {
height: 100% !important;
}
.dataBrowser {
height: calc(100vh - 65px);
margin-top: 65px;
}
body {
position: fixed;
width: 100%;
}
.ds-visible {
display: block;
}
</style>
<body class="ds-nightmode">
<header>
<nav class="ds-toolbar">
<div class="simply-title">
<img src="/assets/img/simplystore-narrow.svg" alt="SimplyStore">
</div>
<button data-simply-command="run-query" class="ds-button ds-button-icon">
<svg class="ds-icon ds-icon-feather">
<use xlink:href="/assets/feather-sprite.svg#search">
</use></svg>
Run
</button>
<button data-simply-command="load-query-dropdown" class="ds-button ds-button-icon">
<svg class="ds-icon ds-icon-feather">
<use xlink:href="/assets/feather-sprite.svg#download">
</use></svg>
Load
<nav class="ds-dropdown-nav">
<ul class="ds-dropdown-list" data-simply-list="queries">
<template>
<li class="ds-dropdown-item" data-simply-command="load-query"><span data-simply-field="name">Query</span></li>
</template>
</ul>
</nav>
</button>
<button data-simply-command="save-query" class="ds-button ds-button-icon">
<svg class="ds-icon ds-icon-feather">
<use xlink:href="/assets/feather-sprite.svg#bookmark">
</use></svg>
Save
</button>
<button data-simply-command="load-fixtures" class="ds-button ds-button-icon">
<svg class="ds-icon ds-icon-feather">
<use xlink:href="/assets/feather-sprite.svg#briefcase">
</use></svg>
Fixtures
</button>
<span class="ds-push-right"></span>
<button class="ds-button ds-button-icon">
<svg class="ds-icon ds-icon-feather">
<use xlink:href="/assets/feather-sprite.svg#download">
</use></svg>
Download
</button>
<button class="ds-button ds-button-icon">
<svg class="ds-icon ds-icon-feather">
<use xlink:href="/assets/feather-sprite.svg#help-circle">
</use></svg>
Help
</button>
</nav>
</header>
<form data-simply-command="query" class="Query" data-simply-keyboard="query">
<textarea name="query" id="codeEditor" class="CodeMirror"></textarea>
</form>
<div class="dataBrowser">
<textarea id="databrowser" class="CodeMirror" data-simply-field="data"></textarea>
</div>
<script src="/codemirror/lib/codemirror.js"></script>
<link rel="stylesheet" href="/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="/codemirror/addon/lint/lint.css">
<script src="/codemirror/mode/javascript/javascript.js"></script>
<link rel="stylesheet" href="/codemirror/theme/material-ocean.css">
<script src="/codemirror/addon/lint/lint.js"></script>
<script src="/codemirror/addon/lint/javascript-lint.js"></script>
<script src="https://cdn.simplyedit.io/1/simply-edit.js"></script>
<script>
var codeEditor = CodeMirror.fromTextArea(document.getElementById('codeEditor'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
theme: 'material-ocean',
mode: 'javascript'
})
codeEditor.focus()
var browser = CodeMirror.fromTextArea(document.getElementById('databrowser'), {
theme: 'material-ocean',
readOnly: true
})
</script>
<script>
const databrowser = simply.app({
routes: {
'/:*': async (params) => {
await databrowser.actions.query(window.location.pathname)
browser.setValue(databrowser.view.data)
}
},
commands: {
'run-query': async function(button, value) {
await this.app.actions.query(window.location.pathname, codeEditor.getValue())
browser.setValue(databrowser.view.data)
},
query: async function(form, values) {
await this.app.actions.query(window.location.pathname, values.query)
browser.setValue(databrowser.view.data)
},
'load-query-dropdown': async function(button) {
button.querySelector('nav.ds-dropdown-nav').classList.toggle('ds-visible')
},
'load-query': async function(el, value) {
let query = await this.app.actions.loadQuery(el.innerText)
codeEditor.setValue(query)
let button = el.closest('.ds-button')
button.querySelector('nav.ds-dropdown-nav').classList.remove('ds-visible')
codeEditor.focus()
},
'save-query': async function() {
let name = prompt('Enter the query name:',this.app.view.queryName || 'query')
this.app.actions.saveQuery(name, codeEditor.getValue())
},
'load-fixtures': async function() {
let fixtures = await this.app.actions.loadFixtures()
codeEditor.setValue(fixtures)
codeEditor.focus()
}
},
actions: {
start: async function() {
this.app.view.queries = JSON.parse(localStorage.getItem('queries') || '[]')
},
loadQuery: async function(name) {
this.app.view.queryName = name
return this.app.actions.getQuery(name)
},
saveQuery: async function(name, query) {
this.app.view.queries = this.app.view.queries.filter(q => q.name!=name)
this.app.view.queries.push({
name: name,
query: codeEditor.getValue()
})
localStorage.setItem('queries',JSON.stringify(this.app.view.queries))
},
loadFixtures: async function() {
return this.app.actions.loadQuery('Fixtures')
},
getQuery: async function(name) {
let query = this.app.view.queries.filter(q => q.name===name)
if (query && query[0]) {
return query[0].query
}
return ''
},
query: async function(path, query) {
query = await this.app.actions.getQuery('Fixtures')+';'+query
try {
let data;
if (query) {
data = await api.post(path, query)
} else {
data = await api.get(path)
}
databrowser.view.data = JSONTag.stringify(data, null, 4)
return data
} catch(err) {
databrowser.view.data = JSONTag.stringify(err, null, 4)
return err
}
}
},
keyboard: {
query: {
'Control+Enter': async function() {
await databrowser.actions.query(window.location.pathname, codeEditor.getValue())
browser.setValue(databrowser.view.data)
}
}
},
view: {
queries: [
{
name: 'Test Query',
query: 'from(data.persons).select({name:_})'
}
]
}
})
const apiBase = window.location.protocol+'//'+window.location.host;
const api = {
get: function(path, params) {
let url = apiBase+path;
if (!params && window.location.search) {
url += window.location.search
}
if (params) {
var args = Object.keys(params).map(param => encodeURIComponent(param)+'='+encodeURIComponent(params[param])).join('&')
url += '?' + args
}
return fetch(url, {
headers: {
'Accept': 'application/jsontag'
}
})
.then(response => {
let jsontag = response.text().then(JSONTag.parse)
if (response.ok) {
return jsontag
}
return jsontag.then(Promise.reject.bind(Promise))
})
},
post: function(path, query, params) {
let url = apiBase+path;
if (!params && window.location.search) {
url += window.location.search
}
if (params) {
var args = Object.keys(params).map(param => encodeURIComponent(param)+'='+encodeURIComponent(params[param])).join('&')
url += '?' + args
}
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/jsontag'
},
body: query
})
.then(response => {
let jsontag = response.text().then(JSONTag.parse)
if (response.ok) {
return jsontag
}
return jsontag.then(Promise.reject.bind(Promise))
})
}
}
databrowser.actions.start()
</script>