mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-05-02 00:07:23 +08:00
Moving to a class-based approach
This commit is contained in:
Vendored
+1
-2
@@ -1,2 +1 @@
|
||||
declare let wrappedPlugins: {};
|
||||
export default wrappedPlugins;
|
||||
export * from './plugins/camera';
|
||||
|
||||
Vendored
+44
-31
@@ -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;
|
||||
|
||||
Vendored
+6
@@ -40,4 +40,10 @@ exports.PluginConfig = [
|
||||
pluginRef: 'CameraRoll',
|
||||
promise: ['saveToCameraRoll', 'getPhotos']
|
||||
},
|
||||
{
|
||||
id: 'contacts',
|
||||
className: 'Contacts',
|
||||
plugin: 'cordova-plugin-contacts',
|
||||
pluginRef: 'navigator.contacts',
|
||||
},
|
||||
];
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
export declare class Camera {
|
||||
static getPicture: (...args: any[]) => any;
|
||||
}
|
||||
Vendored
+9
@@ -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;
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
export declare class Camera {
|
||||
static getPicture: (...args: any[]) => any;
|
||||
}
|
||||
Vendored
+9
@@ -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;
|
||||
Vendored
+2
@@ -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;
|
||||
Vendored
+22
@@ -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);
|
||||
});
|
||||
};
|
||||
};
|
||||
Vendored
+1
@@ -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;
|
||||
|
||||
Vendored
+14
@@ -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);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user