From 3fcf2b3bc8f5a677bd7d743764ea9472e1134b84 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Fri, 25 Sep 2020 18:11:40 -0600 Subject: [PATCH] Add label printing support, use more URLs instead of functions for nav --- www/assets/css/app.css | 20 +++++++ www/assets/js/addevent.js | 2 +- www/assets/js/home.js | 2 +- www/assets/js/labels.js | 113 +++++++++++++++++++++++++++++++++++ www/assets/js/machine.js | 23 ++++--- www/assets/js/settings.js | 36 +++++++++++ www/index.html | 3 + www/package-lock.json | 5 ++ www/package.json | 3 +- www/pages/addevent.html | 2 +- www/pages/client.html | 2 +- www/pages/home.html | 4 +- www/pages/labels.html | 36 +++++++++++ www/pages/machine.html | 32 +++++++++- www/pages/machineeditor.html | 2 +- www/pages/viewlabel.html | 37 ++++++++++++ www/routes.js | 25 ++++++-- 17 files changed, 325 insertions(+), 22 deletions(-) create mode 100644 www/assets/js/labels.js create mode 100644 www/pages/labels.html create mode 100644 www/pages/viewlabel.html diff --git a/www/assets/css/app.css b/www/assets/css/app.css index bb6c5fb..cd1bdda 100644 --- a/www/assets/css/app.css +++ b/www/assets/css/app.css @@ -65,6 +65,26 @@ Framework7 and FontAwesome both have a .fab class --f7-safe-area-right: 0px; } +#pdf-canvas { + margin-left: auto; + margin-top: 2em; + margin-right: auto; + padding-left: 0; + padding-right: 0; + display: block; + box-shadow: var(--f7-elevation-5); +} + +#pdf-loading-message { + margin-left: auto; + margin-top: 2em; + margin-right: auto; + padding-left: 0; + padding-right: 0; + display: block; + text-align: center; +} + /* Allow easily changing help text to reflect finger/mouse usage. */ diff --git a/www/assets/js/addevent.js b/www/assets/js/addevent.js index 337f1d5..6fae915 100644 --- a/www/assets/js/addevent.js +++ b/www/assets/js/addevent.js @@ -110,7 +110,7 @@ function addEvent(machineid) { text: 'Event added!', closeTimeout: 2000, }).open(); - openMachineInfo(machineid); + router.back(); } }, function (xhr) { diff --git a/www/assets/js/home.js b/www/assets/js/home.js index 009dc94..c5f022a 100644 --- a/www/assets/js/home.js +++ b/www/assets/js/home.js @@ -8,7 +8,7 @@ $("#machinesearchbar").submit(function (evt) { evt.preventDefault(); - openMachineInfo($("#machinesearchbar input[type=search]").val()); + router.navigate("/machine/" + $("#machinesearchbar input[type=search]").val()); }); $("#clientsearchbar").submit(function (evt) { diff --git a/www/assets/js/labels.js b/www/assets/js/labels.js new file mode 100644 index 0000000..151c262 --- /dev/null +++ b/www/assets/js/labels.js @@ -0,0 +1,113 @@ +/* + * Copyright 2020 Netsyms Technologies. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +var pdfobject = null; + +function labelListAsync(routeTo, routeFrom, resolve, reject) { + app.dialog.preloader("Loading..."); + apirequest( + "listlabels", + {}, + function (resp) { + app.dialog.close(); + if (resp.status == "ERROR") { + app.dialog.alert(resp.msg, "Error"); + reject(); + } else { + var context = { + labels: resp.labels, + machineid: routeTo.params.machineid + }; + resolve({ + templateUrl: "pages/labels.html", + }, { + context: context + }); + } + }, + function (xhr) { + app.dialog.close(); + var error = $.parseJSON(xhr.responseText); + if (error && typeof error.msg != 'undefined') { + app.dialog.alert(error.msg, "Error"); + } else { + app.dialog.alert("A server or network error occurred.", "Error"); + } + reject(); + }); +} + +function getPDFLabelAndDisplay(machineid, labeltype) { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { + if (this.readyState == 4 && this.status == 200) { + //this.response is what you're looking for + pdfobject = this.response; + + $("#print-fab").removeClass("display-none"); + $("#pdf-canvas").removeClass("display-none"); + $("#pdf-loading-message").addClass("display-none"); + + pdfjsLib.getDocument({data: pdfobject}).promise.then(function (pdf) { + pdf.getPage(1).then(function (page) { + var scale = 1.5; + var viewport = page.getViewport({scale: scale}); + + var canvas = document.getElementById('pdf-canvas'); + var context = canvas.getContext('2d'); + canvas.height = viewport.height; + canvas.width = viewport.width; + + var renderContext = { + canvasContext: context, + viewport: viewport + }; + page.render(renderContext); + }); + }); + } + } + xhr.open('POST', SETTINGS.apiserver + "getlabel", true, getStorage("username"), getStorage("password")); + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xhr.responseType = 'arraybuffer'; + xhr.send("id=" + machineid + "&type=" + labeltype); +} + +function sendPDFToPrintServer() { + var bytesArray = new Uint8Array(pdfobject); + + var printurl = getStorage("printserverurl"); + + $.getJSON(printurl, {action: "list"}, function (resp) { + var printers = resp.printers; + + var buttons = []; + for (var i = 0; i < printers.length; i++) { + buttons.push({ + text: printers[i] + }); + } + app.dialog.create({ + title: 'Print', + text: 'Select a printer', + buttons: buttons, + verticalButtons: true, + onClick: function (dialog, index) { + var printer = printers[index]; + + $.ajax({ + url: printurl + '?action=print&printer=' + printer, + type: 'POST', + contentType: 'application/octet-stream', + data: bytesArray, + processData: false + }); + } + }).open(); + + }); +} \ No newline at end of file diff --git a/www/assets/js/machine.js b/www/assets/js/machine.js index 76247ff..31125c2 100644 --- a/www/assets/js/machine.js +++ b/www/assets/js/machine.js @@ -31,18 +31,19 @@ function addMachineToSearchHistory(machineid) { setStorage("machinehistory", JSON.stringify(history)); } -function openMachineEditor(machineid) { +function machineEditorOpenAsync(routeTo, routeFrom, resolve, reject) { app.dialog.preloader("Loading editor..."); apirequest( "lookup", { - id: machineid + id: routeTo.params.id }, function (resp) { app.dialog.close(); if (resp.status == "ERROR") { app.dialog.alert(resp.msg, "Error"); + reject(); } else { var context = { machineid: resp.id, @@ -76,7 +77,10 @@ function openMachineEditor(machineid) { tabindex++; } } - router.navigate("/machineeditor", { + + resolve({ + templateUrl: "pages/machineeditor.html", + }, { context: context }); } @@ -89,21 +93,23 @@ function openMachineEditor(machineid) { } else { app.dialog.alert("A server or network error occurred.", "Error"); } + reject(); }); } -function openMachineInfo(machineid) { +function machineInfoOpenAsync(routeTo, routeFrom, resolve, reject) { app.dialog.preloader("Loading..."); apirequest( "lookup", { - id: machineid + id: routeTo.params.id }, function (resp) { app.dialog.close(); if (resp.status == "ERROR") { app.dialog.alert(resp.msg, "Error"); + reject(); } else { var events = []; @@ -175,7 +181,9 @@ function openMachineInfo(machineid) { } } addMachineToSearchHistory(resp.id); - router.navigate("/machine", { + resolve({ + templateUrl: "pages/machine.html", + }, { context: context }); } @@ -188,6 +196,7 @@ function openMachineInfo(machineid) { } else { app.dialog.alert("A server or network error occurred.", "Error"); } + reject(); }); } @@ -217,7 +226,7 @@ function saveMachineEdits(machineid) { text: 'Machine saved!', closeTimeout: 2000, }).open(); - openMachineInfo(machineid); + router.back(); } }, function (xhr) { diff --git a/www/assets/js/settings.js b/www/assets/js/settings.js index 5d49c8b..57db820 100644 --- a/www/assets/js/settings.js +++ b/www/assets/js/settings.js @@ -30,6 +30,42 @@ function resyncAndRestart() { }); } +function openPrintServerDialog() { + app.dialog.create({ + title: 'Print Server Setup', + buttons: [ + { + text: 'Manual Input', + }, + { + text: 'Scan Barcode', + }, + { + text: 'Cancel', + }, + ], + verticalButtons: true, + onClick: function (dialog, index) { + switch (index) { + case 0: + app.dialog.prompt('Print Server URL:', function (url) { + setStorage("printserverurl", url); + }); + break; + case 1: + scanBarcode(function (code) { + setStorage("printserverurl", code); + }, function (error) { + app.dialog.alert(error, "Error"); + }); + break; + default: + return; + } + } + }).open(); +} + function clearCaches() { app.toast.show({ text: "Clearing caches. You may need to restart the app to see a difference.", diff --git a/www/index.html b/www/index.html index 9d50354..11cfe0f 100644 --- a/www/index.html +++ b/www/index.html @@ -28,6 +28,8 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/. + + @@ -35,6 +37,7 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/. + diff --git a/www/package-lock.json b/www/package-lock.json index 2193955..ee89a5b 100644 --- a/www/package-lock.json +++ b/www/package-lock.json @@ -63,6 +63,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.1.0.tgz", "integrity": "sha512-h9DqehX3zZZDCEm+xbfU0ZmwCGFCAAraPJWMXJ4+v32NjZJilVg3k1TcKsRgIb8IQ/izZSaydDc1OhJCZvs2Dw==" }, + "pdfjs-dist": { + "version": "2.5.207", + "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-2.5.207.tgz", + "integrity": "sha512-xGDUhnCYPfHy+unMXCLCJtlpZaaZ17Ew3WIL0tnSgKFUZXHAPD49GO9xScyszSsQMoutNDgRb+rfBXIaX/lJbw==" + }, "ssr-window": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ssr-window/-/ssr-window-2.0.0.tgz", diff --git a/www/package.json b/www/package.json index 7579e85..80fe472 100644 --- a/www/package.json +++ b/www/package.json @@ -9,7 +9,8 @@ "framework7": "^5.5.1", "framework7-icons": "^3.0.0", "jquery": "^3.4.1", - "material-design-icons": "^3.0.1" + "material-design-icons": "^3.0.1", + "pdfjs-dist": "^2.5.207" }, "devDependencies": {} } diff --git a/www/pages/addevent.html b/www/pages/addevent.html index 8c33085..9fbc107 100644 --- a/www/pages/addevent.html +++ b/www/pages/addevent.html @@ -11,7 +11,7 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
- Open + Open
    {{#each props}} diff --git a/www/pages/home.html b/www/pages/home.html index 33ec6c4..0bce4e7 100644 --- a/www/pages/home.html +++ b/www/pages/home.html @@ -55,11 +55,11 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/. {{#each machinehistory}}
  • - +
  • {{/each}}
diff --git a/www/pages/labels.html b/www/pages/labels.html new file mode 100644 index 0000000..43c36a1 --- /dev/null +++ b/www/pages/labels.html @@ -0,0 +1,36 @@ + + +
+ + + +
+ +
+ +
\ No newline at end of file diff --git a/www/pages/machine.html b/www/pages/machine.html index 55ef690..a23b915 100644 --- a/www/pages/machine.html +++ b/www/pages/machine.html @@ -16,6 +16,11 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
{{type.label}} #{{machineid}}
+
@@ -30,9 +35,30 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/. -
- - edit + + {{else}} + {{/if}} diff --git a/www/pages/machineeditor.html b/www/pages/machineeditor.html index a4e89a6..ca85478 100644 --- a/www/pages/machineeditor.html +++ b/www/pages/machineeditor.html @@ -11,7 +11,7 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.