From 188079fdef18b9fc8f308f5edb2031f0b1ff2506 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Wed, 25 Nov 2015 11:44:58 -0600 Subject: [PATCH] Moving to a class-based approach --- dist/index.d.ts | 3 +- dist/index.js | 75 +++++++++++++++++++++--------------- dist/plugin-config.js | 6 +++ dist/plugins/camera.d.ts | 3 ++ dist/plugins/camera.js | 9 +++++ dist/src/plugins/camera.d.ts | 3 ++ dist/src/plugins/camera.js | 9 +++++ dist/src/util.d.ts | 2 + dist/src/util.js | 22 +++++++++++ dist/util.d.ts | 1 + dist/util.js | 14 +++++++ src/index.ts | 6 ++- src/plugin-config.ts | 6 +++ src/plugins/camera.ts | 8 ++++ src/util.ts | 16 ++++++++ tsconfig.json | 18 +++++++-- 16 files changed, 163 insertions(+), 38 deletions(-) create mode 100644 dist/plugins/camera.d.ts create mode 100644 dist/plugins/camera.js create mode 100644 dist/src/plugins/camera.d.ts create mode 100644 dist/src/plugins/camera.js create mode 100644 dist/src/util.d.ts create mode 100644 dist/src/util.js create mode 100644 src/plugins/camera.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index 4324c3af..3978d72d 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,2 +1 @@ -declare let wrappedPlugins: {}; -export default wrappedPlugins; +export * from './plugins/camera'; diff --git a/dist/index.js b/dist/index.js index 44f7ee0d..2af1f54f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,35 +1,48 @@ -var plugin_config_1 = require('./plugin-config'); -var cordova_1 = require('./cordova'); -var util_1 = require('./util'); -var wrappedPlugins = {}; -var promised; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +__export(require('./plugins/camera')); +/* +let wrappedPlugins = {} + +let promised; + function newPluginClass(config) { - var obj = { - installed: function () { - return !!obj.plugin(); - }, - // Get the plugin by checking the plugin ref path on window - plugin: function () { - return util_1.get(window, config.pluginRef); - }, - pluginName: config.plugin - }; - return obj; + 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 (var i = 0; i < plugin_config_1.PluginConfig.length; i++) { - var plugin = plugin_config_1.PluginConfig[i]; - // Create the wrapped class - var cls = newPluginClass(plugin); - promised = plugin.promise || []; - for (var j = 0; j < promised.length; j++) { - var method = promised[j]; - var p = cordova_1.promisifyCordova(cls, plugin.id, method); - cls[method] = p; - } - // Save the plugin object - wrappedPlugins[plugin.className] = cls; +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; } -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = wrappedPlugins; -window['Native'] = wrappedPlugins; + +export default wrappedPlugins; +*/ +//window['Native'] = wrappedPlugins; diff --git a/dist/plugin-config.js b/dist/plugin-config.js index 3f1b6410..e3bee0f3 100644 --- a/dist/plugin-config.js +++ b/dist/plugin-config.js @@ -40,4 +40,10 @@ exports.PluginConfig = [ pluginRef: 'CameraRoll', promise: ['saveToCameraRoll', 'getPhotos'] }, + { + id: 'contacts', + className: 'Contacts', + plugin: 'cordova-plugin-contacts', + pluginRef: 'navigator.contacts', + }, ]; diff --git a/dist/plugins/camera.d.ts b/dist/plugins/camera.d.ts new file mode 100644 index 00000000..ed95c88b --- /dev/null +++ b/dist/plugins/camera.d.ts @@ -0,0 +1,3 @@ +export declare class Camera { + static getPicture: (...args: any[]) => any; +} diff --git a/dist/plugins/camera.js b/dist/plugins/camera.js new file mode 100644 index 00000000..973a1671 --- /dev/null +++ b/dist/plugins/camera.js @@ -0,0 +1,9 @@ +var util_1 = require('../util'); +var PLUGIN_REF = 'navigator.camera'; +var Camera = (function () { + function Camera() { + } + Camera.getPicture = util_1.promisify(PLUGIN_REF, 'getPicture', 0, 1); + return Camera; +})(); +exports.Camera = Camera; diff --git a/dist/src/plugins/camera.d.ts b/dist/src/plugins/camera.d.ts new file mode 100644 index 00000000..9a769166 --- /dev/null +++ b/dist/src/plugins/camera.d.ts @@ -0,0 +1,3 @@ +export declare class Camera { + static getPicture: (...args: any[]) => any; +} diff --git a/dist/src/plugins/camera.js b/dist/src/plugins/camera.js new file mode 100644 index 00000000..90c85427 --- /dev/null +++ b/dist/src/plugins/camera.js @@ -0,0 +1,9 @@ +var util_1 = require('../util'); +var PLUGIN_REF = 'navigator.camera'; +var Camera = (function () { + function Camera() { + } + Camera.getPicture = util_1.promisify(PLUGIN_REF, 'getPicture', 0, 1); + return Camera; +})(); +exports.Camera = Camera; diff --git a/dist/src/util.d.ts b/dist/src/util.d.ts new file mode 100644 index 00000000..fb5be801 --- /dev/null +++ b/dist/src/util.d.ts @@ -0,0 +1,2 @@ +export declare function get(obj: any, path: any): any; +export declare const promisify: (pluginRef: any, methodName: any, successIndex: any, errorIndex: any) => (...args: any[]) => any; diff --git a/dist/src/util.js b/dist/src/util.js new file mode 100644 index 00000000..983bf193 --- /dev/null +++ b/dist/src/util.js @@ -0,0 +1,22 @@ +var _this = this; +function get(obj, path) { + for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) { + obj = obj[path[i]]; + } + return obj; +} +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); + }); + }; +}; diff --git a/dist/util.d.ts b/dist/util.d.ts index 9c046eed..b1b29e4c 100644 --- a/dist/util.d.ts +++ b/dist/util.d.ts @@ -1 +1,2 @@ export declare function get(obj: any, path: any): any; +export declare const promisify: (pluginRef: any, methodName: any, successIndex: any, errorIndex: any) => (...args: any[]) => any; diff --git a/dist/util.js b/dist/util.js index 23df0a55..d6428a90 100644 --- a/dist/util.js +++ b/dist/util.js @@ -1,3 +1,4 @@ +var _this = this; function get(obj, path) { for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) { obj = obj[path[i]]; @@ -6,3 +7,16 @@ 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); + }); + }; +}; diff --git a/src/index.ts b/src/index.ts index b1586937..56d4813e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,9 @@ import {promisifyCordova} from './cordova'; import {get} from './util'; +export * from './plugins/camera'; + +/* let wrappedPlugins = {} let promised; @@ -46,5 +49,6 @@ for(let i = 0; i < PluginConfig.length; i++) { } export default wrappedPlugins; +*/ -window['Native'] = wrappedPlugins; +//window['Native'] = wrappedPlugins; diff --git a/src/plugin-config.ts b/src/plugin-config.ts index 032d9a87..e69191ee 100644 --- a/src/plugin-config.ts +++ b/src/plugin-config.ts @@ -49,4 +49,10 @@ export var PluginConfig:CordovaPlugin[] = [ pluginRef: 'CameraRoll', promise: ['saveToCameraRoll', 'getPhotos'] }, + { + id: 'contacts', + className: 'Contacts', + plugin: 'cordova-plugin-contacts', + pluginRef: 'navigator.contacts', + }, ] diff --git a/src/plugins/camera.ts b/src/plugins/camera.ts new file mode 100644 index 00000000..6ce59c97 --- /dev/null +++ b/src/plugins/camera.ts @@ -0,0 +1,8 @@ + +import {promisify} from '../util'; + +let PLUGIN_REF = 'navigator.camera'; + +export class Camera { + static getPicture = promisify(PLUGIN_REF, 'getPicture', 0, 1) +} diff --git a/src/util.ts b/src/util.ts index d83fd049..79152509 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,6 +1,22 @@ +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); + }) + } +} diff --git a/tsconfig.json b/tsconfig.json index 8bc07b58..c818be89 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,10 +8,20 @@ "sourceMap": false, "declaration": true }, - "files": [ - "src/index.ts" + "filesGlob": [ + "src/**/*.ts" ], "exclude": [ "node_modules" - ] -} \ No newline at end of file + ], + "files": [ + "src/cordova.ts", + "src/index.ts", + "src/plugin-config.ts", + "src/plugins/camera.ts", + "src/util.ts" + ], + "atom": { + "rewriteTsconfig": true + } +}