2020-08-14 08:46:34 +02:00
|
|
|
import browser from '../../scripts/browser';
|
|
|
|
import dialog from '../dialog/dialog';
|
|
|
|
import globalize from '../../scripts/globalize';
|
2020-06-18 22:41:43 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
function replaceAll(str, find, replace) {
|
|
|
|
return str.split(find).join(replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
function nativeConfirm(options) {
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
|
|
|
title: '',
|
|
|
|
text: options
|
|
|
|
};
|
2020-03-21 18:14:08 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
const text = replaceAll(options.text || '', '<br/>', '\n');
|
|
|
|
const result = window.confirm(text);
|
2020-03-21 18:14:08 +01:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
if (result) {
|
|
|
|
return Promise.resolve();
|
|
|
|
} else {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 18:14:08 +01:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
function customConfirm(text, title) {
|
|
|
|
let options;
|
|
|
|
if (typeof text === 'string') {
|
|
|
|
options = {
|
|
|
|
title: title,
|
|
|
|
text: text
|
2020-03-21 18:14:08 +01:00
|
|
|
};
|
|
|
|
} else {
|
2020-10-18 15:18:15 +01:00
|
|
|
options = text;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
const items = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
items.push({
|
|
|
|
name: options.cancelText || globalize.translate('ButtonCancel'),
|
|
|
|
id: 'cancel',
|
|
|
|
type: 'cancel'
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
items.push({
|
|
|
|
name: options.confirmText || globalize.translate('ButtonOk'),
|
|
|
|
id: 'ok',
|
|
|
|
type: options.primary === 'delete' ? 'delete' : 'submit'
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
options.buttons = items;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
return dialog.show(options).then(result => {
|
|
|
|
if (result === 'ok') {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-18 15:18:15 +01:00
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseConfirm = browser.tv && window.confirm ? nativeConfirm : customConfirm;
|
|
|
|
|
|
|
|
export default baseConfirm;
|