/
index_m.js
1362 lines (1192 loc) · 59 KB
/
index_m.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
// This will be called by the admin adapter when the settings page loads
const ORDER = {
ASC: 'asc',
DESC: 'desc'
}
const SORT = {
linkedId: 'linkedId',
parentId: 'parentId',
parentName: 'parentName',
isLinked: 'isLinked'
}
var sortLinkedId = ORDER.ASC;
var sortParentId = ORDER.DESC;
var sortParentName = ORDER.DESC;
var currentSort = SORT.linkedId;
var currentOrder = ORDER.DESC;
var myNamespace;
var tableSizeAtStart = 0;
var Input = {};
var Label = {};
var Button = {};
var Checkbox = {};
var language = 'en';
async function load(settings, onChange) {
// Namespace bauen
myNamespace = adapter + '.' + instance;
// example: select elements with id=key and class=value and insert value
if (!settings) return;
$('.value').each(function () {
var $key = $(this);
var id = $key.attr('id');
if ($key.attr('type') === 'checkbox') {
// do not call onChange direct, because onChange could expect some arguments
$key.prop('checked', settings[id])
.on('change', () => onChange())
;
} else {
// do not call onChange direct, because onChange could expect some arguments
$key.val(settings[id])
.on('change', () => onChange())
.on('keyup', () => onChange())
;
}
});
generateJavascriptInstancesDropDown(settings);
// Divs in vars packen
initialize_Divs();
// Table erzeugen
$(`th[data-name=${currentSort}]`).text(`${$(`th[data-name=${currentSort}]`).text()} ▴`);
createTable(onChange);
if (!settings.prefixAsName) {
$('#idAsName').attr('disabled', 'disabled');
}
language = await getLanguage();
console.warn(language);
// GUI Events
await events(onChange);
onChange(false);
// reinitialize all the Materialize labels on the page if you are dynamically adding inputs:
if (M) M.updateTextFields();
}
// This will be called by the admin adapter when the user presses the save button
async function save(callback) {
// example: select elements with class=value and build settings object
var obj = {};
$('.value').each(function () {
var $this = $(this);
if ($this.attr('type') === 'checkbox') {
obj[$this.attr('id')] = $this.prop('checked');
} else {
obj[$this.attr('id')] = $this.val();
}
});
callback(obj);
}
async function initialize_Divs() {
// Inputs
Input.scriptName = $('input[id="scriptName"]');
Input.variableName = $('input[id="variableName"]');
// Labels
Label.ButtonCreateJavaScript = $('label[id="labelBtnJavascript"');
Label.labelTableEntries = $('label[id="labelTableEntries"');
// Buttons
Button.createJavaScript = $('.values-buttons[data-command="btnCreateJavascript"]');
Button.createJavaScript.attr('title', _('generate script'));
Button.repair = $('a[id="repair"');
//CheckBoxes
Checkbox.generateAllFunctions = $('input[id="generateAllFunctions"]');
Checkbox.generateVarsForAllObjectsOfInstance = $('input[id="generateVarsForAllObjectsOfInstance"]');
Checkbox.generateSetStateForReadOnly = $('input[id="generateSetStateForReadOnly"]');
Checkbox.idAsName = $('input[id="idAsName"]');
Checkbox.prefixAsName = $('input[id="prefixAsName"]');
}
//#region Table
//#region Table Data
function generateJavascriptInstancesDropDown(settings) {
socket.emit('getObjectView', 'system', 'instance', { startkey: 'system.adapter.javascript.', endkey: 'system.adapter.javascript.\u9999' }, function (err, doc) {
if (err) {
console.error(err);
} else {
if (doc.rows.length) {
var result = [];
for (var i = 0; i < doc.rows.length; i++) {
result.push(doc.rows[i].value);
}
if (result.length > 0) {
var text = '';
for (var r = 0; r < result.length; r++) {
var name = result[r]._id.substring('system.adapter.'.length);
text += '<option value="' + name + '">' + name + '</option>';
}
if (settings.javascriptInstance && settings.javascriptInstance !== '') {
$('#javascriptInstance').append(text).val(settings.javascriptInstance).select();
} else {
$('#javascriptInstance').append(text).val(result[0]._id.substring('system.adapter.'.length)).select();
}
} else {
var text = '';
text += `<option value="">${_("not installed")}</option>`;
$('#javascriptInstance').append(text).val('').select();
$('.javascriptInstanceExist').find('input').each(function () {
$(this).attr('disabled', 'disabled');
});
$('#btnJavascript').addClass('disabled')
}
}
}
});
}
async function createTable(onChange, filterText = null) {
try {
var progressBar = $('div[id="progressBar"]');
progressBar.show();
let tableData = await getTableData(); // Array für tableFkt
if (tableData && tableSizeAtStart === 0) {
tableSizeAtStart = tableData.length;
}
if (tableData && tableData.length > 0) {
$('h6[id=noTableData]').hide();
if (filterText != null) {
// Tabelle filtern
tableData = tableData.filter(function (res) {
try {
if (res.linkedId.toUpperCase().includes(filterText.toUpperCase())) {
return true;
}
if (typeof (res.linkedName) === 'string') {
if (res.linkedName.toUpperCase().includes(filterText.toUpperCase())) {
return true;
}
} else if (typeof (res.linkedName) === 'object') {
if (res.linkedName[language].toString().toUpperCase().includes(filterText.toUpperCase())) {
return true;
}
}
if (res.parentId.toUpperCase().includes(filterText.toUpperCase())) {
return true;
}
if (typeof (res.parentName) === 'string') {
if (res.parentName.toUpperCase().includes(filterText.toUpperCase())) {
return true;
}
} else if (typeof (res.parentName) === 'object') {
if (res.parentName[language].toString().toUpperCase().includes(filterText.toUpperCase())) {
return true;
}
}
return false;
} catch (err) {
reportError(`[createTable] filter entries item: ${JSON.stringify(res)}, error: ${err.message}, stack: ${err.stack}`);
return false;
}
});
}
Label.labelTableEntries.text(`Einträge: ${tableData.length} / ${tableSizeAtStart}`);
sortData(tableData, currentSort, onChange);
progressBar.hide();
} else {
$('h6[id=noTableData]').show();
progressBar.hide();
}
} catch (err) {
reportError(`[createTable] error: ${err.message}, stack: ${err.stack}`);
}
}
async function getTableData() {
try {
// Alle linkedDevices Objekte der Instanz holen
let linkedDevicesList = await getForeignObjects(myNamespace + '.*');
let tableData = [];
if (linkedDevicesList != null && Object.keys(linkedDevicesList).length > 0) {
for (var id in linkedDevicesList) {
// benötigte Daten in Array für tableFkt packen
let linkedObj = linkedDevicesList[id];
let linkedName = '';
if (linkedObj && linkedObj.common && linkedObj.common.custom && linkedObj.common.custom[myNamespace]) {
let parentId = '';
var parentName = '';
if (linkedObj.common.name) {
if (typeof (linkedObj.common.name) === 'string') {
linkedName = linkedObj.common.name;
} else if (typeof (linkedObj.common.name) === 'object') {
linkedName = linkedObj.common.name[language];
}
}
if (linkedObj.common.custom[myNamespace].isLinked) {
// Verlinkung exitsiert -> parentId übergeben
parentId = linkedObj.common.custom[myNamespace].parentId
// Name des parent Objektes holen
let parentObj = await getObject(parentId);
if (parentObj && parentObj.common && parentObj.common.name) {
if (typeof (parentObj.common.name) === 'string') {
parentName = parentObj.common.name;
} else if (typeof (parentObj.common.name) === 'object') {
parentName = parentObj.common.name[language];
}
}
}
tableData.push({ "linkedId": id, "linkedName": linkedName, "parentId": parentId, "parentName": parentName, "isLinked": linkedObj.common.custom[myNamespace].isLinked });
}
}
return tableData;
} else {
return null;
}
} catch (err) {
reportError(`[getTableData] error: ${err.message}, stack: ${err.stack}`);
}
}
function sortData(data, key, onChange) {
if (currentSort === key) {
// sort order ändert sich, order symbol im col header anpassen
let col = $(`th[data-name=${currentSort}]`);
let colText = $(`th[data-name=${currentSort}]`).text();
if (currentOrder === ORDER.ASC) {
myValues2table('events', sortByKey(data, currentSort, false), onChange, tableOnReady);
currentOrder = ORDER.DESC
if (colText.includes("▴")) {
col.text(colText.replace("▴", "▾"));
}
} else {
myValues2table('events', sortByKey(data, currentSort, true), onChange, tableOnReady);
currentOrder = ORDER.ASC
if (colText.includes("▾")) {
col.text(colText.replace("▾", "▴"));
}
}
} else {
// sort col ändert sich
currentOrder = ORDER.ASC;
currentSort = key;
$(`th[data-name=${"linkedId"}]`).text(`${$(`th[data-name=${"linkedId"}]`).text().replace(" ▴", " ").replace(" ▾", " ")}`);
$(`th[data-name=${"parentId"}]`).text(`${$(`th[data-name=${"parentId"}]`).text().replace(" ▴", " ").replace(" ▾", " ")}`);
$(`th[data-name=${"parentName"}]`).text(`${$(`th[data-name=${"parentName"}]`).text().replace(" ▴", " ").replace(" ▾", " ")}`);
$(`th[data-name=${"isLinked"}]`).text(`${$(`th[data-name=${"isLinked"}]`).text().replace(" ▴", " ").replace(" ▾", " ")}`);
myValues2table('events', sortByKey(data, currentSort, true), onChange, tableOnReady);
$(`th[data-name=${currentSort}]`).text(`${$(`th[data-name=${currentSort}]`).text()} ▴`);
}
}
function sortByKey(array, key, sortASC) {
return array.sort(function (a, b) {
var x = a[key];
var y = b[key];
if (sortASC) {
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
} else {
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}
});
}
//#endregion
//#region Table Buttons Functions
async function assignParentObject(rowNum, parentId) {
try {
let parentObj = await getObject(parentId);
// Prüfen ob ausgewähltes Objekt bereits verlinkt ist bzw. ein linkedObject ist
if (parentObj && parentObj.common && parentObj.common.custom && parentObj.common.custom[myNamespace]) {
if (parentObj.common.custom[myNamespace].isLinked) {
// ist linkedObject
showError(_("link to an already linked object of the same instance is not possible!"));
} else {
// ist bereits verlinkt
showError(_("the selected object is already linked with '%s'!", myNamespace + "." + parentObj.common.custom[myNamespace].linkedId));
}
return;
}
// linkedObject holen
let linkedId = $('#events .values-input[data-name="linkedId"][data-index="' + rowNum + '"]').val();
let linkedObject = await getObject(linkedId);
// Falls type conversion vorhanden, prüfen ob zugewiesenes parentObject vom richtigen type ist
if (linkedObject && linkedObject.common && linkedObject.common.custom && linkedObject.common.custom[myNamespace]) {
if (linkedObject.common.custom[myNamespace].parentType != parentObj.common.type) {
showError(_("the selected object is of type '%s'.<br>The linked object needs an object of type '%s'!", _(parentObj.common.type), _(linkedObject.common.custom[myNamespace].parentType)));
return;
}
}
// Befehl an Instanz schicken
sendTo(myNamespace, "assignTo", { linkedId: linkedId, parentId: parentId }, function (result) {
if (result) {
if (!result.error || result.error.length === 0) {
// Daten in table schreiben
$('#events .values-input[data-name="parentId"][data-index="' + rowNum + '"]').val(parentObj._id).trigger('change');
if (parentObj && parentObj.common && parentObj.common.name) {
$('#events .values-label[data-name="parentName"][data-index="' + rowNum + '"]').text(parentObj.common.name).trigger('change');
}
// CheckBox & Button aktivieren / deaktivieren
$('#events .values-input[data-name="isLinked"][data-index="' + rowNum + '"]').prop('checked', true).trigger('change');
$('#events .values-buttons[data-command="assignLink"][data-index="' + rowNum + '"]').attr('disabled', true).trigger('change');
$('#events .values-buttons[data-command="removeLink"][data-index="' + rowNum + '"]').attr('disabled', false).trigger('change');
$('#events .values-buttons[data-command="openCustom"][data-index="' + rowNum + '"]').attr('disabled', false).trigger('change');
} else {
reportError(result.error);
}
}
});
} catch (err) {
reportError(`[assignParentObject] error: ${err.message}, stack: ${err.stack}`);
}
}
function removeAssignedParentObjectConfirm(rowNum, parentId, linkedId) {
confirmMessage(_('do you really want to delete this link?'), _('attention'), null, [_('Cancel'), _('OK')], function (result) {
if (result === 1) {
removeAssignedParentObject(rowNum, parentId, linkedId);
}
});
}
async function removeAssignedParentObject(rowNum, parentId, linkedId) {
try {
let parentObj = await getObject(parentId);
let linkedObj = await getObject(linkedId);
// custom entfernen
parentObj.common.custom[myNamespace] = null;
linkedObj.common.custom[myNamespace].enabled = false;
linkedObj.common.icon = "linkeddevices_missing.png";
// CheckBox & Button aktivieren / deaktivieren
$('#events .values-input[data-name="parentId"][data-index="' + rowNum + '"]').val('').trigger('change');
$('#events .values-label[data-name="parentName"][data-index="' + rowNum + '"]').text('').trigger('change');
$('#events .values-input[data-name="isLinked"][data-index="' + rowNum + '"]').prop('checked', false).trigger('change');
$('#events .values-buttons[data-command="assignLink"][data-index="' + rowNum + '"]').attr('disabled', false).trigger('change');
$('#events .values-buttons[data-command="removeLink"][data-index="' + rowNum + '"]').attr('disabled', true).trigger('change');
$('#events .values-buttons[data-command="openCustom"][data-index="' + rowNum + '"]').attr('disabled', true).trigger('change');
// neue Zuweisung speichern
await setObject(parentId, parentObj);
await setObject(linkedId, linkedObj);
} catch (err) {
reportError(`[removeAssignedParentObject] error: ${err.message}, stack: ${err.stack}`);
}
}
//#endregion
//#region Mod Functions from admin adapter
function myValues2table(divId, values, onChange, onReady, maxRaw) {
if (typeof values === 'function') {
typeof onChange === 'number' ? maxRaw = onChange : maxRaw = null;
onChange = values;
values = divId;
divId = '';
}
if (typeof onReady === 'number') {
maxRaw = onReady;
onReady = null;
} else if (typeof maxRaw === 'undefined') {
maxRaw = null;
}
values = values || [];
var names = [];
var $div;
if (!divId) {
$div = $('body');
} else {
$div = $('#' + divId);
}
var $add = $div.find('.table-button-add');
$add.data('raw', values.length);
if (maxRaw) {
$add.data('maxraw', maxRaw);
}
if (!$add.data('inited')) {
$add.data('inited', true);
var addText = $add.text();
if (!isMaterialize) {
$add.button({
icons: { primary: 'ui-icon-plus' },
text: !!addText,
label: addText ? _(addText) : undefined
});
}
$add.on('click', function () {
if (!$add.data('maxraw') || ($add.data('raw') < $add.data('maxraw'))) {
var $table = $div.find('.table-values');
var values = $table.data('values');
var names = $table.data('names');
var obj = {};
for (var i = 0; i < names.length; i++) {
if (!names[i]) continue;
obj[names[i].name] = names[i].def;
}
values.push(obj);
onChange && onChange();
setTimeout(function () {
myValues2table(divId, values, onChange, onReady);
}, 100);
$add.data('raw', $add.data('raw') + 1);
} else {
confirmMessage(_('maxTableRaw') + ': ' + $add.data('maxraw'), _('maxTableRawInfo'), 'alert', ['Ok']);
}
});
}
if (values) {
var buttons = [];
var $table = $div.find('.table-values');
$table.data('values', values);
$table.find('th').each(function () {
var name = $(this).data('name');
if (name) {
var obj = {
name: name,
desc: $(this).data('desc'), // Mod: Name für Label
type: $(this).data('type') || 'text',
def: $(this).data('default'),
style: $(this).data('style'),
tdstyle: $(this).data('tdstyle')
};
if (obj.type === 'checkbox') {
if (obj.def === 'false') obj.def = false;
if (obj.def === 'true') obj.def = true;
obj.def = !!obj.def;
} else if (obj.type === 'select' || obj.type === 'select multiple') {
var vals = ($(this).data('options') || '').split(';');
obj.options = {};
for (var v = 0; v < vals.length; v++) {
var parts = vals[v].split('/');
obj.options[parts[0]] = _(parts[1] || parts[0]);
if (v === 0) obj.def = (obj.def === undefined) ? parts[0] : obj.def;
}
} else {
obj.def = obj.def || '';
}
names.push(obj);
} else {
names.push(null);
}
name = $(this).data('buttons');
if (name) {
var bs = name.split(' ');
buttons.push(bs);
} else {
buttons.push(null);
}
});
$table.data('names', names);
var text = '';
for (var v = 0; v < values.length; v++) {
var idName = values[v] && values[v].id;
if (!idName && values[v]) {
if (names[0] === '_index') {
idName = values[v][names[1]];
} else {
idName = values[v][names[0]];
}
}
text += '<tr ' + (idName ? 'data-id="' + idName + '"' : '') + ' data-index="' + v + '">';
for (var i = 0; i < names.length; i++) {
text += '<td';
var line = '';
var style = '';
var tdstyle = '';
if (names[i]) {
if (names[i].name !== '_index') {
tdstyle = names[i].tdstyle || '';
if (tdstyle && tdstyle[0] !== ';') tdstyle = ';' + tdstyle;
}
if (names[i].name === '_index') {
style = (names[i].style ? names[i].style : 'text-align: right;');
line += (v + 1);
} else if (names[i].type === 'checkbox') {
// Mod: disabled Checkbox
line += '<input style="' + (names[i].style || '') + '" class="values-input filled-in" type="checkbox" data-index="' + v + '" data-name="' + names[i].name + '" ' + (values[v][names[i].name] ? 'checked' : '') + '" data-old-value="' + (values[v][names[i].name] === undefined ? '' : values[v][names[i].name]) + '" disabled="true"/>';
if (isMaterialize) {
line += '<span></span>';
}
} else if (names[i].type.substring(0, 6) === 'select') {
line += (names[i].type.substring(7, 16) === 'multiple' ? '<select multiple style="' : '<select style="') + (names[i].style ? names[i].style : 'width: 100%') + '" class="values-input" data-index="' + v + '" data-name="' + names[i].name + '">';
var options;
if (names[i].name === 'room') {
options = $table.data('rooms');
} else if (names[i].name === 'func') {
options = $table.data('functions');
if (names[i].type === 'select multiple') delete options[_('none')];
} else {
options = names[i].options;
}
var val = (values[v][names[i].name] === undefined ? '' : values[v][names[i].name]);
if (typeof val !== 'object') val = [val];
for (var p in options) {
line += '<option value="' + p + '" ' + (val.indexOf(p) !== -1 ? ' selected' : '') + '>' + options[p] + '</option>';
}
line += '</select>';
} else {
// Mod: readonly input (text) & label mit Name
line += '<input class="values-input" style="' + (names[i].style ? names[i].style : 'width: 100%') + '" type="' + names[i].type + '" data-index="' + v + '" data-name="' + names[i].name + '" readonly/>';
line += '<label class="values-label" style="font-style: italic; font-weight: bold; color: #2196f3;" type="' + names[i].type + '" data-index="' + v + '" data-name="' + names[i].desc + '"/>';
}
}
if (buttons[i]) {
style = 'text-align: center; white-space: nowrap;';
for (var b = 0; b < buttons[i].length; b++) {
if ((!v && buttons[i][b] === 'up') || (v === values.length - 1 && buttons[i][b] === 'down')) {
if (isMaterialize) {
line += '<a data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light disabled"><i class="material-icons">add</i></a>';
} else {
line += '<button data-command="' + buttons[i][b] + '" class="values-buttons" disabled> </button>';
}
} else if (buttons[i][b] === 'assignLink') {
// Mod: abhängig ob verlinkt ist Button disabled
if (isMaterialize) {
if (JSON.stringify(values[v].isLinked) === "true") {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light" disabled="true"><i class="material-icons">link</i></a>';
} else {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light"><i class="material-icons">link</i></a>';
}
} else {
if (JSON.stringify(values[v].isLinked) === "true") {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons" disabled="true"></button>';
} else {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons"></button>';
}
}
} else if (buttons[i][b] === 'removeLink') {
// Mod: abhängig ob verlinkt ist Button disabled
if (isMaterialize) {
if (JSON.stringify(values[v].isLinked) === "true") {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light"><i class="material-icons">link_off</i></a>';
} else {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light" disabled="true"><i class="material-icons">link_off</i></a>';
}
} else {
if (JSON.stringify(values[v].isLinked) === "true") {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons" disabled="true"></button>';
} else {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons"></button>';
}
}
} else if (buttons[i][b] === 'openCustom') {
if (isMaterialize) {
if (JSON.stringify(values[v].isLinked) === "true") {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light"><i class="material-icons">link</i></a>';
} else {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light" disabled="true"><i class="material-icons">link</i></a>';
}
} else {
if (JSON.stringify(values[v].isLinked) === "true") {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons"></button>';
} else {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons" disabled="true"></button>';
}
}
} else {
if (isMaterialize) {
line += '<a data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons btn-floating btn-small waves-effect waves-light"><i class="material-icons">add</i></a>';
} else {
line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons"></button>';
}
}
}
}
if (style.length || tdstyle.length) {
text += ' style="' + style + tdstyle + '">' + line + '</td>';
} else {
text += '>' + line + '</td>';
}
}
text += '</tr>';
}
var $lines = $div.find('.table-lines');
if (!$lines.length) {
$table.append('<tbody class="table-lines"></tbody>');
$lines = $div.find('.table-lines');
}
$lines.html(text);
$lines.find('.values-input').each(function () {
var $this = $(this);
var type = $this.attr('type');
var name = $this.data('name');
var id = $this.data('index');
$this.data('old-value', values[id][name]);
if (type === 'checkbox') {
$this.prop('checked', values[id][name]);
} else {
$this.val(values[id][name]);
}
});
// Mod: label mit Name
$lines.find('.values-label').each(function () {
var $this = $(this);
var name = $this.data('name');
var id = $this.data('index');
$this.data('old-value', values[id][name]);
$this.text(values[id][name]);
});
$lines.find('.values-buttons').each(function () {
var command = $(this).data('command');
if (command === 'copy') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-copy' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('content_copy');
}
$(this).on('click', function () {
if (!$add.data('maxraw') || ($add.data('raw') < $add.data('maxraw'))) {
var id = $(this).data('index');
var elem = JSON.parse(JSON.stringify(values[id]));
values.push(elem);
onChange && onChange();
setTimeout(function () {
if (typeof tableEvents === 'function') {
tableEvents(values.length - 1, elem, 'add');
}
myValues2table(divId, values, onChange, onReady);
}, 100);
if ($add.data('maxraw')) {
$add.data('raw', $add.data('raw') + 1);
}
}
});
} else
if (command === 'delete') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-trash' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).addClass('red').find('i').html('delete');
}
$(this).on('click', function () {
var id = $(this).data('index');
var elem = values[id];
values.splice(id, 1);
onChange && onChange();
setTimeout(function () {
if (typeof tableEvents === 'function') {
tableEvents(id, elem, 'delete');
}
myValues2table(divId, values, onChange, onReady);
}, 100);
if ($add.data('maxraw')) {
$add.data('raw', $add.data('raw') - 1);
}
});
} else if (command === 'up') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-triangle-1-n' },
text: false
})
.css({ width: '1em', height: '1em' })
} else {
$(this).find('i').html('arrow_upward');
}
$(this).on('click', function () {
var id = $(this).data('index');
var elem = values[id];
values.splice(id, 1);
values.splice(id - 1, 0, elem);
onChange && onChange();
setTimeout(function () {
myValues2table(divId, values, onChange, onReady);
}, 100);
});
} else if (command === 'down') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-triangle-1-s' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('arrow_downward');
}
$(this).on('click', function () {
var id = $(this).data('index');
var elem = values[id];
values.splice(id, 1);
values.splice(id + 1, 0, elem);
onChange && onChange();
setTimeout(function () {
myValues2table(divId, values, onChange, onReady);
}, 100);
});
} else if (command === 'pair') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-transferthick-e-w' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('leak_add');
}
$(this).on('click', function () {
if (typeof tableEvents === 'function') {
var id = $(this).data('index');
var elem = values[id];
tableEvents(id, elem, 'pair');
}
}).attr('title', _('pair'));
} else if (command === 'unpair') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-scissors' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('leak_remove');
}
$(this).on('click', function () {
if (typeof tableEvents === 'function') {
var id = $(this).data('index');
var elem = values[id];
tableEvents(id, elem, 'unpair');
}
}).attr('title', _('unpair'));
} else if (command === 'edit') {
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-pencil' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('edit');
}
$(this).on('click', function () {
var id = $(this).data('index');
if (typeof editLine === 'function') {
setTimeout(function () {
editLine(id, JSON.parse(JSON.stringify(values[id])), function (err, id, newValues) {
if (!err) {
if (JSON.stringify(values[id]) !== JSON.stringify(newValues)) {
onChange && onChange();
values[id] = newValues;
myValues2table(divId, values, onChange, onReady);
}
}
});
}, 100);
}
});
} else if (command === 'assignLink') {
// Mod: eigener button für link zuweisen
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-pencil' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('link'); //Icon festlegen
}
$(this).on('click', function () {
var id = $(this).data('index');
}).attr('title', _('assign link'));
} else if (command === 'removeLink') {
// Mod: eigener button für link entfernen
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-pencil' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).addClass('red').find('i').html('link_off'); //Icon festlegen
}
$(this).on('click', function () {
var id = $(this).data('index');
}).attr('title', _('remove link'));
} else if (command === 'openCustom') {
// Mod: eigener button für custom dialog öffnen
if (!isMaterialize) {
$(this).button({
icons: { primary: 'ui-icon-gear' },
text: false
})
.css({ width: '1em', height: '1em' });
} else {
$(this).find('i').html('build'); //Icon festlegen
}
$(this).on('click', function () {
var id = $(this).data('index');
}).attr('title', _('Settings'));
}
});
$lines.find('.values-input').on('change.adaptersettings', function () {
if ($(this).attr('type') === 'checkbox') {
if ($(this).prop('checked').toString() !== $(this).data('old-value')) onChange();
values[$(this).data('index')][$(this).data('name')] = $(this).prop('checked');
} else {
if ($(this).val() !== $(this).data('old-value')) onChange();
values[$(this).data('index')][$(this).data('name')] = $(this).val();
}
}).on('keyup', function () {
$(this).trigger('change.adaptersettings');
});
$lines.find('.values-label').on('change.adaptersettings', function () {
if ($(this).text() !== $(this).data('old-value')) onChange();
values[$(this).data('index')][$(this).data('name')] = $(this).text();
}).on('keyup', function () {
$(this).trigger('change.adaptersettings');
});
}
if (isMaterialize) {
if (!divId) {
M.updateTextFields();
$('select').select();
} else {
M.updateTextFields('#' + divId);
$('#' + divId).find('select').select();
}
// workaround for materialize checkbox problem
$div.find('input[type="checkbox"]+span').off('click').on('click', function () {
var $input = $(this).prev();
if (!$input.prop('disabled')) {
$input.prop('checked', !$input.prop('checked')).trigger('change');
}
});
}
if (typeof onReady === 'function') onReady();
}
/**
* Extract the values from table.
*
* This function extracts the values from edit table, that was generated with values2table function.
*
* @param {string} divId name of the html element (or nothing).
* @return {object} array with values
*/
function myTable2values(divId) {
var $div;
if (!divId) {
$div = $('body');
} else {
$div = $('#' + divId);
}
var names = [];
$div.find('.table-values th').each(function () {
var name = $(this).data('name');
if (name) {
names.push(name);
} else {
names.push('___ignore___');
}
});
var values = [];
var j = 0;
$div.find('.table-lines tr').each(function () {
values[j] = {};
$(this).find('td').each(function () {
var $input = $(this).find('input');
if ($input.length) {
var name = $input.data('name');
if (name) {
if ($input.attr('type') === 'checkbox') {
values[j][name] = $input.prop('checked');
} else {
values[j][name] = $input.val();
}
}
}
var $input = $(this).find('label');
if ($input.length) {
var name = $input.data('name');
if (name) {
values[j][name] = $input.text();
}
}
var $select = $(this).find('select');
if ($select.length) {
var name = $select.data('name');
values[j][name] = $select.val() || '';