mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-04-28 05:50:21 +08:00
Moving to a class-based approach
This commit is contained in:
parent
e455e8ebcf
commit
188079fdef
3
dist/index.d.ts
vendored
3
dist/index.d.ts
vendored
@ -1,2 +1 @@
|
|||||||
declare let wrappedPlugins: {};
|
export * from './plugins/camera';
|
||||||
export default wrappedPlugins;
|
|
||||||
|
49
dist/index.js
vendored
49
dist/index.js
vendored
@ -1,35 +1,48 @@
|
|||||||
var plugin_config_1 = require('./plugin-config');
|
function __export(m) {
|
||||||
var cordova_1 = require('./cordova');
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||||
var util_1 = require('./util');
|
}
|
||||||
var wrappedPlugins = {};
|
__export(require('./plugins/camera'));
|
||||||
var promised;
|
/*
|
||||||
|
let wrappedPlugins = {}
|
||||||
|
|
||||||
|
let promised;
|
||||||
|
|
||||||
function newPluginClass(config) {
|
function newPluginClass(config) {
|
||||||
var obj = {
|
let obj = {
|
||||||
installed: function () {
|
installed: () => {
|
||||||
return !!obj.plugin();
|
return !!obj.plugin();
|
||||||
},
|
},
|
||||||
|
|
||||||
// Get the plugin by checking the plugin ref path on window
|
// Get the plugin by checking the plugin ref path on window
|
||||||
plugin: function () {
|
plugin: () => {
|
||||||
return util_1.get(window, config.pluginRef);
|
return get(window, config.pluginRef)
|
||||||
},
|
},
|
||||||
|
|
||||||
pluginName: config.plugin
|
pluginName: config.plugin
|
||||||
};
|
};
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go through each registered plugin
|
// Go through each registered plugin
|
||||||
for (var i = 0; i < plugin_config_1.PluginConfig.length; i++) {
|
for(let i = 0; i < PluginConfig.length; i++) {
|
||||||
var plugin = plugin_config_1.PluginConfig[i];
|
let plugin = PluginConfig[i];
|
||||||
|
|
||||||
// Create the wrapped class
|
// Create the wrapped class
|
||||||
var cls = newPluginClass(plugin);
|
let cls = newPluginClass(plugin);
|
||||||
|
|
||||||
promised = plugin.promise || [];
|
promised = plugin.promise || [];
|
||||||
for (var j = 0; j < promised.length; j++) {
|
|
||||||
var method = promised[j];
|
for(let j = 0; j < promised.length; j++) {
|
||||||
var p = cordova_1.promisifyCordova(cls, plugin.id, method);
|
let method = promised[j];
|
||||||
|
let p = promisifyCordova(cls, plugin.id, method)
|
||||||
cls[method] = p;
|
cls[method] = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the plugin object
|
// Save the plugin object
|
||||||
wrappedPlugins[plugin.className] = cls;
|
wrappedPlugins[plugin.className] = cls;
|
||||||
}
|
}
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
exports.default = wrappedPlugins;
|
export default wrappedPlugins;
|
||||||
window['Native'] = wrappedPlugins;
|
*/
|
||||||
|
//window['Native'] = wrappedPlugins;
|
||||||
|
6
dist/plugin-config.js
vendored
6
dist/plugin-config.js
vendored
@ -40,4 +40,10 @@ exports.PluginConfig = [
|
|||||||
pluginRef: 'CameraRoll',
|
pluginRef: 'CameraRoll',
|
||||||
promise: ['saveToCameraRoll', 'getPhotos']
|
promise: ['saveToCameraRoll', 'getPhotos']
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'contacts',
|
||||||
|
className: 'Contacts',
|
||||||
|
plugin: 'cordova-plugin-contacts',
|
||||||
|
pluginRef: 'navigator.contacts',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
3
dist/plugins/camera.d.ts
vendored
Normal file
3
dist/plugins/camera.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export declare class Camera {
|
||||||
|
static getPicture: (...args: any[]) => any;
|
||||||
|
}
|
9
dist/plugins/camera.js
vendored
Normal file
9
dist/plugins/camera.js
vendored
Normal file
@ -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;
|
3
dist/src/plugins/camera.d.ts
vendored
Normal file
3
dist/src/plugins/camera.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export declare class Camera {
|
||||||
|
static getPicture: (...args: any[]) => any;
|
||||||
|
}
|
9
dist/src/plugins/camera.js
vendored
Normal file
9
dist/src/plugins/camera.js
vendored
Normal file
@ -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;
|
2
dist/src/util.d.ts
vendored
Normal file
2
dist/src/util.d.ts
vendored
Normal file
@ -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;
|
22
dist/src/util.js
vendored
Normal file
22
dist/src/util.js
vendored
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
1
dist/util.d.ts
vendored
1
dist/util.d.ts
vendored
@ -1 +1,2 @@
|
|||||||
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;
|
||||||
|
14
dist/util.js
vendored
14
dist/util.js
vendored
@ -1,3 +1,4 @@
|
|||||||
|
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]];
|
||||||
@ -6,3 +7,16 @@ 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);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
@ -5,6 +5,9 @@ import {promisifyCordova} from './cordova';
|
|||||||
|
|
||||||
import {get} from './util';
|
import {get} from './util';
|
||||||
|
|
||||||
|
export * from './plugins/camera';
|
||||||
|
|
||||||
|
/*
|
||||||
let wrappedPlugins = {}
|
let wrappedPlugins = {}
|
||||||
|
|
||||||
let promised;
|
let promised;
|
||||||
@ -46,5 +49,6 @@ for(let i = 0; i < PluginConfig.length; i++) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default wrappedPlugins;
|
export default wrappedPlugins;
|
||||||
|
*/
|
||||||
|
|
||||||
window['Native'] = wrappedPlugins;
|
//window['Native'] = wrappedPlugins;
|
||||||
|
@ -49,4 +49,10 @@ export var PluginConfig:CordovaPlugin[] = [
|
|||||||
pluginRef: 'CameraRoll',
|
pluginRef: 'CameraRoll',
|
||||||
promise: ['saveToCameraRoll', 'getPhotos']
|
promise: ['saveToCameraRoll', 'getPhotos']
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'contacts',
|
||||||
|
className: 'Contacts',
|
||||||
|
plugin: 'cordova-plugin-contacts',
|
||||||
|
pluginRef: 'navigator.contacts',
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
8
src/plugins/camera.ts
Normal file
8
src/plugins/camera.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
import {promisify} from '../util';
|
||||||
|
|
||||||
|
let PLUGIN_REF = 'navigator.camera';
|
||||||
|
|
||||||
|
export class Camera {
|
||||||
|
static getPicture = promisify(PLUGIN_REF, 'getPicture', 0, 1)
|
||||||
|
}
|
16
src/util.ts
16
src/util.ts
@ -1,6 +1,22 @@
|
|||||||
|
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);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,10 +8,20 @@
|
|||||||
"sourceMap": false,
|
"sourceMap": false,
|
||||||
"declaration": true
|
"declaration": true
|
||||||
},
|
},
|
||||||
"files": [
|
"filesGlob": [
|
||||||
"src/index.ts"
|
"src/**/*.ts"
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules"
|
"node_modules"
|
||||||
]
|
],
|
||||||
|
"files": [
|
||||||
|
"src/cordova.ts",
|
||||||
|
"src/index.ts",
|
||||||
|
"src/plugin-config.ts",
|
||||||
|
"src/plugins/camera.ts",
|
||||||
|
"src/util.ts"
|
||||||
|
],
|
||||||
|
"atom": {
|
||||||
|
"rewriteTsconfig": true
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user