diff --git a/assets/search/js/form.ts b/assets/search/js/form.ts
index f4fead0670d..e8d3b45eb67 100644
--- a/assets/search/js/form.ts
+++ b/assets/search/js/form.ts
@@ -3,6 +3,7 @@ import i18n from './i18n'
import Renderer from './renderer'
import engine from './engine'
import Spinner from './spinner'
+import { default as Historiographer } from './historiographer'
export default class Form {
// Original page title.
@@ -174,6 +175,10 @@ export default class Form {
this.input.addEventListener('search', () => {
this.submit()
})
+ document.addEventListener('search:input:change', (e) => {
+ this.input.value = e.detail.value
+ this.submit()
+ })
this.language = this.ele.querySelector('.search-filter-lang') as HTMLElement
this.language?.addEventListener('change', () => {
@@ -243,14 +248,21 @@ export default class Form {
const query = this.getQuery()
this.updatePage(query)
- this.spinner.show()
const sorting = this.getSorting()
const lang = this.getLanguage()
const years = this.getYears()
const taxonomies = this.getTaxonomies()
+
+ if (query === '' && Object.values(taxonomies).filter((item) => item.length > 0).length == 0) {
+ this.renderer.renderHistories()
+ return
+ }
+
+ this.spinner.show()
engine.search(query, sorting, lang, years, taxonomies).then(({ results, time }) => {
this.renderer.render(query, results, time)
}).finally(() => {
+ Historiographer.save(query)
this.spinner.hide()
})
}
diff --git a/assets/search/js/historiographer.ts b/assets/search/js/historiographer.ts
new file mode 100644
index 00000000000..8f4a5fcae29
--- /dev/null
+++ b/assets/search/js/historiographer.ts
@@ -0,0 +1,35 @@
+import params from '@params'
+
+class Historiographer {
+ private key = 'search-histories'
+
+ get() {
+ let histories = JSON.parse(localStorage.getItem(this.key) ?? '[]')
+ if (!(histories instanceof Array)) {
+ histories = []
+ }
+
+ return histories.slice(0, params.historiesCount)
+ }
+
+ save(query: string): void {
+ if (query === '') {
+ return
+ }
+
+ let histories = this.get()
+ histories = histories.filter((history) => history.query !== query)
+ histories.unshift({
+ query: query,
+ date: (new Date()),
+ })
+
+ if (histories.length > params.historiesCount) {
+ histories.pop()
+ }
+
+ localStorage.setItem(this.key, JSON.stringify(histories))
+ }
+}
+
+export default (new Historiographer())
diff --git a/assets/search/js/modal.ts b/assets/search/js/modal.ts
index 51ed22ecdee..0c54ad76303 100644
--- a/assets/search/js/modal.ts
+++ b/assets/search/js/modal.ts
@@ -21,14 +21,16 @@ export default class Modal {
private container: HTMLElement
+ private renderer: Renderer
+
private form: Form
private shortcuts: Shortcuts
constructor() {
const spinner = new Spinner('.search-modal .search-spinner')
- const renderer = new Renderer('.search-modal .search-results', '.search-modal .search-stat', spinner)
- this.form = new Form(spinner, renderer)
+ this.renderer = new Renderer('.search-modal .search-results', '.search-modal .search-stat', spinner)
+ this.form = new Form(spinner, this.renderer)
this.shortcuts = new Shortcuts([
closeShortcut,
searchShortcut,
@@ -99,6 +101,7 @@ export default class Modal {
${this.renderFooter()}
`
this.wrapper.appendChild(this.container)
+ this.renderer.renderHistories()
}
renderFooter(): string {
diff --git a/assets/search/js/renderer.ts b/assets/search/js/renderer.ts
index 7cbe1027f31..80a9c5b5074 100644
--- a/assets/search/js/renderer.ts
+++ b/assets/search/js/renderer.ts
@@ -1,6 +1,7 @@
import { default as params } from '@params'
import i18n from './i18n'
import Spinner from './spinner'
+import { default as Historiographer } from './historiographer'
export default class Renderer {
private initialized = false
@@ -209,6 +210,33 @@ export default class Renderer {
observer.observe(container, { childList: true });
}
+ renderHistories() {
+ this.results = []
+ this.clean()
+ const histories = Historiographer.get()
+ let html = ''
+ histories.forEach((history) => {
+ html += `
+