diff --git a/frontend/src/routes/search/Search.svelte b/frontend/src/routes/search/Search.svelte
index 14d6ee60..9b49868f 100644
--- a/frontend/src/routes/search/Search.svelte
+++ b/frontend/src/routes/search/Search.svelte
@@ -8,25 +8,38 @@
import Bookmark from '../bookmark/Template.svelte';
+ let fulltext = false;
+ let counter = 0;
const router = useTinyRouter();
- let counter = 9;
+ let key = $derived(router.getQueryParam('key'));
+ search.key = key;
let bookmarks = $state(null);
let companies = $state(null);
let documents = $state(null);
- let fulltext = false;
let notes = $state(null);
let pages = $state(null);
let projects = $state(null);
let stock = $state(null);
+ let tags = $state(null);
let tasks = $state(null);
let times = $state(null);
-
-
- function doSearch(key){
+ function doSearch(){
+ if (counter > 0) return;
warn(t('searching…'));
+ counter = 10;
+ bookmarks = null;
+ companies = null;
+ documents = null;
+ notes = null;
+ pages = null;
+ projects = null;
+ stock = null;
+ tags = null;
+ tasks = null;
+ times = null;
- const data = { key : key, fulltext : fulltext };
+ let data = { key, fulltext : fulltext };
post(api('bookmark/search'),data).then(handleBookmarks);
post(api('company/search '),data).then(handleCompanies);
post(api('document/search'),data).then(handleDocuments);
@@ -110,6 +123,87 @@
}
}
+
+ async function handleTags(resp){
+ if (resp.ok){
+ const json = await resp.json();
+ let map = {};
+ for (let [module, ids] of Object.entries(json)){
+ let list = {};
+ let path = module;
+ map[module] = {};
+ switch(module){
+ case 'bookmark': {
+ if (!bookmarks) bookmarks = {};
+ list = bookmarks;
+ break;
+ }
+ case 'company': {
+ if (!companies) companies = {};
+ list = companies;
+ break;
+ }
+ case 'document': {
+ if (!documents) documents = {};
+ list = documents;
+ break;
+ }
+ case 'note': {
+ if (!notes) notes = {};
+ list = notes;
+ break;
+ }
+ case 'wiki': {
+ if (!pages) pages = {};
+ list = pages;
+ break;
+ }
+ case 'project': {
+ if (!projects) projects = {};
+ list = projects;
+ break;
+ }
+ case 'stock': {
+ if (!stock) stock = { items : {}};
+ list = stock.items;
+ path = 'stock/item';
+ break;
+ }
+ case 'task': {
+ if (!tasks) tasks = {};
+ list = tasks;
+ break;
+ }
+ case 'time': {
+ if (!times) times = {};
+ list = times;
+ break;
+ }
+ default:{
+ console.warn("no handler for "+module+" with tag "+key);
+ continue;
+ }
+ }
+ for (let id of ids){
+ if (!list[id]) {
+ const url = api(`${path}/${id}`);
+ const resp = await get(url);
+ if (resp.ok) {
+ list[id] = await resp.json();
+ } else {
+ console.warn(`Failed to fetch ${module} ${id}: ${resp.statusText}`);
+ }
+ }
+ map[module][id] = list[id];
+ }
+ }
+ tags = map;
+ } else {
+ error(resp);
+ }
+ quitOne();
+ }
+
async function handleTasks(resp){
quitOne();
if (resp.ok){
@@ -142,16 +236,18 @@
function onclick(e){
e.preventDefault();
- var target = e.target;
- while (target && !target.href) target=target.parentNode;
+ let target = e.target;
let href = target.getAttribute('href');
- if (href) router.navigate(href);
+ target = target.getAttribute('target');
+ if (target == '_blank') {
+ window.open(href);
+ } else if (href) router.navigate(href);
return false;
}
async function setKey(ev){
if (ev) ev.preventDefault();
- doSearch(search.key);
+ doSearch();
}
async function setTitle(resp,key,module){
@@ -169,26 +265,28 @@
counter--;
if (counter > 0) {
warn(t('searching…')+" "+counter);
+ if (counter == 1) {
+ let data = { key, fulltext : fulltext };
+ post(api('tags/search'),data).then(handleTags);
+ }
} else yikes();
}
function updateUrl(ev){
ev.preventDefault();
- let url = window.location.origin + window.location.pathname;
- if (search.key) {
- url += '?key=' + encodeURI(search.key);
- window.history.replaceState(history.state, '', url);
- doSearch(search.key);
- }
+ if (search.key) router.navigate(window.location.pathname + '?key=' + encodeURI(search.key));
}
$effect(() => {
- doSearch(router.getQueryParam('key'));
+ key = router.getQueryParam('key');
+ doSearch();
});
+
+