96 lines
3.2 KiB
JavaScript
96 lines
3.2 KiB
JavaScript
/*******************************************************************
|
|
|
|
Module : /assets/js/qz/qz-start.js
|
|
Desc. : Setup QZ Connection
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : May 27th, 2007.
|
|
Last Modified : August 22nd, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
const _divStatus = '#divQZStatus';
|
|
|
|
if (typeof qz !== 'undefined') {
|
|
qz.security.setSignatureAlgorithm("SHA512");
|
|
qz.security.setCertificatePromise((resolve, reject) => {
|
|
jQuery.ajax({url:'qz/cert', cache:false, dataType:'text'}).then(resolve, reject);
|
|
});
|
|
qz.security.setSignaturePromise((toSign) => {
|
|
return (resolve, reject) => {
|
|
jQuery.ajax('qz/sign/'+toSign).then(resolve, reject);
|
|
};
|
|
});
|
|
qz.websocket.setClosedCallbacks((evt) => {
|
|
_updateState('Inactive');
|
|
if (evt.reason) console.info("Connection closed: " + evt.reason);
|
|
});
|
|
qz.websocket.setErrorCallbacks((evt) => {
|
|
_updateState('Error');
|
|
if (evt.target != undefined) {
|
|
var err = (evt.target.readyState >= 2) ? "Connection to QZ Tray was closed" : "A connection error occurred, check log for details";
|
|
console.info(err)
|
|
}
|
|
});
|
|
}
|
|
|
|
function launchQZ() {
|
|
if (!qz.websocket.isActive()) {
|
|
window.location.assign("qz:launch");
|
|
startConnection({ retries: 5, delay: 1 });
|
|
}
|
|
}
|
|
|
|
function startConnection(config) {
|
|
config = config ? config : {};
|
|
if (!qz.websocket.isActive()) {
|
|
_updateState('Waiting');
|
|
qz.websocket.connect({...config, host: 'localhost'}).then(() => {
|
|
_updateState('Active');
|
|
_findDefaultPrinter();
|
|
}).catch(() => _updateState('Error'));
|
|
} else {
|
|
console.info('An active connection with QZ already exists.');
|
|
}
|
|
}
|
|
|
|
function endConnection(){
|
|
if (qz.websocket.isActive()) {
|
|
qz.websocket.disconnect().then(() => {
|
|
_updateState('Inactive');
|
|
}).catch(() => _updateState('Error'));
|
|
} else {
|
|
console.info('No active connection with QZ exists.');
|
|
}
|
|
}
|
|
|
|
function _findDefaultPrinter(){
|
|
qz.printers.getDefault().then((data) => {
|
|
var _printer = data;
|
|
if (typeof data === 'object' && data.name == undefined) {
|
|
if (data.file != undefined) _printer = "FILE: " + data.file;
|
|
if (data.host != undefined) _printer = "HOST: " + data.host + ":" + data.port;
|
|
} else {
|
|
if (data.name != undefined) _printer = data.name;
|
|
if (data== undefined) _printer = 'NONE';
|
|
}
|
|
FoniaJS._def_printer = data;
|
|
FoniaJS._def_printname = _printer;
|
|
setTimeout(() => $(_divStatus).html($(_divStatus).html() + ' // ' + _printer), 2000);
|
|
});
|
|
}
|
|
|
|
function _updateState(text){
|
|
$(_divStatus).html(text).removeClass('border-success border-warning text-success');
|
|
if (text == "Active") {
|
|
$(_divStatus).addClass('border-success text-success');
|
|
} else {
|
|
$(_divStatus).addClass('border-warning');
|
|
}
|
|
$('li#qzL, li#qzS, li#qzZ').removeClass('d-none');
|
|
if (text !== "Error") {
|
|
$('li#qzL').addClass('d-none');
|
|
} else $('li#qzS, li#qzZ').addClass('d-none');
|
|
}
|