This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/static/js/print.pdf.js

313 lines
9.1 KiB
JavaScript
Raw Permalink Normal View History

2022-01-28 10:03:57 +00:00
$(document).ready(function() {
STORAGE_KEY = 'tag-spec-key';
$("#printerType").on("change", change_size);
$(".form-check-input").on("change", change_check);
2022-01-28 10:03:57 +00:00
change_size();
load_settings();
change_check();
$("#imgInp").change(function(){
readURL(this);
});
2022-01-28 10:03:57 +00:00
})
2022-03-16 13:27:55 +00:00
function qr_draw(url, id) {
var qrcode = new QRCode($(id)[0], {
2022-01-28 10:03:57 +00:00
text: url,
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#ffffff",
2022-01-31 09:12:44 +00:00
correctLevel : QRCode.CorrectLevel.Q
2022-01-28 10:03:57 +00:00
});
}
function previewLogo(logo) {
const img = "<img style='max-width: 150px' src='"+logo+"' />";
$("#logo-preview").html(img);
$(".label-logo-dev").html(img);
}
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
previewLogo(e.target.result);
$("#logoCheck").prop('disabled', '');
}
reader.readAsDataURL(input.files[0]);
}
}
function save_settings() {
var logo = $('#logo-preview img').attr('src');
2022-01-28 10:03:57 +00:00
var height = $("#height-tag").val();
var width = $("#width-tag").val();
var sizePreset = $("#printerType").val();
var data = {"height": height, "width": width, "sizePreset": sizePreset, 'logoImg': ''};
if (logo) {
data['logoImg'] = logo;
};
data['logo'] = $("#logoCheck").prop('checked');
data['dhid'] = $("#dhidCheck").prop('checked');
2022-06-08 09:59:47 +00:00
data['sid'] = $("#sidCheck").prop('checked');
2022-07-21 11:56:09 +00:00
data['phid'] = $("#phidCheck").prop('checked');
data['tags'] = $("#tagsCheck").prop('checked');
data['qr'] = $("#qrCheck").prop('checked');
data['serial_number'] = $("#serialNumberCheck").prop('checked');
data['manufacturer'] = $("#manufacturerCheck").prop('checked');
data['model'] = $("#modelCheck").prop('checked');
2022-01-28 10:03:57 +00:00
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
}
function load_settings() {
2022-01-28 10:03:57 +00:00
var data = JSON.parse(localStorage.getItem(STORAGE_KEY));
if (data){
$("#height-tag").val(data.height);
$("#width-tag").val(data.width);
$("#printerType").val(data.sizePreset);
$("#qrCheck").prop('checked', data.qr);
$("#dhidCheck").prop('checked', data.dhid);
2022-06-08 09:59:47 +00:00
$("#sidCheck").prop('checked', data.sid);
2022-07-21 11:56:09 +00:00
$("#phidCheck").prop('checked', data.phid);
$("#tagsCheck").prop('checked', data.tags);
$("#serialNumberCheck").prop('checked', data.serial_number);
$("#manufacturerCheck").prop('checked', data.manufacturer);
$("#modelCheck").prop('checked', data.model);
if (data.logo) {
2022-07-21 11:56:09 +00:00
// $("#logoCheck").prop('checked', data.sid);
previewLogo(data.logoImg);
2022-06-09 14:57:15 +00:00
$("#logoCheck").prop('checked', data.logo);
} else {
$("#logoCheck").prop('checked', false);
$("#logoCheck").prop('disabled', 'disabled');
}
2022-01-28 10:03:57 +00:00
};
}
function reset_settings() {
2022-01-28 10:03:57 +00:00
localStorage.removeItem(STORAGE_KEY);
$("#printerType").val('brotherSmall');
$("#qrCheck").prop('checked', true);
$("#dhidCheck").prop('checked', true);
2022-06-08 09:59:47 +00:00
$("#sidCheck").prop('checked', true);
2022-07-21 11:56:09 +00:00
$("#phidCheck").prop('checked', true);
$("#tagsCheck").prop('checked', false);
$("#serialNumberCheck").prop('checked', false);
$("#logoCheck").prop('checked', false);
$("#manufacturerCheck").prop('checked', false);
$("#modelCheck").prop('checked', false);
$('#logo-preview').html('');
2022-01-28 10:03:57 +00:00
change_size();
change_check();
2022-01-28 10:03:57 +00:00
}
function change_size() {
var sizePreset = $("#printerType").val();
if (sizePreset == 'brotherSmall') {
$("#height-tag").val(29);
$("#width-tag").val(62);
} else if (sizePreset == 'smallTagPrinter') {
$("#height-tag").val(59);
$("#width-tag").val(97);
}
}
function change_check() {
if ($('#logo-preview img').attr('src')) {
$("#logoCheck").prop('disabled', '');
} else {
$("#logoCheck").prop('checked', false);
$("#logoCheck").prop('disabled', 'disabled');
};
if ($("#logoCheck").prop('checked') && $('#logo-preview img').attr('src')) {
$(".label-logo").show();
} else {
$(".label-logo").hide();
};
if ($("#dhidCheck").prop('checked')) {
$(".dhid").show();
} else {
$(".dhid").hide();
};
2022-06-08 09:59:47 +00:00
if ($("#sidCheck").prop('checked')) {
$(".sid").show();
} else {
$(".sid").hide();
};
2022-07-21 11:56:09 +00:00
if ($("#phidCheck").prop('checked')) {
$(".phid").show();
} else {
$(".phid").hide();
};
if ($("#tagsCheck").prop('checked')) {
$(".tags").show();
} else {
$(".tags").hide();
};
if ($("#serialNumberCheck").prop('checked')) {
$(".serial_number").show();
} else {
$(".serial_number").hide();
};
if ($("#manufacturerCheck").prop('checked')) {
$(".manufacturer").show();
} else {
$(".manufacturer").hide();
};
if ($("#modelCheck").prop('checked')) {
$(".model").show();
} else {
$(".model").hide();
};
if ($("#qrCheck").prop('checked')) {
$(".qr").show();
} else {
$(".qr").hide();
};
}
2022-01-28 10:03:57 +00:00
function printpdf() {
2022-01-31 09:12:44 +00:00
var border = 2;
var line = 5;
2022-01-28 11:29:49 +00:00
var height = parseInt($("#height-tag").val());
var width = parseInt($("#width-tag").val());
2022-06-09 14:57:15 +00:00
var logo = '';
var _rel = 1;
if ($('#logoCheck').prop('checked')) {
logo = $("#logo-preview img").attr("src");
if (logo) {
var _img = new Image();
_img.src = logo;
_rel = parseInt(_img.height)/parseInt(_img.width);
}
}
var img_side = (width/2) - 2*border;
2022-03-16 13:27:55 +00:00
var last_tag_code = '';
2022-06-09 14:57:15 +00:00
var height_need = border*2;
if (logo) {
height_need += width*_rel + border;
};
if ($("#qrCheck").prop('checked')) {
height_need += img_side;
} else if ($("#dhidCheck").prop('checked')) {
height_need += line;
};
2022-06-08 09:59:47 +00:00
if ($("#sidCheck").prop('checked')) {
2022-06-09 14:57:15 +00:00
height_need += line;
2022-06-08 09:59:47 +00:00
};
2022-07-21 11:56:09 +00:00
if ($("#phidCheck").prop('checked')) {
height_need += line;
};
if ($("#tagsCheck").prop('checked')) {
height_need += line;
};
if ($("#serialNumberCheck").prop('checked')) {
2022-06-09 14:57:15 +00:00
height_need += line;
};
if ($("#manufacturerCheck").prop('checked')) {
2022-06-09 14:57:15 +00:00
height_need += line;
};
if ($("#modelCheck").prop('checked')) {
2022-06-09 14:57:15 +00:00
height_need += line;
};
2022-06-09 14:57:15 +00:00
height = Math.max(height, height_need);
2022-06-09 14:57:15 +00:00
if (width > height) {
var pdf = new jsPDF('l', 'mm', [width, height]);
} else {
var pdf = new jsPDF('p', 'mm', [height, width]);
};
var hlogo = 0;
2022-03-16 13:27:55 +00:00
$(".tag").map(function(x, y) {
if (x != 0){
pdf.addPage();
};
2022-06-09 14:57:15 +00:00
var hspace = border;
2022-03-16 13:27:55 +00:00
var tag = $(y).text();
last_tag_code = tag;
2022-06-09 14:57:15 +00:00
if (logo) {
var wlogo = (width - border*2);
hlogo = wlogo*_rel;
pdf.addImage(logo, 'PNG', border, hspace, wlogo, hlogo);
hspace += hlogo + border;
};
if ($("#qrCheck").prop('checked')) {
var imgData = $('#'+tag+' img').attr("src");
2022-06-09 14:57:15 +00:00
pdf.addImage(imgData, 'PNG', border, hspace, img_side, img_side);
hspace += img_side;
} else {
hspace += line;
};
if ($("#dhidCheck").prop('checked')) {
2022-06-09 14:57:15 +00:00
pdf.setFontSize(15);
if ($("#qrCheck").prop('checked')) {
var h = hspace + border - img_side/2;
var w = border*2 + img_side;
2022-07-25 15:37:00 +00:00
pdf.text(String(tag), w, h);
2022-06-09 14:57:15 +00:00
} else {
2022-07-25 15:37:00 +00:00
pdf.text(String(tag), border, hspace);
2022-06-09 14:57:15 +00:00
}
hspace += line;
};
2022-06-08 09:59:47 +00:00
if ($("#sidCheck").prop('checked')) {
var sn = $(y).data('sid');
2022-06-09 14:57:15 +00:00
pdf.setFontSize(12);
if (sn) {
2022-07-21 11:56:09 +00:00
pdf.text(String(sn), border, hspace);
hspace += line;
}
};
if ($("#phidCheck").prop('checked')) {
var sn = $(y).data('phid');
pdf.setFontSize(12);
if (sn) {
pdf.text(String(sn), border, hspace);
hspace += line;
}
};
if ($("#tagsCheck").prop('checked')) {
var sn = $(y).data('tags');
pdf.setFontSize(12);
if (sn) {
pdf.text(String(sn), border, hspace);
2022-06-09 14:57:15 +00:00
hspace += line;
}
2022-06-08 09:59:47 +00:00
};
if ($("#serialNumberCheck").prop('checked')) {
var sn = $(y).data('serial-number');
pdf.setFontSize(12);
2022-07-21 11:56:09 +00:00
pdf.text(String(sn), border, hspace);
2022-06-09 14:57:15 +00:00
hspace += line;
};
if ($("#manufacturerCheck").prop('checked')) {
var sn = $(y).data('manufacturer');
pdf.setFontSize(12);
2022-07-21 11:56:09 +00:00
pdf.text(String(sn), border, hspace);
2022-06-09 14:57:15 +00:00
hspace += line;
};
if ($("#modelCheck").prop('checked')) {
var sn = $(y).data('model');
pdf.setFontSize(8);
2022-07-21 11:56:09 +00:00
pdf.text(String(sn), border, hspace);
2022-06-09 14:57:15 +00:00
hspace += line;
};
2022-03-16 13:27:55 +00:00
});
pdf.save('Tag_'+last_tag_code+'.pdf');
2022-01-28 10:03:57 +00:00
}