From a7843f975c53329724315a596f2cb55c53c5b13c Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Wed, 26 Feb 2020 11:45:30 -0500 Subject: [PATCH] Adjust config setup to account for different defaults in previous versions (#595) Adds internal "dirty" config for tracking defaults Allows HTML "rasterize: true" to remain for older QZ Tray versions --- js/qz-tray.js | 28 ++++++++++++++++++---- sample.html | 64 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/js/qz-tray.js b/js/qz-tray.js index 4b6b47e3f..6154b5b02 100644 --- a/js/qz-tray.js +++ b/js/qz-tray.js @@ -697,6 +697,17 @@ var qz = (function() { } }, + /* Converts config defaults to match previous version */ + config: function(config, dirty) { + if (_qz.tools.isVersion(2, 0)) { + if (!dirty.rasterize) { + config.rasterize = true; + } + } + + return config; + }, + /** Compat wrapper with previous version **/ networking: function(hostname, port, signature, signingTimestamp, mappingCallback) { // Use 2.0 @@ -747,6 +758,10 @@ var qz = (function() { /** Object to handle configured printer options. */ function Config(printer, opts) { + + this.config = _qz.tools.extend({}, _qz.printing.defaultConfig); //create a copy of the default options + this._dirtyOpts = {}; //track which config options have changed from the defaults + /** * Set the printer assigned to this config. * @param {string|Object} newPrinter Name of printer. Use object type to specify printing to file or host. @@ -777,6 +792,12 @@ var qz = (function() { * @see qz.configs.setDefaults */ this.reconfigure = function(newOpts) { + for(var key in newOpts) { + if (newOpts[key] !== undefined) { + this._dirtyOpts[key] = true; + } + } + _qz.tools.extend(this.config, newOpts); }; @@ -784,12 +805,12 @@ var qz = (function() { * @returns {Object} The currently applied options on this config. */ this.getOptions = function() { - return this.config; + return _qz.compatible.config(this.config, this._dirtyOpts); }; // init calls for new config object this.setPrinter(printer); - this.config = opts; + this.reconfigure(opts); } /** @@ -1163,8 +1184,7 @@ var qz = (function() { * @memberof qz.configs */ create: function(printer, options) { - var myOpts = _qz.tools.extend({}, _qz.printing.defaultConfig, options); - return new Config(printer, myOpts); + return new Config(printer, options); } }, diff --git a/sample.html b/sample.html index a52dd76fe..4e8c4bad5 100644 --- a/sample.html +++ b/sample.html @@ -2538,7 +2538,7 @@

Options

}); //make dirty when changed - $("input[type=checkbox]").on('click', function() { + $("input").on('change', function() { $(this).addClass("dirty"); }); @@ -2629,9 +2629,11 @@

Options

} function isChecked(checkElm, ifClean) { - if (ifClean !== undefined && !checkElm.hasClass("dirty")) { - var lbl = checkElm.siblings("label").text(); - displayMessage("Forced " + lbl + " " + ifClean + ".", 'alert-warning'); + if (!checkElm.hasClass("dirty")) { + if (ifClean !== undefined) { + var lbl = checkElm.siblings("label").text(); + displayMessage("Forced " + lbl + " " + ifClean + ".", 'alert-warning'); + } return ifClean; } @@ -2639,6 +2641,16 @@

Options

return checkElm.prop("checked"); } + function includedValue(element, value) { + if (value != null) { + return value; + } else if (element.hasClass("dirty")) { + return element.val(); + } else { + return undefined; + } + } + function usbButton(ids, data) { var click = ""; for(var i = 0; i < ids.length; i++) { @@ -2741,7 +2753,7 @@

Options

} function updateConfig(cleanConditions) { - var pxlSize = null; + var pxlSize = undefined; if (isChecked($("#pxlSizeActive"), cleanConditions['pxlSizeActive'])) { pxlSize = { width: $("#pxlSizeWidth").val(), @@ -2749,7 +2761,7 @@

Options

}; } - var pxlBounds = null; + var pxlBounds = undefined; if (isChecked($("#pxlBoundsActive"), cleanConditions['pxlBoundsActive'])) { pxlBounds = { x: $("#pxlBoundX").val(), @@ -2759,7 +2771,7 @@

Options

}; } - var pxlMargins = $("#pxlMargins").val(); + var pxlMargins = includedValue($("#pxlMargins")); if (isChecked($("#pxlMarginsActive"), cleanConditions['pxlMarginsActive'])) { pxlMargins = { top: $("#pxlMarginsTop").val(), @@ -2772,35 +2784,35 @@

Options

var copies = 1; var jobName = null; if ($("#rawTab").hasClass("active")) { - copies = $("#rawCopies").val(); - jobName = $("#rawJobName").val(); + copies = includedValue($("#rawCopies")); + jobName = includedValue($("#rawJobName")); } else { - copies = $("#pxlCopies").val(); - jobName = $("#pxlJobName").val(); + copies = includedValue($("#pxlCopies")); + jobName = includedValue($("#pxlJobName")); } cfg.reconfigure({ - altPrinting: isChecked($("#rawAltPrinting"), cleanConditions['rawAltPrinting']), - encoding: $("#rawEncoding").val(), - endOfDoc: $("#rawEndOfDoc").val(), - perSpool: $("#rawPerSpool").val(), + altPrinting: includedValue($("#rawAltPrinting"), isChecked($("#rawAltPrinting"), cleanConditions['rawAltPrinting'])), + encoding: includedValue($("#rawEncoding")), + endOfDoc: includedValue($("#rawEndOfDoc")), + perSpool: includedValue($("#rawPerSpool")), bounds: pxlBounds, - colorType: $("#pxlColorType").val(), + colorType: includedValue($("#pxlColorType")), copies: copies, - density: $("#pxlDensity").val(), - duplex: $("#pxlDuplex").val(), - interpolation: $("#pxlInterpolation").val(), + density: includedValue($("#pxlDensity")), + duplex: includedValue($("#pxlDuplex")), + interpolation: includedValue($("#pxlInterpolation")), jobName: jobName, margins: pxlMargins, - orientation: $("#pxlOrientation").val(), - paperThickness: $("#pxlPaperThickness").val(), - printerTray: $("#pxlPrinterTray").val(), - rasterize: isChecked($("#pxlRasterize"), cleanConditions['pxlRasterize']), - rotation: $("#pxlRotation").val(), - scaleContent: isChecked($("#pxlScale"), cleanConditions['pxlScale']), + orientation: includedValue($("#pxlOrientation")), + paperThickness: includedValue($("#pxlPaperThickness")), + printerTray: includedValue($("#pxlPrinterTray")), + rasterize: includedValue($("#pxlRasterize"), isChecked($("#pxlRasterize"), cleanConditions['pxlRasterize'])), + rotation: includedValue($("#pxlRotation")), + scaleContent: includedValue($("#pxlScale"), isChecked($("#pxlScale"), cleanConditions['pxlScale'])), size: pxlSize, - units: $("input[name='pxlUnits']:checked").val() + units: includedValue($("input[name='pxlUnits']:checked")) }); }