2013-12-11 20:00:16 +08:00
|
|
|
/*
|
2016-07-23 19:48:24 +08:00
|
|
|
Copyright 2013-2016 appPlant GmbH
|
2013-12-11 20:00:16 +08:00
|
|
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
or more contributor license agreements. See the NOTICE file
|
|
|
|
distributed with this work for additional information
|
|
|
|
regarding copyright ownership. The ASF licenses this file
|
|
|
|
to you under the Apache License, Version 2.0 (the
|
|
|
|
"License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
|
|
software distributed under the License is distributed on an
|
|
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
KIND, either express or implied. See the License for the
|
|
|
|
specific language governing permissions and limitations
|
|
|
|
under the License.
|
|
|
|
*/
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-09-08 04:52:47 +08:00
|
|
|
var exec = require('cordova/exec');
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-10-14 05:31:12 +08:00
|
|
|
/**
|
|
|
|
* List of all available options with their default value.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
exports.getDefaults = function () {
|
|
|
|
return {
|
2019-01-21 22:17:07 +08:00
|
|
|
// name: 'unknown',
|
|
|
|
// duplex: 'none',
|
|
|
|
// landscape: false,
|
|
|
|
// graystyle: false,
|
|
|
|
// border: true,
|
2019-01-03 23:52:10 +08:00
|
|
|
copies: 1,
|
2019-01-21 22:17:07 +08:00
|
|
|
|
|
|
|
ui: {
|
|
|
|
hideNumberOfCopies: false,
|
|
|
|
hidePaperFormat: false,
|
|
|
|
bounds: [40, 30, 0, 0]
|
|
|
|
},
|
|
|
|
|
|
|
|
paper: {
|
|
|
|
unit: 'cm',
|
|
|
|
height: 0,
|
|
|
|
width: 0,
|
|
|
|
length: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
layout: {
|
|
|
|
unit: 'cm',
|
|
|
|
maxHeight: 0,
|
|
|
|
maxWidth: 0,
|
|
|
|
padding: { top: 0, left: 0, right: 0, bottom: 0 }
|
2019-01-22 00:52:28 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
header: {
|
|
|
|
unit: 'cm',
|
2019-01-22 22:27:31 +08:00
|
|
|
height: 1,
|
2019-01-22 00:52:28 +08:00
|
|
|
|
2019-01-22 22:27:31 +08:00
|
|
|
labels: [{
|
|
|
|
text: 'Awesome Printer Plug-in',
|
|
|
|
align: 'center',
|
|
|
|
italic: true,
|
|
|
|
color: '#FF0000'
|
|
|
|
},{
|
|
|
|
showPageIndex: true,
|
|
|
|
align: 'right',
|
|
|
|
bold: true
|
|
|
|
}]
|
2019-01-21 22:17:07 +08:00
|
|
|
}
|
2014-10-14 05:31:12 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-09-08 04:52:47 +08:00
|
|
|
/**
|
|
|
|
* Checks if the printer service is avaible (iOS)
|
2016-07-31 19:32:21 +08:00
|
|
|
* or if services are available (Android).
|
2014-09-08 04:52:47 +08:00
|
|
|
*
|
|
|
|
* @param {Function} callback
|
|
|
|
* A callback function
|
|
|
|
* @param {Object?} scope
|
|
|
|
* The scope of the callback (default: window)
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2016-08-03 23:20:10 +08:00
|
|
|
exports.check = function (callback, scope) {
|
2014-09-08 04:52:47 +08:00
|
|
|
var fn = this._createCallbackFn(callback);
|
2013-08-22 00:04:51 +08:00
|
|
|
|
2016-08-03 23:20:10 +08:00
|
|
|
exec(fn, null, 'Printer', 'check', []);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated API call. Use `check` instead!
|
|
|
|
*/
|
|
|
|
exports.isAvailable = function () {
|
|
|
|
exports.check.apply(exports, arguments);
|
2014-09-08 04:52:47 +08:00
|
|
|
};
|
2013-08-13 17:01:15 +08:00
|
|
|
|
2015-06-24 01:58:41 +08:00
|
|
|
/**
|
2016-07-31 19:32:21 +08:00
|
|
|
* Displays system interface for selecting a printer.
|
2015-06-24 01:58:41 +08:00
|
|
|
*
|
|
|
|
* @param {Function} callback
|
|
|
|
* A callback function
|
|
|
|
* @param {Object} options
|
|
|
|
* Options for the printer picker
|
|
|
|
*/
|
2016-07-25 21:04:20 +08:00
|
|
|
exports.pick = function (callback, options) {
|
|
|
|
var fn = this._createCallbackFn(callback);
|
2015-06-24 01:58:41 +08:00
|
|
|
var params = options || {};
|
2016-07-25 21:04:20 +08:00
|
|
|
|
2015-06-24 01:58:41 +08:00
|
|
|
params = this.mergeWithDefaults(params);
|
2016-07-25 21:04:20 +08:00
|
|
|
|
|
|
|
exec(fn, null, 'Printer', 'pick', [params]);
|
2015-06-24 01:58:41 +08:00
|
|
|
};
|
|
|
|
|
2014-09-08 04:52:47 +08:00
|
|
|
/**
|
2016-07-31 19:32:21 +08:00
|
|
|
* Sends the content to the Printing Framework.
|
2014-09-08 04:52:47 +08:00
|
|
|
*
|
|
|
|
* @param {String} content
|
|
|
|
* HTML string or DOM node
|
|
|
|
* if latter, innerHTML is used to get the content
|
|
|
|
* @param {Object} options
|
|
|
|
* Options for the print job
|
|
|
|
* @param {Function?} callback
|
|
|
|
* A callback function
|
|
|
|
* @param {Object?} scope
|
|
|
|
* The scope of the callback (default: window)
|
|
|
|
*/
|
|
|
|
exports.print = function (content, options, callback, scope) {
|
|
|
|
var page = content.innerHTML || content,
|
|
|
|
params = options || {},
|
|
|
|
fn = this._createCallbackFn(callback);
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2014-09-08 04:52:47 +08:00
|
|
|
if (typeof page != 'string') {
|
|
|
|
console.log('Print function requires an HTML string. Not an object');
|
|
|
|
return;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2015-04-23 05:24:57 +08:00
|
|
|
if (typeof params == 'string') {
|
2014-09-08 04:52:47 +08:00
|
|
|
params = { name: params };
|
2015-04-23 05:24:57 +08:00
|
|
|
}
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-10-14 05:31:12 +08:00
|
|
|
params = this.mergeWithDefaults(params);
|
|
|
|
|
2015-04-23 05:24:57 +08:00
|
|
|
if ([null, undefined, ''].indexOf(params.name) > -1) {
|
2016-07-23 19:48:24 +08:00
|
|
|
params.name = this.getDefaults().name;
|
2015-04-23 05:24:57 +08:00
|
|
|
}
|
2014-09-08 04:52:47 +08:00
|
|
|
|
|
|
|
exec(fn, null, 'Printer', 'print', [page, params]);
|
2013-08-10 00:58:47 +08:00
|
|
|
};
|
|
|
|
|
2014-10-14 05:31:12 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Merge settings with default values.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* The custom options
|
|
|
|
*
|
|
|
|
* @retrun {Object}
|
|
|
|
* Default values merged
|
|
|
|
* with custom values
|
|
|
|
*/
|
|
|
|
exports.mergeWithDefaults = function (options) {
|
|
|
|
var defaults = this.getDefaults();
|
|
|
|
|
|
|
|
if (options.bounds && !options.bounds.length) {
|
|
|
|
options.bounds = [
|
|
|
|
options.bounds.left || defaults.bounds[0],
|
|
|
|
options.bounds.top || defaults.bounds[1],
|
|
|
|
options.bounds.width || defaults.bounds[2],
|
|
|
|
options.bounds.height || defaults.bounds[3],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-07-23 19:48:24 +08:00
|
|
|
if (options.duplex && typeof options.duplex == 'boolean') {
|
|
|
|
options.duplex = options.duplex ? 'long' : 'none';
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:31:12 +08:00
|
|
|
for (var key in defaults) {
|
|
|
|
if (!options.hasOwnProperty(key)) {
|
|
|
|
options[key] = defaults[key];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options[key] != typeof defaults[key]) {
|
|
|
|
delete options[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
2014-09-08 04:52:47 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Creates a callback, which will be executed within a specific scope.
|
|
|
|
*
|
|
|
|
* @param {Function} callbackFn
|
|
|
|
* The callback function
|
|
|
|
* @param {Object} scope
|
|
|
|
* The scope for the function
|
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
* The new callback function
|
|
|
|
*/
|
|
|
|
exports._createCallbackFn = function (callbackFn, scope) {
|
|
|
|
if (typeof callbackFn != 'function')
|
|
|
|
return;
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2014-09-08 04:52:47 +08:00
|
|
|
return function () {
|
|
|
|
callbackFn.apply(scope || this, arguments);
|
|
|
|
};
|
2014-10-14 05:31:12 +08:00
|
|
|
};
|