Skip to content

Commit

Permalink
chore: initial support for the engrave operation
Browse files Browse the repository at this point in the history
Makes use of colony print to run the engraving.
  • Loading branch information
joamag committed Jun 3, 2024
1 parent 2ac0dfe commit 495b854
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
45 changes: 45 additions & 0 deletions static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,24 @@ const serializeText = function(text, separator = "|") {
return buffer.join(separator);
};

const simplifyText = function(text, separator = "") {
const buffer = [];
let font = null;
for (let index = 0; index < text.length; index++) {
const item = text[index];
font = item[0];
buffer.push(item[1]);
}
return [buffer.join(separator), font];
};

jQuery(document).ready(function() {
// runs a series of selections over the current viewport
const body = jQuery("body");
const form = jQuery(".form");
const formInput = jQuery(".form > input");
const buttonClear = jQuery(".button-clear");
const buttonPrint = jQuery(".button-print");
const buttonReport = jQuery(".button-report");
const buttonReceipt = jQuery(".button-receipt");
const buttonDownload = jQuery(".button-download");
Expand Down Expand Up @@ -95,6 +107,35 @@ jQuery(document).ready(function() {
signature.jSignature("reset");
});

// registers for the print/engrave button click operation
// that makes use of colony print to engrave the values
buttonPrint.click(async function() {
const element = jQuery(this);
const text = element.attr("data-text");
const font = element.attr("data-font");

const printUrl = localStorage.getItem("url");
const node = localStorage.getItem("node");
const key = localStorage.getItem("key") || null;

// builds the parameters that are going to be used for the concrete
// printing operation and runs the print with the properly configured
// colony cloud print configuration
const printResponse = await fetch(`${printUrl}/nodes/${node}/print`, {
method: "POST",
body: new URLSearchParams([
["type", "gravo"],
["data", JSON.stringify({ text: text, font: font })]
]),
headers: { "X-Secret-Key": key }
});
if (printResponse.status !== 200) {
const error = await printResponse.json();
const errorMessage = error.message || error.error || "unset";
throw new Error(`Error while running the final print operation: ${errorMessage}`);
}
});

// registers for the download button click operation so that
// we obtain the SVG version and submit the current form with it
// effectively converting the data into HPGL
Expand Down Expand Up @@ -300,6 +341,10 @@ jQuery(document).ready(function() {

const buttonHref = buttonReport.attr("data-href");
buttonReport.attr("href", buttonHref + "?text=" + encodeURIComponent(serializeText(text)));

const [textSimple, font] = simplifyText(text);
buttonPrint.attr("data-text", textSimple);
buttonPrint.attr("data-font", font);
};

const printReceipt = async function() {
Expand Down
1 change: 1 addition & 0 deletions views/viewport.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</div>
<div class="buttons-right">
<a class="button button-report" href="/report" data-href="/report">Report</a>
<a class="button button-print" data-href="/report">Engrave</a>
</div>
<% if (theme === "ldj") { %>
<img class="logo" src="/static/images/logo.svg" />
Expand Down

0 comments on commit 495b854

Please sign in to comment.