From 5b754390f57a3897575f21faff209f1d6a503926 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Sat, 28 Nov 2015 18:26:55 -0600 Subject: [PATCH] Lots o refactor --- demo/www/app/plugin/plugin.js | 13 ++++- dist/index.js | 44 ----------------- dist/plugin.d.ts | 1 + dist/plugin.js | 18 +++++++ dist/plugins/camera.d.ts | 10 ++-- dist/plugins/camera.js | 43 +++++++++++++---- dist/plugins/plugin.d.ts | 3 ++ dist/plugins/plugin.js | 55 +++++++++++++++++++++ dist/plugins/statusbar.d.ts | 24 +++++----- dist/plugins/statusbar.js | 64 ++++++++++++++++++------- dist/plugins/toast.d.ts | 10 ++-- dist/plugins/toast.js | 42 +++++++++++++---- dist/src/cordova.d.ts | 10 ++-- dist/src/cordova.js | 84 ++++++++++++++++----------------- dist/src/plugin-config.d.ts | 8 ++++ dist/src/plugin-config.js | 49 +++++++++++++++++++ dist/src/plugins/camera.d.ts | 10 ++-- dist/src/plugins/camera.js | 45 +++++++++++++++--- dist/src/plugins/plugin.d.ts | 3 ++ dist/src/plugins/plugin.js | 54 +++++++++++++++++++++ dist/src/plugins/statusbar.d.ts | 11 +++++ dist/src/plugins/statusbar.js | 60 +++++++++++++++++++++++ dist/src/plugins/toast.d.ts | 10 ++-- dist/src/plugins/toast.js | 44 ++++++++++++++--- dist/src/util.d.ts | 2 - dist/src/util.js | 33 ------------- dist/util.d.ts | 2 - dist/util.js | 33 ------------- src/index.ts | 46 ------------------ src/plugins/camera.ts | 21 ++++++--- src/plugins/plugin.ts | 62 ++++++++++++++++++++++++ src/plugins/statusbar.ts | 39 ++++++++------- src/plugins/toast.ts | 23 +++++---- src/util.ts | 32 ------------- tsconfig.json | 1 + 35 files changed, 653 insertions(+), 356 deletions(-) create mode 100644 dist/plugin.d.ts create mode 100644 dist/plugin.js create mode 100644 dist/plugins/plugin.d.ts create mode 100644 dist/plugins/plugin.js create mode 100644 dist/src/plugin-config.d.ts create mode 100644 dist/src/plugin-config.js create mode 100644 dist/src/plugins/plugin.d.ts create mode 100644 dist/src/plugins/plugin.js create mode 100644 dist/src/plugins/statusbar.d.ts create mode 100644 dist/src/plugins/statusbar.js create mode 100644 src/plugins/plugin.ts diff --git a/demo/www/app/plugin/plugin.js b/demo/www/app/plugin/plugin.js index ef7d59caa..8843f5fa2 100644 --- a/demo/www/app/plugin/plugin.js +++ b/demo/www/app/plugin/plugin.js @@ -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); + }); } } diff --git a/dist/index.js b/dist/index.js index daf4ab8c6..4fc8da342 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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; diff --git a/dist/plugin.d.ts b/dist/plugin.d.ts new file mode 100644 index 000000000..f42789431 --- /dev/null +++ b/dist/plugin.d.ts @@ -0,0 +1 @@ +export declare function Plugin(config: any): (cls: any) => any; diff --git a/dist/plugin.js b/dist/plugin.js new file mode 100644 index 000000000..d134c0201 --- /dev/null +++ b/dist/plugin.js @@ -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; diff --git a/dist/plugins/camera.d.ts b/dist/plugins/camera.d.ts index 4db576c78..f7245474b 100644 --- a/dist/plugins/camera.d.ts +++ b/dist/plugins/camera.d.ts @@ -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; +} diff --git a/dist/plugins/camera.js b/dist/plugins/camera.js index 9f14755ed..84d0c90d6 100644 --- a/dist/plugins/camera.js +++ b/dist/plugins/camera.js @@ -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; diff --git a/dist/plugins/plugin.d.ts b/dist/plugins/plugin.d.ts new file mode 100644 index 000000000..ad30755d7 --- /dev/null +++ b/dist/plugins/plugin.d.ts @@ -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; diff --git a/dist/plugins/plugin.js b/dist/plugins/plugin.js new file mode 100644 index 000000000..ab81e56da --- /dev/null +++ b/dist/plugins/plugin.js @@ -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; diff --git a/dist/plugins/statusbar.d.ts b/dist/plugins/statusbar.d.ts index 1b018d5a2..8cb7185bb 100644 --- a/dist/plugins/statusbar.d.ts +++ b/dist/plugins/statusbar.d.ts @@ -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; +} diff --git a/dist/plugins/statusbar.js b/dist/plugins/statusbar.js index 75040e6b2..10281f518 100644 --- a/dist/plugins/statusbar.js +++ b/dist/plugins/statusbar.js @@ -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; diff --git a/dist/plugins/toast.d.ts b/dist/plugins/toast.d.ts index 4341e8549..ea51f7008 100644 --- a/dist/plugins/toast.d.ts +++ b/dist/plugins/toast.d.ts @@ -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; +} diff --git a/dist/plugins/toast.js b/dist/plugins/toast.js index cc13d9fd3..1b80cdaae 100644 --- a/dist/plugins/toast.js +++ b/dist/plugins/toast.js @@ -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; diff --git a/dist/src/cordova.d.ts b/dist/src/cordova.d.ts index ebd110ebf..bc833b0fd 100644 --- a/dist/src/cordova.d.ts +++ b/dist/src/cordova.d.ts @@ -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; +} diff --git a/dist/src/cordova.js b/dist/src/cordova.js index 6dc6dbd3b..822d01b26 100644 --- a/dist/src/cordova.js +++ b/dist/src/cordova.js @@ -1,42 +1,42 @@ -var util_1 = require('./util'); -var Cordova = (function () { - function Cordova() { - } - Cordova.hasPlugin = function (pluginRef) { - return !!this.plugin(pluginRef); - }; - Cordova.plugin = function (pluginRef) { - return util_1.get(window, pluginRef); - }; - Cordova.promisify = function (pluginRef, pluginName, methodName, successIndex, errorIndex) { - var _this = this; - return function () { - var args = []; - for (var _i = 0; _i < arguments.length; _i++) { - args[_i - 0] = arguments[_i]; - } - return new Promise(function (resolve, reject) { - 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'); - reject({ - error: 'cordova_not_available' - }); - return; - } - if (!_this.hasPlugin(pluginRef)) { - console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but the ' + pluginName + ' plugin is not installed.'); - reject({ - error: 'plugin_not_installed' - }); - return; - } - console.log('Cordova: exec(' + pluginName + ', ' + methodName + ')'); - args[successIndex] = resolve; - args[errorIndex] = reject; - util_1.get(window, pluginRef)[methodName].apply(_this, args); - }); - }; - }; - return Cordova; -})(); -exports.Cordova = Cordova; +var util_1 = require('./util'); +var Cordova = (function () { + function Cordova() { + } + Cordova.hasPlugin = function (pluginRef) { + return !!this.plugin(pluginRef); + }; + Cordova.plugin = function (pluginRef) { + return util_1.get(window, pluginRef); + }; + Cordova.promisify = function (pluginRef, pluginName, methodName, successIndex, errorIndex) { + var _this = this; + return function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return new Promise(function (resolve, reject) { + 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'); + reject({ + error: 'cordova_not_available' + }); + return; + } + if (!_this.hasPlugin(pluginRef)) { + console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but the ' + pluginName + ' plugin is not installed.'); + reject({ + error: 'plugin_not_installed' + }); + return; + } + console.log('Cordova: exec(' + pluginName + ', ' + methodName + ')'); + args[successIndex] = resolve; + args[errorIndex] = reject; + util_1.get(window, pluginRef)[methodName].apply(_this, args); + }); + }; + }; + return Cordova; +})(); +exports.Cordova = Cordova; diff --git a/dist/src/plugin-config.d.ts b/dist/src/plugin-config.d.ts new file mode 100644 index 000000000..0975baa7a --- /dev/null +++ b/dist/src/plugin-config.d.ts @@ -0,0 +1,8 @@ +export interface CordovaPlugin { + id: string; + className: string; + plugin: string; + pluginRef: string; + promise?: any; +} +export declare var PluginConfig: CordovaPlugin[]; diff --git a/dist/src/plugin-config.js b/dist/src/plugin-config.js new file mode 100644 index 000000000..e3bee0f37 --- /dev/null +++ b/dist/src/plugin-config.js @@ -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', + }, +]; diff --git a/dist/src/plugins/camera.d.ts b/dist/src/plugins/camera.d.ts index 3272f0d93..b32b57ec2 100644 --- a/dist/src/plugins/camera.d.ts +++ b/dist/src/plugins/camera.d.ts @@ -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; +} diff --git a/dist/src/plugins/camera.js b/dist/src/plugins/camera.js index 262532eec..5f79c061f 100644 --- a/dist/src/plugins/camera.js +++ b/dist/src/plugins/camera.js @@ -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; diff --git a/dist/src/plugins/plugin.d.ts b/dist/src/plugins/plugin.d.ts new file mode 100644 index 000000000..8185a016c --- /dev/null +++ b/dist/src/plugins/plugin.d.ts @@ -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; diff --git a/dist/src/plugins/plugin.js b/dist/src/plugins/plugin.js new file mode 100644 index 000000000..07ba08fc0 --- /dev/null +++ b/dist/src/plugins/plugin.js @@ -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; diff --git a/dist/src/plugins/statusbar.d.ts b/dist/src/plugins/statusbar.d.ts new file mode 100644 index 000000000..3aec00fdd --- /dev/null +++ b/dist/src/plugins/statusbar.d.ts @@ -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; +} diff --git a/dist/src/plugins/statusbar.js b/dist/src/plugins/statusbar.js new file mode 100644 index 000000000..d1d60207f --- /dev/null +++ b/dist/src/plugins/statusbar.js @@ -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; diff --git a/dist/src/plugins/toast.d.ts b/dist/src/plugins/toast.d.ts index 45f90d6b9..9c44128a9 100644 --- a/dist/src/plugins/toast.d.ts +++ b/dist/src/plugins/toast.d.ts @@ -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; +} diff --git a/dist/src/plugins/toast.js b/dist/src/plugins/toast.js index 2b9ef8b32..b19e91164 100644 --- a/dist/src/plugins/toast.js +++ b/dist/src/plugins/toast.js @@ -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; diff --git a/dist/src/util.d.ts b/dist/src/util.d.ts index 2e16c3e4c..f9cb3d63c 100644 --- a/dist/src/util.d.ts +++ b/dist/src/util.d.ts @@ -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; diff --git a/dist/src/util.js b/dist/src/util.js index 282fb4fff..80da07f4a 100644 --- a/dist/src/util.js +++ b/dist/src/util.js @@ -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); - }); - }; -}; diff --git a/dist/util.d.ts b/dist/util.d.ts index b04695db5..9c046eed0 100644 --- a/dist/util.d.ts +++ b/dist/util.d.ts @@ -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; diff --git a/dist/util.js b/dist/util.js index 1f6a8ea02..23df0a555 100644 --- a/dist/util.js +++ b/dist/util.js @@ -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); - }); - }; -}; diff --git a/src/index.ts b/src/index.ts index f63e2a237..635c994f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; diff --git a/src/plugins/camera.ts b/src/plugins/camera.ts index 11b383668..75973a8da 100644 --- a/src/plugins/camera.ts +++ b/src/plugins/camera.ts @@ -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; } diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts new file mode 100644 index 000000000..6b6a40166 --- /dev/null +++ b/src/plugins/plugin.ts @@ -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); + } +} diff --git a/src/plugins/statusbar.ts b/src/plugins/statusbar.ts index 0896c8c9d..e1c1c2dd8 100644 --- a/src/plugins/statusbar.ts +++ b/src/plugins/statusbar.ts @@ -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; } diff --git a/src/plugins/toast.ts b/src/plugins/toast.ts index def9ec824..9a07f23f0 100644 --- a/src/plugins/toast.ts +++ b/src/plugins/toast.ts @@ -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; } diff --git a/src/util.ts b/src/util.ts index 80dbec955..d83fd0497 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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); - }) - } -} diff --git a/tsconfig.json b/tsconfig.json index 6f801033e..a5fe79543 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"