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

156 lines
5.0 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'),
FieldViewText = require('./field-view-text'),
2015-11-08 19:24:37 +01:00
FieldView = require('./field-view'),
Keys = require('../../const/keys'),
2016-01-16 13:35:34 +01:00
Locale = require('../../util/locale'),
2015-11-08 19:24:37 +01:00
kdbxweb = require('kdbxweb');
2015-10-17 23:49:24 +02:00
var FieldViewCustom = FieldViewText.extend({
events: {
2015-11-08 19:24:37 +01:00
'mousedown .details__field-label': 'fieldLabelMousedown'
2015-10-17 23:49:24 +02:00
},
initialize: function() {
_.extend(this.events, FieldViewText.prototype.events);
},
startEdit: function() {
FieldViewText.prototype.startEdit.call(this);
2016-01-16 13:35:34 +01:00
if (this.model.newField && this.model.title === Locale.detAddField) {
this.model.title = this.model.newField;
2015-10-17 23:49:24 +02:00
this.$el.find('.details__field-label').text(this.model.newField);
}
this.$el.addClass('details__field--can-edit-title');
2015-11-08 19:24:37 +01:00
if (this.isProtected === undefined) {
this.isProtected = this.value instanceof kdbxweb.ProtectedValue;
}
2016-01-17 13:23:07 +01:00
this.$el.toggleClass('details__field--protected', this.isProtected);
$('<div/>').addClass('details__field-value-btn details__field-value-btn-protect')
2015-11-08 19:24:37 +01:00
.appendTo(this.valueEl)
.mousedown(this.protectBtnClick.bind(this));
2015-10-17 23:49:24 +02:00
},
endEdit: function(newVal, extra) {
2016-01-16 13:35:34 +01:00
this.$el.removeClass('details__field--can-edit-title');
extra = _.extend({}, extra);
if (this.model.titleChanged || this.model.newField) {
extra.newField = this.model.title;
2015-10-17 23:49:24 +02:00
}
2015-11-08 19:24:37 +01:00
if (!this.editing) {
return;
}
delete this.input;
2016-01-16 13:35:34 +01:00
this.stopListening(Backbone, 'click', this.fieldValueBlur);
2015-11-08 19:24:37 +01:00
if (typeof newVal === 'string') {
newVal = $.trim(newVal);
if (this.isProtected) {
newVal = kdbxweb.ProtectedValue.fromString(newVal);
}
}
FieldView.prototype.endEdit.call(this, newVal, extra);
2016-01-16 13:35:34 +01:00
if (!newVal && this.model.newField) {
this.model.title = Locale.detAddField;
this.$el.find('.details__field-label').text(this.model.title);
}
if (this.model.titleChanged) {
delete this.model.titleChanged;
}
2015-10-17 23:49:24 +02:00
},
2016-01-16 13:35:34 +01:00
startEditTitle: function(emptyTitle) {
var text = emptyTitle ? '' : this.model.title || '';
2015-10-17 23:49:24 +02:00
this.labelInput = $('<input/>');
this.labelEl.html('').append(this.labelInput);
this.labelInput.attr({ autocomplete: 'off', spellcheck: 'false' })
.val(text).focus()[0].setSelectionRange(text.length, text.length);
this.labelInput.bind({
input: this.fieldLabelInput.bind(this),
keydown: this.fieldLabelKeydown.bind(this),
keypress: this.fieldLabelInput.bind(this),
mousedown: this.fieldLabelInputClick.bind(this),
click: this.fieldLabelInputClick.bind(this)
});
},
endEditTitle: function(newTitle) {
2016-01-16 13:35:34 +01:00
if (newTitle && newTitle !== this.model.title) {
this.model.title = newTitle;
this.model.titleChanged = true;
}
this.$el.find('.details__field-label').text(this.model.title);
delete this.labelInput;
if (this.editing && this.input) {
this.input.focus();
2015-10-17 23:49:24 +02:00
}
},
fieldLabelClick: function(e) {
e.stopImmediatePropagation();
2016-01-16 13:35:34 +01:00
if (this.editing) {
2015-10-17 23:49:24 +02:00
this.startEditTitle();
2016-01-16 13:35:34 +01:00
} else if (this.model.newField) {
this.edit();
this.startEditTitle(true);
2015-10-17 23:49:24 +02:00
} else {
FieldViewText.prototype.fieldLabelClick.call(this, e);
}
},
2016-01-16 13:35:34 +01:00
fieldLabelMousedown: function(e) {
if (this.editing) {
e.stopPropagation();
2015-10-17 23:49:24 +02:00
}
},
2016-01-16 13:35:34 +01:00
fieldValueBlur: function() {
if (this.labelInput) {
this.endEditTitle(this.labelInput.val());
}
if (this.input) {
this.endEdit(this.input.val());
2015-11-08 19:24:37 +01:00
}
2015-10-17 23:49:24 +02:00
},
fieldLabelInput: function(e) {
e.stopPropagation();
},
fieldLabelInputClick: function(e) {
e.stopPropagation();
},
fieldLabelKeydown: function(e) {
2016-01-16 13:35:34 +01:00
e.stopPropagation();
2015-10-17 23:49:24 +02:00
var code = e.keyCode || e.which;
if (code === Keys.DOM_VK_RETURN) {
this.endEditTitle(e.target.value);
} else if (code === Keys.DOM_VK_ESCAPE) {
this.endEditTitle();
} else if (code === Keys.DOM_VK_TAB) {
e.preventDefault();
this.endEditTitle(e.target.value);
}
2015-11-08 19:24:37 +01:00
},
2016-01-16 13:35:34 +01:00
fieldValueInputClick: function() {
if (this.labelInput) {
this.endEditTitle(this.labelInput.val());
}
FieldViewText.prototype.fieldValueInputClick.call(this);
},
2015-11-08 19:24:37 +01:00
protectBtnClick: function(e) {
e.stopPropagation();
this.isProtected = !this.isProtected;
2016-01-17 13:23:07 +01:00
this.$el.toggleClass('details__field--protected', this.isProtected);
2016-01-16 13:35:34 +01:00
if (this.labelInput) {
this.endEditTitle(this.labelInput.val());
}
this.setTimeout(function() { this.input.focus(); });
2015-10-17 23:49:24 +02:00
}
});
module.exports = FieldViewCustom;