Files
sechskies/concert/search.js
T

121 lines
4.2 KiB
JavaScript
Raw Normal View History

2026-06-03 15:58:01 +08:00
// 1.bind
let myQuery = document.querySelector("#concert-query");
let myMember = document.querySelector("#concert-member");
let myBtn = document.querySelector("#concert-search-btn");
2026-06-03 15:59:52 +08:00
let myResult = document.querySelector("#concert-search-result");
2026-06-03 15:58:01 +08:00
2026-06-03 15:59:52 +08:00
const SEARCH_LIST_MAX = 12;
2026-06-03 15:58:01 +08:00
2026-06-03 15:59:52 +08:00
function getTrackContext(trackEl) {
const venueBlock = trackEl.closest(".track-list")?.parentElement;
const tourCard = trackEl.closest(".con-1, .con-2, .con-3, .con-4");
return {
trackName: trackEl.textContent.trim(),
tourTitle: tourCard?.querySelector(".title")?.textContent.trim().replace(/\s+/g, " ") || "",
location: venueBlock?.querySelector(".location")?.textContent.trim() || ""
};
}
function findMatchingTracks(keyword, member) {
2026-06-03 15:58:01 +08:00
const tracks = document.querySelectorAll(".track-item .track");
const k = keyword.trim().toLowerCase();
const m = member.trim().toLowerCase();
2026-06-03 15:59:52 +08:00
const hits = [];
2026-06-03 15:58:01 +08:00
tracks.forEach(function (trackEl) {
const text = trackEl.textContent.toLowerCase();
const matchKeyword = !k || text.includes(k);
const matchMember = !m || text.includes(m);
if (matchKeyword && matchMember) {
2026-06-03 15:59:52 +08:00
hits.push({
el: trackEl.closest(".track-item"),
...getTrackContext(trackEl)
});
2026-06-03 15:58:01 +08:00
}
});
2026-06-03 15:59:52 +08:00
return hits;
}
function clearSearchHighlights() {
document.querySelectorAll(".track-item.is-search-hit").forEach(function (item) {
item.classList.remove("is-search-hit");
});
}
function renderSearchResults(getQuery, getMember, hits) {
if (!myResult) return;
clearSearchHighlights();
const summary = document.createElement("p");
summary.className = "concert-search-summary";
if (!hits.length) {
summary.textContent = `關鍵字「${getQuery || "(未填)"}${getMember.trim() ? `、成員「${getMember}」` : ""}:沒有比對到曲目,請換字再試。`;
myResult.replaceChildren(summary);
return;
}
if (getMember.trim()) {
summary.textContent = `關鍵字「${getQuery || "(未填)"}」、成員「${getMember}」:共 ${hits.length} 筆,以下列出前 ${Math.min(hits.length, SEARCH_LIST_MAX)} 筆。`;
} else {
summary.textContent = `關鍵字「${getQuery}」:共 ${hits.length} 筆,以下列出前 ${Math.min(hits.length, SEARCH_LIST_MAX)} 筆。`;
}
const list = document.createElement("ul");
list.className = "concert-search-hits";
hits.slice(0, SEARCH_LIST_MAX).forEach(function (hit) {
if (hit.el) hit.el.classList.add("is-search-hit");
const li = document.createElement("li");
const trackLine = document.createElement("span");
trackLine.className = "hit-track";
trackLine.textContent = hit.trackName;
const metaLine = document.createElement("span");
metaLine.className = "hit-meta";
metaLine.textContent = [hit.tourTitle, hit.location].filter(Boolean).join(" · ");
li.appendChild(trackLine);
if (metaLine.textContent) li.appendChild(metaLine);
list.appendChild(li);
});
if (hits.length > SEARCH_LIST_MAX) {
const more = document.createElement("li");
more.className = "hit-more";
more.textContent = `另有 ${hits.length - SEARCH_LIST_MAX} 筆未顯示,請縮小關鍵字。`;
list.appendChild(more);
}
myResult.replaceChildren(summary, list);
const firstHit = hits[0]?.el;
if (firstHit) {
firstHit.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
2026-06-03 15:58:01 +08:00
}
// 2.action
2026-06-03 15:59:52 +08:00
if (myBtn && myResult) {
2026-06-03 15:58:01 +08:00
myBtn.addEventListener("click", function () {
let getQuery = myQuery ? myQuery.value : "";
let getMember = myMember ? myMember.value : "";
if (!getQuery.trim() && !getMember.trim()) {
2026-06-03 15:59:52 +08:00
myResult.replaceChildren();
const summary = document.createElement("p");
summary.className = "concert-search-summary";
summary.textContent = "請至少輸入曲目/場次,或成員其中一項。";
myResult.appendChild(summary);
clearSearchHighlights();
2026-06-03 15:58:01 +08:00
return;
}
2026-06-03 15:59:52 +08:00
const hits = findMatchingTracks(getQuery, getMember);
renderSearchResults(getQuery, getMember, hits);
2026-06-03 15:58:01 +08:00
});
}