-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
1217 lines (1149 loc) · 67.8 KB
/
functions.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
// JavaScript
define([], function () {
const qlikFormulaDatamodelHash = "=If(Count($Table),Hash256('$(=Concat({1}$Rows&$Table&$Field))'),'')";
return {
// settings for QRS API access via a header-authenticated virtual proxy
//=============================================================================================
luiDialog: function (ownId, title, detail, ok, cancel, inverse, width) {
luiDialog(ownId, title, detail, ok, cancel, inverse, width);
},
uploadApp: async function (file, config, httpHeader) {
console.log('uploading file', file, httpHeader);
const xrfKey = httpHeader["X-Qlik-Xrfkey"];
//newXrfKey();
const ret = await $.ajax({
url: config.qrsUrl + "app/upload?name=" + file.name.split('.qvf')[0] + '&keepData=true&xrfkey=' + xrfKey,
type: "POST",
data: file,
contentType: 'application/vnd.qlik.sense.app',
processData: false,
headers: httpHeader,
/*success: function (response) {
if (response != 0) {
alert('File uploaded');
} else {
alert('file not uploaded');
}
}*/
});
//console.log('result of upload:', ret);
return ret
},
showScript: async function (enigma, schema, config, app, deleteAfter, httpHeader) {
console.log('calling function showScript', app);
$('#spinningwheel').show();
$('#contextmenu').hide();
console.log('function showScript ' + app.id);
const session = enigma.create({
schema,
url: config.wssUrl + app.id,
createSocket: url => new WebSocket(url)
})
try {
const global = await session.open();
const enigmaApp = await global.openDoc(app.id);
var script = await enigmaApp.getScript();
scriptRows = script.split('\n');
var tabs = [];
scriptRows.forEach(function (row, i) {
if (row.substr(0, 7) == '///$tab') {
const tabName = row.substr(8).replace('\r', '');
tabs.push(tabName);
scriptRows[i] = '</p><hr /><p class="qscript" id="script_' + tabName + '">';
}
})
$('.work-area').append('<div id="scriptviewerparent"><table><tbody>'
+ '<tr><td style="width:15%"><div id="scriptviewer_header">'
+ '<button class="lui-button" onclick="$(\'#scriptviewerparent\').remove();">Close</button>'
+ '</div></td><td id="scriptviewer2" style="width:85%;"></td></tr></tbody></table>');
//$('#scriptviewer_header').append('<ul class="lui-tabset">');
$('#scriptviewer_header').append('<lu>');
tabs.forEach(function (tab) {
$('#scriptviewer_header').append('<li class="lui-tab" onclick="document.getElementById(\'script_' + tab + '\').scrollIntoView();">' + tab + '</li>');
});
$('#scriptviewer_header').append('</lu>');
$('#scriptviewer2').append('<p class="qscript">' + scriptRows.join('<br />') + '</p>');
//$('#scriptviewer textarea').text(script);
//$('#scriptviewer textarea').html($('#scriptviewer textarea').html().replace(/\/\/\/\$tab/g, '<hr />///$tab'));
/*var wnd = window.open("about:blank", "", "_blank");
wnd.document.write('<html><head></head>'
+ '<body style="font-family:monospace;">'
+ script.replace(/\n/g, '<br/>').replace(/\/\/\/\$tab/g, '<hr/>///$tab')
+ '</body></html>');
*/
await session.close();
if (deleteAfter) qrsCall('DELETE', config.qrsUrl + 'app/' + app.id, httpHeader);
$('#spinningwheel').hide();
$('#contextmenu').show();
$('#msgparent_contextmenu').remove();
}
catch(err) {
$('#spinningwheel').hide();
$('#div_errormsg').text('Access denied on opening app. Maybe Section Access rejects you?').show();
if (deleteAfter) qrsCall('DELETE', config.qrsUrl + 'app/' + app.id, httpHeader);
}
},
setScript: async function (enigma, schema, config, app, forms) {
console.log('calling function setScript', app);
$('#spinningwheel').show();
$('#contextmenu').hide();
$('#editname').remove();
$('#msgparent_contextmenu .lui-dialog').animate({width:'85%'});
$('#msgparent_contextmenu .lui-dialog__footer').hide();
console.log('function setScript ' + app.id);
const session = enigma.create({
schema,
url: config.wssUrl + app.id,
createSocket: url => new WebSocket(url)
})
try {
const global = await session.open();
const enigmaApp = await global.openDoc(app.id);
var script = await enigmaApp.getScript();
$('#spinningwheel').hide();
scriptRows = script.split('\n');
console.log('script has ' + scriptRows.length + ' rows');
console.log(scriptRows[0]);
$('#msgparent_contextmenu .lui-dialog__body').append(forms['setscript.html']);
//functions.luiDialog('8162', 'Script', '<textarea>'+script+'</textarea>', 'Save', 'Cancel', false);
$('#loadscripteditor').val(script);
$('#btn_setloadscript').click(async function(){
var newScript = $('#loadscripteditor').val();
if (newScript) {
$('#spinningwheel').show();
$('#loadscripteditor_parent').hide();
const confirm = await enigmaApp.setScript(newScript);
console.log('New script set.');
await session.close();
$('#spinningwheel').hide();
$('#msgparent_contextmenu .lui-dialog__body').prepend('Script has been saved to app.');
$('#msgparent_contextmenu .lui-dialog__footer').show();
$('#msgparent_contextmenu .lui-dialog').animate({width:'400px'});
//$('#contextmenu').show();
//$('#msgparent_contextmenu').remove();
}
});
$('#btn_cancelloadscript').click(async function(){
await session.close();
$('#contextmenu').show();
$('#msgparent_contextmenu').remove();
});
}
catch(err) {
$('#spinningwheel').hide();
$('#div_errormsg').text('Access denied on opening app. Maybe Section Access rejects you?').show();
if (deleteAfter) qrsCall('DELETE', config.qrsUrl + 'app/' + app.id, httpHeader);
}
},
showDataModel: async function (enigma, schema, config, app, httpHeader) {
$('#spinningwheel').show();
$('#contextmenu').hide();
console.log('calling function showDataModel', app);
const session = enigma.create({
schema,
url: config.wssUrl + app.id,
createSocket: url => new WebSocket(url)
})
try {
const global = await session.open();
const enigmaApp = await global.openDoc(app.id);
const dataModel = await enigmaApp.evaluate("=Concat(DISTINCT '<td>' & $Table & '</td><td>' & $Rows & '</td><td>' & $Fields & '</td>', '</tr><tr>') & '</tr>"
+ "<tr><td></td><td>' & Sum($Rows) & '</td><td>' & Count(DISTINCT $Field) & '</td>'");
//const hash = await enigmaApp.evaluate("=Hash256(Concat($Table & $Field & $Rows))");
const hash = await enigmaApp.evaluate(qlikFormulaDatamodelHash);
await session.close();
//const hash = await qrsCall('GET', config.qrsUrl + 'app/object?filter=app.id eq '
// + app.id + " and objectType eq 'loadModel'", httpHeader);
$('#spinningwheel').hide();
$('#table_datamodel').append('<tr><td>Table</td><td>Rows</td><td>Fields</td></tr><tr>' + dataModel + '</tr>');
//$('#span_hash').text(hash[0].contentHash);
$('#span_hash').text(hash);
$('#div_datamodel').show();
}
catch(err) {
$('#spinningwheel').hide();
$('#div_errormsg').text('Access denied on opening app. Maybe Section Access rejects you?').show();
}
},
enableDisableButton: function () {
if ($('#importdesign').is(':checked')
|| $('#importscript').is(':checked')
|| $('#importdata').is(':checked')
) {
if ($('#refreshfrommyapp').hasClass("lui-button--info")
|| $('#refreshfrompubapp').hasClass("lui-button--info")
) {
$('#btn_replace').attr("disabled", false);
} else if ($('#refreshfromfile').hasClass("lui-button--info")
&& $('#fileinput')[0].files.length > 0) {
$('#btn_replace').attr("disabled", false);
} else if ($('#myappslist :selected').length == 1
&& !$('#refreshfromfile').hasClass("lui-button--info")
&& !$('#refreshfrommyapp').hasClass("lui-button--info")
&& !$('#refreshfrompubapp').hasClass("lui-button--info")
) {
// if the myapplist has a guid instead of a list of app names (comes from "from=" querystring)
if ($('#myappslist :selected').html().match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)) {
$('#btn_replace').attr("disabled", false);
}
} else {
$('#btn_replace').attr("disabled", true);
}
} else {
$('#btn_replace').attr("disabled", true);
}
// enable or disable the "Next >" button
if ($('#refreshfrommyapp').hasClass("lui-button--info")
|| $('#refreshfrompubapp').hasClass("lui-button--info")
) {
$('#btn_next').attr("disabled", false);
} else if ($('#refreshfromfile').hasClass("lui-button--info")
&& $('#fileinput')[0].files.length > 0) {
$('#btn_next').attr("disabled", false);
} else {
$('#btn_next').attr("disabled", true);
}
},
//-----------------------------------------------------------------------------------------------------
previewSteps: function (thisUser, appList, fromFile, licensed) {
//-----------------------------------------------------------------------------------------------------
const importDesign = $('#importdesign').is(':checked');
const importScript = $('#importscript').is(':checked');
const importData = $('#importdata').is(':checked');
const reloadAfter = $('#reloadafter').is(':checked');
const noNewSheets = $('#importdesign').is(':checked') && $('#nonewsheets').is(':checked');
const removeTag = $('#check_removetag').is(':checked') ? $('#select_removetag').val() : null;
const addTag = $('#check_addtag').is(':checked') ? $('#input_addtag').val() : null;
const origAppId = $('#fromapp_id').text();
const isOwnerOfSrcApp = fromFile ? true : (thisUser == $('#fromapp_owner').text());
$('#nonewsheets').attr("disabled", !importDesign);
$('#btn_startcopy').attr("disabled", !(importDesign || importData || importScript || removeTag || addTag || reloadAfter));
$('#nobinaryerror').hide();
if ($('#fromapp_id').text() == $('#toapp_id').text()) {
$('#sameapperror').show();
$('#btn_startcopy').attr('disabled', true);
}
var trgtApps = appList || [{
id: $('#toapp_id').text(),
owner: { userDirectory: $('#toapp_owner').text().split('\\')[0], userId: $('#toapp_owner').text().split('\\')[0] }
}];
if (trgtApps.length > 3 && !licensed) {
$('#freeversionwarning').show();
trgtApps = trgtApps.slice(0, 3);
}
const qrsTag = '<span class="tag-qrs"></span>';
const enigmaTag = '<span class="tag-enigma"></span>';
const binaryPossible = $('#dConnectionIcon').hasClass('lui-icon--tick');
var binaryNeeded = false;
var actions;
// fill a href attribut to deeplink this combination
const deeplink = location.href.substr(0, location.href.length - location.search.length)
+ '?from=' + (fromFile ? 'file' : origAppId)
+ ($('#toapp_id').text().substr(0, 2) == '-m' ? ('&stream=' + $('#select_stream').find(":selected").val()) : ('&app=' + $('#toapp_id').text()))
+ (importDesign ? '&importdesign' : '')
+ (noNewSheets ? '&nonewsheets' : '')
+ (importData ? '&importdata' : '')
+ (importScript ? '&importscript' : '')
+ (reloadAfter ? '&reloadafter' : '')
+ (removeTag ? ('&removetag=' + removeTag) : '')
+ (addTag ? ('&addtag=' + addTag) : '');
$('#a_deeplink').attr('href', deeplink);
// reset step texts and hide them
//for (var step = 1; step <= 8; step++) {
// $("#step" + step + " .steptext").text('');
// $("#step" + step).hide();
//}
$('#steplist_parent tbody').empty();
function createStepHTML(stepNo, text, rowSpan, trgtAppId, rowSpanText, bgColor) {
return '<tr id="step' + stepNo + '" class="steplist">'
+ '<td class="stepcheck"' + (bgColor ? (' style="background-color:' + bgColor + ';">') : '>')
+ ' <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"'
+ ' viewBox="0 0 512 512" xml:space="preserve">'
+ ' <path class="checkcircle" d="M437.019,74.98C388.667,26.629,324.38,0,256,0C187.619,0,123.331,26.629,74.98,74.98C26.628,123.332,0,187.62,0,256'
+ " s26.628,132.667,74.98,181.019C123.332,485.371,187.619,512,256,512c68.38,0,132.667-26.629,181.019-74.981"
+ " C485.371,388.667,512,324.38,512,256S485.371,123.333,437.019,74.98z M256,482C131.383,482,30,380.617,30,256S131.383,30,256,30"
+ ' s226,101.383,226,226S380.617,482,256,482z"/>'
+ ' <path class="checktick" d="M378.305,173.859c-5.857-5.856-15.355-5.856-21.212,0.001L224.634,306.319l-69.727-69.727'
+ " c-5.857-5.857-15.355-5.857-21.213,0c-5.858,5.857-5.858,15.355,0,21.213l80.333,80.333c2.929,2.929,6.768,4.393,10.606,4.393"
+ ' c3.838,0,7.678-1.465,10.606-4.393l143.066-143.066C384.163,189.215,384.163,179.717,378.305,173.859z"/>'
+ ' <path class="checkfailed" d="m156.41796,376.41432c0,0 101.24867,-101.24867 101.24867,-101.24867c0,0 102.91531,102.49865 '
+ ' 102.91531,102.49865c0,0 21.24972,-21.66638 21.24972,-21.66638c0,0 -102.08199,-102.08199 -102.08199,-102.08199c0,0 102.08199,-103.33197 '
+ ' 102.08199,-103.33197c0,0 -19.99974,-21.66638 -19.99974,-21.66638c0,0 -103.74864,103.74864 -103.74864,103.74864c0,0 -102.08199,-102.08199 '
+ ' -102.08199,-102.08199c0,0 -21.24972,21.24972 -21.24972,21.24972c0,0 102.08199,101.24867 101.91198,101.07941c0.17001,0.16926 '
+ ' -101.49532,103.08457 -101.49532,103.08457c0,0 21.24972,20.4164 21.24972,20.4164l0.00001,-0.00001z" />'
+ ' </svg>'
+ '</td>'
+ '<td class="steptext"' + (bgColor ? (' style="background-color:' + bgColor + ';">') : '>') + text + '</td>'
+ (rowSpan ? ('<td style="width:30%;' + (bgColor ? ('background-color:' + bgColor + ';"') : '"') + ' rowspan="' + rowSpan
+ '" class="nextapp" trgtappid="' + trgtAppId + '">' + rowSpanText + '<td>') : '')
+ '</tr>'
};
function getStepsFor(importDesign, importData, importScript, fromFile, isOwnerOfSrcApp, isOwnerOfTrgtApp) {
//console.log('getStepsFor fromFile=', fromFile);
const cos = "<!--cos-->Copy source app as temp source-copy" + qrsTag;
const cot = "<!--cot-->Copy target app as temp target-copy" + qrsTag;
const uts = "<!--uts-->Upload app as temp source-copy" + qrsTag;
const dts = "<!--dts-->Delete temp source-copy" + qrsTag;
const dtt = "<!--dtt-->Delete temp target-copy" + qrsTag;
const ts2ot = "<!--ts2ot-->Temp source-copy replaces target app" + qrsTag;
const os2ot = "<!--os2ot-->Source app replaces target app" + qrsTag;
const tt2ot = "<!--tt2ot-->Temp target-copy replaces target app" + qrsTag;
const goss = "<!--goss-->Get script of original source app" + enigmaTag;
const gtss = "<!--gtss-->Get script of temp source-copy" + enigmaTag;
const gtts = "<!--gtts-->Get script of temp target-copy" + enigmaTag;
const gots = "<!--gots-->Get script of original target" + enigmaTag;
const rtss = "<!--rtss-->Revert script of temp source app" + enigmaTag;
const rtts = "<!--rtts-->Revert script of temp target app" + enigmaTag;
const oss2ot = "<!--oss2ot-->Copy source script to target" + enigmaTag;
const oss2tt = "<!--oss2tt-->Copy source script to target-copy" + enigmaTag;
const osd2tt = "<!--osd2tt-->Copy source data to target copy (binary)" + enigmaTag;
const tss2ot = "<!--tss2ot-->Copy source-copy's script to target" + enigmaTag;
const tss2tt = "<!--tss2tt-->Copy source-copy's script to target-copy" + enigmaTag;
const tsd2tt = "<!--tsd2tt-->Copy source-copy's data to target copy (binary)" + enigmaTag;
const tts2ts = "<!--tts2ts-->Copy target-copy's script to source-copy" + enigmaTag;
const ots2ts = "<!--ots2ts-->Copy target script to source-copy" + enigmaTag;
const otd2ts = "<!--otd2ts-->Copy target data to source-copy (binary)" + enigmaTag;
const otr = "<!--otr-->Start background reload of target app" + qrsTag;
const ottag = "<!--ottag-->Update target app's tags" + qrsTag;
var ret;
if (importDesign && importData && importScript && !fromFile) {
//------------------------------------------------------------------------
ret = {
combination: '7',
descr: "Copy entire app from source to target",
variant: 'default',
binaryNeeded: false,
init: [],
loop: [os2ot],
wrapUp: []
};
//------------------------------------------------------------------------
} else if (importDesign && importData && importScript && fromFile) {
//------------------------------------------------------------------------
ret = {
combination: '7u',
descr: "Upload app and copy entire app to target",
variant: 'default',
binaryNeeded: false,
init: [uts],
loop: [ts2ot],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (importDesign && !importData && importScript) {
//------------------------------------------------------------------------
ret = {
combination: '5',
descr: "Copy design and script but leave the data unchanged",
variant: fromFile ? 'from file' : 'default',
binaryNeeded: true,
init: [(fromFile ? uts : cos), gtss],
loop: [otd2ts, rtss, ts2ot],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (importDesign && !importData && !importScript) {
//------------------------------------------------------------------------
ret = {
combination: '4',
descr: "Copy design but leave the data and script unchanged",
variant: isOwnerOfTrgtApp ? (fromFile ? 'owner of target, from file' : 'owner of target')
: (fromFile ? 'from file' : 'default'),
binaryNeeded: true,
init: [fromFile ? uts : cos],
loop: isOwnerOfTrgtApp ? [gots, otd2ts, ots2ts, ts2ot]
: [cot, gtts, dtt, otd2ts, tts2ts, ts2ot],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (importDesign && importData && !importScript) {
//------------------------------------------------------------------------
ret = {
combination: '6',
descr: "Copy design and data but leave the script unchanged",
variant: isOwnerOfTrgtApp ? (fromFile ? 'owner of target, from file' : 'owner of target')
: (fromFile ? 'from file' : 'default'),
binaryNeeded: false,
init: [fromFile ? uts : cos],
loop: isOwnerOfTrgtApp ? [gots, ots2ts, ts2ot]
: [cot, gtts, dtt, tts2ts, ts2ot],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (!importDesign && importData && importScript && (!isOwnerOfSrcApp || fromFile)) {
//------------------------------------------------------------------------
ret = {
combination: '3',
descr: "Copy data and script but leave design unchanged",
variant: fromFile ? 'from file' : 'default',
binaryNeeded: true,
init: [(fromFile ? uts : cos), gtss],
loop: [cot, tsd2tt, tss2tt, tt2ot, dtt],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (!importDesign && importData && importScript && isOwnerOfSrcApp) {
//------------------------------------------------------------------------
ret = {
combination: '3o',
descr: "Copy data and script but leave design unchanged (you're owner of source app)",
variant: 'from file',
binaryNeeded: true,
init: [goss],
loop: [cot, osd2tt, oss2tt, tt2ot, dtt],
wrapUp: []
};
//------------------------------------------------------------------------
} else if (!importDesign && importData && !importScript && (!isOwnerOfSrcApp || fromFile)) {
//------------------------------------------------------------------------
ret = {
combination: '2',
descr: "Copy data but leave design and script unchanged",
variant: fromFile ? 'from file' : 'default',
binaryNeeded: true,
init: [fromFile ? uts : cos],
loop: [cot, gtts, tsd2tt, rtts, tt2ot, dtt],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (!importDesign && importData && !importScript && isOwnerOfSrcApp) {
//------------------------------------------------------------------------
ret = {
combination: '2o',
descr: "Copy data but leave design and script unchanged (you're owner of source app)",
variant: 'default',
binaryNeeded: true,
init: [],
loop: [cot, gtts, osd2tt, rtts, tt2ot, dtt],
wrapUp: []
};
//------------------------------------------------------------------------
} else if (!importDesign && !importData && importScript && (!isOwnerOfSrcApp || fromFile)) {
//------------------------------------------------------------------------
ret = {
combination: '1',
descr: "Copy only the script to the target app",
variant: isOwnerOfTrgtApp ? (fromFile ? 'owner of target, from file' : 'owner of target')
: (fromFile ? 'from file' : 'default'),
binaryNeeded: false,
init: [(fromFile ? uts : cos), gtss],
loop: isOwnerOfTrgtApp ? [tss2ot]
: [cot, tss2tt, tt2ot, dtt],
wrapUp: [dts]
};
//------------------------------------------------------------------------
} else if (!importDesign && !importData && importScript && isOwnerOfSrcApp) {
//------------------------------------------------------------------------
ret = {
combination: '1o',
descr: "Copy only the script to the target app (you're owner of source app)",
variant: isOwnerOfTrgtApp ? 'owner of target' : 'default',
binaryNeeded: false,
init: [goss],
loop: isOwnerOfTrgtApp ? [oss2ot]
: [cot, oss2tt, tt2ot, dtt],
wrapUp: []
};
//------------------------------------------------------------------------
} else if (!importDesign && !importData && !importScript) {
//------------------------------------------------------------------------
ret = {
combination: '0',
descr: 'Nothing to be copied.',
variant: 'default',
binaryNeeded: false,
init: [],
loop: [],
wrapUp: []
}
//} else {
// ret = { error: true, combination: "none" }
}
//if (ret.error != true && (importDesign || importData || importScript)) {
if (reloadAfter) ret.loop.push(otr);
//}
if (removeTag || addTag) ret.loop.push(ottag);
return ret;
}
/*
console.log('Combinations:');
var log;
log = getStepsFor(false, false, true, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(false, false, true, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(false, false, true, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(false, false, true, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(false, false, true, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(false, false, true, true, true, true); console.log(log.combination, log.variant);
log = getStepsFor(false, true, false, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(false, true, false, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(false, true, false, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(false, true, false, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(false, true, false, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(false, true, false, true, true, true); console.log(log.combination, log.variant);
log = getStepsFor(false, true, true, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(false, true, true, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(false, true, true, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(false, true, true, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(false, true, true, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(false, true, true, true, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, false, false, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(true, false, false, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(true, false, false, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, false, false, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, false, false, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, false, false, true, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, false, true, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(true, false, true, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(true, false, true, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, false, true, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, false, true, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, false, true, true, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, true, false, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(true, true, false, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(true, true, false, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, true, false, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, true, false, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, true, false, true, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, true, true, false, false, false); console.log(log.combination, log.variant);
log = getStepsFor(true, true, true, false, false, true); console.log(log.combination, log.variant);
log = getStepsFor(true, true, true, false, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, true, true, false, true, true); console.log(log.combination, log.variant);
log = getStepsFor(true, true, true, true, true, false); console.log(log.combination, log.variant);
log = getStepsFor(true, true, true, true, true, true); console.log(log.combination, log.variant);
*/
var step = 0;
var block = 1;
var altColors = ['#ffffff', '#f0f0f0']; // background-color for each block
var binaryNeeded = false;
for (var a = 0; a < trgtApps.length; a++) {
const trgtAppId = trgtApps[a].id;
if (trgtAppId == origAppId) {
console.log('Actions skipped where source and target app are the same ' + trgtAppId);
} else {
//const trgtAppOwner = trgtApps[a].owner.userDirectory + '\\' + trgtApps[a].owner.userId;
//console.log('getStepsFor(isOwnerOfSrcApp=',isOwnerOfSrcApp,'ownerOfTrgApp', trgtApps[a].owner);
const actions = getStepsFor(importDesign, importData, importScript, fromFile, isOwnerOfSrcApp, trgtApps[a].owner == thisUser);
//console.log(a, actions);
binaryNeeded = binaryNeeded || actions.binaryNeeded;
if (step == 0) {
$('#steps_info').html('(' + actions.combination + ') ' + actions.descr);
// Get init actions
for (var i = 0; i < actions.init.length; i++) {
step++;
$('#steplist_parent tbody').append(
createStepHTML(step, actions.init[i], i == 0 ? actions.init.length : null, trgtAppId,
'Initial', altColors[block % 2])
);
}
if (step > 0) block++;
}
// get loop actions for current target app
for (var i = 0; i < actions.loop.length; i++) {
step++;
$('#steplist_parent tbody').append(
createStepHTML(step, actions.loop[i], i == 0 ? actions.loop.length : null, trgtAppId,
'Combi <b>' + actions.combination + '/' + actions.variant + '</b><br />'
+ '<i>' + trgtApps[a].name + '</i> '
+ '<span title="' + trgtAppId + '" class="lui-icon lui-icon--info" style="color:#b2b2b2;"></span>'
, altColors[block % 2])
);
}
block++;
if (a == (trgtApps.length - 1)) {
// get wrapup actions
for (var i = 0; i < actions.wrapUp.length; i++) {
step++;
$('#steplist_parent tbody').append(
createStepHTML(step, actions.wrapUp[i], i == 0 ? actions.wrapUp.length : null, trgtAppId,
'Clean-up', altColors[block % 2])
);
}
}
}
}
$('#stepscounter').text('(' + step + ')');
if (binaryNeeded && !binaryPossible) {
$('#nobinaryerror').show();
$('#btn_startcopy').attr('disabled', true);
}
},
//------------------------------------------------------------------------
executeSteps: async function (enigma, schema, httpHeader, from, config, settings, thisUser, trgtAppList, addTag, removeTag, noNewSheets) {
//------------------------------------------------------------------------
// from can be an object (from fileinput2 form) or just a string with the id.
var steps = [];
$('#steplist_parent tr').each(async function (i, e) {
steps.push({
number: i + 1,
descr: $(e).find('.steptext').text(),
actionId: $(e).find('.steptext').html().split('<!--')[1].split('-->')[0],
nextApp: $(e).find('.nextapp').length,
trgtAppId: $(e).find('.nextapp').attr('trgtappid')
});
});
console.log('steps', steps, from);
var os_id = !from ? null : (from.name ? null : from); // source app id or upload-file object
var oss; // original source app script
var os_session, os_global, os_enigma;
var ts_id; // app-id of temp source-copy
var tss; // temp source-copy app script
var ts_session, ts_global, ts_enigma;
var tt_id; // app-id of temp target-copy
var tts; // temp target-copy app script
var tt_session, tt_global, tt_enigma;
var ot_id; // app-id of original target app
var ots; // original target app script
var ot_session, ot_global, ot_enigma;
var ot_sh; // original target sheet list
async function getEnigma_os() {
if (!os_enigma) {
console.log('Opening enigma session to source app ' + os_id);
os_session = enigma.create({
schema,
url: config.wssUrl + os_id,
createSocket: url => new WebSocket(url)
});
os_global = await os_session.open();
os_enigma = await os_global.openDoc(os_id);
}
}
async function getEnigma_ts() {
if (!ts_enigma) {
console.log('Opening enigma session to temp source-copy app ' + ts_id);
ts_session = enigma.create({
schema,
url: config.wssUrl + ts_id,
createSocket: url => new WebSocket(url)
});
ts_global = await ts_session.open();
ts_enigma = await ts_global.openDoc(ts_id);
}
}
async function getEnigma_ot() {
if (!ot_enigma) {
console.log('Opening enigma session to target app ' + ot_id);
ot_session = enigma.create({
schema,
url: config.wssUrl + ot_id,
createSocket: url => new WebSocket(url)
});
ot_global = await ot_session.open();
ot_enigma = await ot_global.openDoc(ot_id);
}
}
async function getEnigma_tt() {
if (!tt_enigma) {
console.log('Opening enigma session to temp target-copy app ' + tt_id);
tt_session = enigma.create({
schema,
url: config.wssUrl + tt_id,
createSocket: url => new WebSocket(url)
});
tt_global = await tt_session.open();
tt_enigma = await tt_global.openDoc(tt_id);
}
}
async function closeEnigma_os() {
if (os_session) {
console.log('Closing enigma session to source app ' + os_id);
await os_session.close();
os_session = null;
os_global = null;
os_enigma = null;
};
}
async function closeEnigma_ts() {
if (ts_session) {
console.log('Closing enigma session to temp source-copy app ' + ts_id);
await ts_session.close();
ts_session = null;
ts_global = null;
ts_enigma = null;
};
}
async function closeEnigma_ot() {
if (ot_session) {
console.log('Closing enigma session to target app ' + ot_id);
await ot_session.close();
ot_session = null;
ot_global = null;
ot_enigma = null;
};
}
async function closeEnigma_tt() {
if (tt_session) {
console.log('Closing enigma session to temp target-copy app ' + tt_id);
await tt_session.close();
tt_session = null;
tt_global = null;
tt_enigma = null;
};
}
async function getOtSheets() {
// gets a list of sheets of the OT app (original target) organized in an array
var sheetList = {};
const res = await qrsCall('GET', config.qrsUrl + "app/object?filter=objectType eq 'sheet' and app.id eq " + ot_id, httpHeader);
for (var s = 0; s < res.length; s++) {
sheetList[res[s].id] = res[s].name;
}
console.log('sheetList before', sheetList)
return sheetList;
}
async function cleanUpSheets(shListBefore) {
// function deletes each sheet that is now in the app which is
// not in the shListBefore object. Only exception is, if the sheet
// description contains the same {tag} (in angular brackets) that
// the app title has. If the app title has no {tag} in angular
// brackets, this exception will not fire and any new sheet be removed
var shListAfter = {};
var appTitle = await qrsCall('GET', config.qrsUrl + "app/" + ot_id, httpHeader);
var appTitle = appTitle.name;
var appTag = appTitle.split('{')[1].split('}')[0];
if (appTag.length > 0) appTag = '{' + appTag + '}';
// console.log('appTitle', appTitle, 'appTag', appTag);
const res = await qrsCall('GET', config.qrsUrl + "app/object?filter=objectType eq 'sheet' and app.id eq " + ot_id, httpHeader);
for (var s = 0; s < res.length; s++) {
shListAfter[res[s].id] = {
name: res[s].name,
description: res[s].description
};
}
// console.log('sheetList after', shListAfter);
for (const objId in shListAfter) {
if (!shListBefore.hasOwnProperty(objId)) {
if (appTag.length > 0 && shListAfter[objId].description.indexOf(appTag) > -1) {
console.log('Sheet "' + shListAfter[objId].name + '" is new but tagged for this app. Keeping.');
} else {
console.log('Sheet "' + shListAfter[objId].name + '" is new. Deleting ' + objId);
await qrsCall('DELETE', config.qrsUrl + "app/object/" + objId, httpHeader);
}
}
}
}
// iterate through the steps found in DOM model
for (var i = 0; i < steps.length; i++) {
//$('#steplist_parent tr').each(async function (i, e) {
const step = steps[i];
var res;
// next app, reset variables
if (step.nextApp > 0) {
console.log('--next app--');
//if (os_session) { await os_session.close() };
//if (ts_session) { await ts_session.close() };
if (ot_session) { await ot_session.close() };
if (tt_session) { await tt_session.close() };
// os_id remains unchanged
// oss remains unchanged;
// ts_id remains unchanged;
// tss remains unchanged;
tt_id = null;
tts = null;
ot_id = step.trgtAppId;
ots = null;
ot_sh = null;
}
console.log('step ' + step.number, step.actionId, 'trgt', ot_id);
$('#stepscounter').text('(' + step.number + ' of ' + steps.length + ')');
$('#step' + step.number + ' svg').addClass('busy');
if ($('#step' + step.number).length > 0) {
// scroll the current step into focus
document.getElementById('step' + step.number).scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
}
try {
var enRet
// to understand the flow best, see this flow diagram:
// https://raw.githubusercontent.com/ChristofSchwarz/db_mash_databridgehub/main/pics/actionflow.png
switch (step.actionId) {
//------------------------------------------------------------------------
case 'cos': //Copy source app as temp source-copy
res = await qrsCall('POST', config.qrsUrl + 'app/' + os_id + '/copy', httpHeader);
ts_id = res.id;
break;
//------------------------------------------------------------------------
case 'cot': //Copy target app as temp target-copy
res = await qrsCall('POST', config.qrsUrl + 'app/' + ot_id + '/copy', httpHeader);
tt_id = res.id;
break;
//------------------------------------------------------------------------
case 'dts': //Delete temp source-copy
await closeEnigma_ts();
res = await qrsCall('DELETE', config.qrsUrl + 'app/' + ts_id, httpHeader);
break;
//------------------------------------------------------------------------
case 'dtt': //Delete temp target-copy
await closeEnigma_tt();
res = await qrsCall('DELETE', config.qrsUrl + 'app/' + tt_id, httpHeader);
break;
//------------------------------------------------------------------------
case 'goss': //Get script of original source app
await getEnigma_os();
oss = await os_enigma.getScript();
break;
//------------------------------------------------------------------------
case 'gots': //Get script of original target
await getEnigma_ot();
ots = await ot_enigma.getScript();
break;
//------------------------------------------------------------------------
case 'gtss': //Get script of temp source-copy
await getEnigma_ts();
tss = await ts_enigma.getScript();
break;
//------------------------------------------------------------------------
case 'gtts': //Get script of temp target-copy
await getEnigma_tt();
tts = await tt_enigma.getScript();
break;
//------------------------------------------------------------------------
case 'os2ot': //Source app replaces target app
if (noNewSheets) ot_sh = await getOtSheets();
await qrsCall('PUT', config.qrsUrl + 'app/' + os_id + '/replace?app=' + ot_id, httpHeader);
if (noNewSheets) await cleanUpSheets(ot_sh);
break;
//------------------------------------------------------------------------
case 'osd2tt': //Copy source data to target copy (binary)
await getEnigma_tt();
await tt_enigma.setScript('BINARY [lib://' + settings.dataConnection + '/' + os_id + '];');
await tt_enigma.doSave();
console.log('tt_enigma.doReload ... Temp target-copy app is binary-loading from original source app');
console.log('BINARY [lib://' + settings.dataConnection + '/' + os_id + '];');
enRet = await tt_enigma.doReload();
if (!enRet) {
console.log('App reload failed. Check out ' + location.href.split('/extensions')[0]
+ '/dataloadeditor/app/' + tt_id);
alert('App reload failed');
$('#step' + step.number + ' svg').removeClass('busy');
$('#step' + step.number + ' .checkfailed').show();
return false;
}
await tt_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'oss2ot': //Copy source script to target
await getEnigma_ot();
await ot_enigma.setScript(oss);
await ot_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'oss2tt': //Copy source script to target-copy
await getEnigma_tt();
await tt_enigma.setScript(oss);
await tt_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'otd2ts': //Copy target data to source-copy (binary)
//await getEnigma_ot();
//const ot_hash = await ot_enigma.evaluate(qlikFormulaDatamodelHash);
//console.log('ot (original target app) has datamodel hash ' + ot_hash);
//await closeEnigma_ot();
await getEnigma_ts();
await ts_enigma.setScript('BINARY [lib://' + settings.dataConnection + '/' + ot_id + '];');
await ts_enigma.doSave();
console.log('ts_enigma.doReload ... Temp source-copy app is binary-loading from original target app');
console.log('BINARY [lib://' + settings.dataConnection + '/' + ot_id + '];');
enRet = await ts_enigma.doReload();
if (!enRet) {
console.log('App reload failed. Check out ' + location.href.split('/extensions')[0]
+ '/dataloadeditor/app/' + ts_id);
alert('App reload failed');
$('#step' + step.number + ' svg').removeClass('busy');
$('#step' + step.number + ' .checkfailed').show();
return false;
}
await ts_enigma.doSave();
// const ts_hash = await ts_enigma.evaluate(qlikFormulaDatamodelHash);
// console.log('ts (temp source-copy app) has datamodel hash ' + ts_hash);
break;
//------------------------------------------------------------------------
case 'otr': //Source app replaces target app
await qrsCall('POST', config.qrsUrl + 'app/' + ot_id + '/reload', httpHeader);
break;
//------------------------------------------------------------------------
case 'ottag': //Set tags of original target app
//console.log('ottag: addTag', addTag, 'removeTag', removeTag);
var otInfo = await qrsCall('GET', config.qrsUrl + "app/" + ot_id, httpHeader);
//console.log('Tags before', otInfo.tags);
var newTags = [];
var alreadyInTags = false;
for (var t = 0; t < otInfo.tags.length; t++) {
if (otInfo.tags[t].name != removeTag) newTags.push({ id: otInfo.tags[t].id });
if (otInfo.tags[t].name == addTag) alreadyInTags = true;
};
if (!alreadyInTags && addTag) {
var addTagId = await qrsCall('GET', config.qrsUrl + "tag?filter=name eq '" + addTag + "'", httpHeader);
if (addTagId.length > 0) {
addTagId = addTagId[0].id;
console.log('add existing tag ' + addTag, addTagId);
} else {
addTagId = await qrsCall('POST', config.qrsUrl + "tag", httpHeader, JSON.stringify({ name: addTag }));
addTagId = addTagId.id;
console.log('add new tag ' + addTag, addTagId);
}
newTags.push({ id: addTagId });
}
//console.log('Tags after', newTags);
await qrsCall('PUT', config.qrsUrl + "app/" + ot_id, httpHeader,
JSON.stringify({ modifiedDate: "2199-12-31T23:59:59.999Z", tags: newTags }));
break;
//------------------------------------------------------------------------
case 'ots2ts': //Copy target script to source-copy
await getEnigma_ts();
await ts_enigma.setScript(ots);
await ts_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'rtss': //Revert script of temp source app
if (!ts_enigma) { alert('Enigma session ts_enigma must be open by now.'); return false; }
await ts_enigma.setScript(tss);
await ts_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'rtts': //Revert script of temp target app
if (!tt_enigma) { alert('Enigma session tt_enigma must be open by now.'); return false; }
await tt_enigma.setScript(tts);
await tt_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'ts2ot': //Temp source copy replaces target app
if (noNewSheets) ot_sh = await getOtSheets();
await qrsCall('PUT', config.qrsUrl + 'app/' + ts_id + '/replace?app=' + ot_id, httpHeader);
if (noNewSheets) await cleanUpSheets(ot_sh);
break;
//------------------------------------------------------------------------
case 'tsd2tt': //Copy source-copy's data to target copy (binary)
await getEnigma_tt();
await tt_enigma.setScript('BINARY [lib://' + settings.dataConnection + '/' + ts_id + '];');
await tt_enigma.doSave();
console.log('tt_enigma.doReload ... Temp target-copy app is binary-loading from temp source-copy');
console.log('BINARY [lib://' + settings.dataConnection + '/' + ts_id + '];');
enRet = await tt_enigma.doReload();
if (!enRet) {
console.log('App reload failed. Check out ' + location.href.split('/extensions')[0]
+ '/dataloadeditor/app/' + ts_id);
alert('App reload failed');
$('#step' + step.number + ' svg').removeClass('busy');
$('#step' + step.number + ' .checkfailed').show();
return false;
}
await tt_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'tss2ot': //Copy source-copy's script to target
await getEnigma_ot();
await ot_enigma.setScript(tss);
await ot_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'tss2tt': //Copy source-copy's script to target-copy
await getEnigma_tt();
await tt_enigma.setScript(tss);
await tt_enigma.doSave();
break;
//------------------------------------------------------------------------
case 'tt2ot': //Temp target-copy replaces target app
if (noNewSheets) ot_sh = await getOtSheets();
await qrsCall('PUT', config.qrsUrl + 'app/' + tt_id + '/replace?app=' + ot_id, httpHeader);
if (noNewSheets) await cleanUpSheets(ot_sh);
break;
//------------------------------------------------------------------------
case 'tts2ts': //Copy target-copy's script to source-copy
//console.log('tts2ts', tts.substr(0,100), ts_id);
await getEnigma_ts();
await ts_enigma.setScript(tts);
await ts_enigma.doSave();
break;