You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
8.7 KiB
JavaScript

const _pos58 = (function(){
var newPrint = document.createElement('iframe');
newPrint.id = 'new-print-js';
newPrint.style.display = 'none';
if(_id('new-print-js') == undefined){
document.body.appendChild(newPrint);
}
return {
data: {
printD: function(content){
if(content != undefined && typeof content === 'string'){
var w = _id('new-print-js').contentWindow || _id('new-print-js').contentDocument;
console.log(w);
w.document.open()
w.document.write(content);
w.document.close()
w.print();
;
}
return this;
},
container: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>POS58</title>
<style>
* {
font-size: 12px;
font-family: 'Times New Roman';
}
td,
th,
tr,
table {
border-collapse: collapse;
}
td.description,
th.description {
width: 85px;
max-width: 85px;
}
td.quantity,
th.quantity {
width: 40px;
max-width: 40px;
word-break: break-all;
}
td.price,
th.price {
width: 40px;
max-width: 40px;
word-break: break-all;
}
.centered {
text-align: center;
align-content: center;
}
.ticket {
width: 155px;
max-width: 155px;
}
img {
max-width: inherit;
width: inherit;
}
*{
font-family: Arial, Helvetica, sans-serif;
}
body{
background:#ddd;
}
.sheet{
width:58mm;
max-width:58mm;
min-width:58mm;
margin:auto;
background:white;
}
@media print {
*{
font-family: Arial, Helvetica, sans-serif;
}
.hidden-print,
.hidden-print * {
display: none !important;
}
}
@page
{
size: 54mm 210mm; /* auto is the initial value */
/* this affects the margin in the printer settings */
margin: 2mm 2mm 2mm 2mm;
}
</style>
</head>
<body class="receipt">
<section class="sheet">
{content}
</section>
</body>
</html>
`,
content: ''
},
content: function(a){
if(a != undefined && Array.isArray(a)){
this.data.content = a.map(function(d){
if( d != null && typeof d === 'object' ){
if(d.type != undefined && d.type == 'text'){
var e = el('p')
if(d.css != undefined && typeof d.css === 'object'){
e.css(d.css);
}
if(d.text != undefined && typeof d.text === 'string'){
e.html(d.text);
}
if(d.text != undefined && typeof d.text === 'function'){
e.html(d.text());
}
return e.get().outerHTML;
}
if(d.type != undefined && d.type == 'image'){
var e = el('img')
if(d.css != undefined && typeof d.css === 'object'){
e.css(d.css);
}
if(d.src != undefined && typeof d.src === 'string'){
e.src(d.src);
}
return div().css('text-align', 'center').child(e).get().outerHTML;
}
if(d.type != undefined && d.type == 'table'){
var e = el('table')
if(d.css != undefined && typeof d.css === 'object'){
e.css(d.css);
}
if(d.data != undefined && Array.isArray(d.data)){
d.data.forEach(function(x){
if( Array.isArray(x)){
var ax = el('tr');
x.forEach(function(ex){
if(typeof ex === 'object'){
var h = el('td');
h.css('vertical-align', 'top');
if(ex.text != undefined && typeof ex.text === 'string'){
h.html(ex.text);
}
if(ex.text != undefined && typeof ex.text === 'function'){
h.html(ex.text());
}
if(ex.colspan != undefined && typeof ex.colspan === 'number'){
h.attr('colspan', ex.colspan);
}
if(ex.rowspan != undefined && typeof ex.rowspan === 'number'){
h.attr('rowspan', ex.rowspan);
}
if(ex.css != undefined && typeof ex.css === 'object'){
h.css(ex.css);
}
ax.child(h)
}
});
e.child(ax);
}
})
}
return e.get().outerHTML;
}
return '';
}
}).join("");
}
return this;
},
print: function(){
var c = this.data.container;
var x = this.data.content;
var prd = this.data.printD;
var cx = c.replace(/\{content\}/g, x);
prd(cx);
// let newWin = window.open("about:blank", "hello", "width=" + screen.availWidth + ",height=" + screen.availHeight);
// newWin.document.write(cx);
// newWin.print();
// setTimeout(function(){
// newWin.close()
// },10)
return this;
}
}
})();