keeweb/app/scripts/views/list-search-view.js

434 lines
14 KiB
JavaScript
Raw Normal View History

2019-09-16 20:42:33 +02:00
import { View } from 'framework/views/view';
2019-09-16 22:57:56 +02:00
import { Events } from 'framework/events';
2019-09-15 14:16:32 +02:00
import { Shortcuts } from 'comp/app/shortcuts';
import { KeyHandler } from 'comp/browser/key-handler';
import { Keys } from 'const/keys';
import { Comparators } from 'util/data/comparators';
import { Features } from 'util/features';
import { StringFormat } from 'util/formatting/string-format';
import { Locale } from 'util/locale';
import { DropdownView } from 'views/dropdown-view';
2019-09-16 17:43:57 +02:00
import template from 'templates/list-search.hbs';
2015-10-17 23:49:24 +02:00
2019-09-16 17:43:57 +02:00
class ListSearchView extends View {
parent = '.list__header';
2015-10-17 23:49:24 +02:00
2019-09-16 17:43:57 +02:00
template = template;
events = {
2015-10-17 23:49:24 +02:00
'keydown .list__search-field': 'inputKeyDown',
'keypress .list__search-field': 'inputKeyPress',
'input .list__search-field': 'inputChange',
2016-08-11 19:51:54 +02:00
'focus .list__search-field': 'inputFocus',
2015-10-17 23:49:24 +02:00
'click .list__search-btn-new': 'createOptionsClick',
'click .list__search-btn-sort': 'sortOptionsClick',
2015-10-24 11:15:54 +02:00
'click .list__search-icon-search': 'advancedSearchClick',
2016-01-13 21:33:14 +01:00
'click .list__search-btn-menu': 'toggleMenu',
'change .list__search-adv input[type=checkbox]': 'toggleAdvCheck'
2019-09-16 17:43:57 +02:00
};
2015-10-17 23:49:24 +02:00
2019-09-16 17:43:57 +02:00
inputEl = null;
sortOptions = null;
sortIcons = null;
createOptions = null;
advancedSearchEnabled = false;
advancedSearch = null;
2015-10-17 23:49:24 +02:00
2019-09-16 17:43:57 +02:00
constructor(model) {
super(model);
2015-10-17 23:49:24 +02:00
this.sortOptions = [
2019-08-16 23:05:39 +02:00
{
value: 'title',
icon: 'sort-alpha-asc',
2019-09-15 08:11:11 +02:00
loc: () =>
StringFormat.capFirst(Locale.title) + ' ' + this.addArrow(Locale.searchAZ)
2019-08-16 23:05:39 +02:00
},
{
value: '-title',
icon: 'sort-alpha-desc',
2019-09-15 08:11:11 +02:00
loc: () =>
StringFormat.capFirst(Locale.title) + ' ' + this.addArrow(Locale.searchZA)
2019-08-16 23:05:39 +02:00
},
{
value: 'website',
icon: 'sort-alpha-asc',
2019-09-15 08:11:11 +02:00
loc: () =>
StringFormat.capFirst(Locale.website) + ' ' + this.addArrow(Locale.searchAZ)
2019-08-16 23:05:39 +02:00
},
{
value: '-website',
icon: 'sort-alpha-desc',
2019-09-15 08:11:11 +02:00
loc: () =>
StringFormat.capFirst(Locale.website) + ' ' + this.addArrow(Locale.searchZA)
2019-08-16 23:05:39 +02:00
},
{
value: 'user',
icon: 'sort-alpha-asc',
2019-09-15 08:11:11 +02:00
loc: () => StringFormat.capFirst(Locale.user) + ' ' + this.addArrow(Locale.searchAZ)
2019-08-16 23:05:39 +02:00
},
{
value: '-user',
icon: 'sort-alpha-desc',
2019-09-15 08:11:11 +02:00
loc: () => StringFormat.capFirst(Locale.user) + ' ' + this.addArrow(Locale.searchZA)
2019-08-16 23:05:39 +02:00
},
{
value: 'created',
icon: 'sort-numeric-asc',
loc: () => Locale.searchCreated + ' ' + this.addArrow(Locale.searchON)
},
{
value: '-created',
icon: 'sort-numeric-desc',
loc: () => Locale.searchCreated + ' ' + this.addArrow(Locale.searchNO)
},
{
value: 'updated',
icon: 'sort-numeric-asc',
loc: () => Locale.searchUpdated + ' ' + this.addArrow(Locale.searchON)
},
{
value: '-updated',
icon: 'sort-numeric-desc',
loc: () => Locale.searchUpdated + ' ' + this.addArrow(Locale.searchNO)
},
2019-08-18 08:05:38 +02:00
{
value: '-attachments',
icon: 'sort-amount-desc',
loc: () => Locale.searchAttachments
},
2019-09-08 12:59:45 +02:00
{ value: '-rank', icon: 'sort-amount-desc', loc: () => Locale.searchRank }
2015-10-17 23:49:24 +02:00
];
this.sortIcons = {};
2020-06-01 16:53:51 +02:00
this.sortOptions.forEach((opt) => {
2015-10-17 23:49:24 +02:00
this.sortIcons[opt.value] = opt.icon;
2019-09-16 21:07:39 +02:00
});
2016-01-13 21:33:14 +01:00
this.advancedSearch = {
2019-08-16 23:05:39 +02:00
user: true,
other: true,
url: true,
protect: false,
notes: true,
pass: false,
cs: false,
regex: false,
history: false,
2019-08-17 20:09:24 +02:00
title: true
2016-01-13 21:33:14 +01:00
};
if (this.model.advancedSearch) {
2019-09-17 23:44:17 +02:00
this.advancedSearch = { ...this.model.advancedSearch };
}
2016-08-25 18:30:22 +02:00
this.setLocale();
2019-09-16 17:43:57 +02:00
this.onKey(Keys.DOM_VK_F, this.findKeyPress, KeyHandler.SHORTCUT_ACTION);
this.onKey(Keys.DOM_VK_N, this.newKeyPress, KeyHandler.SHORTCUT_OPT);
this.onKey(Keys.DOM_VK_DOWN, this.downKeyPress);
this.onKey(Keys.DOM_VK_UP, this.upKeyPress);
2015-10-17 23:49:24 +02:00
this.listenTo(this, 'show', this.viewShown);
this.listenTo(this, 'hide', this.viewHidden);
2019-09-16 22:57:56 +02:00
this.listenTo(Events, 'filter', this.filterChanged);
this.listenTo(Events, 'set-locale', this.setLocale);
this.listenTo(Events, 'page-blur', this.pageBlur);
2020-05-09 23:55:57 +02:00
this.listenTo(this.model.files, 'change', this.fileListUpdated);
2019-09-16 21:07:39 +02:00
this.once('remove', () => {
this.removeKeypressHandler();
});
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
setLocale() {
2020-06-01 16:53:51 +02:00
this.sortOptions.forEach((opt) => {
2020-05-09 10:59:23 +02:00
opt.text = opt.loc();
2019-08-16 23:05:39 +02:00
});
2016-08-25 18:30:22 +02:00
this.createOptions = [
2020-04-23 19:12:15 +02:00
{
value: 'entry',
icon: 'key',
text: StringFormat.capFirst(Locale.entry),
hint: Features.isMobile
? null
: `(${Locale.searchShiftClickOr} ${Shortcuts.altShortcutSymbol(true)})`
},
2019-09-15 08:11:11 +02:00
{ value: 'group', icon: 'folder', text: StringFormat.capFirst(Locale.group) }
2016-08-25 18:30:22 +02:00
];
2019-09-16 17:43:57 +02:00
if (this.el) {
this.render();
}
}
2016-08-25 18:30:22 +02:00
2019-08-18 10:17:09 +02:00
pageBlur() {
2016-09-05 21:14:50 +02:00
this.inputEl.blur();
2019-09-16 17:43:57 +02:00
}
2016-09-05 21:14:50 +02:00
2019-09-16 21:07:39 +02:00
removeKeypressHandler() {}
2019-08-18 10:17:09 +02:00
viewShown() {
2020-06-01 16:53:51 +02:00
const keypressHandler = (e) => this.documentKeyPress(e);
2019-09-17 19:01:12 +02:00
Events.on('keypress', keypressHandler);
this.removeKeypressHandler = () => Events.off('keypress', keypressHandler);
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
viewHidden() {
2019-09-16 21:07:39 +02:00
this.removeKeypressHandler();
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
render() {
2016-09-04 23:51:01 +02:00
let searchVal;
if (this.inputEl) {
searchVal = this.inputEl.val();
}
2019-09-16 17:43:57 +02:00
super.render({
2016-09-04 23:51:01 +02:00
adv: this.advancedSearch,
2020-05-09 23:55:57 +02:00
advEnabled: this.advancedSearchEnabled,
canCreate: this.model.canCreateEntries()
2016-09-04 23:51:01 +02:00
});
2015-10-17 23:49:24 +02:00
this.inputEl = this.$el.find('.list__search-field');
2016-09-04 23:51:01 +02:00
if (searchVal) {
this.inputEl.val(searchVal);
}
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
inputKeyDown(e) {
2015-10-17 23:49:24 +02:00
switch (e.which) {
case Keys.DOM_VK_UP:
case Keys.DOM_VK_DOWN:
break;
case Keys.DOM_VK_RETURN:
e.target.blur();
break;
case Keys.DOM_VK_ESCAPE:
if (this.inputEl.val()) {
this.inputEl.val('');
this.inputChange();
}
e.target.blur();
break;
default:
return;
}
e.preventDefault();
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
inputKeyPress(e) {
2015-10-17 23:49:24 +02:00
e.stopPropagation();
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
inputChange() {
2019-09-16 22:57:56 +02:00
Events.emit('add-filter', { text: this.inputEl.val() });
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
inputFocus(e) {
2016-08-11 19:51:54 +02:00
$(e.target).select();
2019-09-16 17:43:57 +02:00
}
2016-08-11 19:51:54 +02:00
2019-08-18 10:17:09 +02:00
documentKeyPress(e) {
2019-09-16 17:43:57 +02:00
if (this.hidden) {
2015-10-17 23:49:24 +02:00
return;
}
2017-01-31 07:50:28 +01:00
const code = e.charCode;
2015-10-17 23:49:24 +02:00
if (!code) {
return;
}
this.hideSearchOptions();
this.inputEl.val(String.fromCharCode(code)).focus();
this.inputEl[0].setSelectionRange(1, 1);
this.inputChange();
e.preventDefault();
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
findKeyPress(e) {
2019-09-16 17:43:57 +02:00
if (!this.hidden) {
2015-10-17 23:49:24 +02:00
e.preventDefault();
this.hideSearchOptions();
this.inputEl.select().focus();
2015-10-17 23:49:24 +02:00
}
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
newKeyPress(e) {
2019-09-16 17:43:57 +02:00
if (!this.hidden) {
2015-10-17 23:49:24 +02:00
e.preventDefault();
this.hideSearchOptions();
2019-09-16 17:43:57 +02:00
this.emit('create-entry');
2015-10-17 23:49:24 +02:00
}
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
downKeyPress(e) {
2015-10-17 23:49:24 +02:00
e.preventDefault();
this.hideSearchOptions();
2019-09-16 17:43:57 +02:00
this.emit('select-next');
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
upKeyPress(e) {
2015-10-17 23:49:24 +02:00
e.preventDefault();
this.hideSearchOptions();
2019-09-16 17:43:57 +02:00
this.emit('select-prev');
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
filterChanged(filter) {
2015-10-17 23:49:24 +02:00
this.hideSearchOptions();
if (filter.filter.text !== this.inputEl.val()) {
this.inputEl.val(filter.text || '');
}
2017-01-31 07:50:28 +01:00
const sortIconCls = this.sortIcons[filter.sort] || 'sort';
2015-10-17 23:49:24 +02:00
this.$el.find('.list__search-btn-sort>i').attr('class', 'fa fa-' + sortIconCls);
let adv = !!filter.filter.advanced;
if (this.model.advancedSearch) {
adv = filter.filter.advanced !== this.model.advancedSearch;
}
2016-01-16 15:19:33 +01:00
if (this.advancedSearchEnabled !== adv) {
this.advancedSearchEnabled = adv;
2016-01-14 18:35:17 +01:00
this.$el.find('.list__search-adv').toggleClass('hide', !this.advancedSearchEnabled);
}
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
createOptionsClick(e) {
2015-10-17 23:49:24 +02:00
e.stopImmediatePropagation();
if (e.shiftKey) {
this.hideSearchOptions();
2019-09-16 17:43:57 +02:00
this.emit('create-entry');
2015-10-17 23:49:24 +02:00
return;
}
this.toggleCreateOptions();
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
sortOptionsClick(e) {
2015-10-17 23:49:24 +02:00
this.toggleSortOptions();
e.stopImmediatePropagation();
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
advancedSearchClick() {
2016-01-13 21:33:14 +01:00
this.advancedSearchEnabled = !this.advancedSearchEnabled;
this.$el.find('.list__search-adv').toggleClass('hide', !this.advancedSearchEnabled);
let advanced = false;
if (this.advancedSearchEnabled) {
advanced = this.advancedSearch;
} else if (this.model.advancedSearch) {
advanced = this.model.advancedSearch;
}
2019-09-16 22:57:56 +02:00
Events.emit('add-filter', { advanced });
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
toggleMenu() {
2019-09-16 22:57:56 +02:00
Events.emit('toggle-menu');
2019-09-16 17:43:57 +02:00
}
2015-10-24 11:15:54 +02:00
2019-08-18 10:17:09 +02:00
toggleAdvCheck(e) {
2017-01-31 07:50:28 +01:00
const setting = $(e.target).data('id');
2016-01-13 21:33:14 +01:00
this.advancedSearch[setting] = e.target.checked;
2019-09-16 22:57:56 +02:00
Events.emit('add-filter', { advanced: this.advancedSearch });
2019-09-16 17:43:57 +02:00
}
2016-01-13 21:33:14 +01:00
2019-08-18 10:17:09 +02:00
hideSearchOptions() {
2015-10-17 23:49:24 +02:00
if (this.views.searchDropdown) {
this.views.searchDropdown.remove();
this.views.searchDropdown = null;
2019-08-18 08:05:38 +02:00
this.$el
.find('.list__search-btn-sort,.list__search-btn-new')
.removeClass('sel--active');
2015-10-17 23:49:24 +02:00
}
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
toggleSortOptions() {
2017-05-03 20:44:16 +02:00
if (this.views.searchDropdown && this.views.searchDropdown.isSort) {
2015-10-17 23:49:24 +02:00
this.hideSearchOptions();
return;
}
this.hideSearchOptions();
this.$el.find('.list__search-btn-sort').addClass('sel--active');
2017-01-31 07:50:28 +01:00
const view = new DropdownView();
2017-05-03 20:44:16 +02:00
view.isSort = true;
2015-10-17 23:49:24 +02:00
this.listenTo(view, 'cancel', this.hideSearchOptions);
this.listenTo(view, 'select', this.sortDropdownSelect);
2020-06-01 16:53:51 +02:00
this.sortOptions.forEach(function (opt) {
2015-10-17 23:49:24 +02:00
opt.active = this.model.sort === opt.value;
}, this);
view.render({
position: {
top: this.$el.find('.list__search-btn-sort')[0].getBoundingClientRect().bottom,
right: this.$el[0].getBoundingClientRect().right + 1
},
options: this.sortOptions
});
this.views.searchDropdown = view;
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
toggleCreateOptions() {
2017-05-03 20:44:16 +02:00
if (this.views.searchDropdown && this.views.searchDropdown.isCreate) {
2015-10-17 23:49:24 +02:00
this.hideSearchOptions();
return;
}
2017-05-02 21:22:08 +02:00
2015-10-17 23:49:24 +02:00
this.hideSearchOptions();
this.$el.find('.list__search-btn-new').addClass('sel--active');
2017-01-31 07:50:28 +01:00
const view = new DropdownView();
2017-05-03 20:44:16 +02:00
view.isCreate = true;
2015-10-17 23:49:24 +02:00
this.listenTo(view, 'cancel', this.hideSearchOptions);
this.listenTo(view, 'select', this.createDropdownSelect);
view.render({
position: {
top: this.$el.find('.list__search-btn-new')[0].getBoundingClientRect().bottom,
right: this.$el[0].getBoundingClientRect().right + 1
},
2017-05-02 21:22:08 +02:00
options: this.createOptions.concat(this.getCreateEntryTemplateOptions())
2015-10-17 23:49:24 +02:00
});
this.views.searchDropdown = view;
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
getCreateEntryTemplateOptions() {
2017-05-02 21:22:08 +02:00
const entryTemplates = this.model.getEntryTemplates();
const hasMultipleFiles = this.model.files.length > 1;
this.entryTemplates = {};
const options = [];
2020-06-01 16:53:51 +02:00
entryTemplates.forEach((tmpl) => {
2017-05-02 21:22:08 +02:00
const id = 'tmpl:' + tmpl.entry.id;
options.push({
value: id,
icon: tmpl.entry.icon,
2019-08-18 08:05:38 +02:00
text: hasMultipleFiles
2019-09-17 21:39:06 +02:00
? tmpl.file.name + ' / ' + tmpl.entry.title
2019-08-18 08:05:38 +02:00
: tmpl.entry.title
2017-05-02 21:22:08 +02:00
});
this.entryTemplates[id] = tmpl;
});
options.sort(Comparators.stringComparator('text', true));
2019-08-18 08:05:38 +02:00
options.push({
value: 'tmpl',
icon: 'sticky-note-o',
2019-09-15 08:11:11 +02:00
text: StringFormat.capFirst(Locale.template)
2019-08-18 08:05:38 +02:00
});
2017-05-02 21:22:08 +02:00
return options;
2019-09-16 17:43:57 +02:00
}
2017-05-02 21:22:08 +02:00
2019-08-18 10:17:09 +02:00
sortDropdownSelect(e) {
2015-10-17 23:49:24 +02:00
this.hideSearchOptions();
2019-09-16 22:57:56 +02:00
Events.emit('set-sort', e.item);
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
createDropdownSelect(e) {
2015-10-17 23:49:24 +02:00
this.hideSearchOptions();
switch (e.item) {
case 'entry':
2019-09-16 17:43:57 +02:00
this.emit('create-entry');
2015-10-17 23:49:24 +02:00
break;
case 'group':
2019-09-16 17:43:57 +02:00
this.emit('create-group');
2015-10-17 23:49:24 +02:00
break;
2017-05-02 21:22:08 +02:00
case 'tmpl':
2019-09-16 17:43:57 +02:00
this.emit('create-template');
2017-05-02 21:22:08 +02:00
break;
default:
if (this.entryTemplates[e.item]) {
2019-09-16 17:43:57 +02:00
this.emit('create-entry', { template: this.entryTemplates[e.item] });
2017-05-02 21:22:08 +02:00
}
2015-10-17 23:49:24 +02:00
}
2019-09-16 17:43:57 +02:00
}
2016-07-30 11:25:22 +02:00
addArrow(str) {
2020-05-09 10:59:23 +02:00
return str.replace('{}', '→');
2015-10-17 23:49:24 +02:00
}
2020-05-09 23:55:57 +02:00
fileListUpdated() {
this.render();
}
2019-09-16 17:43:57 +02:00
}
2015-10-17 23:49:24 +02:00
2019-09-15 14:16:32 +02:00
export { ListSearchView };