-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.svelte
353 lines (320 loc) · 9.03 KB
/
admin.svelte
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<script>
// main admin screen - shows a login form if user is not logged in
import { tick, onMount } from 'svelte';
import * as utils from './utils';
import * as users from './users';
import ModalUpload from './modal_upload.svelte';
import { writable, derived } from 'svelte/store';
import { DeleteDoc, SetDoc, adminPageMapWatcher, adminFileMapWatcher, SortedListWatcher } from './database.js';
import { getStorage, ref, deleteObject } from 'firebase/storage';
/*
LATER
- make this a modal instead of a separate page
- make it easy for firesite apps just display a quick admin UI button in their header or whatever, and a logout button
- keyword search on images
- let the user rename from the original filename
- usedby - show all pages that have this ID in their text (we have all the pages loaded in memory so...)
- overwrite upload warns
*/
let loggingIn = false;
let userType = null; // null, 'anon', 'member', 'admin'
users.Get().then(u =>
{
loggingIn = false;
if (users.Is('admin'))
userType = 'admin';
else if (users.Is('member'))
userType = 'member';
else if (!users.IsLoggedIn())
userType = 'anon';
});
function Logout()
{
users.Logout()
userType = null;
}
let username = '';
let password = '';
let errMsg = '';
function Login()
{
errMsg = '';
loggingIn = true;
users.Login(username, password).then(() =>
{
loggingIn = true;
},
err =>
{
loggingIn = false;
});
}
$:if (userType == 'admin')
{
adminPageMapWatcher.StartDB();
adminFileMapWatcher.StartDB();
setTimeout(()=>
{
SelectTab(TAB_PAGES);
});
}
const [TAB_PAGES, TAB_FILES] = [0,1];
let tabs = ['Pages', 'Files'];
let curTab = writable(TAB_PAGES);
function SelectTab(n)
{
curRec = null;
$curTab = n;
$filterText = '';
filterEl.focus();
}
let filterEl;
let filterText = writable('');
let sortedPages = SortedListWatcher(adminPageMapWatcher, 'id');
let sortedFiles = SortedListWatcher(adminFileMapWatcher, 'orig');
let filteredRecords = derived([curTab, filterText, sortedPages, sortedFiles], ([$tab, $f, $pages, $files]) =>
{
// select the record set to use as a source based on which tab is selected
let $records;
if ($tab == TAB_PAGES)
$records = $pages;
else
$records = $files;
let ret = [];
let parts = $f.toLowerCase().split(' ');
for (let rec of $records)
{
let allMatch = true;
for (let part of parts)
{
if ($tab == TAB_PAGES)
{
let id = utils.PageIDToPath(rec.id).toLowerCase();
if (id.indexOf(part) == -1)
{
allMatch = false;
break;
}
}
else
{
let found = false;
if (rec.id.toLowerCase().indexOf(part) >= 0)
found = true;
else if (rec.orig.toLowerCase().indexOf(part) >= 0)
found = true;
if (!found)
{
allMatch = false;
break;
}
}
}
if (!allMatch)
continue;
ret.push(rec);
}
return ret;
});
let curRec = null;
function ShowDetails(page)
{
curRec = page;
}
const BAD_CHARS = ' :;`\'"\\=+?()*&^%$#@!<>,.';
let filterTextIsValidRecordName = derived([filterText], ([$f]) =>
{
let s = $f.trim().toLowerCase();
if (s.length == 0)
return false;
if (s.indexOf('__') != -1)
return false;
for (let bad of BAD_CHARS)
{
if (s.indexOf(bad) != -1)
return false;
}
return true;
});
$:preventCreate = !$filterTextIsValidRecordName || $adminPageMapWatcher[utils.PagePathToID($filterText)] != null;
function CreatePage()
{
// TODO: confirmation dialog
let docID = utils.PagePathToID($filterText);
let now = utils.Now();
let args = {created:now, lastmod:now, content:''}
SetDoc('Page', docID, args).then(res =>
{
args.id = docID;
ShowDetails(args);
});
}
// given a record, returns the string to display in the list view
function RecordDisplayName(rec)
{
if ($curTab == TAB_PAGES)
return utils.PageIDToPath(rec.id).substr(1);
else
return rec.orig;
}
function UploadNewFile(overwriteID=null)
{
OpenModal(ModalUpload, {overwriteID}).then(async res =>
{
if (res.closeCode != MODAL_OK) return;
let fileID = res.comp.uploadedFile.id;
$filterText = '';
await tick()
for (let rec of $filteredRecords)
{
if (rec.id == fileID)
{
console.log('yeah yeah', fileID, rec);
ShowDetails(rec);
break;
}
}
});
}
function DeleteFile()
{
OpenConfirmModal('Are you sure you want to delete this file? This action cannot be undone.', 'Confirm delete').then(res =>
{
if (res.closeCode != MODAL_OK) return;
let docID = curRec.id;
let storagePath = curRec.storagePath;
curRec = null;
console.log('Deleting', docID, storagePath);
OpenBusyModal('Deleting...');
deleteObject(ref(getStorage(), storagePath)).then(() => // first delete from storage
{ // now delete DB record
DeleteDoc('File', docID).then(() =>
{
CloseModal();
});
}).catch(err => {
CloseModal();
console.log('Storage deletion failed:', err);
OpenAlertModal('Failed to delete file. Please try again.');
});
});
}
</script>
<p>
{#if loggingIn || userType == null}
Loading...
{:else if userType != 'admin'}
<input placeholder="username" bind:value={username} />
<input placeholder="password" type="password" bind:value={password} />
<button disabled={username==''||password==''} on:click={Login}>Login</button>
<br/>{errMsg}
{:else if userType == 'admin'}
<tabs>
{#each tabs as tab,i}
<tab class:selected={$curTab==i} on:click="{()=>SelectTab(i)}">{tab}</tab>
{/each}
<filler/>
<button on:click={Logout}>Logout</button>
</tabs>
<records>
<searchbar>
<input type="text" placeholder="Start typing to filter" bind:value={$filterText} bind:this={filterEl} />
{#if $curTab == TAB_PAGES}
<button disabled={preventCreate} on:click={CreatePage}>Create page</button>
{:else if $curTab == TAB_FILES}
<button on:click={()=>UploadNewFile(null)}>Upload file</button>
{/if}
</searchbar>
<recordinfoarea>
<recordlist>
<ul>
{#each $filteredRecords as record}
<li class:selected={curRec && curRec.id==record.id} on:click="{()=>ShowDetails(record)}">{RecordDisplayName(record)}</li>
{/each}
</ul>
</recordlist>
<recorddetails>
{#if !curRec}
Select an item on the left to see details about it.
{:else if $curTab == TAB_PAGES}
<h4>{utils.PageIDToPath(curRec.id)}</h4>
<a href="{utils.PageIDToPath(curRec.id)}">[View]</a>
{:else if $curTab == TAB_FILES}
<b>ID: </b> {curRec.id} <b>Type: </b> {curRec.type}
{#if curRec.w && curRec.h}({curRec.w} x {curRec.h}){/if}
<b>Size: </b>{utils.FileSizeStr(curRec.size||0)}
<br/>
<button on:click={()=>UploadNewFile(curRec.id)}>Upload new version</button>
<button on:click={DeleteFile}>Delete this file</button>
<br/><b>Orig filename: </b> {curRec.orig} <a href="{curRec.url}" target="_blank">[link]</a>
<br/>
{#if curRec.type == 'image'}
<img class="preview" src="{curRec.url}" alt="curRec"/>
{/if}
{/if}
</recorddetails>
</recordinfoarea>
</records>
{/if}
</p>
<style>
tabs {
display:flex;
}
tab {
display:inline-block;
border-top:solid black 1px;
border-left:solid black 1px;
border-right:solid black 1px;
padding:5px;
width:100px;
cursor:pointer;
}
tab.selected {
font-weight:bold;
font-style:italic;
}
img.preview {
object-fit:contain;
width:600px;
height:400px;
}
records {
display:block;
margin-top:0;
border:solid black 1px;
padding:0.5em;
}
recordinfoarea {
border-top:solid black 1px;
margin-top:0.5em;
display:flex;
}
recordlist {
display:block;
overflow-x:hidden;
overflow-y:scroll;
width:35ch;
height:70vh;
}
recordlist ul {
margin-block-start:0;
margin-block-end:0;
padding-inline-start:0;
}
recordlist li {
white-space: nowrap;
list-style-type: none;
cursor:pointer;
}
recordlist li.selected {
font-weight:bold;
}
recordlist li:hover {
background-color: #ccc;
}
recorddetails {
display:block;
padding:1em;
}
</style>