2021-04-29 14:31:31 +00:00
|
|
|
let loading = false;
|
2021-04-21 08:22:23 +00:00
|
|
|
|
2021-05-05 10:34:22 +00:00
|
|
|
const MAX_RECENT_SEARCHES = 4;
|
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
const searchBox = document.getElementById("search-box");
|
2021-05-05 10:34:22 +00:00
|
|
|
const searchButton = document.getElementById("search-button");
|
2021-04-21 08:22:23 +00:00
|
|
|
const results = document.getElementById("results");
|
2021-04-26 05:45:20 +00:00
|
|
|
const categoryList = document.getElementById("category-suggestions");
|
2021-05-05 08:17:43 +00:00
|
|
|
const showBrandLine = document.getElementById("show-brand-line");
|
2021-05-05 10:34:22 +00:00
|
|
|
const recentSearchArea = document.getElementById("recent-search-chips");
|
|
|
|
|
|
|
|
function getRecentSearches() {
|
|
|
|
return JSON.parse(localStorage.getItem("recent_searches") || "[]");
|
|
|
|
}
|
|
|
|
|
|
|
|
function attachEventListenersToChips() {
|
|
|
|
const chips = document.getElementsByClassName("recent-chip");
|
|
|
|
|
|
|
|
for (let chip of chips) {
|
|
|
|
chip.addEventListener("click", () => {
|
|
|
|
searchBox.value = chip.innerText;
|
|
|
|
|
|
|
|
// Start search with `recent query`
|
|
|
|
const event = new Event("input");
|
|
|
|
searchBox.dispatchEvent(event);
|
|
|
|
searchBox.focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateRecentSearches() {
|
|
|
|
let recents = getRecentSearches();
|
|
|
|
|
|
|
|
if (!recents.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
html = "Recent Searches: ";
|
|
|
|
for (let query of recents) {
|
|
|
|
html += `<button class="btn btn-secondary btn-sm recent-chip mr-1">${query}</button>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
recentSearchArea.innerHTML = html;
|
|
|
|
attachEventListenersToChips();
|
|
|
|
}
|
2021-04-21 08:22:23 +00:00
|
|
|
|
|
|
|
function populateResults(data) {
|
|
|
|
html = ""
|
|
|
|
for (let res of data.message) {
|
2021-04-29 14:31:31 +00:00
|
|
|
html += `<li class="list-group-item list-group-item-action">
|
2021-05-04 05:38:45 +00:00
|
|
|
<img class="item-thumb" src="${res.thumbnail || 'img/placeholder.png'}" />
|
2021-05-05 08:17:43 +00:00
|
|
|
<a href="/${res.route}">${res.web_item_name} <span class="brand-line">${showBrandLine && res.brand ? "by " + res.brand : ""}</span></a>
|
2021-04-22 08:51:29 +00:00
|
|
|
</li>`
|
2021-04-21 08:22:23 +00:00
|
|
|
}
|
|
|
|
results.innerHTML = html;
|
|
|
|
}
|
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
function populateCategoriesList(data) {
|
|
|
|
if (data.length === 0) {
|
2021-04-29 14:31:31 +00:00
|
|
|
categoryList.innerHTML = 'Type something...';
|
2021-04-26 05:45:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
html = ""
|
|
|
|
for (let category of data.message) {
|
|
|
|
html += `<li>${category}</li>`
|
|
|
|
}
|
|
|
|
|
|
|
|
categoryList.innerHTML = html;
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:31:31 +00:00
|
|
|
function updateLoadingState() {
|
|
|
|
if (loading) {
|
|
|
|
results.innerHTML = `<div class="spinner-border"><span class="sr-only">loading...<span></div>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
searchBox.addEventListener("input", (e) => {
|
2021-04-29 14:31:31 +00:00
|
|
|
loading = true;
|
|
|
|
updateLoadingState();
|
2021-04-21 08:22:23 +00:00
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.templates.pages.product_search.search",
|
|
|
|
args: {
|
|
|
|
query: e.target.value
|
|
|
|
},
|
|
|
|
callback: (data) => {
|
|
|
|
populateResults(data);
|
2021-04-29 14:31:31 +00:00
|
|
|
loading = false;
|
2021-04-21 08:22:23 +00:00
|
|
|
}
|
2021-04-26 05:45:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// If there is a suggestion list node
|
|
|
|
if (categoryList) {
|
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.templates.pages.product_search.get_category_suggestions",
|
|
|
|
args: {
|
|
|
|
query: e.target.value
|
|
|
|
},
|
|
|
|
callback: (data) => {
|
|
|
|
populateCategoriesList(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-05 10:34:22 +00:00
|
|
|
searchButton.addEventListener("click", (e) => {
|
|
|
|
let query = searchBox.value;
|
|
|
|
if (!query) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let recents = getRecentSearches();
|
|
|
|
|
|
|
|
if (recents.length >= MAX_RECENT_SEARCHES) {
|
|
|
|
// Remove the `First` query
|
|
|
|
recents.splice(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recents.indexOf(query) >= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
recents.push(query);
|
|
|
|
|
|
|
|
localStorage.setItem("recent_searches", JSON.stringify(recents));
|
|
|
|
|
|
|
|
// Refresh recent searches
|
|
|
|
populateRecentSearches();
|
|
|
|
});
|
|
|
|
|
|
|
|
populateRecentSearches();
|