Lots o refactor

This commit is contained in:
Max Lynch 2015-11-28 18:26:55 -06:00
parent 044a824f29
commit 5b754390f5
35 changed files with 653 additions and 356 deletions

View File

@ -33,7 +33,6 @@ export class Plugin {
}
doMethod(method) {
let pluginMethodArgEntry = demoArgs[this.plugin];
let args = [];
@ -41,9 +40,19 @@ export class Plugin {
args = pluginMethodArgEntry[method] || [];
}
Toast.showWithOptions({
message: 'Doing ' + this.plugin.name + '.' + method + '()',
duration: "short",
position: "bottom",
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
});
console.log('Doing method', method, 'on Plugin', this.plugin, 'args:', args);
// TODO: Pass args
this.plugin[method].apply(this.plugin, args);
this.plugin[method].apply(this.plugin, args).then(() => {
console.log('Success', arguments);
}, (err) => {
console.error('Error', err);
});
}
}

44
dist/index.js vendored
View File

@ -4,47 +4,3 @@ function __export(m) {
__export(require('./plugins/camera'));
__export(require('./plugins/statusbar'));
__export(require('./plugins/toast'));
/*
let wrappedPlugins = {}
let promised;
function newPluginClass(config) {
let obj = {
installed: () => {
return !!obj.plugin();
},
// Get the plugin by checking the plugin ref path on window
plugin: () => {
return get(window, config.pluginRef)
},
pluginName: config.plugin
};
return obj;
}
// Go through each registered plugin
for(let i = 0; i < PluginConfig.length; i++) {
let plugin = PluginConfig[i];
// Create the wrapped class
let cls = newPluginClass(plugin);
promised = plugin.promise || [];
for(let j = 0; j < promised.length; j++) {
let method = promised[j];
let p = promisifyCordova(cls, plugin.id, method)
cls[method] = p;
}
// Save the plugin object
wrappedPlugins[plugin.className] = cls;
}
export default wrappedPlugins;
*/
//window['Native'] = wrappedPlugins;

1
dist/plugin.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare function Plugin(config: any): (cls: any) => any;

18
dist/plugin.js vendored Normal file
View File

@ -0,0 +1,18 @@
var PluginDecotor = (function () {
function PluginDecotor(cls, config) {
this.cls = cls;
this.config = config;
}
return PluginDecotor;
})();
function Plugin(config) {
return function (cls) {
for (var _i = 0; _i < config.length; _i++) {
var k = config[_i];
cls[k] = config[k];
}
console.log('Decorated', cls, config);
return cls;
};
}
exports.Plugin = Plugin;

View File

@ -1,6 +1,4 @@
export declare var Camera: {
name: string;
plugin: string;
getPicture: (...args: any[]) => any;
cleanup: (...args: any[]) => any;
};
export declare class Camera {
static getPicture: any;
static cleanup: any;
}

View File

@ -1,10 +1,35 @@
var util_1 = require('../util');
var PLUGIN_REF = 'navigator.camera';
exports.Camera = {
// Metadata
name: 'Camera',
plugin: 'cordova-plugin-camera',
// Methods
getPicture: util_1.promisify(PLUGIN_REF, 'getPicture', 0, 1),
cleanup: util_1.promisify(PLUGIN_REF, 'cleanup', 0, 1)
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
switch (arguments.length) {
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
}
};
var plugin_1 = require('./plugin');
var PLUGIN_REF = 'navigator.camera';
var Camera = (function () {
function Camera() {
}
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
})
], Camera, "getPicture");
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
})
], Camera, "cleanup");
Camera = __decorate([
plugin_1.Plugin({
name: 'Camera',
plugin: 'cordova-plugin-camera',
pluginRef: 'navigator.camera'
})
], Camera);
return Camera;
})();
exports.Camera = Camera;

3
dist/plugins/plugin.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
export declare const wrap: (pluginObj: any, methodName: any, opts?: any) => (...args: any[]) => any;
export declare function Plugin(config: any): (cls: any) => any;
export declare function Cordova(opts?: any): (obj: any, methodName: any) => void;

55
dist/plugins/plugin.js vendored Normal file
View File

