Für Callbacks können Scopes angegeben werden. print() benötigt nur noch eine Callback-Funktion.

This commit is contained in:
Sebastián Katzer 2013-08-13 11:01:15 +02:00
parent 2d2b751977
commit 193ff10f94
2 changed files with 34 additions and 21 deletions

View File

@ -13,7 +13,7 @@
@interface APPPrinter (Private) @interface APPPrinter (Private)
// Bereitet den Drucker-Kontroller vor // Bereitet den Drucker-Kontroller vor
- (UIPrintInteractionController *) prepareController:(NSString*)content; - (UIPrintInteractionController *) prepareController:(NSString *)content;
// Überprüft, ob der Drucker-Dienst verfügbar ist // Überprüft, ob der Drucker-Dienst verfügbar ist
- (BOOL) isPrintServiceAvailable; - (BOOL) isPrintServiceAvailable;
@ -26,9 +26,9 @@
* Is printing available. * Is printing available.
* Callback returns true/false if printing is available/unavailable. * Callback returns true/false if printing is available/unavailable.
*/ */
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command - (void) isServiceAvailable:(CDVInvokedUrlCommand *)command
{ {
CDVPluginResult *pluginResult = nil; CDVPluginResult* pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:[self isPrintServiceAvailable]]; messageAsBool:[self isPrintServiceAvailable]];
@ -40,14 +40,14 @@
* Öffnet den Drucker-Kontroller zur Auswahl des Druckers. * Öffnet den Drucker-Kontroller zur Auswahl des Druckers.
* Callback gibt Meta-Informationen an. * Callback gibt Meta-Informationen an.
*/ */
- (void) print:(CDVInvokedUrlCommand*)command - (void) print:(CDVInvokedUrlCommand *)command
{ {
NSArray *arguments = [command arguments]; NSArray* arguments = [command arguments];
CDVPluginResult *pluginResult = nil; CDVPluginResult* pluginResult = nil;
if (![self isPrintServiceAvailable]) if (![self isPrintServiceAvailable])
{ {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:@"{success: false, available: false}"]; messageAsString:@"{success: false, available: false}"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
@ -57,7 +57,7 @@
if ([arguments count] == 0) if ([arguments count] == 0)
{ {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:@"{success: false, available: true}"]; messageAsString:@"{success: false, available: true}"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
@ -76,7 +76,7 @@
{ {
NSString *result = [NSString stringWithFormat:@"{success: false, available: true, error: \"%@\"}", error.localizedDescription]; NSString *result = [NSString stringWithFormat:@"{success: false, available: true, error: \"%@\"}", error.localizedDescription];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:result]; messageAsString:result];
} }
@ -132,7 +132,7 @@
if (printController) if (printController)
{ {
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];
return (controller != nil) && [UIPrintInteractionController isPrintingAvailable]; return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
} }

View File

@ -16,31 +16,44 @@ Printer.prototype = {
* Überprüft, ob der Drucker-Dienst verfügbar ist. * Überprüft, ob der Drucker-Dienst verfügbar ist.
* *
* @param {Function} callback * @param {Function} callback
* @param {Object?} scope callback scope (default: window)
*
* @return {Boolean} * @return {Boolean}
*/ */
isServiceAvailable: function (callback) { isServiceAvailable: function (callback, scope) {
cordova.exec(callback, null, 'Printer', 'isServiceAvailable', []); var callbackFn = function () {
callback.apply(scope || window, arguments);
};
cordova.exec(callbackFn, null, 'Printer', 'isServiceAvailable', []);
}, },
/** /**
* Übergibt den HTML-Content an den Drucker-Dienst. * Ü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 {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?} callback callback function called if print is completed. {success: bool, available: bool, error: reason}
* @param {Function?} failure callback function called if print unsuccessful. If print fails, {error: reason}. If printing not available: {available: false} * @param {Object?} scope callback scope (default: window)
*/ */
print: function (content, success, failure) { print: function (content, callback, scope) {
content = content.innerHTML || content; var page = content.innerHTML || content,
callbackFn;
if (typeof content != 'string') { if (typeof page != 'string') {
console.log('Print function requires an HTML string. Not an object'); console.log('Print function requires an HTML string. Not an object');
return; return;
} }
cordova.exec(success, failure, 'Printer', 'print', [content]); if (typeof callback == 'function'){
callbackFn = function () {
callback.apply(scope || window, arguments);
}
}
cordova.exec(callbackFn, null, 'Printer', 'print', [page]);
} }
}; };
var printer = new Printer(); var plugin = new Printer();
module.exports = printer; module.exports = plugin;