Some fixes and enhancements for Windows

This commit is contained in:
Sebastián Katzer 2019-02-01 14:47:07 +01:00
parent 0dac03a168
commit 49ad2187df

View File

@ -27,47 +27,63 @@ var Printing = Windows.Graphics.Printing,
/** /**
* Verifies if printing is supported on the device. * Verifies if printing is supported on the device.
* *
* @param {Function} success * @param [ Function ] success Success callback function.
* Success callback function * @param [ Function ] error Error callback function.
* @param {Function} error * @param [ Array ] args Interface arguments.
* Error callback function *
* @param {Array} args * @return [ Void ]
* Interface arguments
*/ */
exports.check = function (success, fail, args) { exports.check = function (success, fail, args) {
success(MSApp.hasOwnProperty('getHtmlPrintDocumentSourceAsync'), 0); var item = args[0],
supported = PrintManager.isSupported();
if (!item || !supported) {
success(supported);
return;
}
if (item[0] === '<') {
supported = true;
} else {
supported = item.match(/[a-z0-9]:\/\//) === null;
}
success(supported);
}; };
/** /**
* Displays system interface for selecting a printer. * List of printable document types.
* *
* @param {Function} success * @param [ Function ] success Success callback function.
* Success callback function * @param [ Function ] error Error callback function.
* @param {Function} error * @param [ Array ] args Interface arguments.
* Error callback function *
* @param {Array} args * @return [ Void ]
* Interface arguments
*/ */
exports.pick = function (success, fail, args) { exports.utis = function (success, fail, args) {
success(); // Not supported :( success([]);
}; };
/** /**
* Sends the content to the Printing Framework. * Sends the content to the Printing Framework.
* *
* @param {Function} success * @param [ Function ] success Success callback function.
* Success callback function * @param [ Function ] error Error callback function.
* @param {Function} error * @param [ Array ] args Interface arguments.
* Error callback function *
* @param {Array} args * @return [ Void ]
* Interface arguments
*/ */
exports.print = function (success, fail, args) { exports.print = function (success, fail, args) {
var page = document.createDocumentFragment(), var content = args[0],
content = document.createElement('html'); page = document, body;
content.innerHTML = args[0]; if (content && content.length > 0) {
page.appendChild(content); page = document.createDocumentFragment();
body = document.createElement('html');
body.innerHTML = content;
page.appendChild(body);
}
exports._func = success; exports._func = success;
exports._args = args[1]; exports._args = args[1];
@ -82,40 +98,56 @@ exports.print = function (success, fail, args) {
* Raised when a request to print has occurred. * Raised when a request to print has occurred.
* Create, configure and schedule the print task. * Create, configure and schedule the print task.
* *
* @param {PrintTaskRequestedEventArgs} event * @param [ PrintTaskRequestedEventArgs ] event Event arguments.
* Event arguments associated with the request. *
* @return [ Void ]
*/ */
exports.onPrintTaskRequested = function (event) { exports.onPrintTaskRequested = function (event) {
var config = exports._args, var config = exports._args,
task; task, spec;
task = event.request.createPrintTask(config.name, function (args) { task = event.request.createPrintTask(config.name, function (args) {
args.setSource(exports._page); args.setSource(exports._page);
}); });
spec = task.options;
if (config.monochrome) { if (config.monochrome) {
task.options.colorMode = Printing.PrintColorMode.grayscale; spec.colorMode = Printing.PrintColorMode.grayscale;
} else { } else {
task.options.colorMode = Printing.PrintColorMode.color; spec.colorMode = Printing.PrintColorMode.color;
} }
if (config.landscape) { if (config.landscape) {
task.options.orientation = Printing.PrintOrientation.landscape; spec.orientation = Printing.PrintOrientation.landscape;
} else { } else {
task.options.orientation = Printing.PrintOrientation.portrait; spec.orientation = Printing.PrintOrientation.portrait;
} }
if (config.duplex == 'long') { if (config.duplex == 'long') {
task.options.duplex = Printing.PrintDuplex.twoSidedLongEdge; spec.duplex = Printing.PrintDuplex.twoSidedLongEdge;
} else } else
if (config.duplex == 'short') { if (config.duplex == 'short') {
task.options.duplex = Printing.PrintDuplex.twoSidedShortEdge; spec.duplex = Printing.PrintDuplex.twoSidedShortEdge;
} else { } else {
task.options.duplex = Printing.PrintDuplex.oneSided; spec.duplex = Printing.PrintDuplex.oneSided;
}
if (config.photo) {
spec.printQuality = Printing.PrintQuality.photographic;
spec.mediaType = Printing.PrintMediaType.photographic;
}
if (config.border === false) {
spec.bordering = Printing.PrintBordering.borderless;
}
if (config.paper && config.paper.name) {
spec.mediaSize = Printing.PrintMediaSize[config.paper.name] || Printing.PrintMediaSize.default;
} }
try { try {
task.options.numberOfCopies = config.copies || 1; spec.numberOfCopies = config.copies || 1;
} catch (e) {} } catch (e) {}
task.oncompleted = function (e) { task.oncompleted = function (e) {
@ -123,7 +155,6 @@ exports.onPrintTaskRequested = function (event) {
}; };
}; };
PrintManager.getForCurrentView() PrintManager.getForCurrentView().onprinttaskrequested = exports.onPrintTaskRequested;
.onprinttaskrequested = exports.onPrintTaskRequested;
require('cordova/exec/proxy').add('Printer', exports); require('cordova/exec/proxy').add('Printer', exports);