snappymail/dev/View/Popup/MessageOpenPgp.js

178 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
key = require('key'),
$ = require('$'),
2016-06-30 08:02:45 +08:00
Utils = require('Common/Utils'),
Enums = require('Common/Enums'),
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function MessageOpenPgpPopupView()
{
AbstractView.call(this, 'Popups', 'PopupsMessageOpenPgp');
2016-06-30 08:02:45 +08:00
this.notification = ko.observable('');
2016-06-30 08:02:45 +08:00
this.selectedKey = ko.observable(null);
this.privateKeys = ko.observableArray([]);
2016-06-30 08:02:45 +08:00
this.password = ko.observable('');
this.password.focus = ko.observable(false);
this.buttonFocus = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.resultCallback = null;
2016-06-30 08:02:45 +08:00
this.submitRequest = ko.observable(false);
2016-06-30 08:02:45 +08:00
// commands
this.doCommand = Utils.createCommand(this, function() {
2016-06-30 08:02:45 +08:00
this.submitRequest(true);
2016-06-30 08:02:45 +08:00
_.delay(_.bind(function() {
var
oPrivateKeys = [],
2016-06-30 08:02:45 +08:00
oPrivateKey = null;
try
{
if (this.resultCallback && this.selectedKey())
{
oPrivateKeys = this.selectedKey().getNativeKeys();
oPrivateKey = oPrivateKeys && oPrivateKeys[0] ? oPrivateKeys[0] : null;
if (oPrivateKey)
{
try
{
if (!oPrivateKey.decrypt(Utils.pString(this.password())))
{
2015-09-02 03:50:30 +08:00
Utils.log('Error: Private key cannot be decrypted');
oPrivateKey = null;
}
}
catch (e)
{
2015-09-02 03:50:30 +08:00
Utils.log(e);
oPrivateKey = null;
}
}
2015-09-02 03:50:30 +08:00
else
{
Utils.log('Error: Private key cannot be found');
}
}
}
2015-09-02 03:50:30 +08:00
catch (e)
{
2015-09-02 03:50:30 +08:00
Utils.log(e);
oPrivateKey = null;
}
this.submitRequest(false);
this.cancelCommand();
this.resultCallback(oPrivateKey);
2016-06-30 08:02:45 +08:00
}, this), 100);
2016-06-30 08:02:45 +08:00
}, function() {
return !this.submitRequest();
});
2016-06-30 08:02:45 +08:00
this.sDefaultKeyScope = Enums.KeyState.PopupMessageOpenPGP;
2016-06-30 08:02:45 +08:00
kn.constructorEnd(this);
}
2016-06-30 08:02:45 +08:00
kn.extendAsViewModel(['View/Popup/MessageOpenPgp'], MessageOpenPgpPopupView);
_.extend(MessageOpenPgpPopupView.prototype, AbstractView.prototype);
2016-06-30 08:02:45 +08:00
MessageOpenPgpPopupView.prototype.clearPopup = function()
{
this.notification('');
2016-06-30 08:02:45 +08:00
this.password('');
this.password.focus(false);
this.buttonFocus(false);
2016-06-30 08:02:45 +08:00
this.selectedKey(false);
this.submitRequest(false);
2016-06-30 08:02:45 +08:00
this.resultCallback = null;
this.privateKeys([]);
};
2016-06-30 08:02:45 +08:00
MessageOpenPgpPopupView.prototype.onBuild = function(oDom)
{
key('tab,shift+tab', Enums.KeyState.PopupMessageOpenPGP, _.bind(function() {
2016-06-30 08:02:45 +08:00
switch (true)
{
case this.password.focus():
this.buttonFocus(true);
break;
case this.buttonFocus():
this.password.focus(true);
break;
// no default
}
2016-06-30 08:02:45 +08:00
return false;
2016-06-30 08:02:45 +08:00
}, this));
2016-06-30 08:02:45 +08:00
var self = this;
2016-06-30 08:02:45 +08:00
oDom
.on('click', '.key-list__item', function() {
2016-06-30 08:02:45 +08:00
oDom.find('.key-list__item .key-list__item__radio')
.addClass('icon-radio-unchecked')
.removeClass('icon-radio-checked');
2016-06-30 08:02:45 +08:00
$(this).find('.key-list__item__radio')
.removeClass('icon-radio-unchecked')
.addClass('icon-radio-checked');
2016-06-30 08:02:45 +08:00
self.selectedKey(ko.dataFor(this));
2016-06-30 08:02:45 +08:00
self.password.focus(true);
});
};
2016-06-30 08:02:45 +08:00
MessageOpenPgpPopupView.prototype.onHideWithDelay = function()
{
this.clearPopup();
};
2016-06-30 08:02:45 +08:00
MessageOpenPgpPopupView.prototype.onShowWithDelay = function()
{
this.password.focus(true);
// this.buttonFocus(true);
2016-06-30 08:02:45 +08:00
};
2016-06-30 08:02:45 +08:00
MessageOpenPgpPopupView.prototype.onShow = function(fCallback, aPrivateKeys)
{
this.clearPopup();
2016-06-30 08:02:45 +08:00
this.resultCallback = fCallback;
this.privateKeys(aPrivateKeys);
2016-06-30 08:02:45 +08:00
if (this.viewModelDom)
{
this.viewModelDom.find('.key-list__item').first().click();
}
};
2016-06-30 08:02:45 +08:00
module.exports = MessageOpenPgpPopupView;