@ -0,0 +1,55 @@
var util_1 = require('../util');
exports.wrap = function (pluginObj, methodName, opts) {
if (opts === void 0) { opts = {}; }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
console.log('Trying', pluginObj.name, methodName, args);
return new Promise(function (resolve, reject) {
if (typeof opts.successIndex !== 'undefined') {
args[opts.successIndex] = resolve;
}
if (typeof opts.errorIndex !== 'undefined') {
args[opts.errorIndex] = reject;
}
var pluginInstance = util_1.get(window, pluginObj.pluginRef);
if (!pluginInstance) {
console.warn('Native: tried calling ' + pluginObj.name + '.' + methodName + ', but the ' + pluginObj.name + ' plugin is not installed. Install the ' + pluginObj.plugin + ' plugin');
reject({
error: 'plugin_not_installed'
});
return;
}
util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
});
};
};
var PluginDecotor = (function () {
function PluginDecotor(cls, config) {
this.cls = cls;
this.config = config;
}
return PluginDecotor;
})();
function Plugin(config) {
return function (cls) {
// Add these fields to the class
for (var k in config) {
cls[k] = config[k];
}
return cls;
};
}
exports.Plugin = Plugin;
function Cordova(opts) {
if (opts === void 0) { opts = {}; }
return function (obj, methodName) {
if (opts.promise) {
console.log('TODO: Promise');
}
obj[methodName] = exports.wrap(obj, methodName, opts);
};
}
exports.Cordova = Cordova;

View File

@ -1,13 +1,11 @@
export declare var StatusBar: {
name: string;
plugin: string;
overlaysWebView: (...args: any[]) => any;
styleDefault: (...args: any[]) => any;
styleLightContent: (...args: any[]) => any;
styleBlackTranslucent: (...args: any[]) => any;
styleBlackOpaque: (...args: any[]) => any;
backgroundColorByName: (...args: any[]) => any;
backgroundColorByHexString: (...args: any[]) => any;
hide: (...args: any[]) => any;
show: (...args: any[]) => any;
};
export declare class StatusBar {
static overlaysWebView: any;
static styleDefault: any;
static styleLightContent: any;
static styleBlackTranslucent: any;
static styleBlackOpaque: any;
static backgroundColorByName: any;
static backgroundColorByHexString: any;
static hide: any;
static show: any;
}

View File

@ -1,17 +1,49 @@
var util_1 = require('../util');
var PLUGIN_REF = 'StatusBar';
exports.StatusBar = {
// Metadata
name: 'StatusBar',
plugin: 'cordova-plugin-statusbar',
// Methods
overlaysWebView: util_1.wrap(PLUGIN_REF, 'overlaysWebView'),
styleDefault: util_1.wrap(PLUGIN_REF, 'styleDefault'),
styleLightContent: util_1.wrap(PLUGIN_REF, 'styleLightContent'),
styleBlackTranslucent: util_1.wrap(PLUGIN_REF, 'styleBlackTranslucent'),
styleBlackOpaque: util_1.wrap(PLUGIN_REF, 'styleBlackOpaque'),
backgroundColorByName: util_1.wrap(PLUGIN_REF, 'backgroundColorByName'),
backgroundColorByHexString: util_1.wrap(PLUGIN_REF, 'backgroundColorByHexString'),
hide: util_1.wrap(PLUGIN_REF, 'hide'),
show: util_1.wrap(PLUGIN_REF, 'show')
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
switch (arguments.length) {
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
}
};
var plugin_1 = require('./plugin');
var StatusBar = (function () {
function StatusBar() {
}
__decorate([
plugin_1.Cordova()
], StatusBar, "overlaysWebView");
__decorate([
plugin_1.Cordova()
], StatusBar, "styleDefault");
__decorate([
plugin_1.Cordova()
], StatusBar, "styleLightContent");
__decorate([
plugin_1.Cordova()
], StatusBar, "styleBlackTranslucent");
__decorate([
plugin_1.Cordova()
], StatusBar, "styleBlackOpaque");
__decorate([
plugin_1.Cordova()
], StatusBar, "backgroundColorByName");
__decorate([
plugin_1.Cordova()
], StatusBar, "backgroundColorByHexString");
__decorate([
plugin_1.Cordova()
], StatusBar, "hide");
__decorate([
plugin_1.Cordova()
], StatusBar, "show");
StatusBar = __decorate([
plugin_1.Plugin({
name: 'StatusBar',
plugin: 'cordova-plugin-statusbar',
pluginRef: 'StatusBar'
})
], StatusBar);
return StatusBar;
})();
exports.StatusBar = StatusBar;

