From 5bb490e1ff55329b9c9b6f048ef7f9ea97e35691 Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 17 Jan 2021 21:11:01 +0100 Subject: [PATCH] basic interactivity of search definition --- src/public/app/widgets/search_definition.js | 186 +++++++++++++++----- 1 file changed, 138 insertions(+), 48 deletions(-) diff --git a/src/public/app/widgets/search_definition.js b/src/public/app/widgets/search_definition.js index 726e4837f..76d067716 100644 --- a/src/public/app/widgets/search_definition.js +++ b/src/public/app/widgets/search_definition.js @@ -3,6 +3,7 @@ import SpacedUpdate from "../services/spaced_update.js"; import server from "../services/server.js"; import TabAwareWidget from "./tab_aware_widget.js"; import treeCache from "../services/tree_cache.js"; +import ws from "../services/ws.js"; const TPL = `
@@ -62,25 +63,25 @@ const TPL = ` Add search option: - - - - - @@ -102,36 +103,59 @@ const TPL = ` - + Descendant of:
- +
- + - + Fast search - + - + Include archived notes - + + + + + + + + Order by + + + + + + + + @@ -198,46 +222,100 @@ export default class SearchDefinitionWidget extends TabAwareWidget { doRender() { this.$widget = $(TPL); - this.contentSized(); - this.overflowing(); - this.$searchString = this.$widget.find(".search-string"); - this.$searchString.on('input', () => this.spacedUpdate.scheduleUpdate()); - this.$component = this.$widget.find('.search-definition-widget'); - this.spacedUpdate = new SpacedUpdate(() => this.updateSearch(), 2000); + this.contentSized(); + this.overflowing(); - this.$limitSearchToSubtree = this.$widget.find('.limit-search-to-subtree'); - noteAutocompleteService.initNoteAutocomplete(this.$limitSearchToSubtree); + this.$searchString = this.$widget.find(".search-string"); + this.$searchString.on('input', () => this.searchStringSU.scheduleUpdate()); - this.$limitSearchToSubtree.on('autocomplete:closed', e => { - this.spacedUpdate.scheduleUpdate(); + this.searchStringSU = new SpacedUpdate(async () => { + const searchString = this.$searchString.val(); + + await this.setAttribute('label', 'searchString', searchString); + + if (this.note.title.startsWith('Search: ')) { + await server.put(`notes/${this.noteId}/change-title`, { + title: 'Search: ' + (searchString.length < 30 ? searchString : `${searchString.substr(0, 30)}…`) + }); + } + }, 1000); + + this.$descendantOf = this.$widget.find('.descendant-of'); + noteAutocompleteService.initNoteAutocomplete(this.$descendantOf); + + this.$descendantOf.on('autocomplete:closed', async () => { + const descendantOfNoteId = this.$descendantOf.getSelectedNoteId(); + + await this.setAttribute('relation', 'descendantOf', descendantOfNoteId); }); - this.$searchWithinNoteContent = this.$widget.find('.search-within-note-content'); - this.$searchWithinNoteContent.on('change', () => { - this.spacedUpdate.scheduleUpdate(); + this.$widget.on('click', '[data-search-option-add]', async event => { + const searchOption = $(event.target).attr('data-search-option-add'); + + if (searchOption === 'fastSearch') { + await this.setAttribute('label', 'fastSearch'); + } + else if (searchOption === 'orderBy') { + await this.setAttribute('label', 'orderBy', 'relevancy'); + await this.setAttribute('label', 'orderDirection', 'asc'); + } + else if (searchOption === 'includeArchivedNotes') { + await this.setAttribute('label', 'includeArchivedNotes'); + } + else if (searchOption === 'descendantOf') { + await this.setAttribute('relation', 'descendantOf', 'root'); + } + + this.refresh(); + }); + + this.$widget.on('click', '[data-search-option-del]', async event => { + async function deleteAttr(note, attrName) { + for (const attr of note.getOwnedAttributes()) { + if (attr.name === attrName) { + await server.remove(`notes/${note.noteId}/attributes/${attr.attributeId}`); + } + } + } + + const searchOption = $(event.target).attr('data-search-option-del'); + + await deleteAttr(this.note, searchOption); + + if (searchOption === 'orderBy') { + await deleteAttr(this.note, 'orderDirection'); + } + + await ws.waitForMaxKnownEntityChangeId(); + + this.refresh(); + }); + + this.$orderBy = this.$widget.find('select[name=orderBy]'); + this.$orderBy.on('change', async () => { + const orderBy = this.$orderBy.val(); + + await this.setAttribute('label', 'orderBy', orderBy); + }); + + this.$orderDirection = this.$widget.find('select[name=orderDirection]'); + this.$orderDirection.on('change', async () => { + const orderDirection = this.$orderDirection.val(); + + await this.setAttribute('label', 'orderDirection', orderDirection); }); } - async updateSearch() { - const searchString = this.$searchString.val(); - const subTreeNoteId = this.$limitSearchToSubtree.getSelectedNoteId(); - const includeNoteContent = this.$searchWithinNoteContent.is(":checked"); + async setAttribute(type, name, value = '') { + await server.put(`notes/${this.noteId}/set-attribute`, { + type, + name, + value + }); - await server.put(`notes/${this.noteId}/attributes`, [ - { type: 'label', name: 'searchString', value: searchString }, - { type: 'label', name: 'includeNoteContent', value: includeNoteContent ? 'true' : 'false' }, - subTreeNoteId ? { type: 'label', name: 'subTreeNoteId', value: subTreeNoteId } : undefined, - ].filter(it => !!it)); - - if (this.note.title.startsWith('Search: ')) { - await server.put(`notes/${this.noteId}/change-title`, { - title: 'Search: ' + (searchString.length < 30 ? searchString : `${searchString.substr(0, 30)}…`) - }); - } - - await this.refreshResults(); + await ws.waitForMaxKnownEntityChangeId(); } async refreshResults() { @@ -247,14 +325,26 @@ export default class SearchDefinitionWidget extends TabAwareWidget { async refreshWithNote(note) { this.$component.show(); this.$searchString.val(this.note.getLabelValue('searchString')); - this.$searchWithinNoteContent.prop('checked', this.note.getLabelValue('includeNoteContent') === 'true'); - const subTreeNoteId = this.note.getLabelValue('subTreeNoteId'); - const subTreeNote = subTreeNoteId ? await treeCache.getNote(subTreeNoteId, true) : null; + for (const attrName of ['includeArchivedNotes', 'descendantOf', 'fastSearch', 'orderBy']) { + const has = note.hasLabel(attrName) || note.hasRelation(attrName); - this.$limitSearchToSubtree - .val(subTreeNote ? subTreeNote.title : "") - .setSelectedNotePath(subTreeNoteId); + this.$widget.find(`[data-search-option-add='${attrName}'`).toggle(!has); + this.$widget.find(`[data-search-option-conf='${attrName}'`).toggle(has); + } + + const descendantOfNoteId = this.note.getRelationValue('descendantOf'); + const descendantOfNote = descendantOfNoteId ? await treeCache.getNote(descendantOfNoteId, true) : null; + + this.$descendantOf + .val(descendantOfNote ? descendantOfNote.title : "") + .setSelectedNotePath(descendantOfNoteId); + + + if (note.hasLabel('orderBy')) { + this.$orderBy.val(note.getLabelValue('orderBy')); + this.$orderDirection.val(note.getLabelValue('orderDirection') || 'asc'); + } this.refreshResults(); // important specifically when this search note was not yet refreshed }