mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
Barcode
This commit is contained in:
69
ionic/plugins/barcode/barcode.ts
Normal file
69
ionic/plugins/barcode/barcode.ts
Normal file
@ -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');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -17,7 +17,11 @@ export class NativePluginDecorator {
|
|||||||
cls.pluginWarn();
|
cls.pluginWarn();
|
||||||
|
|
||||||
// If the user supplied a default return value, return it here.
|
// 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 = () => {
|
cls.pluginWarn = () => {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export * from './plugin'
|
export * from './plugin'
|
||||||
|
export * from './barcode/barcode'
|
||||||
export * from './battery/battery'
|
export * from './battery/battery'
|
||||||
export * from './camera/camera'
|
export * from './camera/camera'
|
||||||
export * from './contacts/contacts'
|
export * from './contacts/contacts'
|
||||||
|
Reference in New Issue
Block a user