keeweb/app/scripts/views/fields/field-view-text.js

169 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
2016-01-16 13:35:34 +01:00
var Backbone = require('backbone'),
FieldView = require('./field-view'),
2015-10-20 21:58:07 +02:00
GeneratorView = require('../generator-view'),
2015-11-17 22:49:12 +01:00
KeyHandler = require('../../comp/key-handler'),
2015-10-17 23:49:24 +02:00
Keys = require('../../const/keys'),
PasswordGenerator = require('../../util/password-generator'),
2015-10-17 23:49:24 +02:00
kdbxweb = require('kdbxweb');
var FieldViewText = FieldView.extend({
renderValue: function(value) {
2016-07-17 13:30:38 +02:00
return value && value.isProtected ? PasswordGenerator.present(value.textLength)
: _.escape(value || '').replace(/\n/g, '<br/>');
2015-10-17 23:49:24 +02:00
},
getEditValue: function(value) {
2016-01-16 15:19:33 +01:00
return value && value.isProtected ? value.getText() : value || '';
2015-10-17 23:49:24 +02:00
},
startEdit: function() {
var text = this.getEditValue(this.value);
2016-01-17 13:23:07 +01:00
var isProtected = !!(this.value && this.value.isProtected);
this.$el.toggleClass('details__field--protected', isProtected);
2015-10-17 23:49:24 +02:00
this.input = $(document.createElement(this.model.multiline ? 'textarea' : 'input'));
this.valueEl.html('').append(this.input);
this.input.attr({ autocomplete: 'off', spellcheck: 'false' })
.val(text).focus()[0].setSelectionRange(text.length, text.length);
this.input.bind({
input: this.fieldValueInput.bind(this),
keydown: this.fieldValueKeydown.bind(this),
2015-10-20 21:58:07 +02:00
keypress: this.fieldValueInput.bind(this),
2016-01-16 13:35:34 +01:00
click: this.fieldValueInputClick.bind(this),
mousedown: this.fieldValueInputMouseDown.bind(this)
2015-10-17 23:49:24 +02:00
});
2016-01-16 13:35:34 +01:00
this.listenTo(Backbone, 'click', this.fieldValueBlur);
2015-10-17 23:49:24 +02:00
if (this.model.multiline) {
this.setInputHeight();
}
2015-11-08 19:24:37 +01:00
if (this.model.canGen) {
$('<div/>').addClass('details__field-value-btn details__field-value-btn-gen').appendTo(this.valueEl)
2015-10-20 21:58:07 +02:00
.click(this.showGeneratorClick.bind(this))
.mousedown(this.showGenerator.bind(this));
}
},
showGeneratorClick: function(e) {
e.stopPropagation();
if (!this.gen) {
this.input.focus();
}
},
showGenerator: function() {
if (this.gen) {
this.hideGenerator();
} else {
var fieldRect = this.input[0].getBoundingClientRect();
2016-02-15 22:06:11 +01:00
this.gen = new GeneratorView({model: {pos: {left: fieldRect.left, top: fieldRect.bottom}, password: this.value}}).render();
2015-10-20 21:58:07 +02:00
this.gen.once('remove', this.generatorClosed.bind(this));
this.gen.once('result', this.generatorResult.bind(this));
}
},
hideGenerator: function() {
if (this.gen) {
var gen = this.gen;
delete this.gen;
gen.remove();
}
},
generatorClosed: function() {
if (this.gen) {
delete this.gen;
this.endEdit();
}
},
generatorResult: function(password) {
if (this.gen) {
delete this.gen;
this.endEdit(password);
}
2015-10-17 23:49:24 +02:00
},
setInputHeight: function() {
2016-02-14 15:09:57 +01:00
var MinHeight = 18;
2015-10-17 23:49:24 +02:00
this.input.height(MinHeight);
var newHeight = this.input[0].scrollHeight;
if (newHeight <= MinHeight) {
newHeight = MinHeight;
} else {
newHeight += 2;
}
this.input.height(newHeight);
},
2016-01-16 13:35:34 +01:00
fieldValueBlur: function() {
if (!this.gen && this.input) {
this.endEdit(this.input.val());
2015-10-20 21:58:07 +02:00
}
2015-10-17 23:49:24 +02:00
},
fieldValueInput: function(e) {
e.stopPropagation();
if (this.model.multiline) {
this.setInputHeight();
}
},
2015-10-20 21:58:07 +02:00
fieldValueInputClick: function() {
if (this.gen) {
this.hideGenerator();
}
},
2016-01-16 13:35:34 +01:00
fieldValueInputMouseDown: function(e) {
e.stopPropagation();
},
2015-10-17 23:49:24 +02:00
fieldValueKeydown: function(e) {
2015-11-17 22:49:12 +01:00
KeyHandler.reg();
2015-10-17 23:49:24 +02:00
e.stopPropagation();
var code = e.keyCode || e.which;
if (code === Keys.DOM_VK_RETURN) {
2016-03-12 12:25:25 +01:00
if (!this.model.multiline || (!e.altKey && !e.shiftKey && !e.ctrlKey)) {
let value = this.gen ? this.gen.password : e.target.value;
2016-01-16 13:35:34 +01:00
this.stopListening(Backbone, 'click', this.fieldValueBlur);
this.endEdit(value);
2015-10-17 23:49:24 +02:00
}
} else if (code === Keys.DOM_VK_ESCAPE) {
2016-01-16 13:35:34 +01:00
this.stopListening(Backbone, 'click', this.fieldValueBlur);
2015-10-17 23:49:24 +02:00
this.endEdit();
} else if (code === Keys.DOM_VK_TAB) {
e.preventDefault();
2016-01-16 13:35:34 +01:00
this.stopListening(Backbone, 'click', this.fieldValueBlur);
2015-10-17 23:49:24 +02:00
this.endEdit(e.target.value, { tab: { field: this.model.name, prev: e.shiftKey } });
} else if (code === Keys.DOM_VK_G) {
e.preventDefault();
this.showGenerator();
2015-10-17 23:49:24 +02:00
}
},
endEdit: function(newVal, extra) {
2015-10-20 21:58:07 +02:00
if (this.gen) {
this.hideGenerator();
}
2015-10-17 23:49:24 +02:00
if (!this.editing) {
return;
}
delete this.input;
2016-01-16 13:35:34 +01:00
this.stopListening(Backbone, 'click', this.fieldValueBlur);
2015-10-17 23:49:24 +02:00
if (typeof newVal === 'string' && this.value instanceof kdbxweb.ProtectedValue) {
newVal = kdbxweb.ProtectedValue.fromString(newVal);
}
if (typeof newVal === 'string') {
newVal = $.trim(newVal);
}
FieldView.prototype.endEdit.call(this, newVal, extra);
2015-10-20 21:58:07 +02:00
},
render: function() {
FieldView.prototype.render.call(this);
2015-10-17 23:49:24 +02:00
}
});
module.exports = FieldViewText;