Skip to content

Commit

Permalink
Adjust config setup to account for different defaults in previous ver…
Browse files Browse the repository at this point in the history
…sions (qzind#595)

Adds internal "dirty" config for tracking defaults
Allows HTML "rasterize: true" to remain for older QZ Tray versions
  • Loading branch information
tresf authored Feb 26, 2020
1 parent 34346d3 commit a7843f9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
28 changes: 24 additions & 4 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -777,19 +792,25 @@ 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);
};

/**
* @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);
}

/**
Expand Down Expand Up @@ -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);
}
},

Expand Down
64 changes: 38 additions & 26 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,7 @@ <h4 class="panel-title">Options</h4>
});

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

Expand Down Expand Up @@ -2629,16 +2629,28 @@ <h4 class="panel-title">Options</h4>
}

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;
}

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++) {
Expand Down Expand Up @@ -2741,15 +2753,15 @@ <h4 class="panel-title">Options</h4>
}

function updateConfig(cleanConditions) {
var pxlSize = null;
var pxlSize = undefined;
if (isChecked($("#pxlSizeActive"), cleanConditions['pxlSizeActive'])) {
pxlSize = {
width: $("#pxlSizeWidth").val(),
height: $("#pxlSizeHeight").val()
};
}

var pxlBounds = null;
var pxlBounds = undefined;
if (isChecked($("#pxlBoundsActive"), cleanConditions['pxlBoundsActive'])) {
pxlBounds = {
x: $("#pxlBoundX").val(),
Expand All @@ -2759,7 +2771,7 @@ <h4 class="panel-title">Options</h4>
};
}

var pxlMargins = $("#pxlMargins").val();
var pxlMargins = includedValue($("#pxlMargins"));
if (isChecked($("#pxlMarginsActive"), cleanConditions['pxlMarginsActive'])) {
pxlMargins = {
top: $("#pxlMarginsTop").val(),
Expand All @@ -2772,35 +2784,35 @@ <h4 class="panel-title">Options</h4>
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"))
});
}

Expand Down

0 comments on commit a7843f9

Please sign in to comment.