This commit is contained in:
Max Lynch 2015-11-24 16:55:09 -06:00
parent eb0b857b96
commit d934853b01
8 changed files with 80 additions and 25 deletions

View File

@ -1,2 +1,2 @@
# ionic-native # Cordova Wrap
Native plugins to replace ngCordova Native plugins to replace ngCordova

View File

@ -13,14 +13,14 @@ function compile(watch) {
var bundler = watchify(browserify() var bundler = watchify(browserify()
.require('./src/index.js', { .require('./src/index.js', {
entry: true, entry: true,
expose: 'ionic-native' expose: 'cordova-wrap'
}) })
.transform(babel, {presets: ['es2015']})); .transform(babel, {presets: ['es2015']}));
function rebundle() { function rebundle() {
bundler.bundle() bundler.bundle()
.on('error', function(err) { console.error(err); this.emit('end'); }) .on('error', function(err) { console.error(err); this.emit('end'); })
.pipe(source('ionic-native.js')) .pipe(source('cordova-wrap.js'))
.pipe(buffer()) .pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true })) .pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./')) .pipe(sourcemaps.write('./'))

View File

@ -2,23 +2,25 @@
"name": "ionic-native", "name": "ionic-native",
"version": "1.0.0", "version": "1.0.0",
"description": "Native plugins to replace ngCordova", "description": "Native plugins to replace ngCordova",
"main": "gulpfile.js", "main": "dist/cordova-wrap.js",
"directories": { "directories": {
"test": "test" "test": "test"
}, },
"dependencies": { "dependencies": {
"babel-polyfill": "^6.2.0",
"babel-preset-es2015": "^6.1.18", "babel-preset-es2015": "^6.1.18",
"babelify": "^7.2.0", "babelify": "^7.2.0",
"browserify": "^12.0.1", "browserify": "^12.0.1",
"es6-promise": "^3.0.2",
"gulp": "^3.9.0", "gulp": "^3.9.0",
"gulp-babel": "^6.1.0",
"gulp-concat": "^2.6.0", "gulp-concat": "^2.6.0",
"gulp-connect": "^2.2.0", "gulp-connect": "^2.2.0",
"gulp-serve": "^1.2.0",
"gulp-sourcemaps": "^1.6.0", "gulp-sourcemaps": "^1.6.0",
"vinyl-buffer": "^1.0.0", "vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0", "vinyl-source-stream": "^1.1.0",
"watchify": "^3.6.1", "watchify": "^3.6.1"
"gulp-babel": "^6.1.0",
"gulp-serve": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"babel-preset-stage-0": "^6.1.18" "babel-preset-stage-0": "^6.1.18"

3
src/cordova.js vendored
View File

@ -1,3 +1,6 @@
import {Promise} from 'es6-promise';
const promisifyCordova = (pluginObj, pluginName, methodName) => { const promisifyCordova = (pluginObj, pluginName, methodName) => {
return (...args) => { return (...args) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -10,25 +10,31 @@ let promised;
function newPluginClass(config) { function newPluginClass(config) {
let obj = { let obj = {
installed: () => { installed: () => {
return config.pluginRef && get(window, config.pluginRef); return !!obj.plugin();
}, },
plugin: config.plugin // Get the plugin by checking the plugin ref path on window
plugin: () => {
return get(window, config.pluginRef)
},
pluginName: config.plugin
}; };
return obj; return obj;
} }
// Go through each registered plugin // Go through each registered plugin
for(let plugin of PluginConfig) { for(let i = 0; i < PluginConfig.length; i++) {
console.log('Plugin', plugin.className, plugin); let plugin = PluginConfig[i];
// Create the wrapped class // Create the wrapped class
let cls = newPluginClass(plugin); let cls = newPluginClass(plugin);
promised = plugin.promise || []; promised = plugin.promise || [];
for(let method of promised) { for(let j = 0; j < promised.length; j++) {
let method = promised[j];
let p = promisifyCordova(cls, plugin.id, method) let p = promisifyCordova(cls, plugin.id, method)
cls[method] = p; cls[method] = p;
} }

View File

@ -12,10 +12,32 @@ export var PluginConfig = [
promise: ['takePicture'], promise: ['takePicture'],
pluginRef: 'navigator.camera' pluginRef: 'navigator.camera'
}, },
{
id: 'statusbar',
className: 'StatusBar',
plugin: 'cordova-plugin-statusbar',
pluginRef: 'StatusBar',
promise: ['show', 'hide', 'styleDefault', 'styleLightContent', 'styleBlackTranslucent', 'styleBlackOpaque']
},
{ {
id: 'applinks', id: 'applinks',
className: 'AppLinks', className: 'AppLinks',
plugin: 'com.lampa.startapp', plugin: 'com.lampa.startapp',
pluginRef: 'navigator.startApp' pluginRef: 'navigator.startApp',
promise: ['start', 'check']
},
{
id: 'barcode',
className: 'Barcode',
plugin: 'phonegap-plugin-barcodescanner',
pluginRef: 'cordova.plugins.barcodeScanner',
promise: ['scan', 'encode']
},
{
id: 'camera-roll',
className: 'CameraRoll',
plugin: 'cordova-plugin-camera-roll',
pluginRef: 'CameraRoll',
promise: ['saveToCameraRoll', 'getPhotos']
}, },
] ]

View File

@ -1,9 +1,15 @@
document.addEventListener('deviceready', function() {
console.log(window.Native); console.log(window.Native);
alert('in here!');
if(Native.Camera.installed()) { if(Native.Camera.installed()) {
alert('here!');
console.log('Camera installed'); console.log('Camera installed');
} else { } else {
alert('No cam');
console.log('Camera not installed'); console.log('Camera not installed');
} }
Native.Camera.takePicture().then(function(resp) { Native.Camera.takePicture().then(function(resp) {
@ -11,3 +17,19 @@ Native.Camera.takePicture().then(function(resp) {
}, function(err) { }, function(err) {
console.log('ERROR'); console.log('ERROR');
}); });
if(Native.AppLinks.installed()) {
console.log('AppLinks installed');
} else {
console.log('AppLinks not installed');
}
if(Native.StatusBar.installed()) {
console.log('StatusBar installed');
Native.StatusBar.styleBlackTranslucent();
} else {
alert('No statusbar');
console.log('StatusBar not installed');
}
})

View File

@ -1,7 +1,7 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<script src="../../dist/ionic-native.js"></script> <script src="../../dist/cordova-wrap.js"></script>
<script src="app.js"></script> <script src="app.js"></script>
</head> </head>
<body> <body>