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

173 lines
4.0 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-12-02 08:47:24 +08:00
import {Camera, Calendar, StatusBar, Toast, ActionSheet, Facebook, Push} from 'ionic-native';
2015-11-29 06:52:05 +08:00
2015-12-01 10:17:55 +08:00
import {safeJSONStringify} from '../util';
2015-12-01 04:38:52 +08:00
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-12-01 10:17:55 +08:00
demoArgs[Facebook] = {
login: [
["public_profile"]
]
};
2015-12-02 08:47:24 +08:00
demoArgs[Push] = {
init: [{
android: {
senderID: "12345679"
},
ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
}]
}
2015-12-01 10:17:55 +08:00
var demoCode = {};
2015-12-01 12:15:21 +08:00
demoCode[Calendar] = {
createEventInteractively: function() {
Calendar.createEventInteractively("Grab Coffee", "Johnson Public House", new Date(), new Date()).then((event) => {
console.log("Created event", event);
this.output('Created event', event);
})
}
};
demoCode[Facebook] = {
login: 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);
2015-12-01 10:17:55 +08:00
});
2015-12-01 12:15:21 +08:00
}
2015-12-01 10:17:55 +08:00
}
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];
2015-12-01 12:15:21 +08:00
let pluginCodeEntry = demoCode[this.plugin] && demoCode[this.plugin][method];
2015-11-29 06:52:05 +08:00
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
2015-12-01 10:17:55 +08:00
// Run the custom code
if(pluginCodeEntry) {
pluginCodeEntry.apply(this);
return;
}
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
}
}