awesome-cordova-plugins/demo/www/app/plugin/plugin.js

188 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-12-01 04:38:52 +08:00
import {ElementRef} from 'angular2/angular2';
2015-11-29 06:17:04 +08:00
import {Page, NavParams} from 'ionic/ionic';
2015-11-30 06:30:15 +08:00
import {Camera, StatusBar, Toast, ActionSheet} from 'ionic-native';
2015-11-29 06:52:05 +08:00
2015-12-01 04:38:52 +08:00
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);
}
2015-11-29 06:52:05 +08:00
// To specify arguments for any plugin calls
var demoArgs = {};
2015-11-30 06:30:15 +08:00
demoArgs[ActionSheet] = {
show: {
2015-11-30 12:29:55 +08:00
'buttonLabels': ['Log out'],
2015-11-30 06:30:15 +08:00
'androidEnableCancelButton' : true, // default false
'winphoneEnableCancelButton' : true, // default false
2015-11-30 12:29:55 +08:00
'addCancelButtonWithLabel': 'Cancel'
2015-11-30 06:30:15 +08:00
}
}
2015-11-29 06:52:05 +08:00
demoArgs[Toast] = {
showWithOptions: [
{
message: "hey there",
duration: "short",
position: "bottom",
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
}
]
}
2015-11-29 06:17:04 +08:00
@Page({
templateUrl: 'app/plugin/plugin.html',
})
export class Plugin {
2015-12-01 04:38:52 +08:00
constructor(params: NavParams, elementRef: ElementRef) {
let el = elementRef.nativeElement;
this.textArea = el.querySelector('textarea');
this.content = {
items: [],
value: ''
};
2015-11-29 06:17:04 +08:00
this.plugin = params.get('plugin');
console.log('Plugin', this.plugin);
this.methods = Object.keys(this.plugin).filter((k) => {
if(typeof this.plugin[k] === 'function') {
return true;
}
return false;
});
}
2015-12-01 04:38:52 +08:00
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();
2015-12-01 04:40:56 +08:00
setTimeout(() => {
this.textArea.scrollTop = this.textArea.scrollHeight;
})
2015-12-01 04:38:52 +08:00
}
2015-11-29 06:17:04 +08:00
doMethod(method) {
2015-11-29 06:52:05 +08:00
let pluginMethodArgEntry = demoArgs[this.plugin];
let args = [];
if(pluginMethodArgEntry) {
2015-11-30 12:38:28 +08:00
args = [pluginMethodArgEntry[method]] || [];
2015-11-30 12:29:55 +08:00
console.log('Found some default args', args);
2015-11-29 06:52:05 +08:00
}
2015-11-29 08:26:55 +08:00
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)
});
2015-11-29 06:52:05 +08:00
console.log('Doing method', method, 'on Plugin', this.plugin, 'args:', args);
2015-11-30 07:20:11 +08:00
let v = this.plugin[method].apply(this.plugin, args);
if(v && v.then) {
v.then(() => {
2015-12-01 04:38:52 +08:00
console.log('Success', arguments, this);
this.output(arguments);
2015-11-30 07:20:11 +08:00
}, (err) => {
console.error('Error', err);
2015-12-01 04:38:52 +08:00
this.output(err);
2015-11-30 07:20:11 +08:00
});
} else {
console.log('Response: ', v);
2015-12-01 04:38:52 +08:00
this.output(v);
2015-12-01 03:27:25 +08:00
if(v.subscribe) {
console.log('Observable response, subscribing...');
v.subscribe((val) => {
console.log('Observable val', val);
2015-12-01 04:38:52 +08:00
this.output(val);
2015-12-01 03:27:25 +08:00
}, (err) => {
2015-12-01 04:38:52 +08:00
this.output(err);
2015-12-01 03:27:25 +08:00
console.log('ERROR: Observable', err);
});
}
2015-11-30 07:20:11 +08:00
}
2015-11-29 06:17:04 +08:00
}
}