Skip to content

Commit

Permalink
Lots o refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Nov 29, 2015
1 parent 044a824 commit 5b75439
Show file tree
Hide file tree
Showing 35 changed files with 653 additions and 356 deletions.
13 changes: 11 additions & 2 deletions demo/www/app/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,26 @@ export class Plugin {
}

doMethod(method) {

let pluginMethodArgEntry = demoArgs[this.plugin];

let args = [];
if(pluginMethodArgEntry) {
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 changes: 0 additions & 44 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions dist/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function Plugin(config: any): (cls: any) => any;
18 changes: 18 additions & 0 deletions dist/plugin.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 4 additions & 6 deletions dist/plugins/camera.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
43 changes: 34 additions & 9 deletions dist/plugins/camera.js
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions dist/plugins/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 55 additions & 0 deletions dist/plugins/plugin.js
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 11 additions & 13 deletions dist/plugins/statusbar.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
64 changes: 48 additions & 16 deletions dist/plugins/statusbar.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 4 additions & 6 deletions dist/plugins/toast.d.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 9 deletions dist/plugins/toast.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 5 additions & 5 deletions dist/src/cordova.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export declare class Cordova {
static hasPlugin(pluginRef: string): boolean;
static plugin(pluginRef: string): any;
static promisify(pluginRef: any, pluginName: any, methodName: any, successIndex: any, errorIndex: any): (...args: any[]) => any;
}
export declare class Cordova {
static hasPlugin(pluginRef: string): boolean;
static plugin(pluginRef: string): any;
static promisify(pluginRef: any, pluginName: any, methodName: any, successIndex: any, errorIndex: any): (...args: any[]) => any;
}
Loading

0 comments on commit 5b75439

Please sign in to comment.