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

169 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-05-08 11:38:23 +02:00
import * as kdbxweb from 'kdbxweb';
2019-09-15 14:16:32 +02:00
import { Keys } from 'const/keys';
import { Locale } from 'util/locale';
import { Tip } from 'util/ui/tip';
import { FieldView } from 'views/fields/field-view';
import { FieldViewText } from 'views/fields/field-view-text';
2015-10-17 23:49:24 +02:00
2019-09-16 19:55:06 +02:00
class FieldViewCustom extends FieldViewText {
2019-09-20 06:30:04 +02:00
constructor(model, options) {
super(model, options);
this.events = {
...this.events,
'mousedown .details__field-label': 'fieldLabelMousedown'
};
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
startEdit() {
2019-09-16 19:55:06 +02:00
super.startEdit();
2015-10-17 23:49:24 +02:00
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();
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
endEdit(newVal, extra) {
2016-01-16 13:35:34 +01:00
this.$el.removeClass('details__field--can-edit-title');
2019-09-17 23:44:17 +02:00
extra = { ...extra };
2021-04-26 13:26:27 +02:00
if (this.model.titleChanged) {
2016-01-16 13:35:34 +01:00
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;
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;
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
startEditTitle(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/>');
2020-05-09 20:15:46 +02:00
this.labelEl.empty().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),
keyup: this.fieldLabelKeyup.bind(this),
2015-10-17 23:49:24 +02:00
keypress: this.fieldLabelInput.bind(this),
mousedown: this.fieldLabelInputClick.bind(this),
click: this.fieldLabelInputClick.bind(this)
});
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
endEditTitle(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 === '') {
2019-09-16 19:55:06 +02:00
this.emit('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
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
fieldLabelClick(e) {
2015-10-17 23:49:24 +02:00
e.stopImmediatePropagation();
2021-04-26 12:49:25 +02:00
2021-04-26 13:26:27 +02:00
if (this.model.newField) {
2016-01-16 13:35:34 +01:00
this.startEditTitle(true);
2021-04-26 13:26:27 +02:00
} else if (this.editing) {
2016-03-05 09:35:22 +01:00
this.startEditTitle();
2015-10-17 23:49:24 +02:00
} else {
2019-09-16 19:55:06 +02:00
super.fieldLabelClick.call(this, e);
2015-10-17 23:49:24 +02:00
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
fieldLabelMousedown(e) {
2016-01-16 13:35:34 +01:00
if (this.editing) {
e.stopPropagation();
2015-10-17 23:49:24 +02:00
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
fieldValueBlur() {
2016-01-16 13:35:34 +01:00
if (this.labelInput) {
this.endEditTitle(this.labelInput.val());
}
if (this.input) {
this.endEdit(this.input.val());
2015-11-08 19:24:37 +01:00
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
fieldLabelInput(e) {
2015-10-17 23:49:24 +02:00
e.stopPropagation();
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
fieldLabelInputClick(e) {
2015-10-17 23:49:24 +02:00
e.stopPropagation();
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
fieldLabelKeydown(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;
if (code === Keys.DOM_VK_ESCAPE) {
2015-10-17 23:49:24 +02:00
this.endEditTitle();
} else if (code === Keys.DOM_VK_TAB) {
e.preventDefault();
this.endEditTitle(e.target.value);
}
2019-09-16 19:55:06 +02:00
}
2015-11-08 19:24:37 +01:00
fieldLabelKeyup(e) {
const code = e.keyCode || e.which;
if (code === Keys.DOM_VK_RETURN) {
this.endEditTitle(e.target.value);
}
}
2019-08-18 10:17:09 +02:00
fieldValueInputClick() {
2016-01-16 13:35:34 +01:00
if (this.labelInput) {
this.endEditTitle(this.labelInput.val());
}
2019-09-16 19:55:06 +02:00
super.fieldValueInputClick.call(this);
}
2016-01-16 13:35:34 +01:00
2019-08-18 10:17:09 +02:00
protectBtnClick(e) {
2015-11-08 19:24:37 +01:00
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-09-15 12:12:00 +02:00
setTimeout(() => this.input.focus());
2015-10-17 23:49:24 +02:00
}
2019-09-16 19:55:06 +02:00
}
2015-10-17 23:49:24 +02:00
2019-09-15 14:16:32 +02:00
export { FieldViewCustom };