70 lines
1.6 KiB
JavaScript
70 lines
1.6 KiB
JavaScript
/**
|
|
* @providesModule DialogAndroid
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var { NativeModules } = require('react-native');
|
|
|
|
var callbackNames = [
|
|
'onPositive',
|
|
'onNegative',
|
|
'onNeutral',
|
|
'onAny',
|
|
'itemsCallback',
|
|
'itemsCallbackSingleChoice',
|
|
'itemsCallbackMultiChoice',
|
|
'showListener',
|
|
'cancelListener',
|
|
'dismissListener',
|
|
];
|
|
|
|
class DialogAndroid {
|
|
constructor() {
|
|
this.options = {};
|
|
}
|
|
|
|
set(obj) {
|
|
Object.assign(this.options, obj);
|
|
}
|
|
|
|
show() {
|
|
var finalOptions = Object.assign({}, this.options);
|
|
|
|
var callbacks = {
|
|
error: (err, op) => console.error(err, op),
|
|
}
|
|
|
|
callbackNames.forEach(cb => {
|
|
if (cb in finalOptions) {
|
|
callbacks[cb] = finalOptions[cb];
|
|
finalOptions[cb] = true;
|
|
}
|
|
});
|
|
|
|
// Handle special case of input separately
|
|
if ('input' in finalOptions) {
|
|
finalOptions.input = Object.assign({}, finalOptions.input);
|
|
var inputCallback = finalOptions.input.callback || (x => console.log(x));
|
|
finalOptions.input.callback = true;
|
|
}
|
|
|
|
// Parse the result form multiple choice dialog
|
|
if ('itemsCallbackMultiChoice' in callbacks) {
|
|
var originalCallback = callbacks.itemsCallbackMultiChoice;
|
|
callbacks.itemsCallbackMultiChoice = selected => {
|
|
var indices = selected.split(',').map(x => parseInt(x));
|
|
var elements = indices.map(ind => (finalOptions.items || [])[ind]);
|
|
|
|
originalCallback(indices, elements);
|
|
}
|
|
}
|
|
|
|
var callbackFunc = (cb, ...rest) => callbacks[cb](...rest);
|
|
|
|
NativeModules.DialogAndroid.show(finalOptions, callbackFunc);
|
|
}
|
|
}
|
|
|
|
module.exports = DialogAndroid;
|