View File

@ -1,6 +1,4 @@
export declare var Toast: {
name: string;
plugin: string;
showWithOptions: (...args: any[]) => any;
hide: (...args: any[]) => any;
};
export declare class Toast {
static hide: any;
static showWithOptions: any;
}

42
dist/plugins/toast.js vendored
View File

@ -1,10 +1,34 @@
var util_1 = require('../util');
var PLUGIN_REF = 'plugins.toast';
exports.Toast = {
// Metadata
name: 'Toast',
plugin: 'cordova-plugin-x-toast',
// Methods
showWithOptions: util_1.wrap(PLUGIN_REF, 'showWithOptions', 1, 2),
hide: util_1.promisify(PLUGIN_REF, 'hide', 0, 1),
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
switch (arguments.length) {
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
}
};
var plugin_1 = require('./plugin');
var Toast = (function () {
function Toast() {
}
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
})
], Toast, "hide");
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
})
], Toast, "showWithOptions");
Toast = __decorate([
plugin_1.Plugin({
name: 'Toast',
plugin: 'cordova-plugin-x-toast',
pluginRef: 'plugins.toast'
})
], Toast);
return Toast;
})();
exports.Toast = Toast;

8
dist/src/plugin-config.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
export interface CordovaPlugin {
id: string;
className: string;
plugin: string;
pluginRef: string;
promise?: any;
}
export declare var PluginConfig: CordovaPlugin[];

49
dist/src/plugin-config.js vendored Normal file
View File

@ -0,0 +1,49 @@
exports.PluginConfig = [
{
id: 'device',
className: 'Device',
plugin: 'cordova-plugin-device',
pluginRef: 'device'
},
{
id: 'camera',
className: 'Camera',
plugin: 'cordova-plugin-camera',
promise: ['takePicture'],
pluginRef: 'navigator.camera'
},
{
id: 'statusbar',
className: 'StatusBar',
plugin: 'cordova-plugin-statusbar',
pluginRef: 'StatusBar',
promise: ['show', 'hide', 'styleDefault', 'styleLightContent', 'styleBlackTranslucent', 'styleBlackOpaque']
},
{
id: 'applinks',
className: 'AppLinks',
plugin: 'com.lampa.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']
},
{
id: 'contacts',
className: 'Contacts',
plugin: 'cordova-plugin-contacts',
pluginRef: 'navigator.contacts',
},
];

View File

@ -1,6 +1,4 @@
export declare var Camera: {
name: string;
plugin: string;
getPicture: (...args: any[]) => any;
cleanup: (...args: any[]) => any;
};
export declare class Camera {
static getPicture: any;
static cleanup: any;
}

View File

@ -1,8 +1,39 @@
var util_1 = require('../util');
var PLUGIN_REF = 'navigator.camera';
exports.Camera = {
name: 'Camera',
plugin: 'cordova-plugin-camera',
getPicture: util_1.promisify(PLUGIN_REF, 'getPicture', 0, 1),
cleanup: util_1.promisify(PLUGIN_REF, 'cleanup', 0, 1)
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var plugin_1 = require('./plugin');
var PLUGIN_REF = 'navigator.camera';
var Camera = (function () {
function Camera() {
}
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
}),
__metadata('design:type', Object)
], Camera, "getPicture", void 0);
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
}),
__metadata('design:type', Object)
], Camera, "cleanup", void 0);
Camera = __decorate([
plugin_1.Plugin({
name: 'Camera',
plugin: 'cordova-plugin-camera',
pluginRef: 'navigator.camera'
}),
__metadata('design:paramtypes', [])
], Camera);
return Camera;
})();
exports.Camera = Camera;

3
dist/src/plugins/plugin.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
export declare const wrap: (pluginObj: any, methodName: any, opts?: any) => (...args: any[]) => any;
export declare function Plugin(config: any): (cls: any) => any;
export declare function Cordova(opts?: any): (obj: any, methodName: any) => void;

54
dist/src/plugins/plugin.js vendored Normal file
View File

