keeweb/app/scripts/views/key-change-view.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict';
var Backbone = require('backbone'),
SecureInput = require('../comp/secure-input'),
Alerts = require('../comp/alerts'),
Locale = require('../util/locale'),
Keys = require('../const/keys');
var KeyChangeView = Backbone.View.extend({
template: require('templates/key-change.hbs'),
events: {
'keydown .key-change__pass': 'inputKeydown',
'click .key-change__keyfile': 'keyFileClicked',
'change .key-change__file': 'keyFileSelected',
'click .key-change__btn-ok': 'accept',
'click .key-change__btn-cancel': 'cancel'
},
passwordInput: null,
inputEl: null,
initialize: function() {
this.passwordInput = new SecureInput();
},
render: function() {
2016-07-03 18:46:43 +02:00
this.keyFileName = this.model.file.get('keyFileName') || null;
this.keyFileData = null;
this.renderTemplate({
2016-07-03 18:46:43 +02:00
fileName: this.model.file.get('name'),
keyFileName: this.model.file.get('keyFileName'),
title: this.model.expired ? Locale.keyChangeTitleExpired : Locale.keyChangeTitleRemote,
message: this.model.expired ? Locale.keyChangeMessageExpired : Locale.keyChangeMessageRemote
});
this.$el.find('.key-change__keyfile-name').text(this.keyFileName ? ': ' + this.keyFileName : '');
this.inputEl = this.$el.find('.key-change__pass');
this.passwordInput.reset();
this.passwordInput.setElement(this.inputEl);
},
remove: function() {
Backbone.View.prototype.remove.apply(this, arguments);
},
inputKeydown: function(e) {
var code = e.keyCode || e.which;
if (code === Keys.DOM_VK_RETURN) {
this.accept();
} else if (code === Keys.DOM_VK_A) {
e.stopImmediatePropagation();
}
},
keyFileClicked: function() {
if (this.keyFileName) {
this.keyFileName = null;
this.keyFile = null;
this.$el.find('.key-change__keyfile-name').html('');
}
this.$el.find('.key-change__file').val(null).click();
this.inputEl.focus();
},
keyFileSelected: function(e) {
var file = e.target.files[0];
if (file) {
var reader = new FileReader();
2016-07-17 13:30:38 +02:00
reader.onload = e => {
this.keyFileName = file.name;
this.keyFileData = e.target.result;
this.$el.find('.key-change__keyfile-name').text(': ' + this.keyFileName);
2016-07-17 13:30:38 +02:00
};
reader.onerror = () => {
Alerts.error({ header: Locale.openFailedRead });
};
reader.readAsArrayBuffer(file);
} else {
this.$el.find('.key-change__keyfile-name').html('');
}
this.inputEl.focus();
},
accept: function() {
this.trigger('accept', {
2016-07-03 18:46:43 +02:00
file: this.model.file,
expired: this.model.expired,
password: this.passwordInput.value,
keyFileName: this.keyFileName,
keyFileData: this.keyFileData
});
},
cancel: function() {
this.trigger('cancel');
}
});
module.exports = KeyChangeView;