2021-06-01 07:14:49 +00:00
|
|
|
erpnext.ProductSearch = class {
|
2021-11-29 08:15:33 +00:00
|
|
|
constructor(opts) {
|
2021-11-29 08:53:32 +00:00
|
|
|
/* Options: search_box_id (for custom search box) */
|
2021-11-29 08:15:33 +00:00
|
|
|
$.extend(this, opts);
|
2021-06-01 07:14:49 +00:00
|
|
|
this.MAX_RECENT_SEARCHES = 4;
|
2021-11-29 08:53:32 +00:00
|
|
|
this.search_box_id = this.search_box_id || "#search-box";
|
|
|
|
this.searchBox = $(this.search_box_id);
|
2021-06-01 07:14:49 +00:00
|
|
|
|
|
|
|
this.setupSearchDropDown();
|
|
|
|
this.bindSearchAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSearchDropDown() {
|
|
|
|
this.search_area = $("#dropdownMenuSearch");
|
|
|
|
this.setupSearchResultContainer();
|
|
|
|
this.populateRecentSearches();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindSearchAction() {
|
|
|
|
let me = this;
|
|
|
|
|
2021-06-02 13:02:35 +00:00
|
|
|
// Show Search dropdown
|
2021-06-02 07:54:06 +00:00
|
|
|
this.searchBox.on("focus", () => {
|
2021-06-01 07:14:49 +00:00
|
|
|
this.search_dropdown.removeClass("hidden");
|
|
|
|
});
|
|
|
|
|
2021-06-02 13:02:35 +00:00
|
|
|
// If click occurs outside search input/results, hide results.
|
|
|
|
// Click can happen anywhere on the page
|
|
|
|
$("body").on("click", (e) => {
|
2021-11-29 08:53:32 +00:00
|
|
|
let searchEvent = $(e.target).closest(this.search_box_id).length;
|
2021-06-02 13:02:35 +00:00
|
|
|
let resultsEvent = $(e.target).closest('#search-results-container').length;
|
|
|
|
let isResultHidden = this.search_dropdown.hasClass("hidden");
|
|
|
|
|
|
|
|
if (!searchEvent && !resultsEvent && !isResultHidden) {
|
|
|
|
this.search_dropdown.addClass("hidden");
|
|
|
|
}
|
2021-06-01 07:14:49 +00:00
|
|
|
});
|
|
|
|
|
2021-06-02 13:02:35 +00:00
|
|
|
// Process search input
|
2021-06-01 07:14:49 +00:00
|
|
|
this.searchBox.on("input", (e) => {
|
|
|
|
let query = e.target.value;
|
|
|
|
|
2021-07-07 11:00:56 +00:00
|
|
|
if (query.length == 0) {
|
2021-09-01 09:27:50 +00:00
|
|
|
me.populateResults(null);
|
|
|
|
me.populateCategoriesList(null);
|
2021-07-07 11:00:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
if (query.length < 3 || !query.length) return;
|
|
|
|
|
|
|
|
frappe.call({
|
2021-08-13 08:53:52 +00:00
|
|
|
method: "erpnext.templates.pages.product_search.search",
|
2021-06-01 07:14:49 +00:00
|
|
|
args: {
|
|
|
|
query: query
|
|
|
|
},
|
|
|
|
callback: (data) => {
|
2021-09-01 09:27:50 +00:00
|
|
|
let product_results = null, category_results = null;
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-09-01 09:27:50 +00:00
|
|
|
// Populate product results
|
|
|
|
product_results = data.message ? data.message.product_results : null;
|
|
|
|
me.populateResults(product_results);
|
|
|
|
|
|
|
|
// Populate categories
|
|
|
|
if (me.category_container) {
|
|
|
|
category_results = data.message ? data.message.category_results : null;
|
|
|
|
me.populateCategoriesList(category_results);
|
2021-06-01 07:14:49 +00:00
|
|
|
}
|
2021-09-01 09:27:50 +00:00
|
|
|
|
|
|
|
// Populate recent search chips only on successful queries
|
|
|
|
if (!$.isEmptyObject(product_results) || !$.isEmptyObject(category_results)) {
|
|
|
|
me.setRecentSearches(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-06-01 07:14:49 +00:00
|
|
|
|
|
|
|
this.search_dropdown.removeClass("hidden");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSearchResultContainer() {
|
|
|
|
this.search_dropdown = this.search_area.append(`
|
|
|
|
<div class="overflow-hidden shadow dropdown-menu w-100 hidden"
|
|
|
|
id="search-results-container"
|
|
|
|
aria-labelledby="dropdownMenuSearch"
|
2021-07-11 21:58:33 +00:00
|
|
|
style="display: flex; flex-direction: column;">
|
2021-06-01 07:14:49 +00:00
|
|
|
</div>
|
|
|
|
`).find("#search-results-container");
|
2021-07-11 21:58:33 +00:00
|
|
|
|
2021-07-19 07:57:17 +00:00
|
|
|
this.setupCategoryContainer();
|
2021-07-11 21:58:33 +00:00
|
|
|
this.setupProductsContainer();
|
|
|
|
this.setupRecentsContainer();
|
2021-06-01 07:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setupProductsContainer() {
|
2021-07-11 21:58:33 +00:00
|
|
|
this.products_container = this.search_dropdown.append(`
|
|
|
|
<div id="product-results mt-2">
|
|
|
|
<div id="product-scroll" style="overflow: scroll; max-height: 300px">
|
2021-06-01 07:14:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`).find("#product-scroll");
|
|
|
|
}
|
|
|
|
|
2021-07-11 21:58:33 +00:00
|
|
|
setupCategoryContainer() {
|
|
|
|
this.category_container = this.search_dropdown.append(`
|
|
|
|
<div class="category-container mt-2 mb-1">
|
|
|
|
<div class="category-chips">
|
2021-06-01 07:14:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-07-11 21:58:33 +00:00
|
|
|
`).find(".category-chips");
|
|
|
|
}
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-07-11 21:58:33 +00:00
|
|
|
setupRecentsContainer() {
|
|
|
|
let $recents_section = this.search_dropdown.append(`
|
|
|
|
<div class="mb-2 mt-2 recent-searches">
|
|
|
|
<div>
|
|
|
|
<b>${ __("Recent") }</b>
|
2021-06-01 07:14:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`).find(".recent-searches");
|
|
|
|
|
|
|
|
this.recents_container = $recents_section.append(`
|
2021-07-11 21:58:33 +00:00
|
|
|
<div id="recents" style="padding: .25rem 0 1rem 0;">
|
2021-06-01 07:14:49 +00:00
|
|
|
</div>
|
2021-07-11 21:58:33 +00:00
|
|
|
`).find("#recents");
|
2021-06-01 07:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getRecentSearches() {
|
|
|
|
return JSON.parse(localStorage.getItem("recent_searches") || "[]");
|
|
|
|
}
|
|
|
|
|
|
|
|
attachEventListenersToChips() {
|
|
|
|
let me = this;
|
2021-07-11 21:58:33 +00:00
|
|
|
const chips = $(".recent-search");
|
2021-06-01 07:14:49 +00:00
|
|
|
window.chips = chips;
|
|
|
|
|
|
|
|
for (let chip of chips) {
|
|
|
|
chip.addEventListener("click", () => {
|
2021-07-11 21:58:33 +00:00
|
|
|
me.searchBox[0].value = chip.innerText.trim();
|
2021-06-01 07:14:49 +00:00
|
|
|
|
|
|
|
// Start search with `recent query`
|
|
|
|
me.searchBox.trigger("input");
|
|
|
|
me.searchBox.focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setRecentSearches(query) {
|
|
|
|
let recents = this.getRecentSearches();
|
|
|
|
if (recents.length >= this.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));
|
|
|
|
|
|
|
|
this.populateRecentSearches();
|
|
|
|
}
|
|
|
|
|
|
|
|
populateRecentSearches() {
|
|
|
|
let recents = this.getRecentSearches();
|
|
|
|
|
|
|
|
if (!recents.length) {
|
2021-07-11 21:58:33 +00:00
|
|
|
this.recents_container.html(`<span class=""text-muted">No searches yet.</span>`);
|
2021-06-01 07:14:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let html = "";
|
|
|
|
recents.forEach((key) => {
|
2021-06-08 14:10:26 +00:00
|
|
|
html += `
|
2021-07-11 21:58:33 +00:00
|
|
|
<div class="recent-search mr-1" style="font-size: 13px">
|
|
|
|
<span class="mr-2">
|
|
|
|
<svg width="20" height="20" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<path d="M8 14C11.3137 14 14 11.3137 14 8C14 4.68629 11.3137 2 8 2C4.68629 2 2 4.68629 2 8C2 11.3137 4.68629 14 8 14Z" stroke="var(--gray-500)"" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
|
|
|
|
<path d="M8.00027 5.20947V8.00017L10 10" stroke="var(--gray-500)" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
|
|
|
|
</svg>
|
|
|
|
</span>
|
2021-06-08 14:10:26 +00:00
|
|
|
${ key }
|
2021-07-11 21:58:33 +00:00
|
|
|
</div>
|
2021-06-08 14:10:26 +00:00
|
|
|
`;
|
2021-06-01 07:14:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.recents_container.html(html);
|
|
|
|
this.attachEventListenersToChips();
|
|
|
|
}
|
|
|
|
|
2021-09-01 09:27:50 +00:00
|
|
|
populateResults(product_results) {
|
|
|
|
if (!product_results || product_results.length === 0) {
|
2021-07-11 21:58:33 +00:00
|
|
|
let empty_html = ``;
|
2021-06-02 13:02:35 +00:00
|
|
|
this.products_container.html(empty_html);
|
2021-06-01 07:14:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let html = "";
|
|
|
|
|
2021-09-01 09:27:50 +00:00
|
|
|
product_results.forEach((res) => {
|
2021-07-08 14:04:07 +00:00
|
|
|
let thumbnail = res.thumbnail || '/assets/erpnext/images/ui-states/cart-empty-state.png';
|
2021-06-01 07:14:49 +00:00
|
|
|
html += `
|
|
|
|
<div class="dropdown-item" style="display: flex;">
|
2021-07-08 14:04:07 +00:00
|
|
|
<img class="item-thumb col-2" src=${thumbnail} />
|
2021-06-01 07:14:49 +00:00
|
|
|
<div class="col-9" style="white-space: normal;">
|
|
|
|
<a href="/${res.route}">${res.web_item_name}</a><br>
|
|
|
|
<span class="brand-line">${res.brand ? "by " + res.brand : ""}</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.products_container.html(html);
|
|
|
|
}
|
|
|
|
|
2021-09-01 09:27:50 +00:00
|
|
|
populateCategoriesList(category_results) {
|
|
|
|
if (!category_results || category_results.length === 0) {
|
2021-06-01 07:14:49 +00:00
|
|
|
let empty_html = `
|
2021-07-11 21:58:33 +00:00
|
|
|
<div class="category-container mt-2">
|
|
|
|
<div class="category-chips">
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-01 07:14:49 +00:00
|
|
|
`;
|
|
|
|
this.category_container.html(empty_html);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-11 21:58:33 +00:00
|
|
|
let html = `
|
|
|
|
<div class="mb-2">
|
|
|
|
<b>${ __("Categories") }</b>
|
|
|
|
</div>
|
|
|
|
`;
|
2021-09-01 09:27:50 +00:00
|
|
|
|
|
|
|
category_results.forEach((category) => {
|
2021-06-01 07:14:49 +00:00
|
|
|
html += `
|
2021-07-11 21:58:33 +00:00
|
|
|
<a href="/${category.route}" class="btn btn-sm category-chip mr-2 mb-2"
|
|
|
|
style="font-size: 13px" role="button">
|
|
|
|
${ category.name }
|
|
|
|
</button>
|
2021-06-01 07:14:49 +00:00
|
|
|
`;
|
2021-06-02 07:54:06 +00:00
|
|
|
});
|
2021-06-01 07:14:49 +00:00
|
|
|
|
|
|
|
this.category_container.html(html);
|
|
|
|
}
|
2021-06-02 07:54:06 +00:00
|
|
|
};
|