Created
August 1, 2019 22:28
-
-
Save parzibyte/7972e4f0329870c0aee6f807b488f408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Una clase para interactuar con el plugin | |
* | |
* @author parzibyte | |
* @see https://parzibyte.me/blog | |
*/ | |
const C = { | |
AccionWrite: "write", | |
AccionAlign: "align", | |
AccionFontSize: "fontsize", | |
AccionFont: "font", | |
AccionEmphasize: "emphasize", | |
AccionFeed: "feed", | |
AlineacionCentro: "center", | |
AlineacionDerecha: "right", | |
AlineacionIzquierda: "left", | |
FuenteA: "A", | |
FuenteB: "B" | |
}; | |
class OperacionTicket { | |
constructor(accion, datos) { | |
this.accion = accion + ""; | |
this.datos = datos + ""; | |
} | |
} | |
class Impresora { | |
constructor(ruta) { | |
if (!ruta) ruta = "http://localhost:8000"; | |
this.ruta = ruta; | |
this.operaciones = []; | |
} | |
setFontSize(a, b) { | |
this.operaciones.push(new OperacionTicket(C.AccionFontSize, `${a},${b}`)); | |
} | |
setFont(font) { | |
if (font !== C.FuenteA && font !== C.FuenteB) throw Error("Fuente inválida"); | |
this.operaciones.push(new OperacionTicket(C.AccionFont, font)); | |
} | |
setEmphasize(val) { | |
if (isNaN(parseInt(val)) || parseInt(val) < 0) throw Error("Valor inválido"); | |
this.operaciones.push(new OperacionTicket(C.AccionEmphasize, val)); | |
} | |
setAlign(align) { | |
if (align !== C.AlineacionCentro && align !== C.AlineacionDerecha && align !== C.AlineacionIzquierda) { | |
throw Error(`Alineación ${align} inválida`); | |
} | |
this.operaciones.push(new OperacionTicket(C.AccionAlign, align)); | |
} | |
write(text) { | |
this.operaciones.push(new OperacionTicket(C.AccionWrite, text)); | |
} | |
feed(n) { | |
if (!parseInt(n) || parseInt(n) < 0) { | |
throw Error("Valor para feed inválido"); | |
} | |
this.operaciones.push(new OperacionTicket(C.AccionFeed, n)); | |
} | |
end() { | |
return fetch(this.ruta + "/imprimir", { | |
method: "POST", | |
body: JSON.stringify(this.operaciones), | |
}) | |
.then(r => r.json()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment