awesome-cordova-plugins/demo/www/app/plugin/plugin.js
2015-11-30 20:17:55 -06:00

149 lines
3.4 KiB
JavaScript

import {ElementRef} from 'angular2/angular2';
import {Page, NavParams} from 'ionic/ionic';
import {Camera, StatusBar, Toast, ActionSheet, Facebook} from 'ionic-native';
import {safeJSONStringify} from '../util';
// To specify arguments for any plugin calls
var demoArgs = {};
demoArgs[ActionSheet] = {
show: {
'buttonLabels': ['Log out'],
'androidEnableCancelButton' : true, // default false
'winphoneEnableCancelButton' : true, // default false
'addCancelButtonWithLabel': 'Cancel'
}
}
demoArgs[Toast] = {
showWithOptions: [
{
message: "hey there",
duration: "short",
position: "bottom",
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
}
]
}
demoArgs[Facebook] = {
login: [
["public_profile"]
]
};
var demoCode = {};
demoCode[Facebook] = function() {
Facebook.login(["public_profile"]).then((userData) => {
console.log("Facebook UserInfo: ", userData);
this.output('Facebook UserInfo: ', userData);
Facebook.getAccessToken().then((token) => {
this.output('Facebook Token: ', token);
console.log("Token: " + token);
});
}, (err) => {
console.error(err);
});
}
@Page({
templateUrl: 'app/plugin/plugin.html',
})
export class Plugin {
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);
this.methods = Object.keys(this.plugin).filter((k) => {
if(typeof this.plugin[k] === 'function') {
return true;
}
return false;
});
}
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();
setTimeout(() => {
this.textArea.scrollTop = this.textArea.scrollHeight;
})
}
doMethod(method) {
let pluginMethodArgEntry = demoArgs[this.plugin];
let pluginCodeEntry = demoCode[this.plugin];
let args = [];
if(pluginMethodArgEntry) {
args = [pluginMethodArgEntry[method]] || [];
console.log('Found some default args', args);
}
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)
});
console.log('Doing method', method, 'on Plugin', this.plugin, 'args:', args);
// Run the custom code
if(pluginCodeEntry) {
pluginCodeEntry.apply(this);
return;
}
let v = this.plugin[method].apply(this.plugin, args);
if(v && v.then) {
v.then(() => {
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);
});
}
}
}
}