Printer Plugin für iOS funktioniert für cordova 3.x
This commit is contained in:
114
www/printer.js
114
www/printer.js
@@ -1,86 +1,46 @@
|
||||
|
||||
/**
|
||||
* Printer Plugin
|
||||
* Copyright (c) 2011 Ian Tipton (github.com/itip)
|
||||
* MIT licensed
|
||||
* Printer.m
|
||||
* Cordova Printer Plugin
|
||||
*
|
||||
* Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
|
||||
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
||||
* GPL v2 licensed
|
||||
*/
|
||||
|
||||
var PrintPlugin = function() {
|
||||
var Printer = function () {
|
||||
|
||||
}
|
||||
|
||||
PrintPlugin.prototype.callbackMap = {};
|
||||
PrintPlugin.prototype.callbackIdx = 0;
|
||||
|
||||
/*
|
||||
print - html string or DOM node (if latter, innerHTML is used to get the contents). REQUIRED.
|
||||
success - callback function called if print successful. {success: true}
|
||||
fail - callback function called if print unsuccessful. If print fails, {error: reason}. If printing not available: {available: false}
|
||||
options - {dialogOffset:{left: 0, right: 0}}. Position of popup dialog (iPad only).
|
||||
*/
|
||||
PrintPlugin.prototype.print = function(printHTML, success, fail, options) {
|
||||
if (typeof printHTML != 'string'){
|
||||
console.log("Print function requires an HTML string. Not an object");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//var printHTML = "";
|
||||
|
||||
var dialogLeftPos = 0;
|
||||
var dialogTopPos = 0;
|
||||
|
||||
|
||||
if (options){
|
||||
if (options.dialogOffset){
|
||||
if (options.dialogOffset.left){
|
||||
dialogLeftPos = options.dialogOffset.left;
|
||||
if (isNaN(dialogLeftPos)){
|
||||
dialogLeftPos = 0;
|
||||
}
|
||||
}
|
||||
if (options.dialogOffset.top){
|
||||
dialogTopPos = options.dialogOffset.top;
|
||||
if (isNaN(dialogTopPos)){
|
||||
dialogTopPos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var key = 'print' + this.callbackIdx++;
|
||||
window.plugins.printPlugin.callbackMap[key] = {
|
||||
success: function(result) {
|
||||
delete window.plugins.printPlugin.callbackMap[key];
|
||||
success(result);
|
||||
},
|
||||
fail: function(result) {
|
||||
delete window.plugins.printPlugin.callbackMap[key];
|
||||
fail(result);
|
||||
}
|
||||
};
|
||||
|
||||
var callbackPrefix = 'window.plugins.printPlugin.callbackMap.' + key;
|
||||
return cordova.exec("PrintPlugin.print", printHTML, callbackPrefix + '.success', callbackPrefix + '.fail', dialogLeftPos, dialogTopPos);
|
||||
};
|
||||
|
||||
/*
|
||||
* Callback function returns {available: true/false}
|
||||
*/
|
||||
PrintPlugin.prototype.isPrintingAvailable = function(callback) {
|
||||
var key = 'isPrintingAvailable' + this.callbackIdx++;
|
||||
window.plugins.printPlugin.callbackMap[key] = function(result) {
|
||||
delete window.plugins.printPlugin.callbackMap[key];
|
||||
callback(result);
|
||||
};
|
||||
Printer.prototype = {
|
||||
/**
|
||||
* Überprüft, ob der Drucker-Dienst verfügbar ist.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isServiceAvailable: function (callback) {
|
||||
cordova.exec(callback, null, 'Printer', 'isServiceAvailable', []);
|
||||
},
|
||||
|
||||
var callbackName = 'window.plugins.printPlugin.callbackMap.' + key;
|
||||
cordova.exec("PrintPlugin.isPrintingAvailable", callbackName);
|
||||
/**
|
||||
* Übergibt den HTML-Content an den Drucker-Dienst.
|
||||
*
|
||||
* @param {String} content html string or DOM node (if latter, innerHTML is used to get the contents)
|
||||
* @param {Function?} success callback function called if print successful. {success: true}
|
||||
* @param {Function?} failure callback function called if print unsuccessful. If print fails, {error: reason}. If printing not available: {available: false}
|
||||
*/
|
||||
print: function (content, success, failure) {
|
||||
content = content.innerHTML || content;
|
||||
|
||||
if (typeof content != 'string') {
|
||||
console.log('Print function requires an HTML string. Not an object');
|
||||
return;
|
||||
}
|
||||
|
||||
cordova.exec(success, failure, 'Printer', 'print', [content]);
|
||||
}
|
||||
};
|
||||
|
||||
cordova.addConstructor(function() {
|
||||
if(!window.plugins){
|
||||
window.plugins = {};
|
||||
}
|
||||
window.plugins.printPlugin = new PrintPlugin();
|
||||
});
|
||||
var printer = new Printer();
|
||||
|
||||
module.exports = printer;
|
Reference in New Issue
Block a user