-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathprocess_ui.js
988 lines (886 loc) · 37.8 KB
/
process_ui.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
var ProcessArg = {
VALUE:0,
INPUTID:1,
FEEDID:2,
NONE:3,
TEXT:4,
SCHEDULEID:5
}
var processlist_ui =
{
contexttype: 0, // Editor type (0:input, 1:feed/virtual)
contextid: 0, // The current inputid or virtual feed id being edited
contextprocesslist: [], // The current process list being edited
processlist: [], // Cache this lists
feedlist:[],
inputlist:[],
schedulelist:[],
init_done: -1, // when 0 all lists are loaded
engines_hidden:[],
has_redis: 0,
table: typeof table !== "undefined" ? table : null,
'draw':function(){
var i = 0;
var out="";
// console.log("contextid=" + this.contextid);
// console.log("contextprocesslist=" + this.contextprocesslist);
if (this.contextprocesslist.length==0) {
$("#process-table").hide();
$("#noprocess").show();
} else {
$("#process-table").show();
$("#noprocess").hide();
for (z in this.contextprocesslist) {
out += '<tr>';
// Move process up or down
out += '<td>';
if (i > 0) {
out += '<a class="move-process" title="'+_Tr("Move up")+'" processid='+i+' moveby=-1 ><i class="icon-arrow-up"></i></a>';
} else {
out += "<span style='display: inline-block; width:14px; ' />";
}
if (i < this.contextprocesslist.length-1) {
out += "<a class='move-process' title='"+_Tr("Move down")+"' processid="+i+" moveby=1 ><i class='icon-arrow-down'></i></a>";
}
out += '</td>';
// Process name and argument
var processkey = this.contextprocesslist[z][0];
var arg = {};
var lastvalue = "";
var processname = "";
processkey = this.getProcessKeyById(processkey); // convert id numbers to key names (backward compatible)
if (this.processlist[processkey] != undefined) {
var procneedredis = (this.has_redis == 0 && this.processlist[processkey]['requireredis'] != undefined && this.processlist[processkey]['requireredis'] == true ? 1 : 0);
if (this.processlist[processkey]['internalerror'] !== undefined && this.processlist[processkey]['internalerror'] == true) {
arg.text = this.processlist[processkey]['internalerror_desc']
// arg += "<span class='label label-important' title='Value'>" + this.processlist[processkey]['internalerror_desc'] + "</span>";
processname = "<span class='label label-important' title='Value'>" + this.processlist[processkey][0] + "</span>";
}
else if (procneedredis) {
// arg += "<span class='label label-important' title='Value'>Process ´"+processkey+"´ not available. Redis not installed.</span>";
arg.text = "Process '"+processkey+"' not available. Redis not installed."
processname = this.processlist[processkey].name;
}
else {
// Check ProcessArg Type
switch(this.processlist[processkey].argtype) {
case ProcessArg.VALUE:
arg.text = this.contextprocesslist[z][1]
arg.title = _Tr("Value")
arg.icon = 'icon-edit'
break;
case ProcessArg.INPUTID:
var inpid = this.contextprocesslist[z][1];
if (this.inputlist[inpid]!=undefined) {
arg.text = "Node "+this.inputlist[inpid].nodeid+":"+this.inputlist[inpid].name+' ' + (this.inputlist[inpid].description || '')
arg.title = _Tr("Input")+" "+inpid
arg.icon = 'icon-signal'
lastvalue = (this.inputlist[inpid].value*1).toFixed(2);
} else {
arg.text = 'Input "+schid+" does not exists or was deleted'
}
break;
case ProcessArg.FEEDID:
var feedid = this.contextprocesslist[z][1];
if (this.feedlist[feedid]!=undefined) {
arg.text = (this.feedlist[feedid].tag || '') + ': '+this.feedlist[feedid].name
arg.title = _Tr("Feed")+" "+feedid
arg.icon = 'icon-list-alt'
var feedviewpath = "graph/";
if (_SETTINGS && _SETTINGS.hasOwnProperty('feedviewpath') && _SETTINGS.feedviewpath !== "") {
var feedviewpath = _SETTINGS.feedviewpath;
}
arg.href = [path, feedviewpath, feedid].join("");
lastvalue = (this.feedlist[feedid].value*1).toFixed(2);
} else {
arg.text = 'Feedid "+feedid+" does not exists or was deleted'
}
break;
case ProcessArg.TEXT:
arg.title = _Tr("Text")
arg.text = this.contextprocesslist[z][1]
arg.icon = 'icon-edit'
break;
case ProcessArg.SCHEDULEID:
var schid = this.contextprocesslist[z][1];
if (this.schedulelist[schid]!=undefined) {
arg.title = _Tr("Schedule")+" "+schid
arg.text = this.schedulelist[schid].name
arg.icon = 'icon-time'
} else {
arg.text = "Schedule "+schid+" does not exists or was deleted"
}
break;
}
processname = this.processlist[processkey].name;
}
}
else {
processname = "UNSUPPORTED";
arg.text = "Process ´"+processkey+"´ not available. Module missing?"
}
// create the badge markup to display the process argument detail
label = ""
if(arg.text){
label += arg.href ? '<a href="'+arg.href+'" class="text-info"' : '<span class="muted"'
label += ' title="'+arg.title+'"'
label += ">"
label += arg.icon ? '<i class="'+arg.icon+'"></i> ' : ''
label += arg.text || arg.title
label += arg.href ? '</a>':'</span>'
}
try {
tag = `<span title="${this.processlist[processkey].description.replace(/<(?:.|\n)*?>/gm, '')}"
style="cursor:help"
class="fw-label overflow-hidden label ${this.argtypes[this.processlist[processkey].argtype].cssClass}">${this.processlist[processkey].short.replace(/>/g, ">").replace(/</g, "<")}</span>`
} catch (e) {
tag = ""
}
try {
latest = lastvalue ? `<small title="Last recorded ${this.processlist[processkey].argtype} value" class="muted">(${lastvalue})</small>` : ''
} catch (e) {
latest = ""
}
out += '<td>'+(i+1)+'</td><td>'+processname+'</td><td style="text-align:right">'+tag+'</td><td>'+label+'</td><td>'+latest+'</td>';
// Delete process button (icon)
out += '<td><a class="edit-process" title="'+_Tr("Edit")+'" processid='+i+'><i class="icon-pencil" style="cursor:pointer"></i></a></td>';
out += '<td><a class="delete-process" title="'+_Tr("Delete")+'" processid='+i+'><i class="icon-trash" style="cursor:pointer"></i></a></td>';
out += '</tr>';
i++; // process id
}
}
$('#process-table-elements').html(out);
},
'drawpreview':function(processlist,input){
if (!processlist) return "";
var localprocesslist = processlist_ui.decode(processlist);
if (localprocesslist.length==0) {
return ""
} else {
var out = [];
// create coloured link or span for each process
for(b of this.getBadges(processlist,input)){
let markup = []
markup.push(b.href ? '<a target="_blank" href="'+b.href+'"' : '<span')
markup.push(' class="label '+b.cssClass+'" title="'+b.title+'">')
markup.push((b.text).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'))
markup.push(b.href ? '</a> ' : '</span> ')
out.push(markup.join(''));
}
return out.join('');
}
},
/**
* return array of objects with id,id_num properties
*/
'backward_compatible_list': function(){
if(!this.processlist) return
let pl = this.processlist
let ids = [];
Object.keys(pl).forEach(function(key) {
ids.push({id:key,id_num: pl[key].id_num})
});
return ids
},
/**
* return process "name" when given a valid id (if id not number original input returned)
*/
'getProcessKeyById': function(id){
id_int = parseInt(id)
if (isNaN(id_int)) return id
old_ids = this.backward_compatible_list()
// add numeric and textual ids (backward compatible)
for (id2 in old_ids) {
if (old_ids[id2].id_num === id_int){
return old_ids[id2].id
}
}
},
'argtypes': {
0: {cssClass: 'label-important', title: 'Value: {longText} - {value}'},
1: {cssClass: 'label-warning', title: 'Input: {longText} - ({input.nodeid}:{input.name}) {input.description}'},
2: {cssClass: 'label-info', title: 'Feed: {longText} - ({feed.tag}:{feed.name}) [{feed.id}]'},
3: {cssClass: 'label-important', title: 'Text: {longText} - {value}'},
4: {cssClass: 'label-info', title: 'Topic: {longText} - {value}'},
5: {cssClass: 'label-warning', title: 'Schedule: {longText} - {schedule.name}'}
},
'getBadges': function (processlist,input) {
if (!processlist) return ""
var processPairs = processlist.split(",")
// create empty list of badges
let badges = []
for (z in processPairs)
{
// add badge to list or add a blank one if there are any issues.
let badge = {}
var keyvalue = processPairs[z].split(":")
var key = parseInt(keyvalue[0])
key = isNaN(key) ? keyvalue[0]: this.getProcessKeyById(key);
badge.value = keyvalue[1]
badge.process = this.processlist.hasOwnProperty(key) ? this.processlist[key] : false
if(this.init_done === 0 && badge.process!==false){
// set badge properties
badge.type = this.argtypes[badge.process.argtype]
badge.typeName = badge.type.name
badge.cssClass = badge.type.cssClass
var feedviewpath = "graph/";
if (_SETTINGS && _SETTINGS.hasOwnProperty('feedviewpath') && _SETTINGS.feedviewpath !== "") {
var feedviewpath = _SETTINGS.feedviewpath;
}
badge.href = badge.process.argtype == ProcessArg.FEEDID ? [path, feedviewpath, badge.value].join("") : false;
badge.text = badge.process.short || ''
badge.longText = badge.process.name
badge.input = input
badge.feed = this.feedlist[badge.value] || {}
badge.schedule = this.schedulelist[badge.value] || {}
badge.title = badge.type.title.format(badge);
// pass the collected badge object as values for the title string template
badges.push(badge);
} else if(this.has_redis == 0 && badge.process['requireredis'] !== undefined && badge.process['requireredis'] == true ? 1 : 0){
// no reids
badges.push({
text: badge.process['internalerror_reason'],
title: badge.process['internalerror_desc'],
cssClass: 'badge-important',
href: false
})
} else if(!badge.value){
// input,feed or schedule doesnt exist
badges.push({
title: '{typeName} {value} does not exist or was deleted'.format(badge),
text: 'ERROR',
cssClass: 'badge-important',
href: false
})
} else if(!badge.process){
// process not available
badges.push({
title: '{typeName} {value} does not exist or was deleted'.format(badge),
text: 'UNSUPPORTED',
cssClass: 'badge-important',
href: false
})
} else {
// default else
badges.push({
text: ' 🕒… ',
title: '',
cssClass: 'muted',
href: false
})
}
}
// console.log(badges)
return badges;
},
'group_drawerror':function(processlist){
if (!processlist) return "";
var localprocesslist = processlist_ui.decode(processlist);
if (localprocesslist.length==0) {
return ""
} else {
var out = "";
if (this.init_done === 0)
{
for (z in localprocesslist) {
// Process name and argument
var processkey = parseInt(localprocesslist[z][0]);
processkey = isNaN(processkey) ? localprocesslist[z][0]: this.getProcessKeyById(processkey);
if (this.processlist[processkey] != undefined) {
var procneedredis = (this.has_redis == 0 && this.processlist[processkey]['requireredis'] !== undefined && this.processlist[processkey]['requireredis'] == true ? 1 : 0);
if (this.processlist[processkey]['internalerror'] !== undefined && this.processlist[processkey]['internalerror'] == true) {
out += "<span class='badge badge-important' title='" + this.processlist[processkey]['internalerror_desc'] + "'>"+ this.processlist[processkey]['internalerror_reason'] + "</span> "
}
else if (procneedredis) {
out += "<span class='badge badge-important' title='Process ´"+processkey+"´ not available. Redis not installed.'>NO REDIS</span> "
} else {
// Check ProcessArg Type
value = localprocesslist[z][1];
switch(this.processlist[processkey].argtype) {
case ProcessArg.INPUTID: //INPUTID
var inpid = localprocesslist[z][1];
if (this.inputlist[value]==undefined) {
out += "<span class='badge badge-important' title='Input "+value+" does not exists or was deleted'>ERROR</span> "
}
break;
case ProcessArg.FEEDID: //FEEDID
if (this.feedlist[value]==undefined) {
out += "<span class='badge badge-important' title='Feedid "+value+" does not exists or was deleted'>ERROR</span> "
}
break;
case ProcessArg.SCHEDULEID: // SCHEDULEID
if (this.schedulelist[value]==undefined) {
out += "<span class='badge badge-important' title='Schedule "+value+" does not exists or was deleted'>ERROR</span> "
}
break;
}
}
} else {
out += "<span class='badge badge-important' title='Process ´"+processkey+"´ not available. Module missing?'>UNSUPPORTED</span> "
}
if (out != "") return out; // return first error
}
}
return out;
}
},
'events':function(){
$("#processlist-ui #feed-engine").change(function(){
var engine = $(this).val();
if (engine==6 || engine==5 || engine==4 || engine==1) {
$("#feed-interval").show();
}
else {
$("#feed-interval").hide();
}
if (engine==8 || engine==0) {
$("#feed-table").empty().show();
}
else {
$("#feed-table").hide();
}
});
$('#processlist-ui #process-add, #processlist-ui #process-edit').click(function(){
var processid = $('#process-select').val();
var process = processlist_ui.processlist[processid];
var arg = '';
// Check ProcessArg Type
switch(process.argtype) {
case ProcessArg.VALUE: // VALUE (scale, offset)
arg = $("#value-input").val();
arg = parseFloat(arg.replace(",", "."));
if (isNaN(arg)) {
alert('ERROR: Value must be a valid number');
return false;
}
break;
case ProcessArg.INPUTID: //INPUTID (* / + - by input)
arg = $("#input-select").val();
break;
case ProcessArg.FEEDID: //FEEDID
var feedid = $("#feed-select").val();
if (feedid==-1) {
var feedname = $('#new-feed-name').val();
var feedtag = $('#new-feed-tag').val();
var engine = $('#feed-engine').val();
var options = {};
if (engine==6 || engine==5 || engine==4 || engine==1) {
let interval = $('#feed-interval').val();
if (interval=="") {
alert('ERROR: Please select a feed interval');
return false;
}
options = {"interval":interval};
}
else if (engine==8 || engine==0) {
options = {"name":$('#feed-table').val()};
}
if (feedname == '') {
alert('ERROR: Please enter a feed name');
return false;
}
var unit = '';
if (process.unit!=undefined) unit = process.unit;
var result = feed.create(feedtag,feedname,engine,options,unit);
feedid = result.feedid;
if (!result.success || feedid<1) {
alert('ERROR: Feed could not be created, '+result.message);
return false;
}
processlist_ui.feedlist[feedid] = {'id':feedid, 'name':feedname,'value':'n/a','tag':feedtag};
processlist_ui.showfeedoptions(processid); // Refresh Feedlist
}
arg = feedid;
break;
case "none": // NONE
arg = 0;
break;
case ProcessArg.TEXT: // TEXT
arg = $("#text-input").val();
break;
case ProcessArg.SCHEDULEID: // SCHEDULEID
arg = $("#schedule-select").val();
break;
}
if ($(this).attr("id") == "process-edit") {
processlist_ui.contextprocesslist[$("#type-btn-edit").attr('curpos')] = ([processid,""+arg]);
$("#process-header-add").show();
$("#process-header-edit").hide();
$("#type-btn-add").show();
$("#type-btn-edit").hide();
processlist_ui.scrollto($("a.edit-process[processid='"+$("#type-btn-edit").attr('curpos')+"']"));
} else {
processlist_ui.contextprocesslist.push([processid,""+arg]);
}
processlist_ui.draw();
processlist_ui.modified();
});
$('#processlist-ui #process-select').change(function(){
var processid = $(this).val();
$("#description").html("");
$("#type-value").hide();
$("#type-text").hide();
$("#type-input").hide();
$("#type-feed").hide();
$("#type-schedule").hide();
// Check ProcessArg Type
if (processid) {
switch(processlist_ui.processlist[processid].argtype) {
case ProcessArg.VALUE: // VALUE
$("#type-value").show();
break;
case ProcessArg.INPUTID: //INPUTID
$("#type-input").show();
break;
case ProcessArg.FEEDID: //FEEDID
$("#type-feed").show();
processlist_ui.showfeedoptions(processid);
break;
case ProcessArg.TEXT: // TEXT
$("#type-text").show();
break;
case ProcessArg.SCHEDULEID: // SCHEDULEID
$("#type-schedule").show();
break;
}
if (processlist_ui.processlist[processid]['description'] === undefined || processlist_ui.processlist[processid]['description'] =="") {
$("#description").html("<b style='color: orange'>No process description available for process '"+processlist_ui.processlist[processid][0]+"' with id '"+processid+"'.<br>Add a description to Module\\<i>module_name</i>\\<i>module_name</i>_processlist.php in process_list() function, $list[] array at the 'desc' key.</b><br>Please <a target='_blank' href='https://github.com/emoncms/emoncms/issues/new'>click here</a> and paste the text above to ask a developer to include a process description.</b>");
} else {
$("#description").html(processlist_ui.processlist[processid]['description']);
var does_modify = "<p><b>Output:</b> "+_Tr("Modified value passed onto next process step.")+"</p>";
var does_not_modify = "<p><b>Output:</b> "+_Tr("Does NOT modify value passed onto next process step.")+"</p>";
var redis_required = "<p><b>REDIS:</b> "+_Tr("Requires REDIS.")+"</p>";
var help = _Tr("Click here for additional information about this process.");
if ('helpurl' in processlist_ui.processlist[processid] &&
typeof processlist_ui.processlist[processid]['helpurl'] === 'string') {
$("#description").append('<p><a href="' + processlist_ui.processlist[processid]['help_url'] + '">' + help+'</p>');
}
if ('nochange' in processlist_ui.processlist[processid] &&
processlist_ui.processlist[processid]['nochange'] == true) {
$("#description").append(does_not_modify);
} else {
$("#description").append(does_modify);
}
if ('requireredis' in processlist_ui.processlist[processid] &&
processlist_ui.processlist[processid]['requireredis'] == true) {
$("#description").append(redis_required);
}
}//end of if proccessid
}
});
$('#processlist-ui #feed-select').change(function(){
var feedid = $("#feed-select").val();
if (feedid == -1) {
$("#new-feed-name").show();
$("#new-feed-tag").show();
$('#feed-select').css({'border-radius': 0, 'border-right': 0})
$("#processlist-ui #feed-engine").change(); // select available interval for engine
// If there's only one feed engine to choose from then dont show feed engine selector
// CHAVEIRO: Commented for now so user can see what processor it's using.
//var processid = $('#process-select').val();
//var engines = processlist_ui.processlist[processid][6]; // 0:MYSQL, 5:PHPFINA
//if (engines.length > 1)
$("#feed-engine, .feed-engine-label").show();
} else {
$("#new-feed-name").hide();
$("#new-feed-tag").hide();
$('#feed-select').css({'border-radius': 4, 'border-right': 4})
$("#feed-interval").hide();
$("#feed-table").hide();
$("#feed-engine, .feed-engine-label").hide();
}
if (typeof nodes_display !== 'undefined') {
autocomplete(document.getElementById("new-feed-tag"), Object.keys(nodes_display));
}
});
$('#processlist-ui .table').on('click', '.delete-process', function(){
processlist_ui.contextprocesslist.splice($(this).attr('processid'),1);
var processid = $(this).attr('processid')*1;
processlist_ui.draw();
processlist_ui.modified();
});
$('#processlist-ui .table').on('click', '.move-process', function(){
var processid = $(this).attr('processid')*1;
var curpos = parseInt(processid);
var moveby = parseInt($(this).attr('moveby'));
var newpos = curpos + moveby;
if (newpos>=0 && newpos<processlist_ui.contextprocesslist.length){
processlist_ui.contextprocesslist = processlist_ui.array_move(processlist_ui.contextprocesslist,curpos,newpos);
processlist_ui.draw();
processlist_ui.modified();
}
});
$('#processlist-ui .table').on('click', '.edit-process', function(){
var process = processlist_ui.contextprocesslist[$(this).attr('processid')];
var processid = processlist_ui.getProcessKeyById(process[0]); // get process id name (backward compatible)
var processval = process[1];
var curpos = parseInt($(this).attr('processid'));
$("#process-header-add").hide();
$("#process-header-edit").show();
$("#type-btn-add").hide();
$("#type-btn-edit").show();
$("#type-btn-edit").attr('curpos', curpos);
if (processlist_ui.processlist[processid] == undefined) {
if (processlist_ui.contexttype == 0) {
$("#process-select").val($("#process-select option").first().val()); // default process for input context
} else {
$("#process-select").val('process__source_feed_data_time'); // default process for feed context
}
$("#processlist-ui #process-select").change(); // Force a refresh
} else {
$("#process-select").val( processlist_ui.processlist[processid]['id']);
$("#processlist-ui #process-select").change(); // Force a refresh
// Check ProcessArg Type
switch(processlist_ui.processlist[processid].argtype) {
case ProcessArg.VALUE: // VALUE
$("#value-input").val(processval);
break;
case ProcessArg.INPUTID: //INPUTID
$("#input-select").val(processval);
break;
case ProcessArg.FEEDID: //FEEDID
$("#feed-select").val(processval);
$('#processlist-ui #feed-select').change(); // refresh feed select
break;
case ProcessArg.TEXT: // TEXT
$("#text-input").val(processval);
break;
case ProcessArg.SCHEDULEID: // SCHEDULEID
$("#schedule-select").val(processval);
break;
}
}
processlist_ui.scrollto($('#process-header-edit'));
});
$('#processlist-ui #process-cancel').click(function(){
$("#process-header-add").show();
$("#process-header-edit").hide();
$("#type-btn-add").show();
$("#type-btn-edit").hide();
processlist_ui.scrollto($("a.edit-process[processid='"+$("#type-btn-edit").attr('curpos')+"']"));
});
$("#processlistModal").on('click', '#close', function(){
$("#processlistModal").modal('hide');
});
},
'showfeedoptions':function(processid){
$feedSelect = $('#feed-select');
$feedEngineSelect = $('#feed-engine');
$feedTypeSelect = $('#feed-data-type');
var prc = this.processlist[processid].function; // process function
var feedwrite = this.processlist[processid].feedwrite; // process writes to feed
var engines = this.processlist[processid].engines; // 0:MYSQL, 5:PHPFINA
var feedgroups = [];
for (z in this.feedlist) {
if (parseInt(this.feedlist[z].engine) == 7) { //input context and virtual feed and process writes to feed ?
continue; // Dont list virtual feed
}
var group = (this.feedlist[z].tag === null ? "NoGroup" : this.feedlist[z].tag);
if (group!="Deleted") {
if (!feedgroups[group]) feedgroups[group] = []
feedgroups[group].push(this.feedlist[z]);
}
}
var out = "<option value=-1>CREATE NEW:</option>";
for (z in feedgroups) {
out += "<optgroup label='"+z+"'>";
for (p in feedgroups[z]) {
out += "<option value="+feedgroups[z][p]['id']+">"+feedgroups[z][p].name+"</option>";
}
out += "</optgroup>";
}
// overwrite feed list
var lastval = $feedSelect.val();
if (lastval==null) lastval = -1;
$feedSelect.data('value',lastval);// store previous value before <select> changes
$feedSelect.html(out);
// recall the old value if available
if($feedSelect.data('value')!=""){
$feedSelect.val($feedSelect.data('value'));
$feedSelect.data('value','');
}
$feedTypeSelect.find("option").hide(); // Start by hiding all feed engine options
$feedTypeSelect.find("option").prop('disabled', true); //for IE hide (grayed out)
$feedEngineSelect.find("option").hide(); // Start by hiding all feed engine options
$feedEngineSelect.find("option").prop('disabled', true); //for IE hide (grayed out)
for (e in engines) {
$feedEngineSelect.find("option[value="+engines[e]+"]").show(); // Show only the feed engine options that are available
$feedEngineSelect.find("option[value="+engines[e]+"]").prop('disabled', false); //for IE show
}
$feedEngineSelect.hide();
$(".feed-engine-label").hide();
if (typeof(engines) != "undefined") {
$feedEngineSelect.val(engines[0]); // Select the first feed engine in the engines array by default
$feedSelect.find("option[value=-1]").show(); // enable create new feed
$feedSelect.find("option[value=-1]").prop('disabled', false); //for IE show
} else {
$feedSelect.find("option[value=-1]").hide(); // disable create new feed as we have no supported engines for this proccess
$feedSelect.find("option[value=-1]").prop('disabled', true); //for IE hide (grayed out)
for (f in this.feedlist) {
var exists = false;
$feedSelect.find('option').each(function(){
if (this.value == $feedSelect.val()) {
exists = true;
return false;
}
});
if (!exists) {
if($feedSelect.val()!=this.feedlist[f].id){
$feedSelect.val(this.feedlist[f].id); // select first feed
}
}
break;
}
}
$('#processlist-ui #feed-select').change(); // refresh feed select
},
'modified':function(){
$("#save-processlist").attr('class','btn btn-warning').text(_Tr("Changed, press to save"));
$(".feedaccesslabel").attr("href","#"); // disable access to feeds
},
'saved':function(feeds){
$("#save-processlist").attr('class','btn btn-success').text("Saved");
// compatibility input vs device view transpose
if (feeds.data!=undefined) feeds = feeds.data;
for (z in feeds) {
if (feeds.hasOwnProperty(z) && (feeds[z].id == processlist_ui.contextid)) {
feeds[z].processList = processlist_ui.encode(processlist_ui.contextprocesslist);
}
}
if (window.table!=undefined && window.table.draw!=undefined) {
table.draw();
}
if (typeof update == 'function') update()
},
'decode':function(str){
var processlist = [];
if (str != null && str != "") {
var tmp = str.split(",");
for (n in tmp) {
var process = tmp[n].split(":");
processlist.push(process);
}
}
return processlist;
},
'encode':function(array){
var parts = [];
for (z in array) {
parts.push(array[z][0]+":"+array[z][1]);
}
return parts.join(",");
},
'array_move':function(array,old_index, new_index){
if (new_index >= array.length) {
var k = new_index - array.length;
while ((k--) + 1) {
array.push(undefined);
}
}
array.splice(new_index, 0, array.splice(old_index, 1)[0]);
return array;
},
'scrollto':function(scrollTo){
var container = $('#processlist-ui');
container.animate({
scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop()
});
},
'init':function(contexttype){
this.contexttype = contexttype;
this.init_done = 4; // going to load 4 lists
// Processors Select List
$.ajax({ url: path+"process/list.json", dataType: 'json', async: true, success: function(result){
for (p in result) // for each processor
{
result[p]['feedwrite']=false;
if (result[p]['engines']!=undefined) { // processor has supported engines?
result[p]['feedwrite']=true; // If has an engine so assume process writes to feed
if (processlist_ui.engines_hidden.length > 0) {
for (var e=result[p]['engines'].length-1; e > -1; e--) { // for each processor engine
for (h in processlist_ui.engines_hidden) {
if (result[p]['engines'][e]==processlist_ui.engines_hidden[h]) { // if engine is to be hidden
result[p]['engines'].splice(e, 1); // remove engine from processor
}
}
}
}
if (result[p]['engines'].length == 0) {
result[p]['engines'] = undefined; // if processor now has no engines, undefine its array
}
}
}
processlist_ui.processlist = result;
var processgroups = [];
for (z in processlist_ui.processlist) {
var group = processlist_ui.processlist[z]['group'];
// hide the following from virtual feeds
if (processlist_ui.contexttype == 1) {
if (processlist_ui.processlist[z]['feedwrite'] == true) continue;
if (processlist_ui.processlist[z]['function'] == "sendEmail") continue;
if (processlist_ui.processlist[z]['function'] == "publish_to_mqtt") continue;
if (group=="Feed" || group=="Input") continue;
}
if (processlist_ui.contexttype == 0 && group=="Virtual") {
continue; // in input context and group name is virtual? dont show on input processlist selector
}
if (group!="Deleted") {
if (!processgroups[group]) processgroups[group] = []
processlist_ui.processlist[z]['id'] = z;
processgroups[group].push(processlist_ui.processlist[z]);
}
}
var out = "";
for (pg in processgroups) {
out += "<optgroup " + (pg == "Hidden" ? "hidden " : "") + "label='"+pg+"'>";
for (p in processgroups[pg])
{
var procdisabled = "";
var procneedredis = "";
if ((pg == "Hidden") ||
(processgroups[pg][p]['internalerror'] != undefined && processgroups[pg][p]['internalerror'] == true)
)
{
procdisabled = 'hidden';
procneedredis = "";
}
else if (processlist_ui.has_redis == 0 && processgroups[pg][p]['requireredis'] != undefined && processgroups[pg][p]['requireredis'] == true) {
procdisabled = 'disabled=""';
procneedredis = " (needs REDIS)";
}
out += "<option "+procdisabled+" value="+processgroups[pg][p]['id']+">"+processgroups[pg][p].name+procneedredis+"</option>";
}
out += "</optgroup>";
}
$("#process-select").html(out);
processlist_ui.initprogress();
// Automatic call of feed table update
if (typeof window.update_feed_list == 'function') update_feed_list();
}});
// Feeds Select List
$.ajax({ url: path+"feed/list.json", dataType: 'json', async: true, success: function(result) {
var feeds = {};
for (z in result) {
feeds[result[z].id] = result[z];
}
processlist_ui.feedlist = feeds;
processlist_ui.initprogress();
}});
// Schedule Select List
$.ajax({ url: path+"schedule/list.json", dataType: 'json', async: true, success: function(result) {
var schedules = {};
for (z in result) schedules[result[z].id] = result[z];
processlist_ui.schedulelist = schedules;
var groupname = {0:'Public',1:'Mine'};
var groups = [];
//for (z in result) schedules[result[z].id] = result[z];
for (z in processlist_ui.schedulelist) {
var group = processlist_ui.schedulelist[z].own;
group = groupname[group];
if (!groups[group]) groups[group] = [];
processlist_ui.schedulelist[z]['_index'] = z;
groups[group].push(processlist_ui.schedulelist[z]);
}
var out = "";
for (z in groups) {
out += "<optgroup label='"+z+"'>";
for (p in groups[z])
{
out += "<option value="+groups[z][p]['id']+">"+groups[z][p]['name']+(z!=groupname[1]?" ["+groups[z][p]['id']+"]":"")+"</option>";
}
out += "</optgroup>";
}
$("#schedule-select").html(out);
processlist_ui.initprogress();
}});
// Input Select List
$.ajax({ url: path+"input/list.json", dataType: 'json', async: true, success: function(result) {
var inputs = {};
for (z in result) inputs[result[z].id] = result[z];
processlist_ui.inputlist = inputs;
var groups = [];
for (z in processlist_ui.inputlist) {
var group = processlist_ui.inputlist[z]['nodeid'];
group = 'Node ' + group;
if (!groups[group]) groups[group]=[];
groups[group].push(processlist_ui.inputlist[z]);
}
var out = "";
for (z in groups) {
out += "<optgroup label='"+z+"'>";
for (p in groups[z])
{
out += "<option value="+groups[z][p]['id']+">"+groups[z][p]['name']+ ": " + groups[z][p]['description'] + "</option>";
}
out += "</optgroup>";
}
$("#input-select").html(out);
processlist_ui.initprogress();
}});
processlist_ui.events();
},
'initprogress':function(){
processlist_ui.init_done--;
if (processlist_ui.init_done == 0) {
processlist_ui.draw();
if (window.table!=undefined && window.table.draw!=undefined) table.draw();
if (processlist_ui.contexttype == 0) {
$("#process-select").val(this.getProcessKeyById(1)); // default process for input context
} else {
$("#process-select").val(this.getProcessKeyById(53)); // default process for feed context
}
$("#processlist-ui #process-select").change(); // Force a refresh
}
},
'load': function(contextid,contextprocesslist,contextname,newfeedname,newfeedtag){
this.contextid = contextid;
this.contextprocesslist = contextprocesslist;
$("#contextname").html(contextname);
$("#new-feed-name").val(newfeedname);
$("#new-feed-tag").val(newfeedtag);
$("#process-header-add").show();
$("#process-header-edit").hide();
$("#type-btn-add").show();
$("#type-btn-edit").hide();
processlist_ui.scrollto($('#processlist-ui'));
this.draw();
$("#save-processlist").attr('class','btn btn-success').text(_Tr("Not modified"));
$("#processlist-ui #process-select").change(); // Force a refresh
$("#processlistModal").modal('show'); // Show
this.adjustmodal();
},
'adjustmodal':function() {
if ($("#processlistModal").length) {
var h = $(window).height() - $("#processlistModal").position().top - 180;
$("#processlist-ui").height(h);
}
}
}
// takes plain object with key / value pairs.
// if found swaps placeholder for variable
// can handle 2 deep nested objects
if (!String.prototype.format) {
String.prototype.format = function(data) {
return this.replace(/{([\w\.-]+)}/g, function(match, placeholder) {
if (placeholder.indexOf('.') > -1){
p = placeholder.split('.')
return typeof data[p[0]] != 'undefined' ? data[p[0]][p[1]] : match
} else {
return typeof data[placeholder] != 'undefined' ? data[placeholder] : match
}
});
};
}
/**
* pre select processes dropdown
* @param {ClickEvent} event
*/
function selectProcess(event){
event.preventDefault();
processid = event.target.dataset.processid
select = document.getElementById('process-select')
select.focus()
select.value = processid
select.blur()
$(select).trigger('change');// trigger jquery event
}