-
Notifications
You must be signed in to change notification settings - Fork 2
/
Wikibinator203DragAndDropTree.html
8331 lines (7297 loc) · 364 KB
/
Wikibinator203DragAndDropTree.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
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
<!DOCTYPE html><html><head>
<meta charset="UTF-8">
<script src="Wikibinator203VM.js" charset="UTF-8"></script>
<script>
//https://raw.githubusercontent.com/benrayfield/jsutils/master/src/sha256.js
var sha256 = function(bytesIn){
//var t = typeof bytesIn;
//if(t != 'Uint8Array') throw 'Expected Uint8Array but got a '+t; //this check wont work because its like a map of index to byte
var chunks = Math.floor((bytesIn.byteLength+9+63)/64); //512 bit each
//Copy bytesIn[] into b[], then pad bit1, then pad bit0s,
//then append int64 bit length, finishing the last block of 512 bits.
//byte b[] = new byte[chunks*64];
var b = new Uint8Array(chunks*64);
//System.arraycopy(bytesIn, 0, b, 0, bytesIn.byteLength);
b.set(bytesIn, 0);
b[bytesIn.byteLength] = 0x80;
//long bitLenTemp = bytesIn.byteLength*8;
var bitLenTemp = bytesIn.byteLength*8; //in js, this has float64 precision, which is more than enough for Uint8Array size
for(var i=7; i>=0; i--){
b[b.byteLength-8+i] = bitLenTemp&0xff;
bitLenTemp >>>= 8;
}
//log('b as hex = '+bitfuncs.uint8ArrayToHex(b));
var a = new Uint32Array(136);
//"first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311"
a[0]=0x428a2f98;
a[1]=0x71374491;
a[2]=0xb5c0fbcf;
a[3]=0xe9b5dba5;
a[4]=0x3956c25b;
a[5]=0x59f111f1;
a[6]=0x923f82a4;
a[7]=0xab1c5ed5;
a[8]=0xd807aa98;
a[9]=0x12835b01;
a[10]=0x243185be;
a[11]=0x550c7dc3;
a[12]=0x72be5d74;
a[13]=0x80deb1fe;
a[14]=0x9bdc06a7;
a[15]=0xc19bf174;
a[16]=0xe49b69c1;
a[17]=0xefbe4786;
a[18]=0x0fc19dc6;
a[19]=0x240ca1cc;
a[20]=0x2de92c6f;
a[21]=0x4a7484aa;
a[22]=0x5cb0a9dc;
a[23]=0x76f988da;
a[24]=0x983e5152;
a[25]=0xa831c66d;
a[26]=0xb00327c8;
a[27]=0xbf597fc7;
a[28]=0xc6e00bf3;
a[29]=0xd5a79147;
a[30]=0x06ca6351;
a[31]=0x14292967;
a[32]=0x27b70a85;
a[33]=0x2e1b2138;
a[34]=0x4d2c6dfc;
a[35]=0x53380d13;
a[36]=0x650a7354;
a[37]=0x766a0abb;
a[38]=0x81c2c92e;
a[39]=0x92722c85;
a[40]=0xa2bfe8a1;
a[41]=0xa81a664b;
a[42]=0xc24b8b70;
a[43]=0xc76c51a3;
a[44]=0xd192e819;
a[45]=0xd6990624;
a[46]=0xf40e3585;
a[47]=0x106aa070;
a[48]=0x19a4c116;
a[49]=0x1e376c08;
a[50]=0x2748774c;
a[51]=0x34b0bcb5;
a[52]=0x391c0cb3;
a[53]=0x4ed8aa4a;
a[54]=0x5b9cca4f;
a[55]=0x682e6ff3;
a[56]=0x748f82ee;
a[57]=0x78a5636f;
a[58]=0x84c87814;
a[59]=0x8cc70208;
a[60]=0x90befffa;
a[61]=0xa4506ceb;
a[62]=0xbef9a3f7;
a[63]=0xc67178f2;
//h0-h7 "first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19"
a[64]=0x6a09e667;
a[65]=0xbb67ae85;
a[66]=0x3c6ef372;
a[67]=0xa54ff53a;
a[68]=0x510e527f;
a[69]=0x9b05688c;
a[70]=0x1f83d9ab;
a[71]=0x5be0cd19;
//a[72..135] are the size 64 w array of ints
for(var chunk=0; chunk<chunks; chunk++){
var bOffset = chunk<<6;
//copy chunk into first 16 words w[0..15] of the message schedule array
for(var i=0; i<16; i++){
//Get 4 bytes from b[]
var o = bOffset+(i<<2);
a[72+i] = ((b[o]&0xff)<<24) | ((b[o+1]&0xff)<<16) | ((b[o+2]&0xff)<<8) | (b[o+3]&0xff);
}
//Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
for(var i=16; i<64; i++){
//s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
//s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)
//w[i] := w[i-16] + s0 + w[i-7] + s1
var wim15 = a[72+i-15];
var s0 = ((wim15>>>7)|(wim15<<25)) ^ ((wim15>>>18)|(wim15<<14)) ^ (wim15>>>3);
var wim2 = a[72+i-2];
var s1 = ((wim2>>>17)|(wim2<<15)) ^ ((wim2>>>19)|(wim2<<13)) ^ (wim2>>>10);
a[72+i] = a[72+i-16] + s0 + a[72+i-7] + s1;
}
var A = a[64];
var B = a[65];
var C = a[66];
var D = a[67];
var E = a[68];
var F = a[69];
var G = a[70];
var H = a[71];
for(var i=0; i<64; i++){
/* S1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
ch := (e and f) xor ((not e) and g)
temp1 := h + S1 + ch + k[i] + w[i]
S0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22)
maj := (a and b) xor (a and c) xor (b and c)
temp2 := S0 + maj
h := g
g := f
f := e
e := d + temp1
d := c
c := b
b := a
a := temp1 + temp2
*/
var s1 = ((E>>>6)|(E<<26)) ^ ((E>>>11)|(E<<21)) ^ ((E>>>25)|(E<<7));
var ch = (E&F) ^ ((~E)&G);
var temp1 = H + s1 + ch + a[i] + a[72+i];
var s0 = ((A>>>2)|(A<<30)) ^ ((A>>>13)|(A<<19)) ^ ((A>>>22)|(A<<10));
var maj = (A&B) ^ (A&C) ^ (B&C);
var temp2 = s0 + maj;
H = G;
G = F;
F = E;
E = D + temp1;
D = C;
C = B;
B = A;
A = temp1 + temp2;
}
a[64] += A;
a[65] += B;
a[66] += C;
a[67] += D;
a[68] += E;
a[69] += F;
a[70] += G;
a[71] += H;
}
//RETURN h0..h7 = a[64..71]
//byte ret[] = new byte[32];
var ret = new Uint8Array(32);
for(var i=0; i<8; i++){
var ah = a[64+i];
ret[i*4] = (ah>>>24)&0xff;
ret[i*4+1] = (ah>>>16)&0xff;
ret[i*4+2] = (ah>>>8)&0xff;
ret[i*4+3] = ah&0xff;
}
return ret;
};
</script>
<script>
//https://github.com/benrayfield/jsutils/blob/master/src/unicode.js
var unicode = {
//TODO consider using TextEncoder instead, which is a web standard in progress and works in at least firefox chrome and 1 other
utf8TextEncoder: new TextEncoder('utf-8'),
utf8TextDecoder: new TextDecoder('utf-8'),
stringToUtf8AsUint8Array: function(s){
//log('unicode.utf8TextEncoder='+unicode.utf8TextEncoder+' '+mapToString(unicode.utf8TextEncoder));
//log('param of encode: '+s);
var u = unicode.utf8TextEncoder.encode(s);
//var t = typeof u;
//if(t != 'Uint8Array') throw 'Expected TextEncoder.encode(string) to return Uint8Array but got a '+t+': '+u+': '+mapToString(u);
return u;
},
utf8AsUint8ArrayToString: function(bytes){
return unicode.utf8TextDecoder.decode(bytes);
},
asBytes: function(stringOrBytes){
var t = typeof stringOrBytes;
if(t == 'Uint8Array') return stringOrBytes;
if(t == 'string') return unicode.stringToUtf8AsUint8Array(stringOrBytes);
throw 'Unknown type: '+t;
},
asString: function(stringOrBytes){
var t = typeof stringOrBytes;
if(t == 'string') return stringOrBytes;
if(t == 'Uint8Array') return unicode.utf8AsUint8ArrayToString(stringOrBytes);
throw 'Unknown type: '+t;
}
};
</script>
<script>
//TODO move window out of that lambda then... const Wikibinator203DragAndDropTreeUI = (()=>{
//This script is under the Wikibinator203 license.
//drag and drop tree UI for viewing and calling Wikibinator203 fns/lambdas...
'use strict';
const U = Wikibinator203; //the universal function
if(U(U(U))(U) !== U(U(U))(U)) throw 'Lambda equality is broken';
const vm = Wikibinator203.n.vm; //anyWikibinator203Lambda.n is its vm.Node instance, wrapped in a lambda by vm.lambdize(node)
const loadOpsByName = true;
console.log('loadOpsByName='+loadOpsByName);
//let S = vm.ops.S;
//let T = vm.ops.T;
//let F = vm.ops.F;
//let Pair = vm.ops.Pair;
if(!vm) throw 'No vm';
if(loadOpsByName) for(let op in vm.ops){
if(window[op]) throw 'Already have var window.'+op;
window[op] = vm.ops[op];
console.log('Created var for opcode as lambda: window.'+op);
}
vm.loglev = 1;
//sha256hex of text. TODO optimize by using a normal hashtable to dedup Splits
var hashText = text=>vm.bytesToHex(sha256(unicode.stringToUtf8AsUint8Array(text)));
var randInt = n=>Math.floor(Math.random()*n);
const timeOffset_ = eval('performance.timing.navigationStart'); //evaled to prevent uglifyjs from changing it to something like performance.V.Q but still have to put timing and navigationStart in reserved list
//utc seconds, with a little more precision than Date.now()*.001
const Now = ()=>((timeOffset_+performance.now())*.001);
//FIXME? may be maxExcl or max or might go slightly over, depending on roundoff.
var randBetween = (min,max)=>(min+(max-min)*Math.random());
var between = (min, val, max)=>Math.max(min, Math.min(val, max));
var betweenInt = (min, val, max)=>Math.floor(Math.max(min, Math.min(val, max)));
var asByte = num=>betweenInt(0,num,255);
var colorStr = function(redFraction, greenFraction, blueFraction){
let r = asByte(redFraction*256);
let g = asByte(greenFraction*256);
let b = asByte(blueFraction*256);
let s = '000000'+(r*65536+g*256+b).toString(16);
return '#'+s.substring(s.length-6);
};
//var randColorStr = ()=>colorStr(.45+.55*Math.random(), .45+.55*Math.random(), .45+.55*Math.random());
var randColorStr = ()=>colorStr(randRed(), randGreen(), randBlue());
//sorted
var mapKeys = map=>{
let ret = [];
for(let key in map) ret.push(key);
ret.sort();
return ret;
};
var logMap = {};
//put a key/val in logMap and display them all.
var lm = (key,val)=>{
if(logMap[key]!=val){
logMap[key] = val;
let html = '';
let first = true;
for(let key of mapKeys(logMap)){
if(!first) html += '\r\n<br>';
html += key+' = '+logMap[key]; //FIXME this isnt escaped so could break the html
first = false;
}
Dob('logOut').innerHTML = html;
}
};
/*const firstCode =
`{
(T Sqrt)
{
(T +)
{(T Sqr#{(T *) I#(F U) I}) (P x)}
{(T Sqr) (P y)}
}
}`;*/
/*const firstCode =
`[((Pair [L R]) [[[
I#(F U)
Sqr#{(T *) I I}
Hypot#(
λ [x y]
[this is [a comment] with a function Sqr in it]
{
(T Sqrt)
{
(T +)
{(T Sqr) (P x)}
{(T Sqr) (P y)}
}
}
)
[the hypotenuse of [a right triangle of sides 6 and 8] is (Hypot 6 8) and [that func is Hypot] and [as a 1 param func is (Hypot 6)]]
{[hello world] {abc def ghi jkl}}
(U Pair L U U S U)
(Pair L R)
(T (Pair (T L) R))
2.34
Em#(EmptyTreemap GodelLessThan)
[L R S T Isleaf PutNoBal Del Fo DoAvlBal VarargAx]
[a b c d e f g h i j k l m n o p]
]]])]`;
*/
const firstCode =
`[((Pair [L R]) [[[
I#(F U)
Sqr#{(T *) I I}
Hypot#(
λ [
x y
(CC [this is [a comment] with a function Sqr in it]
{
(T Sqrt)
{
(T +)
{(T Sqr) (P x)}
{(T Sqr) (P y)}
}
}
)
]
)
[the hypotenuse of [a right triangle of sides 6 and 8] is (Hypot 6 8) and [that func is Hypot] and [as a 1 param func is (Hypot 6)]]
{[hello world] {abc def ghi jkl}}
(U Pair L U U S U)
[(Pair L R) this is a (Canvas 100 100 2 2 (0xff00ffffffffffff 0x00ffffffffff00ff (0x8000000000000000 0x0000000000000000))) canvas]
(T (Pair (T L) R))
2.34
Em#(EmptyTreemap GodelLessThan)
[L R S T Isleaf PutNoBal Del Fo DoAvlBal VarargAx]
[a b c d e f g h i j k l m n o p]
]]])]`;
/*const firstCode =
`[
Pair[ sum +[?[pix +[?i ?red]] ?[pix +[?i ?green]] ?[pix +[?i ?blue]]] ]
]`;
*/
/*
const firstCode =
`[
I#(F U)
Sqr#{(T *) I I}
Hypot#(
λ [x y]
[this is [a comment] with a function Sqr in it]
{
(T Sqrt)
{
(T +)
{(T Sqr) (P x)}
{(T Sqr) (P y)}
}
}
)
the hypotenuse of a right triangle of sides 6 and 8 is (Hypot 6 8) and that func is Hypot and as a 1 param func is (Hypot 6)
{[hello world] {abc def ghi}}
(Pair L R)
(T (Pair (T L) R))
2.34
]`;
*/
//TODO 0xf922 aka cbt literal
//const firstCode = '[I#(F U) Sqr#{(T *) I I}]';
var getFirstCode = ()=>{
let code = location.search;
if(code.startsWith('?')) code = code.substring(1); //remove ?
if(code.length > 0) return unescape(code);
return firstCode;
};
var urlPrefix = ()=>{
let s = location.href;
let i = s.indexOf('?');
return i==-1 ? s : s.substring(0,i);
};
//urls generally have to be smaller than about 1024 chars or bytes. fns can get much bigger, but this should work for small fns.
var urlOfSmallFn = fn=>urlPrefix()+'?'+escape(''+fn);
var goToUrlOfCurrentFn = ()=>{
console.log('goToUrlOfCurrentFn');
window.location = urlOfSmallFn(rootSplat.splate().fn);
};
//const exampleFnToDisplay = vm.eval(firstCode); //a Wikibinator203 fn/lambda (which is a js lambda)
const exampleFnToDisplay = vm.eval(getFirstCode()); //a Wikibinator203 fn/lambda (which is a js lambda)
const fnForTestingAction = vm.eval('[for testing action 3.456]');
//START Splat.dobs keys:
/*
//The dom object that the other splat dobs are childs of. This is normally a div.
const SELF = 'self';
//Example: Display '[' or '(' at top left corner.
//This might just be a html label.
const PREFIX = 'prefix';
//drop (or during drag displays where would drop) left/above/before the left of 2 childs, then forkEdit
//rootSplit by some interpretation of what dropping it there means, considering it uses a high level syntax.
//This is normally a div, size 0 and hidden until needed.
const DROPLEFT = 'dropleft';
//left splat child of a splat.
//This is normally a div.
const LEFT = 'left';
//drop (or during drag displays where would drop) between 2 childs, then forkEdit
//rootSplit by some interpretation of what dropping it there means, considering it uses a high level syntax.
//This is normally a div, size 0 and hidden until needed.
const DROPMID = 'dropmid';
//where to display a canvas, text, pdf file, jpg file, or other literal value, but only do this if childs are NOT displayed
//(a necessary but not sufficient condition),
//check split for should it be displayed and caching of the Uint8Array of canvas bytes etc.
//This is normally a div or maybe a canvas, or a div with a canvas in it, etc.
const LITERAL = 'literal';
//right splat child of a splat.
//This is normally a div.
const RIGHT = 'right';
//drop (or during drag displays where would drop) right/below/after the right of 2 childs, then forkEdit
//rootSplit by some interpretation of what dropping it there means, considering it uses a high level syntax.
//This is normally a div, size 0 and hidden until needed.
const DROPRIGHT = 'dropright';
//Example: Display ']#NameXYZ' or ')#NameXYZ' or ']' or ')' at bottom left corner or on right if 1 line.
//This might just be a html label.
const SUFFIX = 'suffix';
*/
//END Splat.dobs keys.
var controls = {};
var globalVars = {
dropCircMenuCenterY: 0, //becomes mouseY when dropCircMenu starts, near end of a dragAndDrop.
dropCircMenuCenterX: 0,
};
controls.mouseY = 0;
controls.mouseX = 0;
controls.mouseXDragFrom = 0;
controls.mouseXDragFrom = 0;
var anyfromSplat = ()=>(findSplatWithDragStep('from') || findSplatWithDragStep('wouldFrom'));
var anytoSplat = ()=>(findSplatWithDragStep('to') || findSplatWithDragStep('wouldTo') || findSplatWithDragStep('dropCircMenu'));
//any splat where the clickCircMenu should appear, that its been clicked by right mouse button.
var anyclickcircSplat = ()=>findSplatWithDragStep('clickcirc');
//returns [fromY fromX toY toX] or null, depending if theres a drag in progress.
var dragLineFromYXToYX = ()=>{
let wouldfromOrFrom = anyfromSplat();
let wouldtoOrTo = anytoSplat();
if(wouldfromOrFrom && wouldtoOrTo){
let dobFrom = wouldfromOrFrom.dobs.self;
let dobTo = wouldtoOrTo.dobs.self;
/*return [ //drag line
dobY(dobFrom)+dobHeight(dobFrom)*wouldfromOrFrom.splate().looseIconYFraction, //fromY
dobX(dobFrom)+dobWidth(dobFrom)*wouldfromOrFrom.splate().looseIconXFraction, //fromX
dobY(dobTo)+dobHeight(dobTo)*wouldtoOrTo.splate().looseIconYFraction, //toY
dobX(dobTo)+dobWidth(dobTo)*wouldtoOrTo.splate().looseIconXFraction //toX
];*/
return [ //drag line
dobY(dobFrom)+dobHeight(dobFrom)*uiState.from.looseIconYFraction, //fromY
dobX(dobFrom)+dobWidth(dobFrom)*uiState.from.looseIconXFraction, //fromX
dobY(dobTo)+dobHeight(dobTo)*uiState.to.looseIconYFraction, //toY
dobX(dobTo)+dobWidth(dobTo)*uiState.to.looseIconXFraction //toX
];
}else{
return null; //dont display drag line
}
};
//const popupDragButton = 'mouseButton0'; //controls[popupDragButton] is 0 (up) or 1 (down)
//const popupDragButton = 'mouseLeftButton'; //controls[popupDragButton] is 0 (up) or 1 (down)
//
//2022-12-28 changing this from left to right button, for the popup menu kind of drag.
//Will make metaInsert kind of drag on left mouse button.
//I noticed that changing this leaves it broken. Its not editable this way alone. Will need to change other code.
const popupDragButton = 'mouseRightButton'; //controls[popupDragButton] is 0 (up) or 1 (down)
const metaInsertButton = 'mouseLeftButton';
var eventToButtonName = event=>{
switch(event.button){
case 0:
return 'mouseLeftButton';
break; case 1:
return 'mouseMiddleButton';
break; case 2:
return 'mouseRightButton';
break; case 3:
return 'mouseFourthButton';
break; case 4:
return 'mouseFifthButton';
default:
return 'unknownButton';
}
};
var scheduledFunc = null;
//schedule a func to run asap. sometimes js gets too busy and misses events etc, leaving drag and drop stuff on screen.
var scheduleFunc = func=>{
if(scheduledFunc){
const prev = scheduledFunc;
scheduledFunc = ()=>{
prev();
func();
};
}else{
scheduledFunc = func;
}
};
var runScheduledFuncs = ()=>{
const prev = scheduledFunc;
scheduledFunc = null;
if(prev){
prev();
}
};
//TODO "SOLUTION: Fixing this by dividing Split into 2 classes: Split (immutable, up from any closed leafs),
//and Splat (mutable, has dom nodes). Split.prototype.displayIn(Splat) fills in those dom nodes,
//and removes some dom nodes if they're no longer used.
//Split can share branches and is a forest (like fn). Split might be made of fns later (TODO?).
//Splat is a tree and cant share branches.
//A split is dragged from any part of Splat to any part of Splat."
//
//var Splat = function(parentSplatOrNull, splate){
//
//OLD: isRight means is right child, else left child, of parentSplatOrNull, or if !parentSplatOrNull then this is a root, not a child.
//
//selfDob is normally parentSplatOrNull.dobs.left or parentSplatOrNull.dobs.right or if !parentSplatOrNull then its probably rootDob or dragDob.
var Splat = function(parentSplatOrNull, selfDob){
selfDob.innerHTML = '';
//this.splate = splate; //doesnt include splates for leftSplat or rightSplat. Its local to this Splat.
//this.splate = defaultSplate; //so Splat.setSplit can display it first. Has no childs.
//this.split = null; //TODO
this.split = defaultSplit;
/*
if(this.splate.isOpen && this.splate.isDisplayName){
fnMeta(this.splate.fn).numOpenNamedSplats++;
//FIXME do fnMeta(this.splate.fn).numOpenNamedSplats--
//when this.splate is replaced with a !isOpen and !isDisplayName or this Splat removed.
}*/
//map of string to dob (dom object).
//TODO dobs for the 2 childs, prefix and suffix, etc.
//TODO also include dobs for "include extra divs, normally empty, in every Splat, in every
//possible place in that splat it could be dropped, including hidden splats".
this.dobs = {
self: selfDob,
};
//const ThisSplat = this;
/*selfDob.addEventListener(
'mouseup',
this.eventListenerMouseUp = globalOnMouseUp
);*/
this.parentSplat = parentSplatOrNull || null; //null if this is a top Splat (probably just these 2: rootSplat and dragSplat)
this.leftSplat = null; //null if not viewing left child
this.rightSplat = null; //null if not viewing right child
this.createDobsIfNotExist(); //other than selfDob which is given
};
var globalOnMouseDown = function(event){
//requestAnimationFrame(()=>{
controls.mouseY = event.pageY;
controls.mouseX = event.pageX;
const ThisSplat = splatAtYX(controls.mouseY,controls.mouseX); //so this function is not specific to a splat and can move it to rootDob.
onDomEvent(event);
event.stopPropagation();
//if(uiState.isDoingMouseUp){
// if(options.logMouseEvents) console.log('mousemove is ignored cuz uiState.isDoingMouseUp');
// return;
//}
document.body.style.backgroundColor = '#ccc'; //FIXME remove this
//console.log('mousedown '+ThisSplat);
//event.stopPropagation();
uiState.removeClickCircMenuAsap = false;
let btn = eventToButtonName(event); //FIXME can be multiple buttons, see event.buttons and event.which
if(options.logMouseEvents) console.log('mousedown '+btn);
controls[uiState.lastMouseButtonDown = btn] = 1;
//if(btn == 'mouseLeftButton'){
//if(btn == metaInsertButton){
/* This was clickCircMenu, but thats being merged into dropCircMenu (put that on right mouse button)
then make a new kind of drag and drop (metaInsert) on left mouse button.
//doDropCircMenu(); //2022-12-17 i'm trying to use doDropCircMenu for both the clickCircMenu and the dropCircMenu so todo rename this func
//doClickCircMenu();
let splatfyfx = screenYXToSplatfyfx(controls.mouseY, controls.mouseX);
console.log('clickcirctest mouseRightButton splatfyfx='+splatfyfx);
if(splatfyfx){
splatfyfx.splat.doStartClickCirc(splatfyfx.fy, splatfyfx.fx);
}else{
console.log('for clickcirc, screenYXToSplatfyfx returned null so cant doStartDrag');
}*/
//console.log('TODO mouseLeftButton');
//}else if(btn == popupDragButton){
if(btn == popupDragButton || btn == metaInsertButton){ //both of these should start a drag, but different ways of ending it.
if(options.simple_setSelSplat_ofWhateverDobMouseTouches){
setSelSplat(ThisSplat);
}
if(options.doMainDragging){
if(options.logMouseEvents) console.log('for dropcirc, mousedown doMainDragging');
//ThisSplat.doStartDrag(
// ThisSplat.looseIconYFractionAtMouseY(controls.mouseY), ThisSplat.looseIconXFractionAtMouseX(controls.mouseX));
let splatfyfx = screenYXToSplatfyfx(controls.mouseY, controls.mouseX);
if(splatfyfx){
splatfyfx.splat.doStartDrag(splatfyfx.fy, splatfyfx.fx);
}else{
if(options.logMouseEvents) console.log('for dropcirc, screenYXToSplatfyfx returned null so cant doStartDrag');
}
//console.log('after doStartDrag from='+findSplatWithDragStep('from'));
}
}
/*if(btn == 'mouseLeftButton'){
if(options.simple_setSelSplat_ofWhateverDobMouseTouches){
setSelSplat(ThisSplat);
}
if(options.doMainDragging){
console.log('for dropcirc, mousedown doMainDragging');
//ThisSplat.doStartDrag(
// ThisSplat.looseIconYFractionAtMouseY(controls.mouseY), ThisSplat.looseIconXFractionAtMouseX(controls.mouseX));
let splatfyfx = screenYXToSplatfyfx(controls.mouseY, controls.mouseX);
if(splatfyfx){
splatfyfx.splat.doStartDrag(splatfyfx.fy, splatfyfx.fx);
}else{
console.log('for dropcirc, screenYXToSplatfyfx returned null so cant doStartDrag');
}
//console.log('after doStartDrag from='+findSplatWithDragStep('from'));
}
}else if(btn == 'mouseRightButton'){
//doDropCircMenu(); //2022-12-17 i'm trying to use doDropCircMenu for both the clickCircMenu and the dropCircMenu so todo rename this func
//doClickCircMenu();
let splatfyfx = screenYXToSplatfyfx(controls.mouseY, controls.mouseX);
console.log('clickcirctest mouseRightButton splatfyfx='+splatfyfx);
if(splatfyfx){
splatfyfx.splat.doStartClickCirc(splatfyfx.fy, splatfyfx.fx);
}else{
console.log('for clickcirc, screenYXToSplatfyfx returned null so cant doStartDrag');
}
}*/
//});
};
//allow event to be null, to use controls.mouseLeftButton and controls.mouseRightButton (each being 0 or 1) etc,
//so it knows which went up. Im gonna try doing that and calling this on null from mousemove
//if theres no buttons pressed AND there were buttons pressed before, cuz its been missing some events.
var globalOnMouseUp = function(event){
//this allows multiple calls of globalOnMouseUp (such as one from mousemove and one from mousedown,
//without duplicating the resulting behaviors. Its been dropping some mouseup events, so I'm going to call this from mousemove too.
if(!uiState.lastMouseButtonDown){
console.log('globalOnMouseUp ignored, cuz no lastMouseButtonDown');
return;
}
//requestAnimationFrame(()=>{
if(event){
onDomEvent(event);
}
//try{
// uiState.isDoingMouseUp = true;
document.body.style.backgroundColor = 'white'; //FIXME remove this
//console.log('mouseup '+ThisSplat);
if(event){
event.stopPropagation();
}
console.log('globalOnMouseUp, controls was = '+JSON.stringify(controls));
//let btn;
/*if(event){
btn = eventToButtonName(event);
}else{
/*
Wikibinator203DragAndDropTree.html:796 globalOnMouseUp, controls was = {"mouseY":504,"mouseX":759,"mouseXDragFrom":0,"mouseLeftButton":1}
Wikibinator203DragAndDropTree.html:796 globalOnMouseUp, controls was = {"mouseY":590,"mouseX":750,"mouseXDragFrom":0,"mouseLeftButton":1}
Wikibinator203DragAndDropTree.html:796 globalOnMouseUp, controls was = {"mouseY":423,"mouseX":870,"mouseXDragFrom":0,"mouseLeftButton":1}
Wikibinator203DragAndDropTree.html:796 globalOnMouseUp, controls was = {"mouseY":481,"mouseX":847,"mouseXDragFrom":0,"mouseLeftButton":0}
Wikibinator203DragAndDropTree.html:796 globalOnMouseUp, controls was = {"mouseY":526,"mouseX":873,"mouseXDragFrom":0,"mouseLeftButton":1}
Wikibinator203DragAndDropTree.html:796 globalOnMouseUp, controls was = {"mouseY":483,"mouseX":855,"mouseXDragFrom":0,"mouseLeftButton":0,"mouseRightButton":1}
*
//if(controls.mouseLeftButton){
//}else{
//}
if(uiState.lastMouseButtonDown){
fixmefixme
}else{
throw 'No lastMouseButtonDown';
}
}*/
let btn = uiState.lastMouseButtonDown;
if(options.logMouseEvents) console.log('mouseup '+btn);
controls[btn] = 0;
//if(options.simple_setSelSplat_ofWhateverDobMouseTouches){
// setSelSplat(ThisSplat);
//}
//if(options.doMainDragging && uiState.lastMouseButtonDown == 'mouseLeftButton'){
//if(options.doMainDragging && btn == 'mouseLeftButton'){
if(options.doMainDragging && btn == popupDragButton){
//last mouse button down was left (for dragging). right button is for clickCircMenu, not dropCircMenu.
//doDropCircMenu();
doDropCircMenuOrHasPreChosen();
//doEndDrag();
}else if(btn == metaInsertButton){
//scheduleFunc(()=>{
endMetaInsert();
//});
}
//dont need this at mouseup cuz that menu appears when mouse down: if(btn == 'mouseRightButton'){
// doClickCircMenu();
//}
uiState.lastMouseButtonDown = null;
//}finally{
// uiState.isDoingMouseUp = false;
//}
//});
};
var globalOnMouseMove = function(event){
//requestAnimationFrame(()=>{
onDomEvent(event);
event.stopPropagation();
//if(uiState.isDoingMouseUp){
// if(options.logMouseEvents) console.log('mousemove is ignored cuz uiState.isDoingMouseUp');
// return;
//}
controls.mouseY = event.pageY;
controls.mouseX = event.pageX;
const ThisSplat = splatAtYX(controls.mouseY,controls.mouseX); //so this function is not specific to a splat and can move it to rootDob.
runScheduledFuncs();
let isPopupDragButtonDown = controls[popupDragButton];
let isMetaInsertDragButtonDown = controls[metaInsertButton];
//TODO use options.doMainDragging
if(options.basicTestOfLooseIconWhenMouseMoves){
//FIXME i updated this code to use setIconStuffInUiState 2023-1-15 but then didnt test it cuz
//options.basicTestOfLooseIconWhenMouseMoves is false, so its not the code thats running.
//FIXME unset this in anything not currently dragging FROM or TO
//(so at most should be 2 splats with this true in the main/rootSplat tree excluding Action/thirdchilds if those are even used)
let isDisplayIcon = true;
//let isDisplayIcon = ThisSplat.splate().isDisplayIcon;
//let isDisplayIcon = false;
let looseIconYFraction = (controls.mouseY-dobY(selfDob))/dobHeight(selfDob); //FIXME what if height is 0?
let looseIconXFraction = (controls.mouseX-dobX(selfDob))/dobWidth(selfDob); //FIXME what if width is 0?
if(options.logMouseEvents) console.log('mousemove setting looseIconYFraction='+looseIconYFraction+' and looseIconXFraction='+looseIconXFraction);
//ThisSplat.transformSplate(splate=>splate.setLooseIconYFraction(looseIconYFraction).setLooseIconXFraction(looseIconXFraction).setIsDisplayIcon(isDisplayIcon));
if(ThisSplat.isDragFrom()){
console.log('mousemove isDragFrom splat='+ThisSplat);
//false is FROM. true is TO. It can be none, one, or both of those.
ThisSplat.setIconStuffInUiState(false, looseIconYFraction, looseIconXFraction, isDisplayIcon);
}
if(ThisSplat.isDragTo()){
console.log('mousemove isDragTo splat='+ThisSplat);
ThisSplat.setIconStuffInUiState(true, looseIconYFraction, looseIconXFraction, isDisplayIcon);
}
if(options.logMouseEvents) console.log('mousemove did transformSplate with new icon position etc');
//this.dobs.looseIcon.style.top = (dobY(self)+dobHeight(self)*splate.looseIconYFraction)+'px';
//this.dobs.looseIcon.style.left = (dobX(self)+dobWidth(self)*splate.looseIconXFraction)+'px';
}
if(options.logMouseEvents) console.log('controls = '+JSON.stringify(controls)+(selSplat ? (' dobDist to selSplat self = '+dobDist(selSplat.dobs.self,controls.mouseY,controls.mouseX)) : ''));
//event.stopPropagation();
if(options.simple_randomizeColor_ofWhateverDobMouseTouches){
//ThisSplat.forkRootSplit_randomizeColor_inRootSplat();
ThisSplat.forkRoot_setColor();
}
//if(ThisSplat.parentSplat) ThisSplat.parentSplat.forkRootSplit_randomizeColor_inRootSplat();
if(options.fillDropDobWithTestWhenNearestToMouse){
//let dob = rootSplat.nearestDrop(controls.mouseY,controls.mouseX);
let splatdob = rootSplat.nearestDrop(controls.mouseY,controls.mouseX);
//dob.innerHTML = 'DROP'+Date.now();
if(splatdob){
//splatdob.splat.dobs[splatdob.dobName].innerHTML = Math.random()<.5 ? 'DROP' : 'drop';
//splatdob.dob().innerHTML = Math.random()<.5 ? 'DROP' : 'drop';
let txt = splatdob.dobName;
txt = Math.random()<.5 ? txt.toUpperCase() : txt.toLowerCase();
//splatdob.dob().innerHTML = txt;
splatdob.dobs.self.innerHTML = txt;
}
}
if(options.doMainDragging){
if(isPopupCircularMenu()){
//console.log('mousemove isPopupCircularMenu (TODO)');
updateOverlays();
}else{
//FIXME if doing the metaInsert kind of drag, set this to 0 or 1 (which side always does self, not parent?).
let verticalFractionBetweenSelfVsParent = options.fractionRecurse;
if(uiState.lastMouseButtonDown == metaInsertButton){
//0 is parent. 1 is self. .5 is height chooses at mid height in rect.
//FIXME should this be 0 or 1? want parent, i think, but need experiments... No, I'm changing it to want self (not parent) and will choose parent or not etc from there.
//verticalFractionBetweenSelfVsParent = 0; //parent
verticalFractionBetweenSelfVsParent = 1; //self
}
//if(isPopupDragButtonDown){
if(isPopupDragButtonDown || isMetaInsertDragButtonDown){
//ThisSplat.doDuringDrag(
// ThisSplat.looseIconYFractionAtMouseY(controls.mouseY), ThisSplat.looseIconXFractionAtMouseX(controls.mouseX));
let splatfyfx = screenYXToSplatfyfx(controls.mouseY, controls.mouseX, verticalFractionBetweenSelfVsParent);
if(splatfyfx){
splatfyfx.splat.doDuringDrag(splatfyfx.fy, splatfyfx.fx);
}else{
console.log('screenYXToSplatfyfx returned null so cant doDuringDrag');
}
//console.log('after doDuringDrag from='+findSplatWithDragStep('from')+"\nwouldTo="+findSplatWithDragStep('wouldTo'));
}else{
//FIXME this is happening after release mouse button, as if drag hadnt just happened.
//ThisSplat.doBeforeDrag(
// ThisSplat.looseIconYFractionAtMouseY(controls.mouseY), ThisSplat.looseIconXFractionAtMouseX(controls.mouseX));
let splatfyfx = screenYXToSplatfyfx(controls.mouseY, controls.mouseX, verticalFractionBetweenSelfVsParent);
if(splatfyfx){
splatfyfx.splat.doBeforeDrag(splatfyfx.fy, splatfyfx.fx);
}else{
console.log('screenYXToSplatfyfx returned null so cant doBeforeDrag');
}
//console.log('after doBeforeDrag wouldFrom='+findSplatWithDragStep('wouldFrom'));
}
}
}
//});
if(uiState.lastMouseButtonDown){ //is doing a drag, and this button is or was down
if(!event.buttons){ //no mouse buttons are down
console.log('mousemove is doing a mouseup just in case that event was dropped, event.buttons='+event.buttons+' and lastMouseButtonDown='+uiState.lastMouseButtonDown+' Its ok if both happen cuz it will ignore later calls of it until another mousedown.');
globalOnMouseUp(null); //call with no event. it will use uiState.lastMouseButtonDown instead.
if(uiState.lastMouseButtonDown) throw 'in mousemove, globalOnMouseUp didnt unset lastMouseButtonDown';
}
}
};
//If mouseY is not given, uses controls.mouseY. Returns a fraction if mouse is inside my rect, else it might be outside that range.
Splat.prototype.looseIconYFractionAtMouseY = function(mouseY){
if(mouseY === undefined) mouseY = controls.mouseY;
return (mouseY-dobY(this.dobs.self))/dobHeight(this.dobs.self); //FIXME what if height is 0?
};
//If mouseX is not given, uses controls.mouseX. Returns a fraction if mouse is inside my rect, else it might be outside that range.
Splat.prototype.looseIconXFractionAtMouseX = function(mouseX){
if(mouseX === undefined) mouseX = controls.mouseX;
return (mouseX-dobX(this.dobs.self))/dobWidth(this.dobs.self); //FIXME what if width is 0?
};
//If you dont give a optionalOtherLooseIconYFraction then it uses its own.
Splat.prototype.screenYOfLooseIconYFraction = function(optionalOtherLooseIconYFraction){
let looseIconYFraction = (optionalOtherLooseIconYFraction!==undefined) ? optionalOtherLooseIconYFraction : this.splate().looseIconYFraction();
return dobY(this.dobs.self)+dobHeight(this.dobs.self)*looseIconYFraction;
};
//If you dont give a optionalOtherLooseIconXFraction then it uses its own.
Splat.prototype.screenXOfLooseIconXFraction = function(optionalOtherLooseIconXFraction){
let looseIconXFraction = (optionalOtherLooseIconXFraction!==undefined) ? optionalOtherLooseIconXFraction : this.splate().looseIconXFraction();
return dobX(this.dobs.self)+dobWidth(this.dobs.self)*looseIconXFraction;
};
//This takes 3 possible params: 'dropleft', 'dropmid', or 'dropright', and creates/reuses a child Splat.
//This is for the 3 possible child Splats for Action, in dobs.dropleft dobs.dropmid or dobs.dropright.
//TODO should L() and R() call this?
Splat.prototype.Child = function(dobName){
if(!this[dobName]){
if(dobName != 'dropleft' && dobName != 'dropmid' && dobName != 'dropright') throw 'dobName='+dobName;
if(!this.dobs[dobName]) throw 'No '+dobName+' dob';
this[dobName] = new Splat(this, Pob('div',this.dobs[dobName])); //div so no dobs are shared between parent.dobs and child.dobs
this[dobName].parentSplat = this;
}
return this[dobName];
};
//lazy create left splat with defaultSplate (waiting for setSplate to cause display of nontrivial contents)
Splat.prototype.L = function(){
if(!this.leftSplat){
//let leftSplate = defaultSplate.copy(); //FIXME shouldnt need to copy, cuz should use as immutable. Also, this shouldnt be defaultSplate.
//let leftSplate = this.splate.simpleLeftSplate();
//this.leftSplat = new Splat(this, leftSplate);
//this.leftSplat = new Splat(this, leftSplate);
if(!this.dobs.left) throw 'No left dob';
this.leftSplat = new Splat(this, Pob('div',this.dobs.left)); //div so no dobs are shared between parent.dobs and child.dobs
this.leftSplat.parentSplat = this;
}
return this.leftSplat;
};
//lazy create right splat with defaultSplate (waiting for setSplate to cause display of nontrivial contents)
Splat.prototype.R = function(){
if(!this.rightSplat){
//let rightSplate = defaultSplate.copy(); //FIXME shouldnt need to copy, cuz should use as immutable. Also, this shouldnt be defaultSplate.
//let rightSplate = this.splate.simpleRightSplate();
if(!this.dobs.right) throw 'No right dob';
this.rightSplat = new Splat(this, Pob('div',this.dobs.right)); //div so no dobs are shared between parent.dobs and child.dobs
this.rightSplat.parentSplat = this;
}
return this.rightSplat;
};
//get or set attribute of dob. If dont give a value, just reads it. If give value, writes it then returns same dob. Use null to remove attribute.
var attr = (dob,attrName,optionalAttrVal)=>{
if(optionalAttrVal === undefined){ //read attr
return dob.getAttribute(attrName);
}else{ //write attr
if(optionalAttrVal === null){
dob.removeAttribute(attrName);
}else{
dob.setAttribute(attrName, optionalAttrVal);
}
return dob;
}
};
//new dob/domObject