forked from bendudz/fpp-zettle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple-readers.js
More file actions
179 lines (157 loc) · 5.09 KB
/
Copy pathmultiple-readers.js
File metadata and controls
179 lines (157 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var zettleConfig = null;
function SaveBigButtonConfig(config) {
var data = JSON.stringify(config);
$(".readerList").addClass("loading");
$.ajax({
type: "POST",
url: 'api/configfile/plugin.fpp-zettle.json',
dataType: 'json',
async: false,
data: data,
processData: false,
contentType: 'application/json',
success: function (data) {
$(".readerList").removeClass("loading");
$.jGrowl('Reader Config Saved!', {
themeState: 'success'
});
}
});
}
function GetReader(i, v) {
var reader = {
"product": $('#reader-' + i + '_Product').val(),
};
CommandToJSON('reader-' + i + '_Command', 'tableReader-' + i, reader);
return reader;
}
function SaveReaders() {
// Reset readers to empty array
zettleConfig["readers"] = [];
// Look over readerList children
$.each($('.readerList').children(), function (i, v) {
var key = "" + i;
var reader = GetReader(i, v);
zettleConfig["readers"][key] = reader;
});
// console.log(zettleConfig);
SaveBigButtonConfig(zettleConfig);
}
function updateReaderRow(i, v) {
var $newReaderRow = $(v);
var newReaderRowCommand = 'reader-' + i + '_Command';
var newReaderRowProduct = 'reader-' + i + '_Product';
var newReaderRowTable = 'tableReader-' + i;
$newReaderRow.data('rKey', i);
$newReaderRow.find('.buttonCommand').attr('id', newReaderRowCommand);
$newReaderRow.find('.readerProduct').attr('id', newReaderRowProduct);
$newReaderRow.find('.readerHeader').html('Reader ' + (i + 1));
$newReaderRow.find('[id^="tableReader"]').each(function () {
var oldId = $(this).prop('id')
var idArr = oldId.split('_');
idArr[0] = newReaderRowTable
$(this).attr('id', idArr.join('_'))
});
return $newReaderRow;
}
function createReaderRow(i, v) {
// console.log('createReaderRow');
var $newReaderRow = $($(".configRowTemplate").html());
var newReaderRowCommand = 'reader-' + i + '_Command';
var newReaderRowProduct = 'reader-' + i + '_Product';
var newReaderRowTable = 'tableReader-' + i;
$newReaderRow.data('rKey', i);
$newReaderRow.find('.readerCommand').attr('id', newReaderRowCommand).on('change', function () {
CommandSelectChanged(newReaderRowCommand, newReaderRowTable, true);
});
$newReaderRow.find('.readerHeader').html('Reader ' + (i + 1));
if (!v) {
$newReaderRow.find('.readerProduct').attr('id', newReaderRowProduct).val();
} else {
$newReaderRow.find('.readerProduct').attr('id', newReaderRowProduct).val(v.product);
}
$newReaderRow.find('.tableReader').attr('id', newReaderRowTable);
$newReaderRow.find('.readerDelete').click(function () {
$(this).closest('.reader').remove();
$.each($('.readerList').children(), function (iteration, value) {
updateReaderRow(iteration, value);
});
});
$newReaderRow.find('.readerTest').click(function () {
var command = $(document).find('#' + newReaderRowCommand).val();
if (command == null) {
$.jGrowl('No command selected, please select a command!', {
themeState: 'danger'
});
} else {
var url = "api/command/";
var data = {};
// Get command data
CommandToJSON(newReaderRowCommand, newReaderRowTable, data);
// Build url with selected command
url += data['command'];
// Send ajax to test command to see if user likes it before they save it
$.ajax({
type: "POST",
url: url,
dataType: 'text',
async: false,
data: JSON.stringify(data['args']),
processData: false,
contentType: 'application/json',
success: function (data) {
if (data != '') {
$.jGrowl('Test Sent!', {
themeState: 'success'
});
$.jGrowl('If you like what you see don\'t forget to save it!!', {
themeState: 'success',
life: 5000
});
}
}
});
}
});
$('.readerList').append($newReaderRow);
LoadCommandList('reader-' + i + '_Command');
return $newReaderRow;
}
$(function () {
// allowMultisyncCommands = true;
$('#saveReaderConfigButton').click(function () {
SaveReaders();
});
$(".readerList").addClass("loading");
$.get('api/configfile/plugin.fpp-zettle.json')
.done(function (data) {
$(".readerList").removeClass("loading");
processBigButtonConfig(data);
})
.fail(function (data) {
$(".readerList").removeClass("loading");
processBigButtonConfig('[]');
});
function processBigButtonConfig(data) {
if (typeof data === "string") {
zettleConfig = $.parseJSON(data);
} else {
zettleConfig = data;
}
// console.log(zettleConfig);
if (zettleConfig.readers.length < 1) {
zettleConfig.readers.push({
"product": "",
"command": "",
})
}
$.each(zettleConfig.readers, function (i, v) {
$newReaderRow = createReaderRow(i, v);
PopulateExistingCommand(v, 'reader-' + i + '_Command', 'tableReader_' + i, true);
});
}
$("#addNewReader").click(function () {
var i = $(".readerList").children().length;
var $newReaderRow = createReaderRow(i, null);
});
});