@ -0,0 +1,54 @@
var util_1 = require('../util');
exports.wrap = function (pluginObj, methodName, opts) {
if (opts === void 0) { opts = {}; }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
console.log('Trying', pluginObj.name, methodName, args);
return new Promise(function (resolve, reject) {
if (typeof opts.successIndex !== 'undefined') {
args[opts.successIndex] = resolve;
}
if (typeof opts.errorIndex !== 'undefined') {
args[opts.errorIndex] = reject;
}
var pluginInstance = util_1.get(window, pluginObj.pluginRef);
if (!pluginInstance) {
console.warn('Native: tried calling ' + pluginObj.name + '.' + methodName + ', but the ' + pluginObj.name + ' plugin is not installed. Install the ' + pluginObj.plugin + ' plugin');
reject({
error: 'plugin_not_installed'
});
return;
}
util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
});
};
};
var PluginDecotor = (function () {
function PluginDecotor(cls, config) {
this.cls = cls;
this.config = config;
}
return PluginDecotor;
})();
function Plugin(config) {
return function (cls) {
for (var k in config) {
cls[k] = config[k];
}
return cls;
};
}
exports.Plugin = Plugin;
function Cordova(opts) {
if (opts === void 0) { opts = {}; }
return function (obj, methodName) {
if (opts.promise) {
console.log('TODO: Promise');
}
obj[methodName] = exports.wrap(obj, methodName, opts);
};
}
exports.Cordova = Cordova;

11
dist/src/plugins/statusbar.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
export declare class StatusBar {
static overlaysWebView: any;
static styleDefault: any;
static styleLightContent: any;
static styleBlackTranslucent: any;
static styleBlackOpaque: any;
static backgroundColorByName: any;
static backgroundColorByHexString: any;
static hide: any;
static show: any;
}

60
dist/src/plugins/statusbar.js vendored Normal file
View File

@ -0,0 +1,60 @@
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var plugin_1 = require('./plugin');
var StatusBar = (function () {
function StatusBar() {
}
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "overlaysWebView", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "styleDefault", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "styleLightContent", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "styleBlackTranslucent", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "styleBlackOpaque", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "backgroundColorByName", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "backgroundColorByHexString", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "hide", void 0);
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Object)
], StatusBar, "show", void 0);
StatusBar = __decorate([
plugin_1.Plugin({
name: 'StatusBar',
plugin: 'cordova-plugin-statusbar',
pluginRef: 'StatusBar'
}),
__metadata('design:paramtypes', [])
], StatusBar);
return StatusBar;
})();
exports.StatusBar = StatusBar;

View File

@ -1,6 +1,4 @@
export declare var Toast: {
name: string;
plugin: string;
showWithOptions: (...args: any[]) => any;
hide: (...args: any[]) => any;
};
export declare class Toast {
static hide: any;
static showWithOptions: any;
}

View File

@ -1,8 +1,38 @@
var util_1 = require('../util');
var PLUGIN_REF = 'plugins.toast';
exports.Toast = {
name: 'Toast',
plugin: 'cordova-plugin-x-toast',
showWithOptions: util_1.wrap(PLUGIN_REF, 'showWithOptions', 1, 2),
hide: util_1.promisify(PLUGIN_REF, 'hide', 0, 1),
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var plugin_1 = require('./plugin');
var Toast = (function () {
function Toast() {
}
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
}),
__metadata('design:type', Object)
], Toast, "hide", void 0);
__decorate([
plugin_1.Cordova({
successIndex: 0,
errIndex: 1
}),
__metadata('design:type', Object)
], Toast, "showWithOptions", void 0);
Toast = __decorate([
plugin_1.Plugin({
name: 'Toast',
plugin: 'cordova-plugin-x-toast',
pluginRef: 'plugins.toast'
}),
__metadata('design:paramtypes', [])
], Toast);
return Toast;
})();
exports.Toast = Toast;

2
dist/src/util.d.ts vendored
View File

@ -1,3 +1 @@
export declare function get(obj: any, path: any): any;
export declare const promisify: (pluginRef: any, methodName: any, successIndex: any, errorIndex: any) => (...args: any[]) => any;
export declare const wrap: (pluginRef: any, methodName: any, successIndex?: any, errorIndex?: any) => (...args: any[]) => any;

33
dist/src/util.js vendored
View File

