You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.1 KiB
68 lines
2.1 KiB
import "./common.js";
|
|
//#region js/search.js
|
|
function getQuery() {
|
|
return new URLSearchParams(window.location.search).get("q")?.trim() || "";
|
|
}
|
|
function matchItem($item, type, keyword) {
|
|
const itemType = $item.attr("data-type") || "";
|
|
if (type !== "all" && itemType !== type) return false;
|
|
if (!keyword) return true;
|
|
return [
|
|
$item.attr("data-keywords") || "",
|
|
$item.find(".sr-item__title").text(),
|
|
$item.find(".sr-item__desc").text(),
|
|
$item.find(".sr-item__type").text()
|
|
].join(" ").toLowerCase().includes(keyword.toLowerCase());
|
|
}
|
|
function updateCounts($items) {
|
|
const keyword = ($(".sr-search .s-search__input").val() || "").trim();
|
|
[
|
|
"all",
|
|
"product",
|
|
"solution",
|
|
"news",
|
|
"faq"
|
|
].forEach((type) => {
|
|
const count = $items.filter((_, el) => matchItem($(el), type, keyword)).length;
|
|
$(`[data-count="${type}"]`).text(String(count));
|
|
});
|
|
}
|
|
function applyFilter() {
|
|
const type = $(".sr-tabs__item.is-active").attr("data-type") || "all";
|
|
const keyword = ($(".sr-search .s-search__input").val() || "").trim();
|
|
const $items = $(".sr-item");
|
|
let visible = 0;
|
|
$items.each(function() {
|
|
const show = matchItem($(this), type, keyword);
|
|
this.hidden = !show;
|
|
if (show) visible += 1;
|
|
});
|
|
updateCounts($items);
|
|
$(".sr-empty").prop("hidden", visible > 0);
|
|
}
|
|
$(function() {
|
|
const q = getQuery();
|
|
if (q) $(".sr-search .s-search__input").val(q);
|
|
$(".sr-tabs__item").on("click", function() {
|
|
const $btn = $(this);
|
|
$btn.addClass("is-active").attr("aria-selected", "true");
|
|
$btn.siblings(".sr-tabs__item").removeClass("is-active").attr("aria-selected", "false");
|
|
applyFilter();
|
|
});
|
|
$(".sr-hot__tag").on("click", function() {
|
|
const keyword = $(this).attr("data-keyword") || $(this).text().trim();
|
|
$(".sr-search .s-search__input").val(keyword);
|
|
applyFilter();
|
|
});
|
|
$(".sr-search").on("submit", function(e) {
|
|
e.preventDefault();
|
|
const keyword = ($(".sr-search .s-search__input").val() || "").trim();
|
|
const url = new URL(window.location.href);
|
|
if (keyword) url.searchParams.set("q", keyword);
|
|
else url.searchParams.delete("q");
|
|
window.history.replaceState({}, "", url);
|
|
applyFilter();
|
|
});
|
|
applyFilter();
|
|
});
|
|
//#endregion
|