erpnext.ProductSearch = class { constructor(opts) { /* Options: search_box_id (for custom search box) */ $.extend(this, opts); this.MAX_RECENT_SEARCHES = 4; this.search_box_id = this.search_box_id || "#search-box"; this.searchBox = $(this.search_box_id); this.setupSearchDropDown(); this.bindSearchAction(); } setupSearchDropDown() { this.search_area = $("#dropdownMenuSearch"); this.setupSearchResultContainer(); this.populateRecentSearches(); } bindSearchAction() { let me = this; // Show Search dropdown this.searchBox.on("focus", () => { this.search_dropdown.removeClass("hidden"); }); // If click occurs outside search input/results, hide results. // Click can happen anywhere on the page $("body").on("click", (e) => { let searchEvent = $(e.target).closest(this.search_box_id).length; 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"); } }); // Process search input this.searchBox.on("input", (e) => { let query = e.target.value; if (query.length == 0) { me.populateResults(null); me.populateCategoriesList(null); } if (query.length < 3 || !query.length) return; frappe.call({ method: "erpnext.templates.pages.product_search.search", args: { query: query }, callback: (data) => { let product_results = null, category_results = null; // 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); } // Populate recent search chips only on successful queries if (!$.isEmptyObject(product_results) || !$.isEmptyObject(category_results)) { me.setRecentSearches(query); } } }); this.search_dropdown.removeClass("hidden"); }); } setupSearchResultContainer() { this.search_dropdown = this.search_area.append(`
`).find("#search-results-container"); this.setupCategoryContainer(); this.setupProductsContainer(); this.setupRecentsContainer(); } setupProductsContainer() { this.products_container = this.search_dropdown.append(`