DEMO with output

This commit is contained in:
Max Lynch 2015-11-30 14:38:52 -06:00
parent 118da948cb
commit c939ae975f
7 changed files with 156 additions and 42 deletions

View File

@ -5,6 +5,9 @@
</ion-navbar>
<ion-content>
<ion-input fixed-label>
<textarea [(ng-model)]="content.value" style="height: 150px; font-size: 11px"></textarea>
</ion-input>
<ion-list>
<button ion-item *ng-for="#method of methods" (click)="doMethod(method)">
{{method}}()

View File

@ -1,7 +1,78 @@
import {ElementRef} from 'angular2/angular2';
import {Page, NavParams} from 'ionic/ionic';
import {Camera, StatusBar, Toast, ActionSheet} from 'ionic-native';
function safeJSONStringify (input, maxDepth)
{
var output,
refs = [],
refsPaths = [];
maxDepth = maxDepth || 5;
function recursion (input, path, depth)
{
var output = {},
pPath,
refIdx;
path = path || "";
depth = depth || 0;
depth++;
if (maxDepth && depth > maxDepth)
{
return "{depth over " + maxDepth + "}";
}
for (var p in input)
{
pPath = (path ? (path+".") : "") + p;
if (typeof input[p] === "function")
{
//output[p] = "{function}";
}
else if (typeof input[p] === "object")
{
/*
refIdx = refs.indexOf(input[p]);
if (-1 !== refIdx)
{
output[p] = recursion(input[p])"{reference to " + refsPaths[refIdx] + "}";
}
else
{
*/
refs.push(input[p]);
refsPaths.push(pPath);
output[p] = recursion(input[p], pPath, depth);
//}
}
else
{
output[p] = input[p];
}
}
return output;
}
if (typeof input === "object")
{
output = recursion(input);
}
else
{
output = input;
}
return JSON.stringify(output);
}
// To specify arguments for any plugin calls
var demoArgs = {};
demoArgs[ActionSheet] = {
@ -28,7 +99,16 @@ demoArgs[Toast] = {
templateUrl: 'app/plugin/plugin.html',
})
export class Plugin {
constructor(params: NavParams) {
constructor(params: NavParams, elementRef: ElementRef) {
let el = elementRef.nativeElement;
this.textArea = el.querySelector('textarea');
this.content = {
items: [],
value: ''
};
this.plugin = params.get('plugin');
console.log('Plugin', this.plugin);
@ -41,6 +121,22 @@ export class Plugin {
});
}
output(...args) {
var s = args.map((v) => {
if(typeof v === 'object') {
console.log('Stringifying', v);
return safeJSONStringify(v, 4);//JSON.stringify(v);
}
return v;
});
this.content.items.push(s.join(' '));
this.content.value = this.content.items.join();
this.textArea.scrollTop = this.textArea.scrollHeight;
}
doMethod(method) {
let pluginMethodArgEntry = demoArgs[this.plugin];
@ -63,18 +159,23 @@ export class Plugin {
if(v && v.then) {
v.then(() => {
console.log('Success', arguments);
console.log('Success', arguments, this);
this.output(arguments);
}, (err) => {
console.error('Error', err);
this.output(err);
});
} else {
console.log('Response: ', v);
this.output(v);
if(v.subscribe) {
console.log('Observable response, subscribing...');
v.subscribe((val) => {
console.log('Observable val', val);
this.output(val);
}, (err) => {
this.output(err);
console.log('ERROR: Observable', err);
});
}

View File

@ -25,12 +25,9 @@ exports.cordovaWarn = function (pluginName, method) {
}
};
function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
if (opts === void 0) { opts = {}; }
if (!window.cordova) {
exports.cordovaWarn(pluginObj.name, methodName);
}
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
if (opts === void 0) { opts = {}; }
// If the plugin method expects myMethod(success, err, options)
if (opts.callbackOrder == 'reverse') {
// Get those arguments in the order [reject, resolve, ...restOfArgs]
@ -50,6 +47,14 @@ function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
}
var pluginInstance = exports.getPlugin(pluginObj.pluginRef);
if (!pluginInstance) {
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
if (!window.cordova) {
exports.cordovaWarn(pluginObj.name, methodName);
reject({
error: 'cordova_not_available'
});
return;
}
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
reject({
error: 'plugin_not_installed'
@ -69,13 +74,15 @@ function wrapPromise(pluginObj, methodName, args, opts) {
function wrapObservable(pluginObj, methodName, args, opts) {
if (opts === void 0) { opts = {}; }
return new Rx_1.Observable(function (observer) {
console.log('Calling inside observable', observer);
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function (d) {
console.log('WATCH RESP', d);
observer.next(d);
}, observer.error);
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
return function () {
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
try {
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
}
catch (e) {
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
console.log(e);
}
};
});
}
@ -87,7 +94,6 @@ exports.wrap = function (pluginObj, methodName, opts) {
args[_i - 0] = arguments[_i];
}
if (opts.observable) {
console.log("Wrapping observable");
return wrapObservable(pluginObj, methodName, args, opts);
}
else {

File diff suppressed because one or more lines are too long

View File

@ -26,9 +26,6 @@ exports.cordovaWarn = function (pluginName, method) {
};
function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
if (opts === void 0) { opts = {}; }
if (!window.cordova) {
exports.cordovaWarn(pluginObj.name, methodName);
}
if (opts.callbackOrder == 'reverse') {
args.unshift(reject);
args.unshift(resolve);
@ -43,6 +40,13 @@ function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
}
var pluginInstance = exports.getPlugin(pluginObj.pluginRef);
if (!pluginInstance) {
if (!window.cordova) {
exports.cordovaWarn(pluginObj.name, methodName);
reject({
error: 'cordova_not_available'
});
return;
}
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
reject({
error: 'plugin_not_installed'
@ -61,13 +65,15 @@ function wrapPromise(pluginObj, methodName, args, opts) {
function wrapObservable(pluginObj, methodName, args, opts) {
if (opts === void 0) { opts = {}; }
return new Rx_1.Observable(function (observer) {
console.log('Calling inside observable', observer);
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function (d) {
console.log('WATCH RESP', d);
observer.next(d);
}, observer.error);
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
return function () {
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
try {
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
}
catch (e) {
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
console.log(e);
}
};
});
}
@ -79,7 +85,6 @@ exports.wrap = function (pluginObj, methodName, opts) {
args[_i - 0] = arguments[_i];
}
if (opts.observable) {
console.log("Wrapping observable");
return wrapObservable(pluginObj, methodName, args, opts);
}
else {

File diff suppressed because one or more lines are too long

View File

@ -30,16 +30,6 @@ export const cordovaWarn = function(pluginName: string, method: string) {
}
function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:any={}, resolve:any, reject:any) {
if(!window.cordova) {
cordovaWarn(pluginObj.name, methodName);
/*
reject({
error: 'cordova_not_available'
})
return;
*/
}
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
@ -62,6 +52,15 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
let pluginInstance = getPlugin(pluginObj.pluginRef);
if(!pluginInstance) {
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
if(!window.cordova) {
cordovaWarn(pluginObj.name, methodName);
reject({
error: 'cordova_not_available'
})
return;
}
pluginWarn(pluginObj.name, methodName, pluginObj.name);
reject({
error: 'plugin_not_installed'
@ -83,14 +82,15 @@ function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={})
function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = {}) {
return new Observable(observer => {
console.log('Calling inside observable', observer);
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, (d) => {
console.log('WATCH RESP', d);
observer.next(d)
}, observer.error);
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
return () => {
return get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
try {
return get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
} catch(e) {
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
console.log(e);
}
}
});
}
@ -99,7 +99,6 @@ export const wrap = function(pluginObj:any, methodName:string, opts:any = {}) {
return (...args) => {
if(opts.observable) {
console.log("Wrapping observable");
return wrapObservable(pluginObj, methodName, args, opts);
} else {
return wrapPromise(pluginObj, methodName, args, opts);