@ -1,4 +1,3 @@
var _this = this;
function get(obj, path) {
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
obj = obj[path[i]];
@ -7,35 +6,3 @@ function get(obj, path) {
}
exports.get = get;
;
exports.promisify = function (pluginRef, methodName, successIndex, errorIndex) {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return new Promise(function (resolve, reject) {
args[successIndex] = resolve;
args[errorIndex] = reject;
get(window, pluginRef)[methodName].apply(_this, args);
});
};
};
exports.wrap = function (pluginRef, methodName, successIndex, errorIndex) {
if (successIndex === void 0) { successIndex = null; }
if (errorIndex === void 0) { errorIndex = null; }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return new Promise(function (resolve, reject) {
if (successIndex) {
args[successIndex] = resolve;
}
if (errorIndex) {
args[errorIndex] = reject;
}
get(window, pluginRef)[methodName].apply(_this, args);
});
};
};

2
dist/util.d.ts vendored
View File

@ -1,3 +1 @@
export declare function get(obj: any, path: any): any;
export declare const promisify: (pluginRef: any, methodName: any, successIndex: any, errorIndex: any) => (...args: any[]) => any;
export declare const wrap: (pluginRef: any, methodName: any, successIndex?: any, errorIndex?: any) => (...args: any[]) => any;

33
dist/util.js vendored
View File

@ -1,4 +1,3 @@
var _this = this;
function get(obj, path) {
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
obj = obj[path[i]];
@ -7,35 +6,3 @@ function get(obj, path) {
}
exports.get = get;
;
exports.promisify = function (pluginRef, methodName, successIndex, errorIndex) {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return new Promise(function (resolve, reject) {
args[successIndex] = resolve;
args[errorIndex] = reject;
get(window, pluginRef)[methodName].apply(_this, args);
});
};
};
exports.wrap = function (pluginRef, methodName, successIndex, errorIndex) {
if (successIndex === void 0) { successIndex = null; }
if (errorIndex === void 0) { errorIndex = null; }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return new Promise(function (resolve, reject) {
if (successIndex) {
args[successIndex] = resolve;
}
if (errorIndex) {
args[errorIndex] = reject;
}
get(window, pluginRef)[methodName].apply(_this, args);
});
};
};

View File

@ -1,49 +1,3 @@
export * from './plugins/camera';
export * from './plugins/statusbar';
export * from './plugins/toast';
/*
let wrappedPlugins = {}
let promised;
function newPluginClass(config) {
let obj = {
installed: () => {
return !!obj.plugin();
},
// Get the plugin by checking the plugin ref path on window
plugin: () => {
return get(window, config.pluginRef)
},
pluginName: config.plugin
};
return obj;
}
// Go through each registered plugin
for(let i = 0; i < PluginConfig.length; i++) {
let plugin = PluginConfig[i];
// Create the wrapped class
let cls = newPluginClass(plugin);
promised = plugin.promise || [];
for(let j = 0; j < promised.length; j++) {
let method = promised[j];
let p = promisifyCordova(cls, plugin.id, method)
cls[method] = p;
}
// Save the plugin object
wrappedPlugins[plugin.className] = cls;
}
export default wrappedPlugins;
*/
//window['Native'] = wrappedPlugins;

View File

