added "limit" search modifier to search definition

This commit is contained in:
zadam 2021-02-13 23:52:52 +01:00
parent a78c8ddad7
commit f528799fed
6 changed files with 61 additions and 3 deletions

View File

@ -0,0 +1,49 @@
import AbstractSearchOption from "./abstract_search_option.js";
const TPL = `
<tr data-search-option-conf="limit">
<td class="title-column">
<span class="bx bx-stop"></span>
Limit
</td>
<td>
<input name="limit" class="form-control" type="number" min="1" step="1" />
</td>
<td class="button-column">
<div class="dropdown help-dropdown">
<span class="bx bx-help-circle icon-action" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu dropdown-menu-right p-4">
Take only first X specified results.
</div>
</div>
<span class="bx bx-x icon-action search-option-del"></span>
</td>
</tr>`;
export default class Limit extends AbstractSearchOption {
static get optionName() { return "limit" };
static get attributeType() { return "label" };
static async create(noteId) {
await AbstractSearchOption.setAttribute(noteId, 'label', 'limit', '10');
}
doRender() {
const $option = $(TPL);
this.$limit = $option.find('input[name=limit]');
this.$limit.on('change', () => this.update());
this.$limit.on('input', () => this.update());
this.$limit.val(this.note.getLabelValue('limit'));
return $option;
}
async update() {
const limit = this.$limit.val();
await this.setAttribute('label', 'limit', limit);
}
}

View File

@ -18,6 +18,7 @@ import Ancestor from "../search_options/ancestor.js";
import IncludeArchivedNotes from "../search_options/include_archived_notes.js";
import OrderBy from "../search_options/order_by.js";
import SearchScript from "../search_options/search_script.js";
import Limit from "../search_options/limit.js";
const TPL = `
<div class="search-definition-widget">
@ -106,6 +107,11 @@ const TPL = `
order by
</button>
<button type="button" class="btn btn-sm" data-search-option-add="limit" title="Limit number of results">
<span class="bx bx-stop"></span>
limit
</button>
<div class="dropdown" style="display: inline-block;">
<button class="btn btn-sm dropdown-toggle action-add-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="bx bxs-zap"></span>
@ -163,7 +169,8 @@ const OPTION_CLASSES = [
Ancestor,
FastSearch,
IncludeArchivedNotes,
OrderBy
OrderBy,
Limit
];
const ACTION_CLASSES = {};

View File

@ -22,6 +22,7 @@ async function search(note) {
includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
orderBy: note.getLabelValue('orderBy'),
orderDirection: note.getLabelValue('orderDirection'),
limit: note.getLabelValue('limit'),
fuzzyAttributeSearch: false
});

View File

@ -14,7 +14,7 @@ class OrderByAndLimitExp extends Expression {
od.larger = od.direction === "asc" ? 1 : -1;
}
this.limit = limit;
this.limit = limit || 0;
/** @type {Expression} */
this.subExpression = null; // it's expected to be set after construction

View File

@ -10,6 +10,7 @@ class SearchContext {
this.includeArchivedNotes = !!params.includeArchivedNotes;
this.orderBy = params.orderBy;
this.orderDirection = params.orderDirection;
this.limit = params.limit;
this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch;
this.highlightedTokens = [];
this.originalQuery = "";

View File

@ -421,7 +421,7 @@ function parse({fulltextTokens, expressionTokens, searchContext}) {
exp = new OrderByAndLimitExp([{
valueExtractor: new ValueExtractor(searchContext, ['note', searchContext.orderBy]),
direction: searchContext.orderDirection
}], 0);
}], searchContext.limit);
exp.subExpression = filterExp;
}