2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
|
|
|
|
2020-08-12 06:25:36 +08:00
|
|
|
import { pString } from 'Common/Utils';
|
2021-03-16 23:06:16 +08:00
|
|
|
import { Scope } from 'Common/Enums';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-02-19 19:09:20 +08:00
|
|
|
import { decorateKoCommands } from 'Knoin/Knoin';
|
2021-01-24 17:25:23 +08:00
|
|
|
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
|
|
|
|
|
|
|
class MessageOpenPgpPopupView extends AbstractViewPopup {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2021-01-24 17:25:23 +08:00
|
|
|
super('MessageOpenPgp');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
this.addObservables({
|
|
|
|
notification: '',
|
|
|
|
selectedKey: null,
|
|
|
|
password: '',
|
|
|
|
submitRequest: false
|
|
|
|
});
|
2021-01-22 19:23:20 +08:00
|
|
|
this.privateKeys = ko.observableArray();
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.resultCallback = null;
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2021-02-19 19:09:20 +08:00
|
|
|
decorateKoCommands(this, {
|
|
|
|
doCommand: self => !self.submitRequest()
|
|
|
|
});
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
doCommand() {
|
|
|
|
this.submitRequest(true);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2020-07-23 02:28:25 +08:00
|
|
|
setTimeout(() => {
|
2016-09-10 06:38:16 +08:00
|
|
|
let privateKey = null;
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
|
|
|
if (this.resultCallback && this.selectedKey()) {
|
2016-09-10 06:38:16 +08:00
|
|
|
const privateKeys = this.selectedKey().getNativeKeys();
|
|
|
|
privateKey = privateKeys && privateKeys[0] ? privateKeys[0] : null;
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (privateKey) {
|
|
|
|
try {
|
|
|
|
if (!privateKey.decrypt(pString(this.password()))) {
|
2020-08-12 06:25:36 +08:00
|
|
|
console.log('Error: Private key cannot be decrypted');
|
2016-08-17 06:01:20 +08:00
|
|
|
privateKey = null;
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {
|
2020-08-12 06:25:36 +08:00
|
|
|
console.log(e);
|
2016-09-10 06:38:16 +08:00
|
|
|
privateKey = null;
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2020-08-12 06:25:36 +08:00
|
|
|
console.log('Error: Private key cannot be found');
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {
|
2020-08-12 06:25:36 +08:00
|
|
|
console.log(e);
|
2016-09-10 06:38:16 +08:00
|
|
|
privateKey = null;
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
this.submitRequest(false);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
this.cancelCommand();
|
|
|
|
this.resultCallback(privateKey);
|
2020-08-14 04:58:41 +08:00
|
|
|
}, 100);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearPopup() {
|
|
|
|
this.notification('');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.password('');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.selectedKey(false);
|
|
|
|
this.submitRequest(false);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.resultCallback = null;
|
|
|
|
this.privateKeys([]);
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onBuild(oDom) {
|
2021-03-16 23:06:16 +08:00
|
|
|
// shortcuts.add('tab', 'shift', Scope.MessageOpenPgp, () => {
|
|
|
|
shortcuts.add('tab', '', Scope.MessageOpenPgp, () => {
|
2020-08-22 15:35:03 +08:00
|
|
|
let btn = this.querySelector('.inputPassword');
|
|
|
|
if (btn.matches(':focus')) {
|
|
|
|
btn = this.querySelector('.buttonDo');
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2020-08-22 15:35:03 +08:00
|
|
|
btn.focus();
|
2016-08-17 06:01:20 +08:00
|
|
|
return false;
|
|
|
|
});
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
const self = this;
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2020-08-30 16:30:50 +08:00
|
|
|
oDom.addEventListener('click', event => {
|
|
|
|
const el = event.target.closestWithin('.key-list__item', oDom);
|
|
|
|
if (el) {
|
2021-02-12 00:46:29 +08:00
|
|
|
oDom.querySelectorAll('.key-list__item .key-list__item__radio').forEach(node =>
|
|
|
|
node.textContent = el === node ? '⦿' : '○'
|
|
|
|
);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2021-03-13 06:56:22 +08:00
|
|
|
self.selectedKey(ko.dataFor(el));
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2020-08-30 16:30:50 +08:00
|
|
|
// this.querySelector('.inputPassword').focus();
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
});
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onHideWithDelay() {
|
|
|
|
this.clearPopup();
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShow(fCallback, privateKeys) {
|
|
|
|
this.clearPopup();
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.resultCallback = fCallback;
|
|
|
|
this.privateKeys(privateKeys);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.viewModelDom) {
|
2021-11-08 04:19:48 +08:00
|
|
|
const el = this.querySelector('.key-list__item');
|
2020-08-27 21:45:47 +08:00
|
|
|
el && el.click();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { MessageOpenPgpPopupView, MessageOpenPgpPopupView as default };
|