@ -1,13 +1,22 @@
import {promisify} from '../util';
import {Plugin, Cordova} from './plugin';
let PLUGIN_REF = 'navigator.camera';
export var Camera = {
// Metadata
@Plugin({
name: 'Camera',
plugin: 'cordova-plugin-camera',
pluginRef: 'navigator.camera'
})
export class Camera {
@Cordova({
successIndex: 0,
errIndex: 1
})
static getPicture;
// Methods
getPicture: promisify(PLUGIN_REF, 'getPicture', 0, 1),
cleanup: promisify(PLUGIN_REF, 'cleanup', 0, 1)
@Cordova({
successIndex: 0,
errIndex: 1
})
static cleanup;
}

62
src/plugins/plugin.ts Normal file
View File

@ -0,0 +1,62 @@
import {get} from '../util';
declare var window;
declare var Promise;
export const wrap = function(pluginObj, methodName, opts: any = {}) {
return (...args) => {
console.log('Trying', pluginObj.name, methodName, args);
return new Promise((resolve, reject) => {
if(typeof opts.successIndex !== 'undefined') {
args[opts.successIndex] = resolve;
}
if(typeof opts.errorIndex !== 'undefined') {
args[opts.errorIndex] = reject;
}
let pluginInstance = get(window, pluginObj.pluginRef);
if(!pluginInstance) {
console.warn('Native: tried calling ' + pluginObj.name + '.' + methodName + ', but the ' + pluginObj.name + ' plugin is not installed. Install the ' + pluginObj.plugin + ' plugin');
reject({
error: 'plugin_not_installed'
});
return;
}
get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
})
}
}
class PluginDecotor {
cls: any;
config: any;
constructor(cls, config) {
this.cls = cls;
this.config = config;
}
}
export function Plugin(config) {
return function(cls) {
// Add these fields to the class
for(let k in config) {
cls[k] = config[k];
}
return cls;
}
}
export function Cordova(opts:any = {}) {
return function(obj, methodName) {
if(opts.promise) {
console.log('TODO: Promise');
}
obj[methodName] = wrap(obj, methodName, opts);
}
}

View File

@ -1,20 +1,27 @@
import {wrap} from '../util';
import {Plugin, Cordova} from './plugin';
let PLUGIN_REF = 'StatusBar';
export var StatusBar = {
// Metadata
@Plugin({
name: 'StatusBar',
plugin: 'cordova-plugin-statusbar',
// Methods
overlaysWebView: wrap(PLUGIN_REF, 'overlaysWebView'),
styleDefault: wrap(PLUGIN_REF, 'styleDefault'),
styleLightContent: wrap(PLUGIN_REF, 'styleLightContent'),
styleBlackTranslucent: wrap(PLUGIN_REF, 'styleBlackTranslucent'),
styleBlackOpaque: wrap(PLUGIN_REF, 'styleBlackOpaque'),
backgroundColorByName: wrap(PLUGIN_REF, 'backgroundColorByName'),
backgroundColorByHexString: wrap(PLUGIN_REF, 'backgroundColorByHexString'),
hide: wrap(PLUGIN_REF, 'hide'),
show: wrap(PLUGIN_REF, 'show')
pluginRef: 'StatusBar'
})
export class StatusBar {
@Cordova()
static overlaysWebView;
@Cordova()
static styleDefault;
@Cordova()
static styleLightContent;
@Cordova()
static styleBlackTranslucent;
@Cordova()
static styleBlackOpaque;
@Cordova()
static backgroundColorByName;
@Cordova()
static backgroundColorByHexString;
@Cordova()
static hide;
@Cordova()
static show;
}

View File

@ -1,13 +1,20 @@
import {wrap, promisify} from '../util';
import {Plugin, Cordova} from './plugin';
let PLUGIN_REF = 'plugins.toast'
export var Toast = {
// Metadata
@Plugin({
name: 'Toast',
plugin: 'cordova-plugin-x-toast',
pluginRef: 'plugins.toast'
})
export class Toast {
@Cordova({
successIndex: 0,
errIndex: 1
})
static hide;
// Methods
showWithOptions: wrap(PLUGIN_REF, 'showWithOptions', 1, 2),
hide: promisify(PLUGIN_REF, 'hide', 0, 1),
@Cordova({
successIndex: 0,
errIndex: 1
})
static showWithOptions;
}

View File

@ -1,38 +1,6 @@
declare var window;
declare var Promise;
export function get(obj, path) {
for (var i=0, path = path.split('.'), len = path.length; i < len; i++) {
obj = obj[path[i]];
}
return obj;
};
export const promisify = (pluginRef, methodName, successIndex, errorIndex) => {
return (...args) => {
return new Promise((resolve, reject) => {
args[successIndex] = resolve;
args[errorIndex] = reject;
get(window, pluginRef)[methodName].apply(this, args);
})
}
}
export const wrap = (pluginRef, methodName, successIndex=null, errorIndex=null) => {
return (...args) => {
return new Promise((resolve, reject) => {
if(successIndex) {
args[successIndex] = resolve;
}
if(errorIndex) {
args[errorIndex] = reject;
}
get(window, pluginRef)[methodName].apply(this, args);
})
}
}

View File

@ -19,6 +19,7 @@
"src/index.ts",
"src/plugin-config.ts",
"src/plugins/camera.ts",
"src/plugins/plugin.ts",
"src/plugins/statusbar.ts",
"src/plugins/toast.ts",
"src/util.ts"