Notes/apps/client/src/widgets/search_result.ts

80 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-09-24 09:57:16 +08:00
import { t } from "../services/i18n.js";
2021-05-22 12:35:41 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import NoteListRenderer from "../services/note_list_renderer.js";
2025-03-16 00:45:46 +02:00
import type FNote from "../entities/fnote.js";
import type { EventData } from "../components/app_context.js";
const TPL = /*html*/`
<div class="search-result-widget">
<style>
.search-result-widget {
flex-grow: 100000;
flex-shrink: 100000;
min-height: 0;
overflow: auto;
}
2025-03-16 00:45:46 +02:00
.search-result-widget .note-list {
padding: 10px;
}
2025-03-16 00:45:46 +02:00
.search-no-results, .search-not-executed-yet {
margin: 20px;
padding: 20px;
}
</style>
2025-03-16 00:45:46 +02:00
<div class="search-no-results alert alert-info">
2025-01-09 18:07:02 +02:00
${t("search_result.no_notes_found")}
</div>
2025-03-16 00:45:46 +02:00
<div class="search-not-executed-yet alert alert-info">
2025-01-09 18:07:02 +02:00
${t("search_result.search_not_executed")}
</div>
2025-03-16 00:45:46 +02:00
<div class="search-result-widget-content">
</div>
</div>`;
2021-05-22 12:35:41 +02:00
export default class SearchResultWidget extends NoteContextAwareWidget {
2025-03-16 00:45:46 +02:00
private $content!: JQuery<HTMLElement>;
private $noResults!: JQuery<HTMLElement>;
private $notExecutedYet!: JQuery<HTMLElement>;
isEnabled() {
2025-03-16 00:45:46 +02:00
return super.isEnabled() && this.note?.type === "search";
}
doRender() {
this.$widget = $(TPL);
2021-07-02 23:22:10 +02:00
this.contentSized();
2025-01-09 18:07:02 +02:00
this.$content = this.$widget.find(".search-result-widget-content");
this.$noResults = this.$widget.find(".search-no-results");
this.$notExecutedYet = this.$widget.find(".search-not-executed-yet");
}
2025-03-16 00:45:46 +02:00
async refreshWithNote(note: FNote) {
this.$content.empty();
this.$noResults.toggle(note.getChildNoteIds().length === 0 && !!note.searchResultsLoaded);
this.$notExecutedYet.toggle(!note.searchResultsLoaded);
2021-02-20 20:10:45 +01:00
const noteListRenderer = new NoteListRenderer(this.$content, note, note.getChildNoteIds(), true);
await noteListRenderer.renderList();
}
2025-03-16 00:45:46 +02:00
searchRefreshedEvent({ ntxId }: EventData<"searchRefreshed">) {
2021-05-22 12:42:34 +02:00
if (!this.isNoteContext(ntxId)) {
return;
}
this.refresh();
}
2025-03-16 00:45:46 +02:00
notesReloadedEvent({ noteIds }: EventData<"notesReloaded">) {
if (this.noteId && noteIds.includes(this.noteId)) {
this.refresh();
}
}
}