3 changed files with 98 additions and 2 deletions
-
30public/themes/dist_static/static/js/search.js
-
35public/themes/website/1/zh/demo/search.html
-
35public/themes/website/1/zh/demo/single_search.html
@ -1,13 +1,43 @@ |
|||||
import "./common.js"; |
import "./common.js"; |
||||
import "./pagination.js"; |
import "./pagination.js"; |
||||
//#region js/search.js
|
//#region js/search.js
|
||||
|
function matchItem($item, type) { |
||||
|
const itemType = $item.attr("data-type") || ""; |
||||
|
return type === "all" || itemType === type; |
||||
|
} |
||||
|
function updateCounts($items) { |
||||
|
["all", "product", "solution", "news", "faq"].forEach((type) => { |
||||
|
const count = $items.filter((_, el) => matchItem($(el), type)).length; |
||||
|
$(`[data-count="${type}"]`).text(String(count)); |
||||
|
}); |
||||
|
} |
||||
|
function applyFilter() { |
||||
|
const type = $(".sr-tabs__item.is-active").attr("data-type") || "all"; |
||||
|
const $items = $(".sr-item"); |
||||
|
let visible = 0; |
||||
|
$items.each(function() { |
||||
|
const show = matchItem($(this), type); |
||||
|
this.hidden = !show; |
||||
|
if (show) visible += 1; |
||||
|
}); |
||||
|
updateCounts($items); |
||||
|
const $empty = $(".sr-tabs-empty"); |
||||
|
if ($empty.length) $empty.prop("hidden", visible > 0 || !$items.length); |
||||
|
} |
||||
$(function() { |
$(function() { |
||||
const q = new URLSearchParams(window.location.search).get("q")?.trim() || ""; |
const q = new URLSearchParams(window.location.search).get("q")?.trim() || ""; |
||||
if (q) $(".sr-search .s-search__input").val(q); |
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() { |
$(".sr-hot__tag").on("click", function() { |
||||
const keyword = $(this).attr("data-keyword") || $(this).text().trim(); |
const keyword = $(this).attr("data-keyword") || $(this).text().trim(); |
||||
$(".sr-search .s-search__input").val(keyword); |
$(".sr-search .s-search__input").val(keyword); |
||||
$(".sr-search").trigger("submit"); |
$(".sr-search").trigger("submit"); |
||||
}); |
}); |
||||
|
if ($(".sr-tabs").length) applyFilter(); |
||||
}); |
}); |
||||
//#endregion
|
//#endregion
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue