www/js/profile_fermentation.js

changeset 796
6ce2c2e6796e
parent 795
9472106a3143
child 797
d0fedeb32f05
equal deleted inserted replaced
795:9472106a3143 796:6ce2c2e6796e
1 /*****************************************************************************
2 * Copyright (C) 2019-2021
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of BMS
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * Brewery Management System is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23
24 function createDelElements() {
25 $('#eventWindow').jqxWindow({
26 theme: theme,
27 position: { x: 490, y: 210 },
28 width: 300,
29 height: 175,
30 resizable: false,
31 isModal: true,
32 modalOpacity: 0.4,
33 okButton: $('#delOk'),
34 cancelButton: $('#delCancel'),
35 initContent: function() {
36 $('#delOk').jqxButton({ template: 'danger', width: '65px', theme: theme });
37 $('#delCancel').jqxButton({ template: 'success', width: '65px', theme: theme });
38 $('#delCancel').focus();
39 }
40 });
41 $('#eventWindow').jqxWindow('hide');
42 }
43
44
45 $(document).ready(function() {
46
47 // tooltips
48 $('#name').jqxTooltip({ content: 'De naam voor dit vergisting profiel.' });
49 $('#inittemp_lo').jqxTooltip({ content: 'De minimale begin temperatuur van dit profiel.' });
50 $('#inittemp_hi').jqxTooltip({ content: 'De maximale begin temperatuur van dit profiel.' });
51
52 var dataRecord = {},
53 url = 'includes/db_profile_fermentation.php',
54 source = {
55 datatype: 'json',
56 cache: false,
57 datafields: [
58 { name: 'record', type: 'number' },
59 { name: 'uuid', type: 'string' },
60 { name: 'name', type: 'string' },
61 { name: 'inittemp_lo', type: 'float' },
62 { name: 'inittemp_hi', type: 'float' },
63 { name: 'fridgemode', type: 'int' },
64 { name: 'totalsteps', type: 'int' },
65 { name: 'duration', type: 'int' },
66 { name: 'steps', type: 'array' }
67 ],
68 id: 'record',
69 url: url,
70 deleterow: function(rowid, commit) {
71 var data = 'delete=true&' + $.param({ record: rowid });
72 $.ajax({
73 dataType: 'json',
74 url: url,
75 cache: false,
76 data: data,
77 type: 'POST',
78 success: function(data) {
79 if (data.error) {
80 console.log('delete: ' + data.msg);
81 alert('Fout: ' + data.msg);
82 } else {
83 console.log('delete: success');
84 }
85 location.reload(true);
86 },
87 error: function(jqXHR, textStatus, errorThrown) { commit(false); }
88 });
89 },
90 addrow: function(rowid, rowdata, position, commit) {
91 var data = 'insert=true&' + $.param(rowdata);
92 $.ajax({
93 dataType: 'json',
94 url: url,
95 cache: false,
96 data: data,
97 type: 'POST',
98 success: function(data) {
99 if (data.error) {
100 console.log('insert: ' + data.msg);
101 alert('Fout: ' + data.msg);
102 } else {
103 console.log('insert: success');
104 }
105 location.reload(true);
106 },
107 error: function(jqXHR, textStatus, errorThrown) { commit(false); }
108 });
109 },
110 updaterow: function(rowid, rowdata, commit) {
111 var data = 'update=true&' + $.param(rowdata);
112 $.ajax({
113 dataType: 'json',
114 url: url,
115 cache: false,
116 data: data,
117 type: 'POST',
118 success: function(data) {
119 if (data.error) {
120 console.log('updaterow: ' + data.msg);
121 alert('Fout: ' + data.msg);
122 } else {
123 console.log('updaterow: success');
124 }
125 location.reload(true);
126 },
127 error: function(jqXHR, textStatus, errorThrown) { commit(false); }
128 });
129 }
130 },
131 dataAdapter = new $.jqx.dataAdapter(source),
132 editrow = -1,
133
134 // Inline steps editor
135 editsteps = function(data) {
136 var generaterow = function() {
137 var row = {};
138 row['name'] = 'Stap 1';
139 row['steptime'] = 12;
140 row['resttime'] = 24;
141 row['target_lo'] = 22.0;
142 row['target_hi'] = 23.0;
143 row['fridgemode'] = 0;
144 return row;
145 };
146 var stepSource = {
147 localdata: data.steps,
148 datatype: 'local',
149 datafields: [
150 { name: 'name', type: 'string' },
151 { name: 'steptime', type: 'float' },
152 { name: 'resttime', type: 'float' },
153 { name: 'target_lo', type: 'float' },
154 { name: 'target_hi', type: 'float' },
155 { name: 'fridgemode', type: 'int' }
156 ],
157 addrow: function(rowid, rowdata, position, commit) { commit(true); },
158 deleterow: function(rowid, commit) { commit(true); }
159 },
160 stepAdapter = new $.jqx.dataAdapter(stepSource);
161 $('#grid').jqxGrid({
162 width: 800,
163 height: 330,
164 source: stepAdapter,
165 theme: theme,
166 selectionmode: 'singlerow',
167 editmode: 'selectedcell',
168 editable: true,
169 showtoolbar: true,
170 rendertoolbar: function(toolbar) {
171 var container = $('<div style="margin: 5px;"></div>');
172 toolbar.append(container);
173 container.append('<input style="margin-left: 100px;" id="addrowbutton" type="button" value="Nieuwe stap" />');
174 container.append('<input style="margin-left: 290px;" id="deleterowbutton" type="button" value="Verwijder stap" />');
175 $('#addrowbutton').jqxButton({ template: 'primary', theme: theme, width: 150 });
176 $('#deleterowbutton').jqxButton({ template: 'danger', theme: theme, width: 150 });
177 // create new row.
178 $('#addrowbutton').on('click', function() {
179 var datarow = generaterow();
180 $('#grid').jqxGrid('addrow', null, datarow);
181 });
182 // delete row.
183 $('#deleterowbutton').on('click', function() {
184 var selectedrowindex = $('#grid').jqxGrid('getselectedrowindex'),
185 rowscount = $('#grid').jqxGrid('getdatainformation').rowscount,
186 id;
187 if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
188 id = $('#grid').jqxGrid('getrowid', selectedrowindex);
189 $('#grid').jqxGrid('deleterow', id);
190 }
191 });
192 },
193 columns: [
194 { text: 'Stap naam', datafield: 'name' },
195 { text: 'Min. &deg;C', datafield: 'target_lo', width: 100, align: 'right', cellsalign: 'right', cellsformat: 'f1',
196 validation: function(cell, value) {
197 if (value < 0 || value > 45) {
198 return { result: false, message: 'De temperatuur moet tussen 0 en 45 zijn.' };
199 }
200 return true;
201 }
202 },
203 { text: 'Max. &deg;C', datafield: 'target_hi', width: 100, align: 'right', cellsalign: 'right', cellsformat: 'f1',
204 validation: function(cell, value) {
205 if (value < 0 || value > 45) {
206 return { result: false, message: 'De temperatuur moet tussen 0 en 45 zijn.' };
207 }
208 return true;
209 }
210 },
211 { text: 'Koelkast', datafield: 'fridgemode', columntype: 'checkbox', width: 80 },
212 { text: 'Stap tijd', datafield: 'steptime', width: 80, align: 'right', cellsalign: 'right',
213 validation: function(cell, value) {
214 if (value < 0 || value > 14400) {
215 return { result: false, message: 'De tijd moet tussen 0 en 14400 zijn.' };
216 }
217 return true;
218 }
219 },
220 { text: 'Rust tijd', datafield: 'resttime', width: 80, align: 'right', cellsalign: 'right',
221 validation: function(cell, value) {
222 if (value < 0 || value > 14400) {
223 return { result: false, message: 'De tijd moet tussen 0 en 14400 zijn.' };
224 }
225 return true;
226 }
227 }
228 ]
229 });
230 };
231
232 // initialize the input fields.
233 $('#name').jqxInput({ theme: theme, width: 640, height: 23 });
234 $('#inittemp_lo').jqxNumberInput(Spin1dec);
235 $('#inittemp_lo').jqxNumberInput({ max: 45 });
236 $('#inittemp_hi').jqxNumberInput(Spin1dec);
237 $('#inittemp_hi').jqxNumberInput({ max: 45 });
238 $('#fridgemode').jqxCheckBox({ theme: theme, height: 23, enableContainerClick: false });
239
240 var localizationobj = {};
241 localizationobj.filterchoosestring= "Keuze:";
242
243 // initialize jqxGrid
244 $('#jqxgrid').jqxGrid({
245 width: 1280,
246 height: 630,
247 source: dataAdapter,
248 theme: theme,
249 showstatusbar: true,
250 renderstatusbar: function(statusbar) {
251 var rowCount = $("#jqxgrid").jqxGrid('getrows').length;
252 statusbar.append('<div style="float: left; margin: 8px; color: orange !important;">Aantal items: ' + rowCount + '</div>');
253 var container = $('<div style="overflow: hidden; position: relative; margin: 5px;"></div>');
254 var addButton = $('<div style="float: right; margin-right: 15px;"><img style="position: relative; margin-top: 2px;" ' +
255 'src="images/add.png"/><span style="margin-left: 4px; position: relative; top: -4px;">Nieuw</span></div>');
256 container.append(addButton);
257 statusbar.append(container);
258 addButton.jqxButton({ theme: theme, width: 90, height: 17 });
259 // add new row.
260 addButton.click(function(event) {
261 editrow = -1;
262 $('#name').val('Nieuw vergist profiel');
263 dataRecord.uuid = '';
264 $('#inittemp_lo').val(20.0);
265 $('#inittemp_hi').val(20.0);
266 $('#fridgemode').val(0);
267 dataRecord.totalsteps = 0;
268 dataRecord.duration = 0;
269 editsteps('');
270 $('#popupWindow').jqxWindow('open');
271 });
272 },
273 ready: function () {
274 $("#jqxgrid").jqxGrid('localizestrings', localizationobj);
275 },
276 filterable: true,
277 showfilterrow: true,
278 columns: [
279 { text: 'Vergisting profiel', datafield: 'name', filtertype: 'textbox' },
280 { text: 'Min. start &deg;C', datafield: 'inittemp_lo', width: 120, align: 'right', cellsalign: 'right', cellsformat: 'f1', filtertype: 'number' },
281 { text: 'Max. start &deg;C', datafield: 'inittemp_hi', width: 120, align: 'right', cellsalign: 'right', cellsformat: 'f1', filtertype: 'number' },
282 { text: 'Sensor', datafield: 'fridgemode', align: 'right', width: 80, filterable: false,
283 cellsrenderer: function(row, columnfield, value, defaulthtml, column) {
284 if (value == 0)
285 return '<span style="margin: 3px; margin-top: 6px; float: right;">Bier</span>';
286 else
287 return '<span style="margin: 3px; margin-top: 6px; float: right;">Koelkast</span>';
288 }
289 },
290 { text: 'Stappen', datafield: 'totalsteps', width: 80, align: 'right', cellsalign: 'right', filterable: false },
291 { text: 'Tijdsduur', datafield: 'duration', width: 150, align: 'right', filterable: false,
292 cellsrenderer: function(row, columnfield, value, defaulthtml, column) {
293 var show, days, hours;
294 if (value < 24) {
295 show = value + ' uur';
296 } else {
297 days = Math.floor(value / 24);
298 hours = value % 24;
299 if (days == 1)
300 show = days + ' dag, ' + hours + ' uur';
301 else
302 show = days + ' dagen, ' + hours + ' uur';
303 }
304 return '<span style="margin: 3px; margin-top: 6px; float: right;">' + show + '</span>';
305 }
306 },
307 { text: '', datafield: 'Edit', width: 100, align: 'center', columntype: 'button', filterable: false, cellsrenderer:
308 function() {
309 return 'Wijzig';
310 }, buttonclick: function(row) {
311 editrow = row;
312 // get the clicked row's data and initialize the input fields.
313 dataRecord = $('#jqxgrid').jqxGrid('getrowdata', editrow);
314 $('#name').val(dataRecord.name);
315 $('#inittemp_lo').val(parseFloat(dataRecord.inittemp_lo));
316 $('#inittemp_hi').val(parseFloat(dataRecord.inittemp_hi));
317 $('#fridgemode').val(parseFloat(dataRecord.fridgemode));
318 editsteps(dataRecord);
319 // show the popup window.
320 $('#popupWindow').jqxWindow('open');
321 }
322 }
323 ]
324 });
325 // initialize the popup window and buttons.
326 $('#popupWindow').jqxWindow({
327 width: 1050,
328 height: 550,
329 position: { x: 110, y: 30 },
330 resizable: false,
331 theme: theme,
332 isModal: true,
333 autoOpen: false,
334 cancelButton: $('#Cancel'),
335 modalOpacity: 0.40
336 });
337 $('#popupWindow').on('open', function() {
338 $('#name').jqxInput('selectAll');
339 });
340 $('#Delete').jqxButton({ template: 'danger', width: '90px', theme: theme });
341 $('#Delete').click(function() {
342 if (editrow >= 0) {
343 // Open a popup to confirm this action.
344 $('#eventWindow').jqxWindow('open');
345 $('#delOk').click(function() {
346 var rowID = $('#jqxgrid').jqxGrid('getrowid', editrow);
347 $('#jqxgrid').jqxGrid('deleterow', rowID);
348 });
349 }
350 $('#popupWindow').jqxWindow('hide');
351 });
352 $('#Cancel').jqxButton({ template: 'primary', width: '90px', theme: theme });
353 $('#Clone').jqxButton({ template: 'warning', width: '90px', theme: theme });
354 $('#Clone').click(function() {
355 var steprows = $('#grid').jqxGrid('getrows');
356 var row = {
357 record: -1,
358 name: $('#name').val() + ' kopie',
359 inittemp_lo: parseFloat($('#inittemp_lo').jqxNumberInput('decimal')),
360 inittemp_hi: parseFloat($('#inittemp_hi').jqxNumberInput('decimal')),
361 fridgemode: $('#fridgemode').val(),
362 steps: steprows
363 };
364 $('#jqxgrid').jqxGrid('addrow', null, row);
365 $('#popupWindow').jqxWindow('hide');
366 });
367 $('#Save').jqxButton({ template: 'success', width: '90px', theme: theme });
368 // update the edited row when the user clicks the 'Save' button.
369 $('#Save').click(function() {
370 var row, rowID = -1, steprows = $('#grid').jqxGrid('getrows');
371 if (editrow >= 0) {
372 rowID = $('#jqxgrid').jqxGrid('getrowid', editrow);
373 }
374 row = {
375 record: rowID,
376 uuid: dataRecord.uuid,
377 name: $('#name').val(),
378 inittemp_lo: parseFloat($('#inittemp_lo').jqxNumberInput('decimal')),
379 inittemp_hi: parseFloat($('#inittemp_hi').jqxNumberInput('decimal')),
380 fridgemode: $('#fridgemode').val(),
381 steps: steprows
382 };
383 if (editrow >= 0) {
384 $('#jqxgrid').jqxGrid('updaterow', rowID, row);
385 } else {
386 $('#jqxgrid').jqxGrid('addrow', null, row);
387 }
388 $('#popupWindow').jqxWindow('hide');
389 });
390 createDelElements();
391 });
392

mercurial