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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
////////////////////////////////////////////////////////////////////////////////
class Component
{
constructor(element)
{
if (element instanceof Component)
element = element._element;
this._element = element;
}
inner()
{
return this._element;
}
destroy()
{
this._element.parentNode.removeChild(this._element);
}
tag(tag="div")
{
var element = document.createElement(tag);
this._element.appendChild(element);
return new Component(element);
}
text(value)
{
value = String(value);
this._element.innerHTML = (value != "") ? value : "";
return this;
}
id(value)
{
this._element.id = value;
return this;
}
classify(value)
{
var cur = this._element.className;
cur += cur ? " " : "";
cur += value;
this._element.className = cur;
return this;
}
css_var(key, value)
{
this._element.style.setProperty("--" + key, value);
return this;
}
attr(key, value)
{
this._element.setAttribute(key, value);
return this;
}
link(resource=undefined, query_params={})
{
if (resource != undefined)
{
var href = resource;
var sep = "?";
for (const key in query_params)
{
href += sep + key + "=" + query_params[key];
sep = "&";
}
}
else
href = "javascript:void(0);";
var text = this._element.innerHTML;
this._element.innerHTML = "";
this.tag("a").text(text).attr("href", href);
return this;
}
on(what, func)
{
const thunk = (src) => {
if (src.target != this._element)
return;
func(src.target);
src.stopPropagation();
};
this._element.addEventListener(what, thunk);
return this;
}
on_click(func)
{
this.classify("zen_action");
return this.on("click", func);
}
}
////////////////////////////////////////////////////////////////////////////////
class Cell extends Component
{
}
////////////////////////////////////////////////////////////////////////////////
class Table extends Component
{
constructor(parent, column_names, index_base=0)
{
var root = parent.tag().classify("zen_table");
super(root);
this._index = index_base;
if (index_base >= 0)
column_names = ["#", ...column_names];
this._num_columns = column_names.length;
root.css_var("zen_columns", this._num_columns);
this._num_columns = column_names.length;
this.add_row(column_names, false);
}
add_row(cells, indexed=true)
{
if (indexed && this._index >= 0)
cells = [this._index++, ...cells];
cells = cells.slice(0, this._num_columns);
while (cells.length < this._num_columns)
cells.push("");
var ret = [];
var row = this.tag().classify("zen_row");
for (const cell of cells)
{
var leaf = row.tag().classify("zen_cell").text(cell);
ret.push(new Cell(leaf));
}
var bias = (this._index >= 0) ? 1 : 0;
return ret.slice(bias);
}
clear(index=0)
{
const elem = this._element;
elem.replaceChildren(elem.firstElementChild);
this._index = index;
}
}
////////////////////////////////////////////////////////////////////////////////
class PropTable extends Table
{
constructor(parent)
{
super(parent, ["prop", "value"], -1);
}
add_property(key, value)
{
var ret = this.add_row([key, value]);
ret[0].classify("zen_prop_key");
ret[1].classify("zen_prop_value");
return ret;
}
}
////////////////////////////////////////////////////////////////////////////////
class Modal
{
constructor()
{
const body = new Component(document.body);
this._root = body.tag().classify("zen_modal");
const bg = this._root.tag().classify("zen_modal_bg");
bg.on("click", () => this._root.destroy());
const rect = this._root.tag();
this._title = rect.tag().classify("zen_modal_title")
this._content = rect.tag().classify("zen_modal_message")
this._buttons = rect.tag().classify("zen_modal_buttons");
}
title(value)
{
this._title.text(value);
return this;
}
message(value)
{
this._content.text(value);
return this;
}
option(name, func)
{
const thunk = (src) => {
this._root.destroy();
func(src);
};
this._buttons.tag().text(name).on("click", thunk);
return this;
}
}
////////////////////////////////////////////////////////////////////////////////
class Toolbar extends Component
{
static Side = class extends Component
{
add(name, tag="div") { return this.tag(tag).text(name); }
sep() { return this.tag().text("|").classify("zen_toolbar_sep"); }
}
constructor(parent, inline=false)
{
var root = parent.tag().classify("zen_toolbar");
super(root);
if (inline)
root.classify("zen_toolbar_inline");
this._left = new Toolbar.Side(root.tag())
this._right = new Toolbar.Side(root.tag())
}
left() { return this._left; }
right() { return this._right; }
}
////////////////////////////////////////////////////////////////////////////////
class Sectormatron extends Component
{
constructor(parent, depth=1)
{
super(parent);
this._depth = depth;
}
add_section(name)
{
var node = this.tag();
if (this._depth == 1)
node.classify("zen_sector");
node.tag("h" + this._depth).text(name);
return new Sectormatron(node, this._depth + 1);
}
}
////////////////////////////////////////////////////////////////////////////////
async function zen_fetch(resource, method="GET")
{
var response = await fetch(resource, {
"method" : method,
"headers" : { "Accept": "application/json" },
});
return await response.json();
}
////////////////////////////////////////////////////////////////////////////////
function zen_flatten(object, ret=null, prefix="")
{
if (ret == null)
ret = new Object();
for (const key in object)
{
const value = object[key];
if (value instanceof Object)
{
zen_flatten(value, ret, key + ".");
continue;
}
ret[prefix + key] = value;
}
return ret;
}
////////////////////////////////////////////////////////////////////////////////
class Page
{
constructor(parent, params)
{
this._parent = parent;
this._params = params;
this._sectormatron = new Sectormatron(parent);
}
set_title(name)
{
document.title = "zen - " + name;
}
get_param(name, fallback=undefined)
{
var ret = this._params.get(name);
return (ret != undefined) ? ret : fallback;
}
add_section(name)
{
return this._sectormatron.add_section(name);
}
}
////////////////////////////////////////////////////////////////////////////////
class Entry extends Page
{
async main()
{
this.set_title("oplog entry");
const project = this.get_param("project");
const oplog = this.get_param("oplog");
const key = this.get_param("key");
const uri = `/prj/${project}/oplog/${oplog}/entries?opkey=${key}`;
var entry = await zen_fetch(uri);
var text = JSON.stringify(entry, null, 2);
this._parent.tag("pre").text(text);
}
}
////////////////////////////////////////////////////////////////////////////////
class Oplog extends Page
{
constructor(...args)
{
super(...args);
this._index_start = this.get_param("start", 0);
this._index_count = this.get_param("start", 50);
this._entry_table = undefined;
}
async main()
{
this.set_title("oplog");
const project = this.get_param("project");
const oplog = this.get_param("oplog");
var section = this.add_section(project + " - " + oplog);
this._build_nav(section)
this._entry_table = new Table(section, ["key"]);
await this._build_table();
}
_build_nav(section)
{
var nav = new Toolbar(section);
nav.left().add("prev").on_click(() => this._on_next_prev(-1));
nav.left().add("next").on_click(() => this._on_next_prev( 1));
nav.left().sep();
for (var count of [10, 25, 50, 100])
{
var handler = (e) => this._on_change_count(e.innerHTML);
nav.left().add(count).on_click(handler);
}
nav.right().add("search:", "span");
nav.right().add("", "input").attr("disabled", "");
}
async _build_table()
{
const project = this.get_param("project");
const oplog = this.get_param("oplog");
var uri = `/prj/${project}/oplog/${oplog}/entries`;
uri += "?start=" + this._index_start;
uri += "&count=" + this._index_count;
var entries = zen_fetch(uri);
this._entry_table.clear(this._index_start);
for (const entry of (await entries)["entries"])
{
var cells = this._entry_table.add_row([
entry["key"]
]);
cells[0].link("", {
"page" : "entry",
"project" : project,
"oplog" : oplog,
"key" : entry["key"],
});
}
}
_on_change_count(value)
{
this._index_count = parseInt(value);
this._build_table();
}
_on_next_prev(direction)
{
const index = this._index_start + (this._index_count * direction);
this._index_start = Math.max(0, index);
this._build_table();
}
}
////////////////////////////////////////////////////////////////////////////////
class Project extends Page
{
async main()
{
this.set_title("project");
// info
var section = this.add_section("info");
const project = this.get_param("project");
const prefix = "/prj/" + project;
var info = await zen_fetch(prefix);
var prop_table = new PropTable(section);
for (const key in info)
{
if (key == "oplogs")
continue;
prop_table.add_property(key, info[key]);
}
// oplog
section = this.add_section("oplogs");
var oplog_table = new Table(section, ["name", "actions"])
var count = 0;
for (const oplog of info["oplogs"])
{
const name = oplog["id"];
var cells = oplog_table.add_row([
name,
]);
var cell = cells[0];
cell.link("", {
"page" : "oplog",
"project" : project,
"oplog" : name,
});
var action_tb = new Toolbar(cells.at(-1), true);
action_tb.left().add("drop").attr("zen_param", name).on_click((e) => this._on_drop(e));
}
// files
/*
section = this.add_section("files");
for (const oplog of info["oplogs"])
{
const name = oplog["id"];
var files = await zen_fetch(prefix + "/oplog/" + name + "/files");
if (files["files"].length == 0)
continue;
var sub_section = section.add_section(name);
var table = new Table(sub_section, [
"id",
"clientpath",
"serverpath",
]);
var count = 0;
for (const file of files["files"])
{
table.add_row([
file["id"],
file["clientpath"],
file["serverpath"],
]);
if (++count > 10)
break;
}
}
*/
}
drop()
{
alert("\\o/");
}
_on_drop(e)
{
new Modal()
.title("Confirmation")
.message(`Drop oplog '${e.getAttribute("zen_param")}'?`)
.option("Yes", () => this.drop())
.option("No", () => void(0))
}
}
////////////////////////////////////////////////////////////////////////////////
class Test extends Page
{
main()
{
var gen_word = (function() {
var s = 0x314251;
var r = function(a, b) {
s = (s * 0x493) & 0x7fffffff;
return ((s >> 3) % (b - a)) + a;
};
return function(a=5, b=10) {
const co = "aeioubcdfghjklmnpqrstvwxyz";
var ret = ""
for (var i = 0, n = r(a,b); i < n; ++i)
ret += co[r(0, co.length)];
return ret
};
})();
var gen_para = function(a=5, b=10, s=" ") {
var ret = gen_word(2, 9);
for (var i = 0; i < ((ret.length * 0x493) % (b - a)) + b; ++i)
ret += s + gen_word(2, 9);
return ret;
}
this.set_title("test");
// section
var section0 = this.add_section("section");
var section1 = section0.add_section("sub-section");
var section2 = section1.add_section("sub-sub-section");
// table
const cols = [gen_word(), gen_word(), gen_word(), gen_word()];
var tables = [
new Table(section0, cols),
new Table(section1, cols, 5),
new Table(section2, cols, -1),
];
for (const table of tables)
{
table.add_row([gen_word()]);
table.add_row([gen_word(), gen_word(), gen_word(), gen_word()]);
table.add_row([gen_word(), gen_word(), gen_para(15, 25), gen_word(), gen_word(), gen_word(), gen_word(), gen_word()]);
}
// prop-table
var pt_section = section0.add_section("prop-table")
var prop_table = new PropTable(pt_section);
for (var i = 0; i < 7; ++i)
prop_table.add_property(gen_word(), gen_para(1, 14, "/"));
// misc
const misc_section = section0.add_section("misc").add_section("misc");
misc_section.tag().text("just text");
misc_section.tag().text("this is a link").link();
misc_section.tag().text("MODAL DIALOG").on_click((e) => {
new Modal()
.title("modal")
.message("here is a message what I wrote")
.option("press me!", () => { alert("hi"); })
.option("cancel", () => void(0));
});
// toolbar
pt_section.add_section("toolbar");
var toolbar = new Toolbar(pt_section);
for (const side of [toolbar.left(), toolbar.right()])
{
side.add("tb_item0");
side.add("tb_item1");
side.sep();
side.add("tb_item2");
}
var cell = prop_table.add_property("toolbar", "");
toolbar = new Toolbar(cell[1], true);
toolbar.left().add("tbitem0");
toolbar.left().add("tbitem1");
toolbar.right().add("tbitem2");
toolbar.right().add("tbitem3");
// error
throw Error("deliberate error");
}
}
////////////////////////////////////////////////////////////////////////////////
class Start extends Page
{
async main()
{
this.set_title("main");
var section = this.add_section("projects");
// project list
var columns = [
"name",
"project_dir",
"engine_dir",
"actions",
];
var table = new Table(section, columns);
for (const project of await zen_fetch("/prj/list"))
{
var cells = table.add_row([
project.Id,
project.ProjectRootDir,
project.EngineRootDir,
]);
cells[0].link("", {"page" : "project", "project" : project.Id});
var action_tb = new Toolbar(cells.at(-1), true);
action_tb.left().add("view").on_click(() => void(0));
action_tb.left().add("drop").on_click(() => this.on_drop());
}
// stats
section = this.add_section("stats");
var providers = zen_fetch("/stats");
for (var provider of (await providers)["providers"])
{
var stats = zen_fetch("/stats/" + provider);
var section_provider = section.add_section(provider);
var table = new PropTable(section_provider);
stats = zen_flatten(await stats);
for (const key in stats)
table.add_property(key, stats[key]);
}
}
on_drop()
{
new Modal().title("Confirmation").message("TODO").option("Okay");
}
}
////////////////////////////////////////////////////////////////////////////////
function add_branding(parent)
{
var root = parent.tag().id("branding");
root.tag("pre").id("logo").text(
"_________ _______ __\n" +
"\\____ /___ ___ / ___// |__ ___ ______ ____\n" +
" / __/ __ \\ / \\ \\___ \\\\_ __// \\\\_ \\/ __ \\\n" +
" / \\ __// | \\/ \\| | ( - )| |\\/\\ __/\n" +
"/______/\\___/\\__|__/\\______/|__| \\___/ |__| \\___|"
);
root.tag("img").attr("src", "favicon.ico").id("ue_logo");
}
////////////////////////////////////////////////////////////////////////////////
async function main_guarded()
{
const root = new Component(document.body).tag().id("container").tag();
add_branding(root);
const params = new URLSearchParams(window.location.search);
const page = params.get("page");
var impl = undefined;
if (page == "project") impl = new Project(root, params);
if (page == "oplog") impl = new Oplog(root, params);
if (page == "entry") impl = new Entry(root, params);
if (page == "test") impl = new Test(root, params);
if (page == undefined) impl = new Start(root, params);
if (impl == undefined)
{
root.tag().text("unknown page");
return;
}
return impl.main();
}
////////////////////////////////////////////////////////////////////////////////
async function main()
{
try
{
return await main_guarded();
}
catch (e)
{
var pane = new Component(document.body).tag().id("error");
pane.tag("pre").text(e);
pane.tag("pre").text(e.stack);
throw e;
}
}
/*
_________ _______ __
\____ /___ ___ / ___// |__ ___ ______ ____
/ __/ __ \ / \ \___ \\_ __// \\_ \/ __ \
/ \ __// | \/ \| | ( - )| |\/\ __/
/______/\___/\__|__/\______/|__| \___/ |__| \___|
*/
|