mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-03-04 00:13:06 +08:00
Lots o refactor
This commit is contained in:
parent
044a824f29
commit
5b754390f5
@ -33,7 +33,6 @@ export class Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
doMethod(method) {
|
doMethod(method) {
|
||||||
|
|
||||||
let pluginMethodArgEntry = demoArgs[this.plugin];
|
let pluginMethodArgEntry = demoArgs[this.plugin];
|
||||||
|
|
||||||
let args = [];
|
let args = [];
|
||||||
@ -41,9 +40,19 @@ export class Plugin {
|
|||||||
args = pluginMethodArgEntry[method] || [];
|
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);
|
console.log('Doing method', method, 'on Plugin', this.plugin, 'args:', args);
|
||||||
// TODO: Pass 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
44
dist/index.js
vendored
@ -4,47 +4,3 @@ function __export(m) {
|
|||||||
__export(require('./plugins/camera'));
|
__export(require('./plugins/camera'));
|
||||||
__export(require('./plugins/statusbar'));
|
__export(require('./plugins/statusbar'));
|
||||||
__export(require('./plugins/toast'));
|
__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
1
dist/plugin.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
export declare function Plugin(config: any): (cls: any) => any;
|
18
dist/plugin.js
vendored
Normal file
18
dist/plugin.js
vendored
Normal 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;
|
10
dist/plugins/camera.d.ts
vendored
10
dist/plugins/camera.d.ts
vendored
@ -1,6 +1,4 @@
|
|||||||
export declare var Camera: {
|
export declare class Camera {
|
||||||
name: string;
|
static getPicture: any;
|
||||||
plugin: string;
|
static cleanup: any;
|
||||||
getPicture: (...args: any[]) => any;
|
}
|
||||||
cleanup: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
43
dist/plugins/camera.js
vendored
43
dist/plugins/camera.js
vendored
@ -1,10 +1,35 @@
|
|||||||
var util_1 = require('../util');
|
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
|
||||||
var PLUGIN_REF = 'navigator.camera';
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
|
||||||
exports.Camera = {
|
switch (arguments.length) {
|
||||||
// Metadata
|
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
|
||||||
name: 'Camera',
|
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
|
||||||
plugin: 'cordova-plugin-camera',
|
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
|
||||||
// Methods
|
}
|
||||||
getPicture: util_1.promisify(PLUGIN_REF, 'getPicture', 0, 1),
|
|
||||||
cleanup: util_1.promisify(PLUGIN_REF, 'cleanup', 0, 1)
|
|
||||||
};
|
};
|
||||||
|
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
3
dist/plugins/plugin.d.ts
vendored
Normal 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
55
dist/plugins/plugin.js
vendored
Normal 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;
|
24
dist/plugins/statusbar.d.ts
vendored
24
dist/plugins/statusbar.d.ts
vendored
@ -1,13 +1,11 @@
|
|||||||
export declare var StatusBar: {
|
export declare class StatusBar {
|
||||||
name: string;
|
static overlaysWebView: any;
|
||||||
plugin: string;
|
static styleDefault: any;
|
||||||
overlaysWebView: (...args: any[]) => any;
|
static styleLightContent: any;
|
||||||
styleDefault: (...args: any[]) => any;
|
static styleBlackTranslucent: any;
|
||||||
styleLightContent: (...args: any[]) => any;
|
static styleBlackOpaque: any;
|
||||||
styleBlackTranslucent: (...args: any[]) => any;
|
static backgroundColorByName: any;
|
||||||
styleBlackOpaque: (...args: any[]) => any;
|
static backgroundColorByHexString: any;
|
||||||
backgroundColorByName: (...args: any[]) => any;
|
static hide: any;
|
||||||
backgroundColorByHexString: (...args: any[]) => any;
|
static show: any;
|
||||||
hide: (...args: any[]) => any;
|
}
|
||||||
show: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
64
dist/plugins/statusbar.js
vendored
64
dist/plugins/statusbar.js
vendored
@ -1,17 +1,49 @@
|
|||||||
var util_1 = require('../util');
|
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
|
||||||
var PLUGIN_REF = 'StatusBar';
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
|
||||||
exports.StatusBar = {
|
switch (arguments.length) {
|
||||||
// Metadata
|
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
|
||||||
name: 'StatusBar',
|
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
|
||||||
plugin: 'cordova-plugin-statusbar',
|
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
|
||||||
// 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')
|
|
||||||
};
|
};
|
||||||
|
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
dist/plugins/toast.d.ts
vendored
10
dist/plugins/toast.d.ts
vendored
@ -1,6 +1,4 @@
|
|||||||
export declare var Toast: {
|
export declare class Toast {
|
||||||
name: string;
|
static hide: any;
|
||||||
plugin: string;
|
static showWithOptions: any;
|
||||||
showWithOptions: (...args: any[]) => any;
|
}
|
||||||
hide: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
42
dist/plugins/toast.js
vendored
42
dist/plugins/toast.js
vendored
@ -1,10 +1,34 @@
|
|||||||
var util_1 = require('../util');
|
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
|
||||||
var PLUGIN_REF = 'plugins.toast';
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
|
||||||
exports.Toast = {
|
switch (arguments.length) {
|
||||||
// Metadata
|
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
|
||||||
name: 'Toast',
|
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
|
||||||
plugin: 'cordova-plugin-x-toast',
|
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
|
||||||
// Methods
|
}
|
||||||
showWithOptions: util_1.wrap(PLUGIN_REF, 'showWithOptions', 1, 2),
|
|
||||||
hide: util_1.promisify(PLUGIN_REF, 'hide', 0, 1),
|
|
||||||
};
|
};
|
||||||
|
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
dist/src/cordova.d.ts
vendored
10
dist/src/cordova.d.ts
vendored
@ -1,5 +1,5 @@
|
|||||||
export declare class Cordova {
|
export declare class Cordova {
|
||||||
static hasPlugin(pluginRef: string): boolean;
|
static hasPlugin(pluginRef: string): boolean;
|
||||||
static plugin(pluginRef: string): any;
|
static plugin(pluginRef: string): any;
|
||||||
static promisify(pluginRef: any, pluginName: any, methodName: any, successIndex: any, errorIndex: any): (...args: any[]) => any;
|
static promisify(pluginRef: any, pluginName: any, methodName: any, successIndex: any, errorIndex: any): (...args: any[]) => any;
|
||||||
}
|
}
|
||||||
|
84
dist/src/cordova.js
vendored
84
dist/src/cordova.js
vendored
@ -1,42 +1,42 @@
|
|||||||
var util_1 = require('./util');
|
var util_1 = require('./util');
|
||||||
var Cordova = (function () {
|
var Cordova = (function () {
|
||||||
function Cordova() {
|
function Cordova() {
|
||||||
}
|
}
|
||||||
Cordova.hasPlugin = function (pluginRef) {
|
Cordova.hasPlugin = function (pluginRef) {
|
||||||
return !!this.plugin(pluginRef);
|
return !!this.plugin(pluginRef);
|
||||||
};
|
};
|
||||||
Cordova.plugin = function (pluginRef) {
|
Cordova.plugin = function (pluginRef) {
|
||||||
return util_1.get(window, pluginRef);
|
return util_1.get(window, pluginRef);
|
||||||
};
|
};
|
||||||
Cordova.promisify = function (pluginRef, pluginName, methodName, successIndex, errorIndex) {
|
Cordova.promisify = function (pluginRef, pluginName, methodName, successIndex, errorIndex) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
return function () {
|
return function () {
|
||||||
var args = [];
|
var args = [];
|
||||||
for (var _i = 0; _i < arguments.length; _i++) {
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
args[_i - 0] = arguments[_i];
|
args[_i - 0] = arguments[_i];
|
||||||
}
|
}
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
if (!window.cordova) {
|
if (!window.cordova) {
|
||||||
console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but Cordova is not defined. Please make sure you have cordova.js included in your index.html file and you are running in a proper cordova environment');
|
console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but Cordova is not defined. Please make sure you have cordova.js included in your index.html file and you are running in a proper cordova environment');
|
||||||
reject({
|
reject({
|
||||||
error: 'cordova_not_available'
|
error: 'cordova_not_available'
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!_this.hasPlugin(pluginRef)) {
|
if (!_this.hasPlugin(pluginRef)) {
|
||||||
console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but the ' + pluginName + ' plugin is not installed.');
|
console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but the ' + pluginName + ' plugin is not installed.');
|
||||||
reject({
|
reject({
|
||||||
error: 'plugin_not_installed'
|
error: 'plugin_not_installed'
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('Cordova: exec(' + pluginName + ', ' + methodName + ')');
|
console.log('Cordova: exec(' + pluginName + ', ' + methodName + ')');
|
||||||
args[successIndex] = resolve;
|
args[successIndex] = resolve;
|
||||||
args[errorIndex] = reject;
|
args[errorIndex] = reject;
|
||||||
util_1.get(window, pluginRef)[methodName].apply(_this, args);
|
util_1.get(window, pluginRef)[methodName].apply(_this, args);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
return Cordova;
|
return Cordova;
|
||||||
})();
|
})();
|
||||||
exports.Cordova = Cordova;
|
exports.Cordova = Cordova;
|
||||||
|
8
dist/src/plugin-config.d.ts
vendored
Normal file
8
dist/src/plugin-config.d.ts
vendored
Normal 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
49
dist/src/plugin-config.js
vendored
Normal 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',
|
||||||
|
},
|
||||||
|
];
|
10
dist/src/plugins/camera.d.ts
vendored
10
dist/src/plugins/camera.d.ts
vendored
@ -1,6 +1,4 @@
|
|||||||
export declare var Camera: {
|
export declare class Camera {
|
||||||
name: string;
|
static getPicture: any;
|
||||||
plugin: string;
|
static cleanup: any;
|
||||||
getPicture: (...args: any[]) => any;
|
}
|
||||||
cleanup: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
45
dist/src/plugins/camera.js
vendored
45
dist/src/plugins/camera.js
vendored
@ -1,8 +1,39 @@
|
|||||||
var util_1 = require('../util');
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||||
var PLUGIN_REF = 'navigator.camera';
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||||
exports.Camera = {
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||||
name: 'Camera',
|
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;
|
||||||
plugin: 'cordova-plugin-camera',
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||||
getPicture: util_1.promisify(PLUGIN_REF, 'getPicture', 0, 1),
|
|
||||||
cleanup: util_1.promisify(PLUGIN_REF, 'cleanup', 0, 1)
|
|
||||||
};
|
};
|
||||||
|
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
3
dist/src/plugins/plugin.d.ts
vendored
Normal 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
54
dist/src/plugins/plugin.js
vendored
Normal 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
11
dist/src/plugins/statusbar.d.ts
vendored
Normal 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
60
dist/src/plugins/statusbar.js
vendored
Normal 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;
|
10
dist/src/plugins/toast.d.ts
vendored
10
dist/src/plugins/toast.d.ts
vendored
@ -1,6 +1,4 @@
|
|||||||
export declare var Toast: {
|
export declare class Toast {
|
||||||
name: string;
|
static hide: any;
|
||||||
plugin: string;
|
static showWithOptions: any;
|
||||||
showWithOptions: (...args: any[]) => any;
|
}
|
||||||
hide: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
44
dist/src/plugins/toast.js
vendored
44
dist/src/plugins/toast.js
vendored
@ -1,8 +1,38 @@
|
|||||||
var util_1 = require('../util');
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||||
var PLUGIN_REF = 'plugins.toast';
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||||
exports.Toast = {
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||||
name: 'Toast',
|
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;
|
||||||
plugin: 'cordova-plugin-x-toast',
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||||
showWithOptions: util_1.wrap(PLUGIN_REF, 'showWithOptions', 1, 2),
|
|
||||||
hide: util_1.promisify(PLUGIN_REF, 'hide', 0, 1),
|
|
||||||
};
|
};
|
||||||
|
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
2
dist/src/util.d.ts
vendored
@ -1,3 +1 @@
|
|||||||
export declare function get(obj: any, path: any): any;
|
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
33
dist/src/util.js
vendored
@ -1,4 +1,3 @@
|
|||||||
var _this = this;
|
|
||||||
function get(obj, path) {
|
function get(obj, path) {
|
||||||
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
||||||
obj = obj[path[i]];
|
obj = obj[path[i]];
|
||||||
@ -7,35 +6,3 @@ function get(obj, path) {
|
|||||||
}
|
}
|
||||||
exports.get = get;
|
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
2
dist/util.d.ts
vendored
@ -1,3 +1 @@
|
|||||||
export declare function get(obj: any, path: any): any;
|
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
33
dist/util.js
vendored
@ -1,4 +1,3 @@
|
|||||||
var _this = this;
|
|
||||||
function get(obj, path) {
|
function get(obj, path) {
|
||||||
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
||||||
obj = obj[path[i]];
|
obj = obj[path[i]];
|
||||||
@ -7,35 +6,3 @@ function get(obj, path) {
|
|||||||
}
|
}
|
||||||
exports.get = get;
|
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);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
46
src/index.ts
46
src/index.ts
@ -1,49 +1,3 @@
|
|||||||
export * from './plugins/camera';
|
export * from './plugins/camera';
|
||||||
export * from './plugins/statusbar';
|
export * from './plugins/statusbar';
|
||||||
export * from './plugins/toast';
|
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;
|
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
import {promisify} from '../util';
|
import {Plugin, Cordova} from './plugin';
|
||||||
|
|
||||||
let PLUGIN_REF = 'navigator.camera';
|
let PLUGIN_REF = 'navigator.camera';
|
||||||
|
|
||||||
export var Camera = {
|
@Plugin({
|
||||||
// Metadata
|
|
||||||
name: 'Camera',
|
name: 'Camera',
|
||||||
plugin: 'cordova-plugin-camera',
|
plugin: 'cordova-plugin-camera',
|
||||||
|
pluginRef: 'navigator.camera'
|
||||||
|
})
|
||||||
|
export class Camera {
|
||||||
|
@Cordova({
|
||||||
|
successIndex: 0,
|
||||||
|
errIndex: 1
|
||||||
|
})
|
||||||
|
static getPicture;
|
||||||
|
|
||||||
// Methods
|
@Cordova({
|
||||||
getPicture: promisify(PLUGIN_REF, 'getPicture', 0, 1),
|
successIndex: 0,
|
||||||
cleanup: promisify(PLUGIN_REF, 'cleanup', 0, 1)
|
errIndex: 1
|
||||||
|
})
|
||||||
|
static cleanup;
|
||||||
}
|
}
|
||||||
|
62
src/plugins/plugin.ts
Normal file
62
src/plugins/plugin.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,27 @@
|
|||||||
import {wrap} from '../util';
|
import {Plugin, Cordova} from './plugin';
|
||||||
|
|
||||||
let PLUGIN_REF = 'StatusBar';
|
@Plugin({
|
||||||
|
|
||||||
export var StatusBar = {
|
|
||||||
// Metadata
|
|
||||||
name: 'StatusBar',
|
name: 'StatusBar',
|
||||||
plugin: 'cordova-plugin-statusbar',
|
plugin: 'cordova-plugin-statusbar',
|
||||||
|
pluginRef: 'StatusBar'
|
||||||
// Methods
|
})
|
||||||
overlaysWebView: wrap(PLUGIN_REF, 'overlaysWebView'),
|
export class StatusBar {
|
||||||
styleDefault: wrap(PLUGIN_REF, 'styleDefault'),
|
@Cordova()
|
||||||
styleLightContent: wrap(PLUGIN_REF, 'styleLightContent'),
|
static overlaysWebView;
|
||||||
styleBlackTranslucent: wrap(PLUGIN_REF, 'styleBlackTranslucent'),
|
@Cordova()
|
||||||
styleBlackOpaque: wrap(PLUGIN_REF, 'styleBlackOpaque'),
|
static styleDefault;
|
||||||
backgroundColorByName: wrap(PLUGIN_REF, 'backgroundColorByName'),
|
@Cordova()
|
||||||
backgroundColorByHexString: wrap(PLUGIN_REF, 'backgroundColorByHexString'),
|
static styleLightContent;
|
||||||
hide: wrap(PLUGIN_REF, 'hide'),
|
@Cordova()
|
||||||
show: wrap(PLUGIN_REF, 'show')
|
static styleBlackTranslucent;
|
||||||
|
@Cordova()
|
||||||
|
static styleBlackOpaque;
|
||||||
|
@Cordova()
|
||||||
|
static backgroundColorByName;
|
||||||
|
@Cordova()
|
||||||
|
static backgroundColorByHexString;
|
||||||
|
@Cordova()
|
||||||
|
static hide;
|
||||||
|
@Cordova()
|
||||||
|
static show;
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
import {wrap, promisify} from '../util';
|
import {Plugin, Cordova} from './plugin';
|
||||||
|
|
||||||
let PLUGIN_REF = 'plugins.toast'
|
@Plugin({
|
||||||
|
|
||||||
export var Toast = {
|
|
||||||
// Metadata
|
|
||||||
name: 'Toast',
|
name: 'Toast',
|
||||||
plugin: 'cordova-plugin-x-toast',
|
plugin: 'cordova-plugin-x-toast',
|
||||||
|
pluginRef: 'plugins.toast'
|
||||||
|
})
|
||||||
|
export class Toast {
|
||||||
|
@Cordova({
|
||||||
|
successIndex: 0,
|
||||||
|
errIndex: 1
|
||||||
|
})
|
||||||
|
static hide;
|
||||||
|
|
||||||
// Methods
|
@Cordova({
|
||||||
showWithOptions: wrap(PLUGIN_REF, 'showWithOptions', 1, 2),
|
successIndex: 0,
|
||||||
hide: promisify(PLUGIN_REF, 'hide', 0, 1),
|
errIndex: 1
|
||||||
|
})
|
||||||
|
static showWithOptions;
|
||||||
}
|
}
|
||||||
|
32
src/util.ts
32
src/util.ts
@ -1,38 +1,6 @@
|
|||||||
declare var window;
|
|
||||||
|
|
||||||
declare var Promise;
|
|
||||||
|
|
||||||
export function get(obj, path) {
|
export function get(obj, path) {
|
||||||
for (var i=0, path = path.split('.'), len = path.length; i < len; i++) {
|
for (var i=0, path = path.split('.'), len = path.length; i < len; i++) {
|
||||||
obj = obj[path[i]];
|
obj = obj[path[i]];
|
||||||
}
|
}
|
||||||
return obj;
|
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);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
"src/index.ts",
|
"src/index.ts",
|
||||||
"src/plugin-config.ts",
|
"src/plugin-config.ts",
|
||||||
"src/plugins/camera.ts",
|
"src/plugins/camera.ts",
|
||||||
|
"src/plugins/plugin.ts",
|
||||||
"src/plugins/statusbar.ts",
|
"src/plugins/statusbar.ts",
|
||||||
"src/plugins/toast.ts",
|
"src/plugins/toast.ts",
|
||||||
"src/util.ts"
|
"src/util.ts"
|
||||||
|
Loading…
Reference in New Issue
Block a user