2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import _ from '_';
|
|
|
|
import ko from 'ko';
|
|
|
|
import key from 'key';
|
|
|
|
import $ from '$';
|
|
|
|
|
|
|
|
import {createCommand, pString, log} from 'Common/Utils';
|
|
|
|
import {KeyState, Magics} from 'Common/Enums';
|
|
|
|
|
|
|
|
import {view, ViewType} from 'Knoin/Knoin';
|
|
|
|
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
|
|
|
|
|
|
|
|
@view({
|
|
|
|
name: 'View/Popup/MessageOpenPgp',
|
|
|
|
type: ViewType.Popup,
|
|
|
|
templateID: 'PopupsMessageOpenPgp'
|
|
|
|
})
|
|
|
|
class MessageOpenPgpPopupView extends AbstractViewNext
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.notification = ko.observable('');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.selectedKey = ko.observable(null);
|
|
|
|
this.privateKeys = ko.observableArray([]);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.password = ko.observable('');
|
|
|
|
this.password.focus = ko.observable(false);
|
|
|
|
this.buttonFocus = ko.observable(false);
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest = ko.observable(false);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
// commands
|
|
|
|
this.doCommand = createCommand(() => {
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest(true);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
_.delay(() => {
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
let privateKey = null;
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
try
|
2015-06-23 05:33:27 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
if (this.resultCallback && this.selectedKey())
|
2015-06-23 05:33:27 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
const privateKeys = this.selectedKey().getNativeKeys();
|
|
|
|
privateKey = privateKeys && privateKeys[0] ? privateKeys[0] : null;
|
|
|
|
|
|
|
|
if (privateKey)
|
2015-06-23 05:33:27 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!privateKey.decrypt(pString(this.password())))
|
|
|
|
{
|
|
|
|
log('Error: Private key cannot be decrypted');
|
|
|
|
privateKey = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e)
|
2015-06-23 05:33:27 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
log(e);
|
|
|
|
privateKey = null;
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
else
|
2015-06-23 05:33:27 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
log('Error: Private key cannot be found');
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
log(e);
|
|
|
|
privateKey = null;
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest(false);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.cancelCommand();
|
|
|
|
this.resultCallback(privateKey);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
}, Magics.Time100ms);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
}, () => !this.submitRequest());
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.sDefaultKeyScope = KeyState.PopupMessageOpenPGP;
|
|
|
|
}
|
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('');
|
|
|
|
this.password.focus(false);
|
|
|
|
this.buttonFocus(false);
|
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) {
|
|
|
|
key('tab,shift+tab', KeyState.PopupMessageOpenPGP, () => {
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
switch (true)
|
|
|
|
{
|
|
|
|
case this.password.focus():
|
|
|
|
this.buttonFocus(true);
|
|
|
|
break;
|
|
|
|
case this.buttonFocus():
|
|
|
|
this.password.focus(true);
|
|
|
|
break;
|
|
|
|
// no default
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
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
|
|
|
});
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
oDom
|
|
|
|
.on('click', '.key-list__item', function() { // eslint-disable-line prefer-arrow-callback
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
oDom.find('.key-list__item .key-list__item__radio')
|
|
|
|
.addClass('icon-radio-unchecked')
|
|
|
|
.removeClass('icon-radio-checked');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
$(this).find('.key-list__item__radio') // eslint-disable-line no-invalid-this
|
|
|
|
.removeClass('icon-radio-unchecked')
|
|
|
|
.addClass('icon-radio-checked');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
self.selectedKey(ko.dataFor(this)); // eslint-disable-line no-invalid-this
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
self.password.focus(true);
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
onShowWithDelay() {
|
|
|
|
this.password.focus(true);
|
|
|
|
// this.buttonFocus(true);
|
|
|
|
}
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (this.viewModelDom)
|
|
|
|
{
|
|
|
|
this.viewModelDom.find('.key-list__item').first().click();
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
module.exports = MessageOpenPgpPopupView;
|