2015-11-01 17:28:44 +00:00
|
|
|
/**
|
|
|
|
* @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),
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:30:21 +00:00
|
|
|
// Remove callbacks from the options, and store them separately
|
2015-11-01 17:28:44 +00:00
|
|
|
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;
|
2015-11-18 23:34:43 +00:00
|
|
|
callbacks['input'] = inputCallback;
|
2015-11-01 17:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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]);
|
2016-06-17 09:25:31 +00:00
|
|
|
if(indices.length === 1 && isNaN(indices[0])){
|
|
|
|
indices=[] // the case of empty selection
|
2016-08-09 20:57:07 +00:00
|
|
|
elements=[]
|
2016-06-17 09:25:31 +00:00
|
|
|
}
|
2015-11-01 17:28:44 +00:00
|
|
|
originalCallback(indices, elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var callbackFunc = (cb, ...rest) => callbacks[cb](...rest);
|
|
|
|
|
|
|
|
NativeModules.DialogAndroid.show(finalOptions, callbackFunc);
|
|
|
|
}
|
2016-01-06 03:56:22 +00:00
|
|
|
|
|
|
|
dismiss() {
|
|
|
|
NativeModules.DialogAndroid.dismiss();
|
|
|
|
}
|
2016-03-11 22:54:44 +00:00
|
|
|
|
|
|
|
list(options, cb){
|
|
|
|
NativeModules.DialogAndroid.list(options, cb)
|
|
|
|
}
|
2015-11-01 17:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DialogAndroid;
|