-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
422 lines (351 loc) · 11.8 KB
/
index.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
var express = require('express')
var app = express();
var httpProxy = require('http-proxy');
var leihsProxy = httpProxy.createProxyServer();
var config = require('./config');
var serverLeihs = config.SERVER_LEIHS;
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
user: config.MYSQL_USER,
password : config.MYSQL_PASS,
database: config.MYSQL_DB
});
var selects = [];
var simpleselect = {};
var selects2 = [];
var simpleselect_handover = {};
var customscript = `
<!-- Modal -->
<script>
console.log("searching for packages...");
var modal = '<div id="mhtPackageDialog" aria-hidden="false" class="modal fade ui-modal medium in ui-shown"><h2>Check package components:</h2><form id="packageForm" onsubmit="return false;"><input style=" border:solid 2px gray; margin-bottom: 10px;" id="check_barcode" value="" width=40><ul id="mht_package_list"></ul><button type="button" onClick="checkPackageCodes();">Check & Close</button></form></div>';
var showModal = false;
var packageCodes = [];
var parentIDs = {};
var itemVisible = {};
$(document).ready(function(){
$( ".col2of10.line-col.text-align-center").each(function(i, obj) {
var item_id = $(obj).children().first().text();
//$(this).parent().find(":checkbox");
var item_number = parseInt(item_id);
console.log("ajax url: " + "/mht_get_package_children?id=" + item_number);
$.get( "/mht_get_package_children?id=" + item_id, function( data ) {
console.log("Result: " + JSON.stringify(data));
if(data.length > 0) {
if(!showModal) {
$('.button.green').hide();
$( "body" ).append(modal);
showModal = true;
}
for(var k in data) {
// mht_package_list
$('#mht_package_list').append('<li id="li' + data[k].inventory_code + '">' + data[k].product + '</li>');
packageCodes.push(data[k].inventory_code);
parentIDs[data[k].inventory_code] = item_id;
}
$("#packageForm").submit(function() {
checkPackageCodes();
return false;
});
}
});
});
//check checkboxes periodically, update modal view:
setInterval(function() {
var willTakeBack = {};
$( ".col2of10.line-col.text-align-center").each(function(i, obj) {
var item_id = $(obj).children().first().text();
willTakeBack[item_id] = $(this).parent().find(":checkbox").is(":checked");
});
for(var i = 0; i < packageCodes.length; i++) {
if(willTakeBack[parentIDs[packageCodes[i]]]) {
$('#li' + packageCodes[i]).show();
itemVisible[packageCodes[i]] = true;
}
else {
$('#li' + packageCodes[i]).hide();
itemVisible[packageCodes[i]] = false;
}
}
checkPackageCodesAuto();
}, 500)
});
function checkPackageCodesAuto() {
if(packageCodes.length == 0) {
$('#mhtPackageDialog').hide();
$('.button.green').show();
}
else {
canClose = true;
if(packageCodes.length > 0) { //check if remaining package items are visible (will be borrowed)
for(var i = 0; i < packageCodes.length; i++) {
if(itemVisible[packageCodes[i]]) {
canClose = false; break;
}
}
}
//else: canClose stays true
if(canClose) {
$('#mhtPackageDialog').hide();
$('.button.green').show();
}
else {
$('#mhtPackageDialog').show();
$('.button.green').hide();
}
}
}
function checkPackageCodes() {
if(packageCodes.length == 0) {
$('#mhtPackageDialog').hide();
$('.button.green').show();
}
else {
var canClose = true;
var k = $("#check_barcode").val();
if(typeof k == 'undefined') {
$("#check_barcode").val("");
return;
}
if($.inArray(k, packageCodes) != -1) {
$('#li' + k).append('<i class="fa fa-check margin-right-m"></i> (' + k + ')');
//remove element from array:
for(var i = packageCodes.length - 1; i >= 0; i--) {
if(packageCodes[i] == k) {
packageCodes.splice(i, 1);
}
}
$("#check_barcode").val("");
}
if(packageCodes.length > 0) { //check if remaining package items are visible (will be borrowed)
for(var i = 0; i < packageCodes.length; i++) {
if(itemVisible[packageCodes[i]]) {
canClose = false; break;
}
}
}
//else: canClose stays true
if(canClose) {
$('#mhtPackageDialog').hide();
$('.button.green').show();
}
else {
$('#mhtPackageDialog').show();
$('.button.green').hide();
}
}
}
</script>
`;
var handoverscript = `
<script>
var showModal = false;
var packageCodes = [];
var replacedHtml = '';
$(document).ready(function(){
console.log("hand over proxy script at work.");
$('.button.green').click(function() {
setTimeout(function() {
$("input[id^=assigned-item-]").each(function(i, obj) {
//line row focus-hover-thin green
var willBorrow= $(this).parent().parent().parent().find(":checkbox").is(":checked");
if(willBorrow) {
var item_id = $(obj).val();
console.log("will take back " + item_id + ": " + willBorrow);
if(item_id.length > 0) {
$.get( "/mht_get_package_children?id=" + item_id, function( data ) {
console.log("Result for item " + item_id + ": " + JSON.stringify(data))
for(var k in data) {
if(!showModal) {
showModal = true;
$('.button.green').hide();
replacedHtml = $('.padding-bottom-m.margin-bottom-m.no-last-child-margin').html();
$('.padding-bottom-m.margin-bottom-m.no-last-child-margin').html('<form onsubmit="return false;" id="packageForm"><input style=" border:solid 2px gray; margin-bottom: 10px;" id="check_barcode" value="" width=40></input><ul id="mht_package_list"></ul><button type="button" style="margin-top:20px" onClick="checkPackageCodes();">Check & Close</button></form>');
}
$('#mht_package_list').append('<li id="li' + data[k].inventory_code + '">' + data[k].product + '</li>');
packageCodes.push(data[k].inventory_code);
$("#packageForm").submit(function() {
checkPackageCodes();
return false;
});
}
});
}
}
});
// div aria-hidden="false" class="modal fade ui-modal medium in ui-shown"
$("div.modal.fade.ui-modal.medium.in.ui-shown").on('hidden.bs.modal', function (e) {
closedModal();
});
$("div.modal-close").click(function() {
closedModal();
});
}, 1000);
});
$("input[id^=assigned-item-]").each(function(i, obj) {
console.log("found item: " + obj + " value: " + $(obj).val());
});
});
function restoreHtml() {
if(showModal) {
$('.padding-bottom-m.margin-bottom-m.no-last-child-margin').html(replacedHtml);
showModal = false;
}
}
function closedModal() {
console.log("closed modal!");
packageCodes = []; //reset
restoreHtml();
$('.button.green').show();
}
function checkPackageCodes() {
if(packageCodes.length == 0) {
restoreHtml();
$('.button.green').show();
}
else {
var canClose = true;
var k = $("#check_barcode").val();
if(typeof k == 'undefined') {
$("#check_barcode").val("");
return;
}
if($.inArray(k, packageCodes) != -1) {
$('#li' + k).append('<i class="fa fa-check margin-right-m"></i> (' + k + ')');
//remove element from array:
for(var i = packageCodes.length - 1; i >= 0; i--) {
if(packageCodes[i] == k) {
packageCodes.splice(i, 1);
}
}
$("#check_barcode").val("");
if(packageCodes.length > 0) canClose = false;
}
else {
canClose = false;
}
if(canClose) {
restoreHtml();
$('.button.green').show();
}
}
}
</script>
`;
// col2of10 line-col text-align-center
simpleselect.query = 'body';
simpleselect.func = function (node) {
var rs = node.createReadStream();
var ws = node.createWriteStream({outer: false});
rs.pipe(ws, {end: false});
// When the read stream has ended, attach our style to the end
rs.on('end', function(){
ws.end(customscript);
});
}
simpleselect_handover.query = 'body';
simpleselect_handover.func = function(node) {
var rs = node.createReadStream();
var ws = node.createWriteStream({outer: false});
rs.pipe(ws, {end: false});
// When the read stream has ended, attach our style to the end
rs.on('end', function(){
ws.end(handoverscript);
});
}
selects.push(simpleselect);
selects2.push(simpleselect_handover);
var only = function(middleware) {
return function(req, res, next) {
var tmp = req.path.split('/');
var endswith = tmp.pop();
if((endswith == 'take_back') && (req.method == 'GET')) {
return middleware(req, res, next);
}
else {
return next();
}
};
};
var only2 = function(middleware) {
return function(req, res, next) {
var tmp = req.path.split('/');
var endswith = tmp.pop();
if((endswith == 'hand_over') && (req.method == 'GET')) {
console.log("hand over URL detected.");
return middleware(req, res, next);
}
else {
return next();
}
};
};
var harmon = require('harmon')([], selects);
var harmon2 = require('harmon')([], selects2);
app.use(only(harmon)); //take back
app.use(only2(harmon2)); //hand over
app.get('/mht_get_package_children', function(req, res) {
console.log('processing /mht_get_package_children');
var id = req.query.id;
console.log("request for id: " + id);
// 1st: get id for inventory_code
// 2nd: get inventory code list with matching parent_id
var query = "SELECT id FROM items WHERE inventory_code = " + connection.escape(id) + ";";
connection.query(query, function(err, rows, fields) {
if(err) {
console.log("mysql error: " + err);
res.end(JSON.stringify([]));
}
else {
if(rows.length > 0) {
var package_id = rows[0].id;
var query2 = "SELECT inventory_code, model_id FROM items WHERE parent_id = " + connection.escape(package_id) + ";";
connection.query(query2, function (err, rows, fields) {
if(err) {
console.log("mysql error: " + err);
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify([]));
}
else {
var id_list = [];
for(var k in rows) {
id_list.push({inventory_code : rows[k].inventory_code, model_id : rows[k].model_id});
}
productChain(id_list, [], res);
}
});
}
}
});
});
function productChain(id_list, result_list, res) {
if(id_list.length == 0) { //done
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(result_list));
}
else {
var idItem = id_list.pop();
var query = "SELECT product FROM models WHERE id = " + connection.escape(idItem.model_id) + " LIMIT 1;";
connection.query(query, function(err, rows, fields) {
if((err) || (rows.length == 0)) {
console.log("mysql error: " + err);
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify([]));
}
else {
idItem.product = rows[0].product;
result_list.push(idItem);
productChain(id_list, result_list, res);
}
});
}
}
app.all('/*', function(req, res) {
console.log("redirecting " + req.url);
leihsProxy.web(req, res, {target : serverLeihs});
});
//var bodyParser = require('body-parser')
app.listen(config.PROXY_PORT, function () {
console.log('Leihs proxy app listening on port 8888!')
});