From c391314ede1553ddbdbd64ac80d79d91e219bb6c Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Tue, 15 Sep 2015 12:58:12 -0500 Subject: [PATCH] Barcode --- ionic/plugins/barcode/barcode.ts | 69 ++++++++++++++++++++++++++++++++ ionic/plugins/plugin.ts | 6 ++- ionic/plugins/plugins.ts | 1 + 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 ionic/plugins/barcode/barcode.ts diff --git a/ionic/plugins/barcode/barcode.ts b/ionic/plugins/barcode/barcode.ts new file mode 100644 index 0000000000..947769cc3e --- /dev/null +++ b/ionic/plugins/barcode/barcode.ts @@ -0,0 +1,69 @@ +import * as Rx from 'rx'; + +import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; + +@NativePlugin({ + name: 'Barcode', + platforms: ['ios', 'android'], + engines: { + cordova: 'phonegap-plugin-barcodescanner' + }, + pluginCheck: () => { + return window.cordova && window.cordova.plugins && window.cordova.plugins.barcodeScanner; + } +}) +export class Barcode { + static TEXT_TYPE = "TEXT_TYPE" + static EMAIL_TYPE = "EMAIL_TYPE" + static PHONE_TYPE = "PHONE_TYPE" + static SMS_TYPE = "SMS_TYPE" + + /** + * Scan a barcode. + * + * @return Promise that resolves with an object of the format: { + * text: text that was scanned, + * format: format of barcode, + * cancelled: was it canceled? + * } + */ + static scan() { + return new Promise((resolve, reject) => { + let hasPlugin = this.ifPlugin(() => { + window.cordova.plugins.barcodeScanner.scan((result) => { + resolve(result); + }, (err) => { + reject(err); + }) + }); + + if(!hasPlugin) { + reject('No scanner available'); + } + }); + } + + /** + * Encode the given data in a barcode. + * + * @param type the type to use for encoding (if in doubt, use TYPE_TEXT). + * @param data the data to encode + * @return Promise + */ + static encode(type, data) { + return new Promise((resolve, reject) => { + let hasPlugin = this.ifPlugin(() => { + window.cordova.plugins.barcodeScanner.encode(type, data, (result) => { + resolve(result); + }, (err) => { + reject(err); + }) + }); + + if(!hasPlugin) { + reject('No scanner available'); + } + }); + } +} diff --git a/ionic/plugins/plugin.ts b/ionic/plugins/plugin.ts index 94862335c9..49f4e5e6a5 100644 --- a/ionic/plugins/plugin.ts +++ b/ionic/plugins/plugin.ts @@ -17,7 +17,11 @@ export class NativePluginDecorator { cls.pluginWarn(); // If the user supplied a default return value, return it here. - return (typeof returnType === 'function') ? returnType() : returnType; + if(returnType) { + return (typeof returnType === 'function') ? returnType() : returnType; + } + + return false; }; cls.pluginWarn = () => { diff --git a/ionic/plugins/plugins.ts b/ionic/plugins/plugins.ts index 606d4eb963..ed313ea108 100644 --- a/ionic/plugins/plugins.ts +++ b/ionic/plugins/plugins.ts @@ -1,4 +1,5 @@ export * from './plugin' +export * from './barcode/barcode' export * from './battery/battery' export * from './camera/camera' export * from './contacts/contacts'