-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
2653 lines (2544 loc) · 91.9 KB
/
bundle.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
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
// shim for using process in browser
var process = module.exports = {};
// cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.
var cachedSetTimeout;
var cachedClearTimeout;
function defaultSetTimout() {
throw new Error('setTimeout has not been defined');
}
function defaultClearTimeout () {
throw new Error('clearTimeout has not been defined');
}
(function () {
try {
if (typeof setTimeout === 'function') {
cachedSetTimeout = setTimeout;
} else {
cachedSetTimeout = defaultSetTimout;
}
} catch (e) {
cachedSetTimeout = defaultSetTimout;
}
try {
if (typeof clearTimeout === 'function') {
cachedClearTimeout = clearTimeout;
} else {
cachedClearTimeout = defaultClearTimeout;
}
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
} ())
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
//normal enviroments in sane situations
return setTimeout(fun, 0);
}
// if setTimeout wasn't available but was latter defined
if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch(e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedSetTimeout.call(null, fun, 0);
} catch(e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
//normal enviroments in sane situations
return clearTimeout(marker);
}
// if clearTimeout wasn't available but was latter defined
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch (e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedClearTimeout.call(null, marker);
} catch (e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = runTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) { return [] }
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
},{}],2:[function(require,module,exports){
(function (process,__filename){(function (){
// ----------------------------------------
// MODULE STATE & ID
var count = 0
const [cwd, dir] = [process.cwd(), __filename].map(x => new URL(x, 'file://').href)
const ID = dir.slice(cwd.length)
const STATE = { ids: {}, net: {} } // all state of component module
// ----------------------------------------
const sheet = new CSSStyleSheet()
sheet.replaceSync(get_theme())
const shopts = { mode: 'closed' }
// ----------------------------------------
module.exports = chat_input
// ----------------------------------------
async function chat_input (opts, protocol) {
// ----------------------------------------
// ID + JSON STATE
// ----------------------------------------
const id = `${ID}:${count++}` // assigns their own name
const status = {}
const state = STATE.ids[id] = { id, status, wait: {}, net: {}, aka: {} } // all state of component instance
state.shift_status = true
state.textmode = "msg"
state.username = opts.host
// ----------------------------------------
// PROTOCOL
// ----------------------------------------
const on = {
activate_input
}
const channel_up = use_protocol('up')({ protocol, state, on })
// ----------------------------------------
// TEMPLATE
// ----------------------------------------
const el = document.createElement('div');
const shadow = el.attachShadow(shopts)
shadow.adoptedStyleSheets = [sheet]
shadow.innerHTML = `
<div class="crud">
<div class="btn_wrapper">
<div class="popup" tabindex='0'>
<div class="noblur">📥 Input </div>
<div class="noblur">📤 Output </div>
<div class="noblur">📭 Task </div>
</div>
<button class="add noblur">
+
</button>
</div>
<button class="join noblur">
join
</button>
<button class="export noblur">
export
</button>
<div class="box">
<div class="overlay">
<div>/🗃list <span>📌</span></div>
<div>/📩text <span>📌</span></div>
<div>/🆕🔳task <span>📌</span></div>
<div>/🔳subtask <span>📌</span></div>
<div>/📨invite <span>📌</span></div>
<div>/📥input <span>📌</span></div>
<div>/📤output <span>📌</span></div>
<div>/😀emoji <span>📌</span></div>
<div>/📎attach file <span>📌</span></div>
</div>
<textarea class="noblur" placeholder="Enter a command"></textarea>
</div>
<div class="send">></div>
</div>`
// ----------------------------------------
const btn_add = shadow.querySelector('.add')
const btn_join = shadow.querySelector('.join')
const btn_export = shadow.querySelector('.export')
const btn_send = shadow.querySelector('.send')
const textarea = shadow.querySelector('textarea')
const popup = shadow.querySelector('.popup')
const overlay = shadow.querySelector('.overlay')
// ----------------------------------------
btn_add.onclick = () => popup.focus()
btn_join.onclick = handle_join
btn_export.onclick = handle_export
btn_send.onclick = handle_send
textarea.onkeyup = handle_keyup
textarea.onkeydown = handle_keydown
for (const child of popup.children){
child.onclick = handle_add
}
// ----------------------------------------
// INIT
// ----------------------------------------
return el
/******************************************
Event handlers
******************************************/
async function handle_export () {
const channel = state.net[state.aka.task_explorer]
channel.send({
head: [id, channel.send.id, channel.mid++],
type: 'handle_export',
})
}
async function handle_send () {
if(textarea.disabled)
return
if(state.textmode === "msg")
on_tx()
else
join()
}
async function handle_keydown (e) {
if(state.shift_status)
switch (e.key){
case 'Enter':
e.preventDefault()
if(state.textmode === "msg")
on_tx()
else
join()
break
case 'Shift':
state.shift_status = false
}
}
async function handle_add (e) {
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'send',
data: {to: 'task_explorer', route: ['task_explorer'], type: 'handle_add', data: e.target.innerHTML}
})
}
async function handle_keyup (e) {
e.target.style.height = "1px";
e.target.style.height = (2+e.target.scrollHeight)+"px";
if(e.key === 'Shift')
state.shift_status = true
if(textarea.value === '/'){
overlay.classList.add('show')
textarea.addEventListener('blur', handle_blur)
textarea.addEventListener('focus', handle_blur)
}
else{
overlay.classList.remove('show')
textarea.removeEventListener('blur', handle_blur)
textarea.removeEventListener('focus', handle_blur)
}
}
async function handle_blur () {
overlay.classList.toggle('show')
}
async function handle_join () {
textarea.disabled = false
textarea.placeholder = "Enter a invite code"
state.textmode = 'join'
}
/******************************************
Communication
******************************************/
async function on_tx () {
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'on_tx',
data: {content: textarea.value.replaceAll('\n', '<br>'), username: state.username}
})
textarea.value = ''
}
async function join () {
const [user, task_id] = textarea.value.split('-')
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'send',
data: {to: 'task_explorer', route: ['task_explorer'], users: [user], type: 'handle_invite', data: {sender: opts.host, task_id}}
})
}
async function activate_input ({ data }) {
textarea.disabled = false
data ? textarea.placeholder = data : ''
}
}
function get_theme () {
return `
*{
box-sizing: border-box;
}
.crud{
display: flex;
gap: 10px;
justify-content: space-between;
align-items: center;
padding: 10px;
border: 1px solid gray;
}
.box{
position: relative;
margin: 40px 20px;
}
.box > textarea{
height: 40px;
min-height: 40px;
padding: 10px;
width: 100%;
}
.box > .overlay{
display: none;
position: absolute;
background-color: #222;
box-shadow: 0 0 2px 1px rgb(255, 255, 255);
width: 100%;
bottom: 50px;
}
.box > .overlay.show{
display: block;
}
.box > .overlay > div{
width: 100%;
display: flex;
justify-content: space-between;
padding: 4px 10px;
}
.box > .overlay > div:hover{
background-color: #555;
}
textarea::-webkit-scrollbar{
display: none;
}
.btn_wrapper{
position: relative;
}
.btn_wrapper .popup{
height: 0;
position: absolute;
bottom: 100%;
background: black;
cursor: pointer;
white-space: nowrap;
overflow: hidden;
}
.btn_wrapper .popup:focus{
height: auto;
padding: 5px;
border: 1px solid gray;
}
.send{
padding: 7px 10px;
background-color: black;
position: absolute;
right: 40px;
width: 30px;
height: 30px;
cursor: pointer;
border-radius: 4px;
}
`
}
function use_protocol (petname) {
return ({ protocol, state, on = { } }) => {
if (petname in state.aka) throw new Error('petname already initialized')
const { id } = state
const invalid = on[''] || (message => console.error('invalid type', message))
if (protocol) return handshake(protocol(Object.assign(listen, { id })))
else return handshake
// ----------------------------------------
// @TODO: how to disjoin channel
// ----------------------------------------
function handshake (send) {
state.aka[petname] = send.id
const channel = state.net[send.id] = { petname, mid: 0, send, on }
return protocol ? channel : Object.assign(listen, { id })
}
function listen (message) {
const [from] = message.head
const by = state.aka[petname]
if (from !== by) return invalid(message) // @TODO: maybe forward
console.log(`[${id}]:${petname}>`, message)
const { on } = state.net[by]
const action = on[message.type] || invalid
action(message)
}
}
}
}).call(this)}).call(this,require('_process'),"/src/node_modules/chat_input/chat_input.js")
},{"_process":1}],3:[function(require,module,exports){
(function (process,__filename){(function (){
const taskdb = require('taskdb')
// MODULE STATE & ID
var count = 0
const [cwd, dir] = [process.cwd(), __filename].map(x => new URL(x, 'file://').href)
const ID = dir.slice(cwd.length)
const STATE = { ids: {}, net: {} } // all state of component module
// ----------------------------------------
const sheet = new CSSStyleSheet()
sheet.replaceSync(get_theme())
const shopts = { mode: 'closed' }
// ----------------------------------------
module.exports = task_explorer
// ----------------------------------------
function task_explorer (opts, protocol) {
// ----------------------------------------
// ID + JSON STATE
// ----------------------------------------
const id = `${ID}:${count++}` // assigns their own name
const status = {}
const state = STATE.ids[id] = { id, status, wait: {}, net: {}, aka: {} } // all state of component instance
const { host } = opts
state.name = 'task_explorer'
state.code_words = {inputs: 'io', outputs: 'io', tasks: 'task'}
state.add_words = {tasks: 'sub'}
// ----------------------------------------
// PROTOCOL
// ----------------------------------------
const on = {
'on_add_node': on_add_node,
'handle_add': handle_add,
'send': send,
'save_msg': save_msg,
'handle_invite': handle_invite,
'on_invite': on_invite,
'handle_export': handle_export
}
const on_add = {
'io': add_node_io,
'link': add_node_link,
'tasks': add_node_sub,
}
const channel_up = use_protocol('up')({ protocol, state, on })
// ----------------------------------------
// TEMPLATE
// ----------------------------------------
const el = document.createElement('div');
const shadow = el.attachShadow(shopts)
shadow.adoptedStyleSheets = [sheet]
shadow.innerHTML = `
<div class="box">
<main>
</main>
<div class="popup" tabindex="0">
<div>Edit</div>
<div>Drop</div>
</div>
</div>
`
// ----------------------------------------
const tree_el = shadow.querySelector('main')
const popup = shadow.querySelector('.popup')
// ----------------------------------------
// ELEMENTS
// ----------------------------------------
{
const on = {
'set': set
}
const protocol = use_protocol('taskdb')({ state, on })
const opts = { host }
taskdb(opts, protocol)
async function set ({ data }) {
state.query_result = data
}
}
// ----------------------------------------
// INIT
// ----------------------------------------
let json_data
const channel = state.net[state.aka.taskdb]
channel.send({
head: [id, channel.send.id, channel.mid++],
type: 'get',
data: '/'
})
if(state.query_result){
json_data = state.query_result
fill_tree_el()
}
else
fetch("./data_"+host+".json")
.then(res => res.json())
.then(data => {
json_data = data
fill_tree_el()
channel.send({
head: [id, channel.send.id, channel.mid++],
type: 'set',
data: json_data
})
}
)
return el
async function fill_tree_el () {
const root_nodes = json_data.filter(data => data.root)
const length = root_nodes.length - 1
tree_el.append(...root_nodes.map((data, i) => add_node_root({ data, last: i === length })))
}
function create_node (type, id) {
const element = document.createElement('div')
element.classList.add(type, 'node')
element.tabIndex = '0'
element.id = 'a'+id
return element
}
function html_template (data, last, space, grand_last){
const element = create_node(data.type, data.id)
if(data.root)
space = ''
else
space += grand_last ? '  ' : '│  '
element.dataset.space = space
element.dataset.grand_last = last ? 'a' : ''
return [element, last, space]
}
/******************************************
Addition Operation
******************************************/
function add_node_el ({ data, parent, space, grand_last, type }){
const is_single = parent.children.length ? false : true
if(data.root){
parent.prepend(add_node_root({ data, last: false}))
return
}
//hub or sub node check
if(type === 'inputs')
parent.append(on_add[type]({ data, space, grand_last, first: is_single}))
else
parent.prepend(on_add[type]({ data, space, grand_last, last: is_single}))
}
function add_node_root ({ data, last }) {
[ element, last, space ] = html_template(data, last)
element.innerHTML = `
<div class="details">
${last ? '└' : '├'}<span class="tas">📓─</span>${data.name}<span class="last">...</span>
</div>
<div class="tasks nodes">
</div>
`
const sub_emo = element.querySelector('.details > .tas')
const last_el = element.querySelector('.details > .last')
const tasks = element.querySelector('.tasks')
let is_on
sub_emo.onclick = sub_click
element.onfocus = handle_focus
last_el.onclick = handle_popup
return element
function sub_click () {
sub_emo.innerHTML = is_on ? '📓─' : '📖┬'
is_on = handle_click({ el: tasks, type: 'tasks', data: data.sub, space, is_on, grand_last: last, pos: false })
}
}
function add_node_sub ({ data, last, grand_last, space }) {
[ element, last, space ] = html_template(data, last, space, grand_last)
element.innerHTML = `
<div class="hub nodes">
</div>
<div class="inputs nodes">
</div>
<div class="details">
${space}${last ? '└' : '├'}<span class="hub_emo">📪</span><span class="tas">─📪</span><span class="inp">🗃</span><span class="out">─🗃</span><span class="name">${data.name}</span><span class="last">...</span>
</div>
<div class="outputs nodes">
</div>
<div class="tasks nodes">
</div>
`
const details = element.querySelector('.details > .name')
const hub_emo = element.querySelector('.details > .hub_emo')
const sub_emo = element.querySelector('.details > .tas')
const inp = element.querySelector('.details > .inp')
const out = element.querySelector('.details > .out')
const last_el = element.querySelector('.details > .last')
// const after = element.querySelector('.details > .after')
const hub = element.querySelector('.hub')
const outputs = element.querySelector('.outputs')
const inputs = element.querySelector('.inputs')
const tasks = element.querySelector('.tasks')
let hub_on, sub_on, inp_on, out_on
hub_emo.onclick = hub_click
sub_emo.onclick = sub_click
inp.onclick = inp_click
out.onclick = out_click
element.onfocus = handle_focus
details.onclick = open_chat
last_el.onclick = handle_popup
// after.onclick = () => {
// alert(host+'-'+data.id)
// try{
// navigator.clipboard.writeText(host+'-'+data.id)
// }
// catch{
// }
// }
return element
function hub_click () {
if(hub_on){
hub_emo.innerHTML = '📪'
sub_on ? sub_emo.innerHTML = '┬'+sub_emo.innerHTML.slice(1) : sub_emo.innerHTML = '─'+sub_emo.innerHTML.slice(1)
} else{
hub_emo.innerHTML = '📭'
sub_on ? sub_emo.innerHTML = '┼'+sub_emo.innerHTML.slice(1) : sub_emo.innerHTML = '┴'+sub_emo.innerHTML.slice(1)
}
hub_on = handle_click({ el: hub, type: 'link', data: data.hub, space, is_on: hub_on, pos: true })
}
function sub_click () {
if(sub_on){
hub_on ? sub_emo.innerHTML = '┴📪' : sub_emo.innerHTML = '─📪'
} else{
hub_on ? sub_emo.innerHTML = '┼📭' : sub_emo.innerHTML = '┬📭'
}
sub_on = handle_click({ el: tasks, type: 'tasks', data: data.sub, space, is_on: sub_on, grand_last: last, pos: false })
}
function inp_click () {
if(inp_on){
inp.innerHTML = '🗃'
out_on ? out.innerHTML = '┬'+out.innerHTML.slice(1) : out.innerHTML = '─'+out.innerHTML.slice(1)
} else{
inp.innerHTML = '🗂'
out_on ? out.innerHTML = '┼'+out.innerHTML.slice(1) : out.innerHTML = '┴'+out.innerHTML.slice(1)
}
inp_on = handle_click({ el: inputs, type: 'io', data: data.inputs, space, is_on: inp_on, pos: true })
}
function out_click () {
if(out_on){
inp_on ? out.innerHTML = '┴🗃' : out.innerHTML = '─🗃'
} else{
inp_on ? out.innerHTML = '┼🗂' : out.innerHTML = '┬🗂'
}
out_on = handle_click({ el: outputs, type: 'io', data: data.outputs, space, is_on: out_open, pos: false })
}
}
function add_node_io ({ data, first, last, space }) {
const element = create_node(data.type, data.id)
const grand_space = space + '│  │   '
space += '│     '
element.innerHTML = `
<div class="details">
<span class="space">${space}</span><span class="grand_space">${grand_space}</span>${first ? '┌' : last ? '└' : '├'}</span><span class="btn">📥─</span>${data.name}<span class="after">🔗</span>
</div>
<div class="tasks nodes">
</div>
`
const btn = element.querySelector('.details > .btn')
const tasks = element.querySelector('.tasks')
btn.onclick = () => handle_click({ el: tasks, type: 'link', data: data.sub, space, pos: false })
return element
}
function add_node_link ({ data, first, last, space }) {
const element = document.createElement('div')
element.classList.add('next', 'node')
element.dataset.id = data.id
space += '│  '
element.innerHTML = `
<div class="details">
${space}${last ? '└' : first ? '┌' : '├'} ${data.name}
</div>`
element.onclick = jump
return element
}
async function add_node_data (name, type, parent_id, users, author){
const node_id = json_data.length
json_data.push({ id: node_id, name, type: state.code_words[type], room: {}, users })
if(parent_id){
save_msg({
head: [id],
type: 'save_msg',
data: {username: 'system', content: author + ' added ' + type.slice(0,-1)+': '+name, chat_id: parent_id}
})
//Add a message in the chat
if(state.chat_task && parent_id === state.chat_task.id.slice(1))
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'render_msg',
data: {username: 'system', content: author+' added '+type.slice(0,-1)+': '+name}
})
const sub_nodes = json_data[parent_id][state.add_words[type]]
sub_nodes ? sub_nodes.push(node_id) : json_data[parent_id][state.add_words[type]] = [node_id]
}
else{
json_data[node_id].root = true
json_data[node_id].users = [opts.host]
}
save_msg({
head: [id],
type: 'save_msg',
data: {username: 'system', content: author + ' created ' + type.slice(0,-1)+': '+name, chat_id: node_id}
})
const channel = state.net[state.aka.taskdb]
channel.send({
head: [id, channel.send.id, channel.mid++],
type: 'set',
data: json_data
})
}
async function on_add_node (data) {
const node = data.id ? shadow.querySelector('#a' + data.id + ' > .'+data.type) : tree_el
node && node.children.length && add_node_el({ data: { name: data.name, id: json_data.length, type: state.code_words[data.type] }, parent: node, grand_last: data.grand_last, type: data.type, space: data.space })
add_node_data(data.name, data.type, data.id, data.users, data.user)
}
/******************************************
Event handlers
******************************************/
function handle_focus (e) {
state.xtask = e.target
state.xtask.classList.add('focus')
state.xtask.addEventListener('blur', e => {
if(e.relatedTarget && e.relatedTarget.classList.contains('noblur'))
return
state.xtask.classList.remove('focus')
state.xtask = undefined
}, { once: true })
}
function handle_popup (e) {
const el = e.target
el.classList.add('show')
popup.style.top = el.offsetTop - 20 + 'px'
popup.style.left = el.offsetLeft - 56 + 'px'
popup.focus()
popup.addEventListener('blur', () => {
el.classList.remove('show')
}, { once: true })
}
function handle_click ({ el, type, data, space, is_on, grand_last, pos }) {
el.classList.toggle('show')
if(data && el.children.length < 1){
length = data.length - 1
data.forEach((value, i) => el.append(on_add[type]({ data: json_data[value], first: pos ? 0 === i : false, last: pos ? false : length === i, space, grand_last })))
}
return !is_on
}
async function handle_export () {
const data = await traverse( state.xtask.id.slice(1) )
const json_string = JSON.stringify(data, null, 2);
const blob = new Blob([json_string], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'data.json';
link.click();
}
async function handle_add (data) {
data = data.slice(2).trim().toLowerCase() + 's'
const input = document.createElement('input')
let node, task_id, space = '', grand_last = true, root = true
//expand other siblings
if(state.xtask){
node = state.xtask.querySelector('.' + data)
task_id = state.xtask.id.slice(1)
const before = state.xtask.querySelector('.' + data.slice(0,3))
before.dispatchEvent(new MouseEvent('click', {bubbles:true, cancelable: true, view: window}))
node.classList.add('show')
grand_last = state.xtask.dataset.grand_last
space = state.xtask.dataset.space
state.xtask.classList.remove('focus')
state.xtask = undefined
root = false
}
else{
node = tree_el
task_id = ''
}
node.prepend(input)
input.onkeydown = async (event) => {
if (event.key === 'Enter') {
input.blur()
add_node_el({ data : { name: input.value, id: json_data.length, type: state.code_words[data], root }, space, grand_last, type: data, parent: node })
const users = task_id ? json_data[task_id].users : [host]
add_node_data(input.value, data, task_id, users, host)
//sync with other users
if(users.length > 1)
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'send',
data: {to: 'task_explorer', route: ['up', 'task_explorer'], users: json_data[task_id].users.filter(user => user !== host), type: 'on_add_node', data: {name: input.value, id: task_id, type: data, users, grand_last, space, user: host} }
})
}
}
input.focus()
input.onblur = () => input.remove()
}
/******************************************
Tree traversal
******************************************/
async function jump (e){
let target_id = e.currentTarget.dataset.id
const el = tree_el.querySelector('#a'+target_id)
if(el)
el.focus()
else{
const path = []
for(let temp = json_data[target_id]; !temp.root; temp = json_data[temp.parent])
path.push(temp.id)
temp = tree_el.querySelector('#a'+temp.id)
target_id = 'a'+target_id
while(temp.id !== target_id){
const before = temp.querySelector('.before')
before.dispatchEvent(new MouseEvent('click', {bubbles:true, cancelable: true, view: window}))
temp.classList.add('show')
temp = temp.querySelector('#a'+path.pop())
}
temp.focus()
}
}
async function traverse (id) {
state.result = []
state.track = []
recurse(id)
return state.result
}
function recurse (id){
if(state.track.includes(id))
return
state.result.push(json_data[id])
state.track.push(id)
for(temp = 0; json_data[id].tasks && temp < json_data[id].tasks.length; temp++)
recurse(json_data[id].tasks[temp])
for(temp = 0; json_data[id].inputs && temp < json_data[id].inputs.length; temp++)
recurse(json_data[id].inputs[temp])
for(temp = 0; json_data[id].outputs && temp < json_data[id].outputs.length; temp++)
recurse(json_data[id].outputs[temp])
}
/******************************************
Communication
******************************************/
async function open_chat () {
const node = json_data[Number(state.xtask.id.slice(1))]
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'open_chat',
data: node
})
if(state.chat_task)
state.chat_task.classList.remove('chat_active')
state.chat_task = state.xtask
state.chat_task.classList.add('chat_active')
}
async function save_msg (msg) {
const {data} = msg
msg.data = data.content,
msg.meta = {
date: new Date().getTime()
}
msg.refs = ''
const node = json_data[Number(data.chat_id)]
const username = data.username === host ? '' : data.username
node.room[username] ? node.room[username].push(msg) : node.room[username] = [msg]
const channel = state.net[state.aka.taskdb]
channel.send({
head: [id, channel.send.id, channel.mid++],
type: 'set',
data: json_data
})
}
async function handle_invite ({ sender, task_id }) {
const node = json_data[Number(task_id)]
node.users.push(sender)
channel_up.send({
head: [id, channel_up.send.id, channel_up.mid++],
type: 'send',
data: {to: 'task_explorer', route: ['up', 'task_explorer'], users: [sender], type: 'on_invite', data: node }
})
}
async function on_invite (data) {
const {name, id, type} = data
tree_el.prepend(add_node_sub({ name, id, type }))
json_data.push(data)
}
async function send ({ data }) {
const {to, route} = data
if(to === state.name){
const {type, data: shuttle_data} = data
on[type](shuttle_data)
return
}
const channel = state.net[state.aka[route[0]]]
data.route = data.route.slice(1)
channel.send({
head: [id, channel.send.id, channel.mid++],
type: 'send',
data
})
}
}
function get_theme () {
return `
.box{
position: relative;
}
main{
max-height: 300px;
overflow: scroll;
max-width: 5px;
min-width: 100%;
}
main > .task{
position: relative;
min-width: fit-content;
}
.task{
cursor: pointer;
margin: 5px 0;