keeweb/app/scripts/views/details/details-view.js

992 lines
35 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const kdbxweb = require('kdbxweb');
const GroupModel = require('../../models/group-model');
const AppSettingsModel = require('../../models/app-settings-model');
const Scrollable = require('../../mixins/scrollable');
const FieldViewText = require('../fields/field-view-text');
const FieldViewSelect = require('../fields/field-view-select');
const FieldViewAutocomplete = require('../fields/field-view-autocomplete');
const FieldViewDate = require('../fields/field-view-date');
const FieldViewTags = require('../fields/field-view-tags');
const FieldViewUrl = require('../fields/field-view-url');
const FieldViewReadOnly = require('../fields/field-view-read-only');
const FieldViewHistory = require('../fields/field-view-history');
const FieldViewCustom = require('../fields/field-view-custom');
const FieldViewOtp = require('../fields/field-view-otp');
const IconSelectView = require('../icon-select-view');
const DetailsHistoryView = require('./details-history-view');
const DetailsAttachmentView = require('./details-attachment-view');
const DetailsAddFieldView = require('./details-add-field-view');
const DetailsAutoTypeView = require('./details-auto-type-view');
const DropdownView = require('../../views/dropdown-view');
const Keys = require('../../const/keys');
const KeyHandler = require('../../comp/key-handler');
const Alerts = require('../../comp/alerts');
const CopyPaste = require('../../comp/copy-paste');
2019-01-01 20:18:33 +01:00
const OtpQrReader = require('../../comp/otp-qr-reader');
2017-01-31 07:50:28 +01:00
const AutoType = require('../../auto-type');
const Format = require('../../util/format');
const Locale = require('../../util/locale');
const Tip = require('../../util/tip');
2019-01-06 15:38:03 +01:00
const FileSaver = require('../../util/file-saver');
2017-01-31 07:50:28 +01:00
const Timeouts = require('../../const/timeouts');
2019-01-02 10:49:12 +01:00
const Copyable = require('../../mixins/copyable');
2017-01-31 07:50:28 +01:00
const DetailsView = Backbone.View.extend({
2015-12-16 22:50:45 +01:00
template: require('templates/details/details.hbs'),
emptyTemplate: require('templates/details/details-empty.hbs'),
groupTemplate: require('templates/details/details-group.hbs'),
2015-10-17 23:49:24 +02:00
fieldViews: null,
views: null,
2016-01-11 18:56:52 +01:00
passEditView: null,
2016-02-22 08:09:23 +01:00
userEditView: null,
urlEditView: null,
fieldCopyTip: null,
2015-10-17 23:49:24 +02:00
events: {
'click .details__colors-popup-item': 'selectColor',
'click .details__header-icon': 'toggleIcons',
'click .details__attachment': 'toggleAttachment',
'click .details__header-title': 'editTitle',
'click .details__history-link': 'showHistory',
'click .details__buttons-trash': 'moveToTrash',
2015-11-09 19:15:39 +01:00
'click .details__buttons-trash-del': 'deleteFromTrash',
2015-10-26 22:07:19 +01:00
'click .details__back-button': 'backClick',
'click .details__attachment-add': 'attachmentBtnClick',
'change .details__attachment-input-file': 'attachmentFileChange',
2015-10-17 23:49:24 +02:00
'dragover .details': 'dragover',
'dragleave .details': 'dragleave',
2016-07-30 11:25:22 +02:00
'drop .details': 'drop',
'contextmenu .details': 'contextMenu'
2015-10-17 23:49:24 +02:00
},
2019-08-16 23:05:39 +02:00
initialize: function() {
2015-10-17 23:49:24 +02:00
this.fieldViews = [];
this.views = {};
this.initScroll();
2016-08-21 19:39:02 +02:00
this.listenTo(Backbone, 'entry-selected', this.showEntry);
this.listenTo(Backbone, 'copy-password', this.copyPassword);
this.listenTo(Backbone, 'copy-user', this.copyUserName);
this.listenTo(Backbone, 'copy-url', this.copyUrl);
2016-07-16 15:30:17 +02:00
this.listenTo(Backbone, 'toggle-settings', this.settingsToggled);
2016-07-30 11:25:22 +02:00
this.listenTo(Backbone, 'context-menu-select', this.contextMenuSelect);
2016-08-24 21:30:43 +02:00
this.listenTo(Backbone, 'set-locale', this.render);
2019-01-01 20:18:33 +01:00
this.listenTo(OtpQrReader, 'qr-read', this.otpCodeRead);
this.listenTo(OtpQrReader, 'enter-manually', this.otpEnterManually);
KeyHandler.onKey(Keys.DOM_VK_C, this.copyPassword, this, KeyHandler.SHORTCUT_ACTION);
KeyHandler.onKey(Keys.DOM_VK_B, this.copyUserName, this, KeyHandler.SHORTCUT_ACTION);
KeyHandler.onKey(Keys.DOM_VK_U, this.copyUrl, this, KeyHandler.SHORTCUT_ACTION);
if (AutoType.enabled) {
KeyHandler.onKey(Keys.DOM_VK_T, this.autoType, this, KeyHandler.SHORTCUT_ACTION);
}
2016-09-04 09:26:54 +02:00
KeyHandler.onKey(Keys.DOM_VK_DELETE, this.deleteKeyPress, this, KeyHandler.SHORTCUT_ACTION, false, true);
KeyHandler.onKey(Keys.DOM_VK_BACK_SPACE, this.deleteKeyPress, this, KeyHandler.SHORTCUT_ACTION, false, true);
2015-10-17 23:49:24 +02:00
},
remove: function() {
KeyHandler.offKey(Keys.DOM_VK_C, this.copyPassword, this);
KeyHandler.offKey(Keys.DOM_VK_B, this.copyUserName, this);
KeyHandler.offKey(Keys.DOM_VK_U, this.copyUrl, this);
2015-10-17 23:49:24 +02:00
KeyHandler.offKey(Keys.DOM_VK_DELETE, this.deleteKeyPress, this, KeyHandler.SHORTCUT_ACTION);
KeyHandler.offKey(Keys.DOM_VK_BACK_SPACE, this.deleteKeyPress, this, KeyHandler.SHORTCUT_ACTION);
this.removeFieldViews();
Backbone.View.prototype.remove.call(this);
},
removeFieldViews: function() {
2016-07-17 13:30:38 +02:00
this.fieldViews.forEach(fieldView => fieldView.remove());
2015-10-17 23:49:24 +02:00
this.fieldViews = [];
2016-07-16 15:30:17 +02:00
this.hideFieldCopyTip();
2015-10-17 23:49:24 +02:00
},
2019-08-16 23:05:39 +02:00
render: function() {
this.removeScroll();
2015-10-17 23:49:24 +02:00
this.removeFieldViews();
2016-04-23 16:50:40 +02:00
this.removeInnerViews();
2015-10-17 23:49:24 +02:00
if (!this.model) {
this.$el.html(this.emptyTemplate());
return;
}
2015-11-08 22:25:00 +01:00
if (this.model instanceof GroupModel) {
this.$el.html(this.groupTemplate());
2016-01-10 21:58:21 +01:00
Tip.createTips(this.$el);
2015-11-08 22:25:00 +01:00
return;
}
2017-01-31 07:50:28 +01:00
const model = $.extend({ deleted: this.appModel.filter.trash }, this.model);
2015-11-09 19:15:39 +01:00
this.$el.html(this.template(model));
2016-01-10 21:58:21 +01:00
Tip.createTips(this.$el);
2015-10-17 23:49:24 +02:00
this.setSelectedColor(this.model.color);
2016-04-01 23:09:16 +02:00
this.model.initOtpGenerator();
2015-10-17 23:49:24 +02:00
this.addFieldViews();
2016-01-17 21:19:42 +01:00
this.createScroll({
2015-10-17 23:49:24 +02:00
root: this.$el.find('.details__body')[0],
scroller: this.$el.find('.scroller')[0],
2016-01-17 21:19:42 +01:00
bar: this.$el.find('.scroller__bar')[0]
2015-10-17 23:49:24 +02:00
});
this.$el.find('.details').removeClass('details--drag');
this.dragging = false;
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
this.pageResized();
2016-01-13 18:46:43 +01:00
this.showCopyTip();
2015-10-17 23:49:24 +02:00
return this;
},
addFieldViews: function() {
2017-01-31 07:50:28 +01:00
const model = this.model;
2016-06-05 16:49:00 +02:00
if (model.isJustCreated && this.appModel.files.length > 1) {
2017-01-31 07:50:28 +01:00
const fileNames = this.appModel.files.map(function(file) {
2016-06-05 16:49:00 +02:00
return { id: file.id, value: file.get('name'), selected: file === this.model.file };
}, this);
2019-08-16 23:05:39 +02:00
this.fileEditView = new FieldViewSelect({
model: {
name: '$File',
title: Format.capFirst(Locale.file),
value: function() {
return fileNames;
}
}
});
2016-06-05 16:49:00 +02:00
this.fieldViews.push(this.fileEditView);
} else {
2019-08-16 23:05:39 +02:00
this.fieldViews.push(
new FieldViewReadOnly({
model: {
name: 'File',
title: Format.capFirst(Locale.file),
value: function() {
return model.fileName;
}
}
})
);
2016-06-05 16:49:00 +02:00
}
2019-08-16 23:05:39 +02:00
this.userEditView = new FieldViewAutocomplete({
model: {
name: '$UserName',
title: Format.capFirst(Locale.user),
value: function() {
return model.user;
},
getCompletions: this.getUserNameCompletions.bind(this)
}
});
2016-02-22 08:09:23 +01:00
this.fieldViews.push(this.userEditView);
2019-08-16 23:05:39 +02:00
this.passEditView = new FieldViewText({
model: {
name: '$Password',
title: Format.capFirst(Locale.password),
canGen: true,
value: function() {
return model.password;
}
}
});
2016-01-11 18:56:52 +01:00
this.fieldViews.push(this.passEditView);
2019-08-16 23:05:39 +02:00
this.urlEditView = new FieldViewUrl({
model: {
name: '$URL',
title: Format.capFirst(Locale.website),
value: function() {
return model.url;
}
2016-04-01 21:19:27 +02:00
}
2019-08-16 23:05:39 +02:00
});
this.fieldViews.push(this.urlEditView);
this.fieldViews.push(
new FieldViewText({
model: {
name: '$Notes',
title: Format.capFirst(Locale.notes),
multiline: 'true',
value: function() {
return model.notes;
}
}
})
);
this.fieldViews.push(
new FieldViewTags({
model: {
name: 'Tags',
title: Format.capFirst(Locale.tags),
tags: this.appModel.tags,
value: function() {
return model.tags;
}
}
})
);
this.fieldViews.push(
new FieldViewDate({
model: {
name: 'Expires',
title: Locale.detExpires,
lessThanNow: '(' + Locale.detExpired + ')',
value: function() {
return model.expires;
}
}
})
);
this.fieldViews.push(
new FieldViewReadOnly({
model: {
name: 'Group',
title: Locale.detGroup,
value: function() {
return model.groupName;
},
tip: function() {
return model.getGroupPath().join(' / ');
}
}
})
);
this.fieldViews.push(
new FieldViewReadOnly({
model: {
name: 'Created',
title: Locale.detCreated,
value: function() {
return Format.dtStr(model.created);
}
}
})
);
this.fieldViews.push(
new FieldViewReadOnly({
model: {
name: 'Updated',
title: Locale.detUpdated,
value: function() {
return Format.dtStr(model.updated);
}
}
})
);
this.fieldViews.push(
new FieldViewHistory({
model: {
name: 'History',
title: Format.capFirst(Locale.history),
value: function() {
return { length: model.historyLength, unsaved: model.unsaved };
}
}
})
);
_.forEach(
model.fields,
function(value, field) {
if (field === 'otp' && this.model.otpGenerator) {
this.fieldViews.push(
new FieldViewOtp({
model: {
name: '$' + field,
title: field,
value: function() {
return model.otpGenerator;
}
}
})
);
} else {
this.fieldViews.push(
new FieldViewCustom({
model: {
name: '$' + field,
title: field,
value: function() {
return model.fields[field];
}
}
})
);
}
},
this
);
2016-03-05 09:35:22 +01:00
2017-01-31 07:50:28 +01:00
const hideEmptyFields = AppSettingsModel.instance.get('hideEmptyFields');
2016-03-05 09:35:22 +01:00
2017-01-31 07:50:28 +01:00
const fieldsMainEl = this.$el.find('.details__body-fields');
const fieldsAsideEl = this.$el.find('.details__body-aside');
2016-03-05 09:35:22 +01:00
this.fieldViews.forEach(function(fieldView) {
fieldView.setElement(fieldView.readonly ? fieldsAsideEl : fieldsMainEl).render();
fieldView.on('change', this.fieldChanged.bind(this));
fieldView.on('copy', this.fieldCopied.bind(this));
2016-03-05 10:00:43 +01:00
if (hideEmptyFields) {
2017-01-31 07:50:28 +01:00
const value = fieldView.model.value();
if (!value || value.length === 0 || value.byteLength === 0) {
if (this.model.isJustCreated && ['$UserName', '$Password'].indexOf(fieldView.model.name) >= 0) {
2016-03-05 10:00:43 +01:00
return; // don't hide user for new records
}
fieldView.hide();
}
2016-03-05 09:35:22 +01:00
}
}, this);
this.moreView = new DetailsAddFieldView();
this.moreView.setElement(fieldsMainEl).render();
this.moreView.on('add-field', this.addNewField.bind(this));
this.moreView.on('more-click', this.toggleMoreOptions.bind(this));
},
addNewField: function() {
this.moreView.remove();
this.moreView = null;
2017-01-31 07:50:28 +01:00
let newFieldTitle = Locale.detNetField;
2016-03-05 09:35:22 +01:00
if (this.model.fields[newFieldTitle]) {
2017-01-31 07:50:28 +01:00
for (let i = 1; ; i++) {
const newFieldTitleVariant = newFieldTitle + i;
2016-03-05 09:35:22 +01:00
if (!this.model.fields[newFieldTitleVariant]) {
2015-10-17 23:49:24 +02:00
newFieldTitle = newFieldTitleVariant;
break;
}
}
}
2019-08-16 23:05:39 +02:00
const fieldView = new FieldViewCustom({
model: {
name: '$' + newFieldTitle,
title: newFieldTitle,
newField: newFieldTitle,
value: function() {
return '';
}
}
});
2016-03-05 09:35:22 +01:00
fieldView.on('change', this.fieldChanged.bind(this));
fieldView.setElement(this.$el.find('.details__body-fields')).render();
fieldView.edit();
this.fieldViews.push(fieldView);
},
2015-10-17 23:49:24 +02:00
2016-03-05 09:35:22 +01:00
toggleMoreOptions: function() {
if (this.views.dropdownView) {
this.views.dropdownView.remove();
this.views.dropdownView = null;
} else {
this.setTimeout(function() {
2017-01-31 07:50:28 +01:00
const dropdownView = new DropdownView();
2016-03-05 09:35:22 +01:00
this.listenTo(dropdownView, 'cancel', this.toggleMoreOptions);
this.listenTo(dropdownView, 'select', this.moreOptionsSelect);
2017-01-31 07:50:28 +01:00
const hideEmptyFields = AppSettingsModel.instance.get('hideEmptyFields');
const moreOptions = [];
2016-03-05 09:35:22 +01:00
if (hideEmptyFields) {
2016-07-17 13:30:38 +02:00
this.fieldViews.forEach(fieldView => {
2016-03-05 09:40:07 +01:00
if (fieldView.isHidden()) {
2019-08-16 23:05:39 +02:00
moreOptions.push({
value: 'add:' + fieldView.model.name,
icon: 'pencil',
text: Locale.detMenuAddField.replace('{}', fieldView.model.title)
});
2016-03-05 09:35:22 +01:00
}
}, this);
2019-08-16 23:05:39 +02:00
moreOptions.push({ value: 'add-new', icon: 'plus', text: Locale.detMenuAddNewField });
moreOptions.push({ value: 'toggle-empty', icon: 'eye', text: Locale.detMenuShowEmpty });
2016-03-05 09:35:22 +01:00
} else {
2019-08-16 23:05:39 +02:00
moreOptions.push({ value: 'add-new', icon: 'plus', text: Locale.detMenuAddNewField });
moreOptions.push({ value: 'toggle-empty', icon: 'eye-slash', text: Locale.detMenuHideEmpty });
2016-03-05 09:35:22 +01:00
}
2019-08-16 23:05:39 +02:00
moreOptions.push({ value: 'otp', icon: 'clock-o', text: Locale.detSetupOtp });
2016-04-23 16:50:40 +02:00
if (AutoType.enabled) {
2019-08-16 23:05:39 +02:00
moreOptions.push({ value: 'auto-type', icon: 'keyboard-o', text: Locale.detAutoTypeSettings });
2016-04-23 16:50:40 +02:00
}
2019-08-16 23:05:39 +02:00
moreOptions.push({ value: 'clone', icon: 'clone', text: Locale.detClone });
2017-01-31 07:50:28 +01:00
const rect = this.moreView.labelEl[0].getBoundingClientRect();
2016-03-05 09:35:22 +01:00
dropdownView.render({
2019-08-16 23:05:39 +02:00
position: { top: rect.bottom, left: rect.left },
2016-03-05 09:35:22 +01:00
options: moreOptions
});
this.views.dropdownView = dropdownView;
});
}
},
moreOptionsSelect: function(e) {
this.views.dropdownView.remove();
this.views.dropdownView = null;
switch (e.item) {
case 'add-new':
this.addNewField();
break;
case 'toggle-empty':
2017-01-31 07:50:28 +01:00
const hideEmptyFields = AppSettingsModel.instance.get('hideEmptyFields');
2016-03-05 09:35:22 +01:00
AppSettingsModel.instance.set('hideEmptyFields', !hideEmptyFields);
this.render();
break;
2016-03-31 22:52:04 +02:00
case 'otp':
this.setupOtp();
break;
2016-04-23 16:50:40 +02:00
case 'auto-type':
this.toggleAutoType();
break;
2016-08-21 19:39:02 +02:00
case 'clone':
this.clone();
break;
2016-03-05 09:35:22 +01:00
default:
if (e.item.lastIndexOf('add:', 0) === 0) {
2017-01-31 07:50:28 +01:00
const fieldName = e.item.substr(4);
const fieldView = _.find(this.fieldViews, f => f.model.name === fieldName);
2016-03-05 09:35:22 +01:00
fieldView.show();
fieldView.edit();
}
}
2015-10-17 23:49:24 +02:00
},
2016-02-28 12:16:05 +01:00
getUserNameCompletions: function(part) {
return this.appModel.completeUserNames(part);
},
2015-10-17 23:49:24 +02:00
setSelectedColor: function(color) {
2019-08-16 23:05:39 +02:00
this.$el
.find('.details__colors-popup > .details__colors-popup-item')
.removeClass('details__colors-popup-item--active');
2017-01-31 07:50:28 +01:00
const colorEl = this.$el.find('.details__header-color')[0];
2016-07-17 13:30:38 +02:00
_.forEach(colorEl.classList, cls => {
2015-10-17 23:49:24 +02:00
if (cls.indexOf('color') > 0 && cls.lastIndexOf('details', 0) !== 0) {
colorEl.classList.remove(cls);
}
});
if (color) {
2019-08-16 23:05:39 +02:00
this.$el
.find('.details__colors-popup > .' + color + '-color')
.addClass('details__colors-popup-item--active');
2015-10-17 23:49:24 +02:00
colorEl.classList.add(color + '-color');
}
},
selectColor: function(e) {
2019-08-16 23:05:39 +02:00
let color = $(e.target)
.closest('.details__colors-popup-item')
.data('color');
2015-10-17 23:49:24 +02:00
if (!color) {
return;
}
if (color === this.model.color) {
color = null;
}
this.model.setColor(color);
this.entryUpdated();
},
toggleIcons: function() {
2015-10-31 20:09:32 +01:00
if (this.views.sub && this.views.sub instanceof IconSelectView) {
2015-10-17 23:49:24 +02:00
this.render();
return;
}
this.removeSubView();
2017-01-31 07:50:28 +01:00
const subView = new IconSelectView({
2015-11-21 23:15:51 +01:00
el: this.scroller,
model: {
iconId: this.model.customIconId || this.model.iconId,
2019-08-16 23:05:39 +02:00
url: this.model.url,
file: this.model.file
2015-11-21 23:15:51 +01:00
}
});
2015-10-17 23:49:24 +02:00
this.listenTo(subView, 'select', this.iconSelected);
subView.render();
this.pageResized();
this.views.sub = subView;
},
toggleAttachment: function(e) {
2017-01-31 07:50:28 +01:00
const attBtn = $(e.target).closest('.details__attachment');
const id = attBtn.data('id');
const attachment = this.model.attachments[id];
2015-10-17 23:49:24 +02:00
if (e.altKey || e.shiftKey || e.ctrlKey || e.metaKey) {
this.downloadAttachment(attachment);
return;
}
if (this.views.sub && this.views.sub.attId === id) {
this.render();
return;
}
this.removeSubView();
2017-01-31 07:50:28 +01:00
const subView = new DetailsAttachmentView({ el: this.scroller, model: attachment });
2015-10-17 23:49:24 +02:00
subView.attId = id;
subView.render(this.pageResized.bind(this));
this.views.sub = subView;
attBtn.addClass('details__attachment--active');
},
removeSubView: function() {
this.$el.find('.details__attachment').removeClass('details__attachment--active');
if (this.views.sub) {
this.views.sub.remove();
delete this.views.sub;
}
},
downloadAttachment: function(attachment) {
2017-01-31 07:50:28 +01:00
const data = attachment.getBinary();
2015-10-17 23:49:24 +02:00
if (!data) {
return;
}
2017-01-31 07:50:28 +01:00
const mimeType = attachment.mimeType || 'application/octet-stream';
2019-08-16 23:05:39 +02:00
const blob = new Blob([data], { type: mimeType });
2015-10-17 23:49:24 +02:00
FileSaver.saveAs(blob, attachment.title);
},
2015-11-21 23:15:51 +01:00
iconSelected: function(sel) {
if (sel.custom) {
if (sel.id !== this.model.customIconId) {
this.model.setCustomIcon(sel.id);
this.entryUpdated();
} else {
this.render();
}
} else if (sel.id !== this.model.iconId) {
2015-11-22 11:59:13 +01:00
this.model.setIcon(+sel.id);
2015-10-17 23:49:24 +02:00
this.entryUpdated();
} else {
this.render();
}
},
showEntry: function(entry) {
this.model = entry;
this.render();
2015-10-27 22:07:48 +01:00
if (entry && !entry.title && entry.isJustCreated) {
2015-10-18 14:18:53 +02:00
this.editTitle();
}
2015-10-17 23:49:24 +02:00
},
copyKeyPress: function(editView) {
2019-08-16 23:05:39 +02:00
if (this.isHidden()) {
return;
}
2015-10-17 23:49:24 +02:00
if (!window.getSelection().toString()) {
2017-01-31 07:50:28 +01:00
const fieldValue = editView.value;
const fieldText = fieldValue && fieldValue.isProtected ? fieldValue.getText() : fieldValue;
if (!fieldText) {
2016-02-22 08:09:23 +01:00
return;
}
2016-01-22 18:51:36 +01:00
if (!CopyPaste.simpleCopy) {
CopyPaste.createHiddenInput(fieldText);
2016-01-22 18:51:36 +01:00
}
2017-01-31 07:50:28 +01:00
const copyRes = CopyPaste.copy(fieldText);
this.fieldCopied({ source: editView, copyRes: copyRes });
2015-10-17 23:49:24 +02:00
}
},
copyPassword: function() {
this.copyKeyPress(this.passEditView);
},
copyUserName: function() {
this.copyKeyPress(this.userEditView);
},
copyUrl: function() {
this.copyKeyPress(this.urlEditView);
2016-02-22 08:09:23 +01:00
},
2016-01-13 18:46:43 +01:00
showCopyTip: function() {
if (this.helpTipCopyShown) {
return;
}
this.helpTipCopyShown = AppSettingsModel.instance.get('helpTipCopyShown');
if (this.helpTipCopyShown) {
return;
}
AppSettingsModel.instance.set('helpTipCopyShown', true);
this.helpTipCopyShown = true;
2017-01-31 07:50:28 +01:00
const label = this.moreView.labelEl;
const tip = new Tip(label, { title: Locale.detCopyHint, placement: 'right' });
2016-01-13 18:46:43 +01:00
tip.show();
2016-03-27 15:41:13 +02:00
this.fieldCopyTip = tip;
2019-08-16 23:05:39 +02:00
setTimeout(() => {
tip.hide();
}, Timeouts.AutoHideHint);
2016-01-13 18:46:43 +01:00
},
2016-07-16 15:30:17 +02:00
settingsToggled: function() {
this.hideFieldCopyTip();
},
2015-10-17 23:49:24 +02:00
fieldChanged: function(e) {
if (e.field) {
if (e.field[0] === '$') {
2017-01-31 07:50:28 +01:00
let fieldName = e.field.substr(1);
2016-04-02 15:20:48 +02:00
if (fieldName === 'otp') {
if (this.otpFieldChanged(e.val)) {
this.entryUpdated();
return;
}
} else if (e.newField) {
2016-01-16 13:35:34 +01:00
if (fieldName) {
this.model.setField(fieldName, undefined);
}
fieldName = e.newField;
2017-01-31 07:50:28 +01:00
let i = 0;
2016-01-16 13:35:34 +01:00
while (this.model.hasField(fieldName)) {
i++;
fieldName = e.newField + i;
}
2017-05-03 21:21:29 +02:00
const allowEmpty = this.model.group.isEntryTemplatesGroup();
this.model.setField(fieldName, e.val, allowEmpty);
2015-10-17 23:49:24 +02:00
this.entryUpdated();
return;
2016-06-05 16:49:00 +02:00
} else if (fieldName === 'File') {
2017-01-31 07:50:28 +01:00
const newFile = this.appModel.files.get(e.val);
2016-06-05 16:49:00 +02:00
this.model.moveToFile(newFile);
this.appModel.activeEntryId = this.model.id;
this.entryUpdated();
2016-08-21 19:39:02 +02:00
Backbone.trigger('entry-selected', this.model);
2016-06-05 16:49:00 +02:00
return;
2016-01-16 13:35:34 +01:00
} else if (fieldName) {
2015-10-17 23:49:24 +02:00
this.model.setField(fieldName, e.val);
}
} else if (e.field === 'Tags') {
this.model.setTags(e.val);
this.appModel.updateTags();
} else if (e.field === 'Expires') {
2017-01-31 07:50:28 +01:00
const dt = e.val || undefined;
2015-10-17 23:49:24 +02:00
if (!_.isEqual(dt, this.model.expires)) {
this.model.setExpires(dt);
}
}
this.entryUpdated(true);
this.fieldViews.forEach(function(fieldView, ix) {
2019-08-16 23:05:39 +02:00
if (
fieldView instanceof FieldViewCustom &&
!fieldView.model.newField &&
!this.model.hasField(fieldView.model.title)
) {
2015-10-17 23:49:24 +02:00
fieldView.remove();
this.fieldViews.splice(ix, 1);
} else {
fieldView.update();
}
}, this);
2016-03-05 09:35:22 +01:00
} else if (e.newField) {
this.render();
return;
2015-10-17 23:49:24 +02:00
}
if (e.tab) {
this.focusNextField(e.tab);
}
},
2016-04-02 15:20:48 +02:00
otpFieldChanged: function(value) {
2017-01-31 07:50:28 +01:00
let oldValue = this.model.fields.otp;
2016-04-02 15:20:48 +02:00
if (oldValue && oldValue.isProtected) {
oldValue = oldValue.getText();
}
2016-04-04 20:45:17 +02:00
if (value && value.isProtected) {
value = value.getText();
}
2016-04-02 15:20:48 +02:00
if (oldValue === value) {
2016-04-04 20:45:17 +02:00
this.render();
2016-04-02 15:20:48 +02:00
return false;
}
this.model.setOtpUrl(value);
return true;
},
2015-10-17 23:49:24 +02:00
dragover: function(e) {
e.preventDefault();
e.stopPropagation();
const dt = e.originalEvent.dataTransfer;
if (!dt.types || (dt.types.indexOf ? dt.types.indexOf('Files') === -1 : !dt.types.contains('Files'))) {
dt.dropEffect = 'none';
return;
}
dt.dropEffect = 'copy';
2015-10-17 23:49:24 +02:00
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
if (this.model && !this.dragging) {
this.dragging = true;
this.$el.find('.details').addClass('details--drag');
}
},
dragleave: function() {
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
2016-07-17 13:30:38 +02:00
this.dragTimeout = setTimeout(() => {
2015-10-17 23:49:24 +02:00
this.$el.find('.details').removeClass('details--drag');
this.dragging = false;
2016-07-17 13:30:38 +02:00
}, 100);
2015-10-17 23:49:24 +02:00
},
drop: function(e) {
e.preventDefault();
if (!this.model) {
return;
}
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
this.$el.find('.details').removeClass('details--drag');
this.dragging = false;
2017-01-31 07:50:28 +01:00
const files = e.target.files || e.originalEvent.dataTransfer.files;
this.addAttachedFiles(files);
},
attachmentBtnClick: function() {
this.$el.find('.details__attachment-input-file')[0].click();
},
attachmentFileChange: function(e) {
this.addAttachedFiles(e.target.files);
},
addAttachedFiles: function(files) {
2019-08-16 23:05:39 +02:00
_.forEach(
files,
function(file) {
const reader = new FileReader();
reader.onload = () => {
this.addAttachment(file.name, reader.result);
};
reader.readAsArrayBuffer(file);
},
this
);
2015-10-17 23:49:24 +02:00
},
addAttachment: function(name, data) {
2017-01-29 23:28:04 +01:00
this.model.addAttachment(name, data).then(() => {
this.entryUpdated();
});
2015-10-17 23:49:24 +02:00
},
2016-09-04 09:26:54 +02:00
deleteKeyPress: function(e) {
2015-10-17 23:49:24 +02:00
if (this.views.sub && this.views.sub.attId !== undefined) {
2016-09-04 09:26:54 +02:00
e.preventDefault();
2017-01-31 07:50:28 +01:00
const attachment = this.model.attachments[this.views.sub.attId];
2015-10-17 23:49:24 +02:00
this.model.removeAttachment(attachment.title);
this.render();
}
},
editTitle: function() {
2017-01-31 07:50:28 +01:00
const input = $('<input/>')
2015-10-17 23:49:24 +02:00
.addClass('details__header-title-input')
.attr({ autocomplete: 'off', spellcheck: 'false', placeholder: 'Title' })
.val(this.model.title);
input.bind({
blur: this.titleInputBlur.bind(this),
input: this.titleInputInput.bind(this),
keydown: this.titleInputKeydown.bind(this),
keypress: this.titleInputInput.bind(this)
});
$('.details__header-title').replaceWith(input);
input.focus()[0].setSelectionRange(this.model.title.length, this.model.title.length);
},
titleInputBlur: function(e) {
this.setTitle(e.target.value);
},
titleInputInput: function(e) {
e.stopPropagation();
},
titleInputKeydown: function(e) {
2015-11-17 22:49:12 +01:00
KeyHandler.reg();
2015-10-17 23:49:24 +02: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) {
$(e.target).unbind('blur');
this.setTitle(e.target.value);
} else if (code === Keys.DOM_VK_ESCAPE) {
$(e.target).unbind('blur');
2015-10-27 22:07:48 +01:00
if (this.model.isJustCreated) {
2015-10-27 20:40:34 +01:00
this.model.removeWithoutHistory();
Backbone.trigger('refresh');
return;
}
2015-10-17 23:49:24 +02:00
this.render();
} else if (code === Keys.DOM_VK_TAB) {
e.preventDefault();
$(e.target).unbind('blur');
this.setTitle(e.target.value);
if (!e.shiftKey) {
this.focusNextField({ field: '$Title' });
}
}
},
setTitle: function(title) {
if (this.model.title instanceof kdbxweb.ProtectedValue) {
title = kdbxweb.ProtectedValue.fromString(title);
}
if (title !== this.model.title) {
this.model.setField('Title', title);
this.entryUpdated(true);
}
2017-01-31 07:50:28 +01:00
const newTitle = $('<h1 class="details__header-title"></h1>').text(title || '(no title)');
2015-10-17 23:49:24 +02:00
this.$el.find('.details__header-title-input').replaceWith(newTitle);
},
entryUpdated: function(skipRender) {
Backbone.trigger('entry-updated', { entry: this.model });
if (!skipRender) {
this.render();
}
},
focusNextField: function(config) {
2019-08-16 23:05:39 +02:00
let found = false,
nextFieldView;
2015-10-17 23:49:24 +02:00
if (config.field === '$Title' && !config.prev) {
found = true;
}
2017-01-31 07:50:28 +01:00
const start = config.prev ? this.fieldViews.length - 1 : 0;
const end = config.prev ? -1 : this.fieldViews.length;
const inc = config.prev ? -1 : 1;
for (let i = start; i !== end; i += inc) {
const fieldView = this.fieldViews[i];
2015-10-17 23:49:24 +02:00
if (fieldView.model.name === config.field) {
found = true;
2016-03-05 09:35:22 +01:00
} else if (found && !fieldView.readonly && !fieldView.isHidden()) {
2015-10-17 23:49:24 +02:00
nextFieldView = fieldView;
break;
}
}
if (nextFieldView) {
nextFieldView.edit();
}
},
showHistory: function() {
this.removeSubView();
2017-01-31 07:50:28 +01:00
const subView = new DetailsHistoryView({ el: this.scroller, model: this.model });
2015-10-17 23:49:24 +02:00
this.listenTo(subView, 'close', this.historyClosed.bind(this));
subView.render();
this.pageResized();
this.views.sub = subView;
},
historyClosed: function(e) {
if (e.updated) {
this.entryUpdated();
} else {
this.render();
}
},
moveToTrash: function() {
this.model.moveToTrash();
Backbone.trigger('refresh');
2015-10-26 22:07:19 +01:00
},
2016-08-21 19:39:02 +02:00
clone: function() {
2017-01-31 07:50:28 +01:00
const newEntry = this.model.cloneEntry(' ' + Locale.detClonedName);
2016-08-21 19:39:02 +02:00
Backbone.trigger('select-entry', newEntry);
},
2015-11-09 19:15:39 +01:00
deleteFromTrash: function() {
Alerts.yesno({
2015-12-17 19:25:25 +01:00
header: Locale.detDelFromTrash,
body: Locale.detDelFromTrashBody + ' <p class="muted-color">' + Locale.detDelFromTrashBodyHint + '</p>',
2015-11-09 19:15:39 +01:00
icon: 'minus-circle',
2016-07-17 13:30:38 +02:00
success: () => {
2015-11-09 19:15:39 +01:00
this.model.deleteFromTrash();
Backbone.trigger('refresh');
2016-07-17 13:30:38 +02:00
}
2015-11-09 19:15:39 +01:00
});
},
2015-10-26 22:07:19 +01:00
backClick: function() {
Backbone.trigger('toggle-details', false);
2016-03-31 22:52:04 +02:00
},
2016-07-30 11:25:22 +02:00
contextMenu(e) {
2017-01-31 07:50:28 +01:00
const canCopy = document.queryCommandSupported('copy');
const options = [];
2016-07-30 11:25:22 +02:00
if (canCopy) {
options.push({ value: 'det-copy-password', icon: 'clipboard', text: Locale.detMenuCopyPassword });
options.push({ value: 'det-copy-user', icon: 'clipboard', text: Locale.detMenuCopyUser });
}
options.push({ value: 'det-add-new', icon: 'plus', text: Locale.detMenuAddNewField });
2016-08-21 19:39:02 +02:00
options.push({ value: 'det-clone', icon: 'clone', text: Locale.detClone });
2017-02-05 15:16:40 +01:00
if (AutoType.enabled) {
options.push({ value: 'det-auto-type', icon: 'keyboard-o', text: Locale.detAutoType });
}
2016-07-30 11:25:22 +02:00
Backbone.trigger('show-context-menu', _.extend(e, { options }));
},
contextMenuSelect(e) {
switch (e.item) {
case 'det-copy-password':
this.copyPassword();
break;
case 'det-copy-user':
this.copyUserName();
break;
case 'det-add-new':
this.addNewField();
break;
2016-08-21 19:39:02 +02:00
case 'det-clone':
this.clone();
break;
2017-02-05 15:16:40 +01:00
case 'det-auto-type':
this.autoType();
break;
2016-07-30 11:25:22 +02:00
}
},
2016-03-31 22:52:04 +02:00
setupOtp: function() {
2019-01-01 20:18:33 +01:00
OtpQrReader.read();
2016-03-31 22:52:04 +02:00
},
2016-04-01 23:09:16 +02:00
otpCodeRead: function(otp) {
this.model.setOtp(otp);
this.entryUpdated();
2016-04-04 20:45:17 +02:00
},
otpEnterManually: function() {
if (this.model.fields.otp) {
2017-01-31 07:50:28 +01:00
const otpField = this.fieldViews.find(f => f.model.name === '$otp');
2016-04-04 20:45:17 +02:00
if (otpField) {
otpField.edit();
}
} else {
this.moreView.remove();
this.moreView = null;
2019-08-16 23:05:39 +02:00
const fieldView = new FieldViewCustom({
model: {
name: '$otp',
title: 'otp',
newField: 'otp',
value: kdbxweb.ProtectedValue.fromString('')
}
});
2016-04-04 20:45:17 +02:00
fieldView.on('change', this.fieldChanged.bind(this));
fieldView.setElement(this.$el.find('.details__body-fields')).render();
fieldView.edit();
this.fieldViews.push(fieldView);
}
2016-04-10 09:02:32 +02:00
},
2016-04-23 16:50:40 +02:00
toggleAutoType: function() {
if (this.views.autoType) {
this.views.autoType.remove();
delete this.views.autoType;
return;
}
this.views.autoType = new DetailsAutoTypeView({
2016-04-23 17:19:48 +02:00
el: this.$el.find('.details__body-after'),
2016-04-23 16:50:40 +02:00
model: this.model
}).render();
},
2016-04-10 09:02:32 +02:00
autoType: function() {
2016-07-30 21:12:29 +02:00
Backbone.trigger('auto-type', { entry: this.model });
2015-10-17 23:49:24 +02:00
}
});
_.extend(DetailsView.prototype, Scrollable);
2019-01-02 10:49:12 +01:00
_.extend(DetailsView.prototype, Copyable);
2015-10-17 23:49:24 +02:00
module.exports = DetailsView;