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

166 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const FieldViewText = require('./field-view-text');
const FieldView = require('./field-view');
const Keys = require('../../const/keys');
const kdbxweb = require('kdbxweb');
const Tip = require('../../util/tip');
const Locale = require('../../util/locale');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const FieldViewCustom = FieldViewText.extend({
2015-10-17 23:49:24 +02:00
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);
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);
2019-08-16 23:05:39 +02:00
$('<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));
let securityTipTitle = Locale.detLockField;
if (this.isProtected) {
securityTipTitle = Locale.detUnlockField;
}
const securityTip = new Tip($(this.valueEl).find('.details__field-value-btn'), {
title: securityTipTitle
});
securityTip.init();
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') {
if (this.isProtected) {
newVal = kdbxweb.ProtectedValue.fromString(newVal);
} else {
newVal = $.trim(newVal);
2015-11-08 19:24:37 +01:00
}
}
FieldView.prototype.endEdit.call(this, newVal, extra);
2016-01-16 13:35:34 +01:00
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) {
2017-01-31 07:50:28 +01:00
const text = emptyTitle ? '' : this.model.title || '';
2015-10-17 23:49:24 +02:00
this.labelInput = $('<input/>');
this.labelEl.html('').append(this.labelInput);
2019-08-16 23:05:39 +02:00
this.labelInput
.attr({ autocomplete: 'off', spellcheck: 'false' })
.val(text)
.focus()[0]
.setSelectionRange(text.length, text.length);
2015-10-17 23:49:24 +02:00
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;
} else if (newTitle === '') {
this.trigger('change', {
field: '$' + this.model.title,
val: ''
});
2016-01-16 13:35:34 +01:00
}
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-03-05 09:35:22 +01:00
if (this.model.newField) {
2016-01-16 13:35:34 +01:00
this.startEditTitle(true);
2016-03-05 09:35:22 +01:00
} else if (this.editing) {
this.startEditTitle();
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();
2017-01-31 07:50:28 +01:00
const code = e.keyCode || e.which;
2015-10-17 23:49:24 +02:00
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());
}
2019-08-16 23:05:39 +02:00
this.setTimeout(function() {
this.input.focus();
});
2015-10-17 23:49:24 +02:00
}
});
module.exports = FieldViewCustom;