snappymail/dev/View/Popup/Ask.js

114 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-07-05 03:19:24 +08:00
import { i18n } from 'Common/Translator';
2021-03-24 21:22:25 +08:00
import { isFunction } from 'Common/Utils';
import { addObservablesTo } from 'External/ko';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
2022-02-24 21:01:41 +08:00
export class AskPopupView extends AbstractViewPopup {
constructor() {
super('Ask');
2014-08-25 15:10:51 +08:00
addObservablesTo(this, {
askDesc: '',
yesButton: '',
2022-02-08 02:35:24 +08:00
noButton: '',
username: '',
askUsername: false,
2022-02-08 02:35:24 +08:00
passphrase: '',
2023-02-02 19:39:21 +08:00
askPass: false,
remember: true,
askRemeber: false
});
2014-08-21 23:08:34 +08:00
this.fYesAction = null;
this.fNoAction = null;
2022-02-08 02:35:24 +08:00
this.focusOnShow = true;
}
yesClick() {
this.close();
2022-10-19 05:32:47 +08:00
isFunction(this.fYesAction) && this.fYesAction(this);
2016-06-30 08:02:45 +08:00
}
noClick() {
this.close();
2022-10-19 05:32:47 +08:00
isFunction(this.fNoAction) && this.fNoAction(this);
2016-06-30 08:02:45 +08:00
}
/**
* @param {string} sAskDesc
* @param {Function=} fYesFunc
* @param {Function=} fNoFunc
2022-02-08 02:35:24 +08:00
* @param {boolean=} focusOnShow = true
* @returns {void}
*/
onShow(sAskDesc, fYesFunc = null, fNoFunc = null, focusOnShow = true, ask = 0, btnText = '') {
this.askDesc(sAskDesc || '');
this.askUsername(ask & 2);
this.askPass(ask & 1);
2023-02-02 19:39:21 +08:00
this.askRemeber(ask & 4);
this.username('');
2022-02-08 02:35:24 +08:00
this.passphrase('');
2023-02-02 19:39:21 +08:00
this.remember(true);
this.yesButton(i18n(btnText || 'GLOBAL/YES'));
this.noButton(i18n(ask ? 'GLOBAL/CANCEL' : 'GLOBAL/NO'));
2021-11-03 21:58:34 +08:00
this.fYesAction = fYesFunc;
this.fNoAction = fNoFunc;
this.focusOnShow = focusOnShow
? (ask ? 'input[type="'+(ask&2?'text':'password')+'"]' : '.buttonYes')
: '';
}
afterShow() {
2022-02-08 02:35:24 +08:00
this.focusOnShow && this.querySelector(this.focusOnShow).focus();
}
onClose() {
this.noClick();
return false;
}
onBuild() {
// shortcuts.add('tab', 'shift', 'Ask', () => {
shortcuts.add('tab,arrowright,arrowleft', '', 'Ask', () => {
let yes = this.querySelector('.buttonYes'),
no = this.querySelector('.buttonNo');
if (yes.matches(':focus')) {
no.focus();
return false;
} else if (no.matches(':focus')) {
yes.focus();
return false;
}
});
}
}
2016-06-30 08:02:45 +08:00
AskPopupView.password = function(sAskDesc, btnText) {
return new Promise(resolve => {
this.showModal([
2022-02-08 02:35:24 +08:00
sAskDesc,
2023-02-02 19:39:21 +08:00
view => resolve({password:view.passphrase(), remember:view.remember()}),
() => resolve(null),
2022-02-08 02:35:24 +08:00
true,
2023-02-02 19:39:21 +08:00
5,
btnText
]);
});
}
AskPopupView.credentials = function(sAskDesc, btnText) {
return new Promise(resolve => {
this.showModal([
sAskDesc,
2023-02-02 19:39:21 +08:00
view => resolve({username:view.username(), password:view.passphrase(), remember:view.remember()}),
() => resolve(null),
true,
3,
btnText
]);
});
}