-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcjdom.js
More file actions
1069 lines (913 loc) · 33.3 KB
/
cjdom.js
File metadata and controls
1069 lines (913 loc) · 33.3 KB
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
/**
* CJWebEnv method: getMemberImpl()
*/
function Java_snap_webenv_CJWebEnv_getMemberImpl(lib, jsObj, aName) { return jsObj[aName]; }
/**
* CJWebEnv method: setMemberImpl()
*/
function Java_snap_webenv_CJWebEnv_setMemberImpl(lib, jsObj, aName, aValue) { jsObj[aName] = aValue; }
/**
* CJWebEnv method: getMemberStringImpl()
*/
function Java_snap_webenv_CJWebEnv_getMemberStringImpl(lib, jsObj, aName) { return jsObj[aName]; }
/**
* CJWebEnv method: setMemberStringImpl()
*/
function Java_snap_webenv_CJWebEnv_setMemberStringImpl(lib, jsObj, aName, aValue) { jsObj[aName] = aValue; }
/**
* CJWebEnv method: getMemberBooleanImpl()
*/
function Java_snap_webenv_CJWebEnv_getMemberBooleanImpl(lib, jsObj, aName) { return jsObj[aName] ? 1 : 0; }
/**
* CJWebEnv method: setMemberBooleanImpl()
*/
function Java_snap_webenv_CJWebEnv_setMemberBooleanImpl(lib, jsObj, aName, aValue) { jsObj[aName] = aValue; }
/**
* CJWebEnv method: getMemberIntImpl()
*/
function Java_snap_webenv_CJWebEnv_getMemberIntImpl(lib, jsObj, aName) { return jsObj[aName]; }
/**
* CJWebEnv method: setMemberIntImpl()
*/
function Java_snap_webenv_CJWebEnv_setMemberIntImpl(lib, jsObj, aName, aValue) { jsObj[aName] = aValue; }
/**
* CJWebEnv method: getMemberDoubleImpl()
*/
function Java_snap_webenv_CJWebEnv_getMemberDoubleImpl(lib, jsObj, aName) { return jsObj[aName]; }
/**
* CJWebEnv method: setMemberDoubleImpl()
*/
function Java_snap_webenv_CJWebEnv_setMemberDoubleImpl(lib, jsObj, aName, aValue) { jsObj[aName] = aValue; }
/**
* CJWebEnv: getSlotImpl()
*/
function Java_snap_webenv_CJWebEnv_getSlotImpl(lib, array, index) { return array[index]; }
/**
* CJWebEnv: setSlotImpl()
*/
function Java_snap_webenv_CJWebEnv_setSlotImpl(lib, array, index, aValue) { array[index] = aValue; }
/**
* CJWebEnv: windowImpl().
*/
function Java_snap_webenv_CJWebEnv_windowImpl(lib, anObj) { return window; }
/**
* CJWebEnv: consoleImpl().
*/
function Java_snap_webenv_CJWebEnv_consoleImpl(lib, anObj) { return console; }
/**
* CJWebEnv: newObjectImpl()
*/
function Java_snap_webenv_CJWebEnv_newObjectImpl(lib) { return { }; }
/**
* CJWebEnv method: awaitForPromiseImpl()
*/
async function Java_snap_webenv_CJWebEnv_awaitForPromiseImpl(lib, promiseJS) { return await promiseJS; }
// An element that needs a click
var _needsClickElement;
/**
* CJWebEnv setNeedsClickElement(): Called to set an element that needs a click.
*/
function Java_snap_webenv_CJWebEnv_setNeedsClickElement(lib, needsClickElement) { _needsClickElement = needsClickElement; }
/**
* CJWebEnv: newArrayJSForLengthImpl().
*/
function Java_snap_webenv_CJWebEnv_newArrayJSForLengthImpl(lib, arrayLength) { return new Array(arrayLength); }
/**
* CJWebEnv: getBytesArrayForArrayBufferJSImpl().
*/
function Java_snap_webenv_CJWebEnv_getBytesArrayForArrayBufferJSImpl(lib, arrayBufferJS) { return new Int8Array(arrayBufferJS); }
/**
* CJWebEnv: getBytesArrayForTypedArrayJSImpl().
*/
function Java_snap_webenv_CJWebEnv_getBytesArrayForTypedArrayJSImpl(lib, typedArrayJS) { return Int8Array.from(typedArrayJS); }
/**
* CJWebEnv: getShortsArrayForTypedArrayJSImpl().
*/
function Java_snap_webenv_CJWebEnv_getShortsArrayForTypedArrayJSImpl(lib, typedArrayJS) { return Int16Array.from(typedArrayJS); }
/**
* CJWebEnv: getShortsArrayForTypedArrayJSAndChannelIndexAndCountImpl().
*/
function Java_snap_webenv_CJWebEnv_getShortsArrayForTypedArrayJSAndChannelIndexAndCountImpl(lib, typedArrayJS, channelIndex, channelCount)
{
var length = typedArrayJS.length / channelCount;
var int16_Array = new Int16Array(length);
for (let i = 0, j = channelIndex; i < length; i++, j = j + channelCount)
int16_Array[i + 1] = typedArrayJS[j];
return int16_Array;
}
/**
* CJWebEnv: getInt8ArrayForObject().
*/
function Java_snap_webenv_CJWebEnv_getInt8ArrayForObject(lib, arrayObj)
{
return arrayObj instanceof Int8Array ? arrayObj : new Int8Array(arrayObj);
}
/**
* CJWebEnv: getInt16ArrayForObject().
*/
function Java_snap_webenv_CJWebEnv_getInt16ArrayForObject(lib, arrayObj)
{
return arrayObj instanceof Int16Array ? arrayObj : new Int16Array(arrayObj);
}
/**
* CJWebEnv: getFloat32ArrayForObject().
*/
function Java_snap_webenv_CJWebEnv_getFloat32ArrayForObject(lib, arrayObj)
{
return arrayObj instanceof Float32Array ? arrayObj : new Float32Array(arrayObj);
}
/**
* CJWebEnv: getUint16ArrayForObject().
*/
function Java_snap_webenv_CJWebEnv_getUint16ArrayForObject(lib, arrayObj)
{
return arrayObj instanceof Uint16Array ? arrayObj : new Uint16Array(arrayObj);
}
/**
* CJWebEnv: getUint8ClampedArrayForObject().
*/
function Java_snap_webenv_CJWebEnv_getUint8ClampedArrayForObject(lib, arrayObj)
{
return arrayObj instanceof Uint8ClampedArray ? arrayObj : new Uint8ClampedArray(arrayObj);
}
/**
* CJWebEnv: newImageDataJSForRgbaArrayAndWidthAndHeightImpl().
*/
function Java_snap_webenv_CJWebEnv_newImageDataJSForRgbaArrayAndWidthAndHeightImpl(lib, arrayObj, aWidth, aHeight)
{
const uint8ClampedArrayJS = arrayObj instanceof Uint8ClampedArray ? arrayObj : new Uint8ClampedArray(arrayObj);
return new ImageData(uint8ClampedArrayJS, aWidth, aHeight);
}
/**
* CJWebEnv: Creates a Blob from given bytes in JS.
*/
function Java_snap_webenv_CJWebEnv_newBlobJSForBytesAndTypeImpl(lib, arrayObj, typeStr)
{
const int8Array = arrayObj instanceof Int8Array ? arrayObj : new Int8Array(arrayObj);
return new Blob([ int8Array ], typeStr ? { type: typeStr } : null);
}
/**
* CJWebEnv: createUrlForBlobJSImpl().
*/
function Java_snap_webenv_CJWebEnv_createUrlForBlobJSImpl(lib, blobJS) { return URL.createObjectURL(blobJS); }
/**
* CJWebEnv: newFileJSForNameAndTypeAndBytesImpl().
*/
function Java_snap_webenv_CJWebEnv_newFileJSForNameAndTypeAndBytesImpl(lib, name, type, arrayObj)
{
const int8Array = arrayObj instanceof Int8Array ? arrayObj : new Int8Array(arrayObj);
return new File([ int8Array ], name, type ? { type: type } : null);
}
/**
* CJWebEnv: newFileReaderJSImpl().
*/
function Java_snap_webenv_CJWebEnv_newFileReaderJSImpl(lib) { return new FileReader(); }
/**
* CJWebEnv: newMutationObserverImpl().
*/
function Java_snap_webenv_CJWebEnv_newMutationObserverImpl(lib, aCallback)
{
return new MutationObserver((mutationRecords, observer) => mutationObserved(aCallback, mutationRecords));
}
/**
* CJWebEnv: addMutationObserverImpl().
*/
function Java_snap_webenv_CJWebEnv_addMutationObserverImpl(lib, mutationObserverJS, nodeJS, callback, optionsObj)
{
mutationObserverJS.observe(nodeJS, optionsObj);
}
/**
* CJWebEnv: newClipboardItemForMimeTypeAndDataStringImpl()
*/
function Java_snap_webenv_CJWebEnv_newClipboardItemForMimeTypeAndDataStringImpl(lib, type, string)
{
var blob = new Blob([ string ], { type });
var entry = { [blob.type]: blob };
return new ClipboardItem(entry);
}
/**
* CJWebEnv: newClipboardItemForBlobImpl()
*/
function Java_snap_webenv_CJWebEnv_newClipboardItemForBlobImpl(lib, blob)
{
var entry = { [blob.type]: blob };
return new ClipboardItem(entry);
}
// Clipboard.read() ClipboardItems - read upon meta+v key press, write() upon meta-c
var clipboardReadItems;
var clipboardWriteItems;
/**
* CJWebEnv: readClipboardItemsImpl()
*/
async function Java_snap_webenv_CJWebEnv_readClipboardItemsImpl(lib)
{
// If clipboardReadItems set, clear and return
if (clipboardReadItems != null) {
var temp = clipboardReadItems;
clipboardReadItems = null;
return temp;
}
// Try to read items
try {
var clipboardReadPromise = navigator.clipboard.read()
.catch((e) => { console.log("Clipboard.readClipboardItemsImpl: Ignoring error: " + e); return [ ]; });
return await clipboardReadPromise;
}
// Can happen on Safari iOS with localhost
catch (e) { console.log("Clipboard.readClipboardItemsImpl:" + e); return [ ]; }
}
/**
* CJWebEnv: writeClipboardItemsImpl().
*/
async function Java_snap_webenv_CJWebEnv_writeClipboardItemsImpl(lib, clipboardItems)
{
// Will fail on Safari because this is not directly triggered from user event
try {
navigator.clipboard.write(clipboardItems).catch((e) => clipboardWriteItems = clipboardItems);
clipboardReadItems = clipboardItems;
}
// Can happen on Safari iOS with localhost
catch (e) { console.log("Clipboard.writeClipboardItemsImpl:" + e); }
}
/**
* delayedClipboardWrite(): Called a moment after meta+C to try to write lingering clipboard items.
*/
function delayedClipboardWrite()
{
if (clipboardWriteItems != null) {
try { navigator.clipboard.write(clipboardWriteItems); }
catch (e) { console.log("delayedClipboardWrite:" + e); }
clipboardReadItems = clipboardWriteItems;
clipboardWriteItems = null;
}
// If NeedsClickElement is set, tell it to click (Safari)
if (_needsClickElement != null) {
_needsClickElement.click();
_needsClickElement = null;
}
}
/**
* eagerClipboardRead(): Called on meta+V (paste) to try to read items.
*/
async function eagerClipboardRead()
{
try {
clipboardReadItems = await navigator.clipboard.read().catch((e) => console.log("Ignoring: " + e));
}
// Can happen on Safari iOS with localhost
catch (e) { console.log("eagerClipboardRead:" + e); }
}
/**
* CJDataTransfer: newDataTransfer().
*/
function Java_snap_webenv_CJDataTransfer_newDataTransfer(lib) { return new DataTransfer(); }
// Cached drag drop data
var _dragDataTransferTypes;
var _dropDataTransfer;
var _dropDataTransferFiles
/**
* CJDataTransfer: getDropDataTransferImpl().
*/
function Java_snap_webenv_CJDataTransfer_getDropDataTransferImpl(lib) { return _dropDataTransfer; }
/**
* CJDataTransfer: getDropDataTransferTypesImpl().
*/
function Java_snap_webenv_CJDataTransfer_getDropDataTransferTypesImpl(lib) { return _dragDataTransferTypes; }
/**
* CJDataTransfer: getDropDataTransferFilesImpl().
*/
function Java_snap_webenv_CJDataTransfer_getDropDataTransferFilesImpl(lib) { return _dropDataTransferFiles; }
// Drag gesture data transfer
var _dragGestureDataTransfer = null;
var _dragGestureDragImage;
var _dragGestureDragImageX;
var _dragGestureDragImageY;
/**
* CJDataTransfer: startDragImpl().
*/
function Java_snap_webenv_CJDataTransfer_startDragImpl(lib, dataTransfer, dragImage, dx, dy)
{
_dragGestureDataTransfer = dataTransfer;
_dragGestureDragImage = dragImage;
_dragGestureDragImageX = dx;
_dragGestureDragImageY = dy;
}
/**
* Called when 'dragstart' called to configure dragGestureEvent.dataTransfer if values set in mousedown.
*/
function handleDragstart(dragEvent)
{
// If dragGestureDataTransfer not set, just suppress and return
if (_dragGestureDataTransfer === null) {
dragEvent.preventDefault();
dragEvent.stopPropagation();
return;
}
// Copy _dragGestureDataTransfer to dragEvent.dataTransfer and image and return
for (var type of _dragGestureDataTransfer.types) {
var dataStr = _dragGestureDataTransfer.getData(type);
dragEvent.dataTransfer.setData(type, dataStr);
}
// Set DragImage, add to body (and register to remove later), and clear dataTransfer / dragImage
var dragImage = _dragGestureDragImage;
dragEvent.dataTransfer.setDragImage(dragImage, _dragGestureDragImageX, _dragGestureDragImageY);
document.body.appendChild(dragImage);
setTimeout(() => dragImage.parentNode.removeChild(dragImage), 1000);
_dragGestureDataTransfer = null; _dragGestureDragImage = null;
}
/**
* Called on 'drop' event to cache data transfer, since it gets reset before CJ can use it.
*/
function handleDrop(dropEvent)
{
// Create new DataTransfer to persist after drop DataTransfer is reset
var dropDataTransfer = dropEvent.dataTransfer;
var dropTypes = dropDataTransfer.types;
_dropDataTransfer = new DataTransfer();
// Copy data types to cached DataTransfer
for (var type of dropTypes) {
var dataStr = dropDataTransfer.getData(type);
_dropDataTransfer.setData(type, dataStr);
}
// Copy files to cached DataTransfer
_dropDataTransferFiles = [ ];
for (var dropFile of dropDataTransfer.files)
_dropDataTransferFiles.push(dropFile);
}
// This wrapped promise is used to trigger getNextEvent
var _eventNotifyMutex = null;
// This array holds event records (which are also arrays of name, lambda func and optional arg)
let _eventQueue = [ ];
function createMutex()
{
let fulfill = null;
let promise = new Promise(f => { fulfill = f; });
return { fulfill, promise };
}
async function fireEvent(name, callback, arg1, arg2)
{
// Assume we want to steal all events, since preventDefault won't work with async event delivery)
if (arg1 instanceof Event) {
// If KeyboardEvent, suppress some browser keys and do some copy/paste
if (arg1 instanceof KeyboardEvent) {
if (arg1.metaKey) {
var key = arg1.key;
// Ignore meta+l (select address bar) and meta+alt+i (show dev tools)
if (key === "l" || arg1.altKey)
return;
// If meta+C (copy) or meta+X (cut), write clipboardWriteItems
if (key === 'c' || key ==='x')
setTimeout(delayedClipboardWrite, 100);
// If meta+V (paste), read and set clipboardReadItems
else if (key === 'v')
eagerClipboardRead();
}
}
// If MouseEvent, do some copy/paste
if (arg1 instanceof MouseEvent) {
if (arg1.type === 'mouseup') {
setTimeout(delayedClipboardWrite, 100);
_dragGestureDataTransfer = null; _dragGestureDragImage = null;
}
}
// Handle DragStart: Forward to handleDragstart() and return (_dragGestureDataTransfer needs to be set in mousedown)
var type = arg1.type;
if (type === 'dragstart') {
handleDragstart(arg1);
return;
}
// Handle DragEnd
if (type === 'dragend') {
if (_dragGestureDataTransfer === null) {
arg1.preventDefault();
arg1.stopPropagation();
}
}
// Stop default/propagation for most events
else if (type !== 'mousedown' && type !== 'mousemove' && type !== 'mouseup' && type !== 'click' && type !== 'pointerdown') { // && type != "wheel") {
arg1.preventDefault();
arg1.stopPropagation();
}
// Special support for drag/drop
if (arg1 instanceof DragEvent) {
_dragDataTransferTypes = arg1.dataTransfer.types;
if (arg1.type === 'drop')
handleDrop(arg1);
}
}
// Add event to queue
_eventQueue.push([ name, callback, arg1, arg2 ]);
// If mutex set, trigger it
if (_eventNotifyMutex != null) {
_eventNotifyMutex.fulfill();
_eventNotifyMutex = null;
}
}
/**
* EventQueue: getNextEvent().
*/
async function Java_snap_webenv_EventQueue_getNextEvent(lib)
{
// If event already in queue, just return it
if (_eventQueue.length > 0)
return _eventQueue.shift();
// Otherwise create mutex and wait for next event
_eventNotifyMutex = createMutex();
await _eventNotifyMutex.promise;
// Clear mutex and return event
_eventNotifyMutex = null;
return _eventQueue.shift();
}
/**
* EventQueue: requestAnimationFrameImpl().
*/
function Java_snap_webenv_EventQueue_requestAnimationFrameImpl(lib, aName, doubleFunction)
{
requestAnimationFrame(timestamp => fireEvent(aName, doubleFunction, timestamp));
}
/**
* EventQueue: setTimeoutImpl().
*/
function Java_snap_webenv_EventQueue_setTimeoutImpl(lib, aName, aRun, aDelay)
{
setTimeout(() => fireEvent(aName, aRun, null), aDelay);
}
/**
* EventQueue: setIntervalImpl().
*/
function Java_snap_webenv_EventQueue_setIntervalImpl(lib, aName, aRun, aDelay)
{
return setInterval(() => fireEvent(aName, aRun, null), aDelay);
}
// This dictionary holds all addEventListener() listeners with the JS mapped version so removeEventListener() can work
let _listenerDict = { };
/**
* Registers an event handler of a specific event type on the EventTarget
*/
function Java_snap_webenv_EventQueue_addEventListenerImpl(lib, eventTargetJS, name, eventLsnr, lsnrId, useCapture)
{
let lsnrJS = e => fireEvent(name, eventLsnr, e, null);
if (name !== 'load')
_listenerDict[lsnrId] = lsnrJS;
eventTargetJS.addEventListener(name, lsnrJS, useCapture);
}
/**
* Removes an event handler of a specific event type from the EventTarget
*/
function Java_snap_webenv_EventQueue_removeEventListenerImpl(lib, eventTarget, aName, eventLsnr, lsnrId, useCapture)
{
let lsnrJS = _listenerDict[lsnrId];
if (lsnrJS != null) {
eventTarget.removeEventListener(aName, lsnrJS, useCapture);
_listenerDict[lsnrId] = null;
}
}
/**
* Called when mutation observed. Have to wrap mutation records, since event array is returned as Object[] and JNI doesn't know
* whether to convert the array to JSObject or Object[].
*/
function mutationObserved(callback, mutationRecords)
{
fireEvent("mutation", callback, { value : mutationRecords }, null);
}
/**
* EventQueue: setPromiseThenImpl().
*/
function Java_snap_webenv_EventQueue_setPromiseThenImpl(lib, promiseWrapper, aFunc)
{
let promise = promiseWrapper[0]; // Could pass a mutex in to supported chained promises with a fireEventAndWait() method
return [ promise.then(value => fireEvent("promise", aFunc, value, null)) ];
}
// This wrapped promise is used to trigger getNextEvent
var _loadEventNotifyMutex = null;
// This array holds event records (which are also arrays of name, lambda func and optional arg)
let _loadEventQueue = [ ];
function createLoadMutex()
{
let fulfill = null;
let promise = new Promise(f => { fulfill = f; });
return { fulfill, promise };
}
async function fireLoadEvent(name, callback, arg1, arg2)
{
// Add event to queue
_loadEventQueue.push([ name, callback, arg1, arg2 ]);
// If mutex set, trigger it
if (_loadEventNotifyMutex != null) {
_loadEventNotifyMutex.fulfill();
_loadEventNotifyMutex = null;
}
}
/**
* LoadEventQueue: getNextEvent().
*/
async function Java_snap_webenv_LoadEventQueue_getNextEvent(lib)
{
// If event already in queue, just return it
if (_loadEventQueue.length > 0)
return _loadEventQueue.shift();
// Otherwise create mutex and wait for next event
_loadEventNotifyMutex = createLoadMutex();
await _loadEventNotifyMutex.promise;
// Clear mutex and return event
_loadEventNotifyMutex = null;
return _loadEventQueue.shift();
}
/**
* Registers a load event handler on the EventTarget
*/
function Java_snap_webenv_LoadEventQueue_addLoadEventListenerImpl(lib, eventTargetJS, eventLsnr)
{
let lsnrJS = e => fireLoadEvent('load', eventLsnr, e, null);
eventTargetJS.addEventListener('load', lsnrJS);
}
/**
* CJCanvasRenderingContext2D: setLineDashImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_setLineDashImpl(lib, cntxJS, doubleArray)
{
var dashArray = Array.from(doubleArray);
cntxJS.setLineDash(dashArray);
}
/**
* CJCanvasRenderingContext2D: fillTextImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_fillTextImpl(lib, cntxJS, aString, aX, aY) { cntxJS.fillText(aString, aX, aY); }
/**
* CJCanvasRenderingContext2D: fillTextImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_fillTextImpl2(lib, cntxJS, aString, aX, aY, maxWidth) { cntxJS.fillText(aString, aX, aY, maxWidth); }
/**
* CJCanvasRenderingContext2D: strokeText().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_strokeTextImpl(lib, cntxJS, aString, aX, aY) { cntxJS.strokeText(aString, aX, aY); }
/**
* CJCanvasRenderingContext2D: strokeText().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_strokeTextImpl2(lib, cntxJS, aString, aX, aY, maxWidth) { cntxJS.strokeText(aString, aX, aY, maxWidth); }
/**
* CJCanvasRenderingContext2D: drawImageImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_drawImageImpl(lib, cntxJS, imageJS, aX, aY) { cntxJS.drawImage(imageJS, aX, aY); }
/**
* CJCanvasRenderingContext2D: drawImageImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_drawImageImpl2(lib, cntxJS, imageJS, aX, aY, aW, aH) { cntxJS.drawImage(imageJS, aX, aY, aW, aH); }
/**
* CJCanvasRenderingContext2D: drawImageImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_drawImageImpl3(lib, cntxJS, imageJS, srcX, srcY, srcW, srcH, destX, destY, destW, destH)
{
cntxJS.drawImage(imageJS, srcX, srcY, srcW, srcH, destX, destY, destW, destH);
}
/**
* CJCanvasRenderingContext2D: getImageData().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_getImageDataImpl(lib, canvas, x, y, w, h)
{
return canvas.getImageData(x, y, w, h);
}
/**
* CJCanvasRenderingContext2D: putImageDataImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_putImageDataImpl(lib, canvas, imageDataJS, aX, aY, dirtyX, dirtyY, dirtyW, dirtyH)
{
canvas.putImageData(imageDataJS, aX, aY, dirtyX, dirtyY, dirtyW, dirtyH);
}
/**
* CJCanvasRenderingContext2D: createLinearGradientImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_createLinearGradientImpl(lib, contextJS, x0, y0, x1, y1)
{
return contextJS.createLinearGradient(x0, y0, x1, y1);
}
/**
* CJCanvasRenderingContext2D: createRadialGradientImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_createRadialGradientImpl(lib, contextJS, x0, y0, r0, x1, y1, r1)
{
return contextJS.createRadialGradient(x0, y0, r0, x1, y1, r1);
}
/**
* CJCanvasRenderingContext2D: createPatternImpl().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_createPatternImpl(lib, contextJS, imageJS, repetition)
{
return contextJS.createPattern(imageJS, repetition);
}
var _cntx;
var _cntxScale;
var _instructionStack;
var _intStack; var _intIndex;
var _doubleStack; var _doubleIndex;
var _stringStack; var _stringIndex;
var _nativeStack; var _nativeIndex;
/**
* CJCanvasRenderingContext2D: paintStacks().
*/
function Java_snap_webenv_CJCanvasRenderingContext2D_paintStacksImpl(lib, contextJS, contextScale, instructionStack, instructionStackSize, intStack, doubleStack, stringStack, objectStack)
{
_cntx = contextJS;
_cntxScale = contextScale;
_instructionStack = instructionStack;
_intStack = intStack; _intIndex = 0;
_doubleStack = doubleStack; _doubleIndex = 0;
_stringStack = stringStack; _stringIndex = 0;
_nativeStack = objectStack; _nativeIndex = 0;
_cntx.setTransform(_cntxScale, 0, 0, _cntxScale, 0, 0);
for (var i = 0; i < instructionStackSize; i++) {
switch (_instructionStack[i]) {
case 1: setFont(); break;
case 2: setPaint(); break;
case 3: setStroke(); break;
case 4: setOpacity(); break;
case 5: drawShape(); break;
case 6: fillShape(); break;
case 7: clipShape(); break;
case 8: drawImage(); break;
case 9: drawImage2(); break;
case 10: drawString(); break;
case 11: strokeString(); break;
case 12: transform(); break;
case 13: setTransform(); break;
case 14: save(); break;
case 15: restore(); break;
case 16: clearRect(); break;
case 17: setImageQuality(); break;
default: console.log("CJDom.js-paintStacks: Unknown instruction"); break;
}
}
}
function setFont(){ _cntx.font = getNative(); }
function setPaint()
{
var cstr = getNative();
_cntx.fillStyle = cstr;
_cntx.strokeStyle = cstr;
}
function setStroke()
{
// Set line width
_cntx.lineWidth = getDouble();
// set line dash
var dashArrayLen = getInt();
var dashArray = [];
for (var i = 0; i < dashArrayLen; i++)
dashArray.push(getDouble());
_cntx.setLineDash(dashArray);
// Set line dash offset
_cntx.lineDashOffset = dashArrayLen > 0 ? getDouble() : 0;
// Set line cap
var lineCap = getInt();
_cntx.lineCap = lineCap === 0 ? 'round' : lineCap === 1 ? 'butt' : 'square';
// Set line join
var lineJoin = getInt();
_cntx.lineJoin = lineJoin === 0 ? 'round' : lineJoin === 1 ? 'bevel' : 'miter';
if (lineJoin === 2)
_cntx.miterLimit = getDouble();
}
function setOpacity(){ _cntx.globalAlpha = getDouble(); }
function drawShape()
{
setShape();
_cntx.stroke();
}
function fillShape()
{
setShape();
_cntx.fill();
}
function clipShape()
{
setShape();
_cntx.clip();
}
function drawImage2()
{
var image = getNative();
_cntx.save();
setTransform();
if (image != null)
_cntx.drawImage(image, 0, 0);
_cntx.restore();
}
function drawImage()
{
var image = getNative();
var srcX = getDouble();
var srcY = getDouble();
var srcW = getDouble();
var srcH = getDouble();
var dx = getDouble();
var dy = getDouble();
var dw = getDouble();
var dh = getDouble();
// Correct source width/height for image dpi
// double scaleX = anImg.getDpiX() / 72;
// double scaleY = anImg.getDpiY() / 72;
// if (scaleX != 1 || scaleY != 1) {
// srcX *= scaleX;
// srcW *= scaleX;
// srcY *= scaleY;
// srcH *= scaleY;
// }
if (image != null)
_cntx.drawImage(image, srcX, srcY, srcW, srcH, dx, dy, dw, dh);
}
/** Draw string at location with char spacing. */
function drawString()
{
var str = getString();
var x = getDouble();
var y = getDouble();
var cs = getDouble();
if (cs === 0)
_cntx.fillText(str, x, y);
else {
let charX = x;
for (let i = 0; i < str.length; i++) {
const char = str[i];
_cntx.fillText(char, charX, y);
charX += _cntx.measureText(char).width + cs;
}
}
}
/** Stroke string at location with char spacing. */
function strokeString()
{
var str = getString();
var x = getDouble();
var y = getDouble();
var cs = getDouble();
if (cs === 0)
_cntx.strokeText(str, x, y);
else {
let charX = x;
for (let i = 0; i < str.length; i++) {
const char = str[i];
_cntx.strokeText(char, charX, y);
charX += _cntx.measureText(char).width + cs;
}
}
}
/**
* Transform by transform.
*/
function setTransform()
{
var m0 = getDouble(); var m1 = getDouble();
var m2 = getDouble(); var m3 = getDouble();
var m4 = getDouble(); var m5 = getDouble();
_cntx.setTransform(m0 * _cntxScale, m1, m2, m3 * _cntxScale, m4, m5);
}
/** Transform by transform. */
function transform()
{
var m0 = getDouble(); var m1 = getDouble();
var m2 = getDouble(); var m3 = getDouble();
var m4 = getDouble(); var m5 = getDouble();
_cntx.transform(m0, m1, m2, m3, m4, m5);
}
function save() { _cntx.save(); }
function restore() { _cntx.restore(); }
function clearRect()
{
var x = getDouble(); var y = getDouble();
var w = getDouble(); var h = getDouble();
_cntx.clearRect(x, y, w, h);
}
function setImageQuality()
{
var quality = getDouble();
var qualityStr = quality > .67 ? "high" : quality >.33 ? "medium" : "low";
_cntx.imageSmoothingQuality = qualityStr;
_cntx.imageSmoothingEnabled = quality > .33;
}
function setShape()
{
var opCount = getInt();
_cntx.beginPath();
// Handle rect shape
if (opCount === -1) {
_cntx.rect(getDouble(), getDouble(), getDouble(), getDouble());
return;
}
// Handle path shape: Iterate over path ops and add to context
for (var i = 0; i < opCount; i++) {
var op = getInt();
switch (op) {
case 0: _cntx.moveTo(getDouble(), getDouble()); break;
case 1: _cntx.lineTo(getDouble(), getDouble()); break;
case 2: _cntx.bezierCurveTo(getDouble(), getDouble(), getDouble(), getDouble(), getDouble(), getDouble()); break;
case 3: _cntx.closePath(); break;
}
}
}
// Get stack values
function getInt() { return _intStack[_intIndex++]; }
function getDouble() { return _doubleStack[_doubleIndex++]; }
function getString() { return _stringStack[_stringIndex++]; }
function getNative() { return _nativeStack[_nativeIndex++]; }
// Open the database
function openDB()
{
return new Promise((resolve, reject) => {
const request = indexedDB.open("cjFS_/files/");
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
// Read a record and modify a field
async function Java_snap_web_FileSite_setLastModified(lib, filePath, newModTime)
{
// Open db
const db = await openDB();
// Fetch file and reset mod time
return new Promise((resolve, reject) => {
const tx = db.transaction("files", "readwrite");
const store = tx.objectStore("files");
const getRequest = store.get(filePath);
// Handle success
getRequest.onsuccess = function() {
// Get file
const file = getRequest.result;
if (!file) {
reject("File not found: " + filePath);
return;
}
// Reset file lastModified to new mod time in seconds from given millis BigInt
//console.log("Last mod time for " + filePath + " is " + file.lastModified + " " + file.lastModified.constructor.name);
file.lastModified = Number(newModTime / 1000n);
// Save new file
const putRequest = store.put(file, filePath);
putRequest.onsuccess = () => resolve("File updated successfully");
putRequest.onerror = () => reject(putRequest.error);
};
// Handle error
getRequest.onerror = () => reject(getRequest.error);
});
}