Use url query string to store item search queries
This commit is contained in:
parent
8fc374e4c0
commit
60451e8f78
1 changed files with 35 additions and 7 deletions
42
items.js
42
items.js
|
@ -7,10 +7,14 @@ class ExprField {
|
||||||
this.output = null;
|
this.output = null;
|
||||||
this.text = null;
|
this.text = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get value() {
|
||||||
|
return this.field.value;
|
||||||
|
}
|
||||||
|
|
||||||
compile() {
|
compile() {
|
||||||
if (this.field.value === this.text) return;
|
if (this.value === this.text) return false;
|
||||||
this.text = this.field.value;
|
this.text = this.value;
|
||||||
this.errorText.innerText = '';
|
this.errorText.innerText = '';
|
||||||
try {
|
try {
|
||||||
this.output = this.compiler(this.text);
|
this.output = this.compiler(this.text);
|
||||||
|
@ -18,6 +22,7 @@ class ExprField {
|
||||||
this.errorText.innerText = e.message;
|
this.errorText.innerText = e.message;
|
||||||
this.output = null;
|
this.output = null;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,10 +94,14 @@ function init() {
|
||||||
itemListFooter.innerText = '';
|
itemListFooter.innerText = '';
|
||||||
for (const itemEntry of itemEntries) itemEntry.style.display = 'none';
|
for (const itemEntry of itemEntries) itemEntry.style.display = 'none';
|
||||||
|
|
||||||
// compile query expressions, aborting if either fails to compile
|
// compile query expressions, aborting if nothing has changed or either fails to compile
|
||||||
searchFilterField.compile();
|
const changed = searchFilterField.compile() | searchSortField.compile();
|
||||||
searchSortField.compile();
|
if (!changed || searchFilterField.output === null || searchSortField.output === null) return;
|
||||||
if (searchFilterField.output === null || searchSortField.output === null) return;
|
|
||||||
|
// update url query string
|
||||||
|
const newUrl = `${window.location.protocol}//${window.location.host}${window.location.pathname}`
|
||||||
|
+ `?f=${encodeURIComponent(searchFilterField.value)}&s=${encodeURIComponent(searchSortField.value)}`;
|
||||||
|
window.history.pushState({ path: newUrl }, '', newUrl);
|
||||||
|
|
||||||
// index and sort search results
|
// index and sort search results
|
||||||
const searchResults = [];
|
const searchResults = [];
|
||||||
|
@ -158,8 +167,27 @@ function init() {
|
||||||
searchFilterField.field.addEventListener('input', e => scheduleSearchUpdate());
|
searchFilterField.field.addEventListener('input', e => scheduleSearchUpdate());
|
||||||
searchSortField.field.addEventListener('input', e => scheduleSearchUpdate());
|
searchSortField.field.addEventListener('input', e => scheduleSearchUpdate());
|
||||||
|
|
||||||
// display initial items and focus the filter field
|
// parse query string, display initial search results
|
||||||
|
if (window.location.search.startsWith('?')) {
|
||||||
|
for (const entryStr of window.location.search.substring(1).split('&')) {
|
||||||
|
const ndx = entryStr.indexOf('=');
|
||||||
|
if (ndx !== -1) {
|
||||||
|
console.log(entryStr.substring(0, ndx));
|
||||||
|
console.log(entryStr.substring(ndx + 1));
|
||||||
|
switch (entryStr.substring(0, ndx)) {
|
||||||
|
case 'f':
|
||||||
|
searchFilterField.field.value = decodeURIComponent(entryStr.substring(ndx + 1));
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
searchSortField.field.value = decodeURIComponent(entryStr.substring(ndx + 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
updateSearch();
|
updateSearch();
|
||||||
|
|
||||||
|
// focus the query filter text box
|
||||||
searchFilterField.field.focus();
|
searchFilterField.field.focus();
|
||||||
searchFilterField.field.select();
|
searchFilterField.field.select();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue