refactor(widget): remove search history display
This commit is contained in:
1 parent
389ab4f19a
commit
7127f4c3a8
3 files changed
+4
-220
No files matched your search
@@ -392,16 +392,6 @@
|
||||
window.__aksrtOpenSearchPanel = function () { setOpen(true); };
|
||||
window.__aksrtCloseSearchPanel = function () { setOpen(false); };
|
||||
|
||||
// Sidebar / widget search history (zibll widget_ui_search show_history).
|
||||
initWidgetSearchHistory({
|
||||
historyEnabled: historyEnabled,
|
||||
readHistory: readHistory,
|
||||
writeHistory: writeHistory,
|
||||
requestHistorySync: requestHistorySync,
|
||||
pushHistory: pushHistory,
|
||||
getSearchType: getSearchType,
|
||||
searchConfig: searchConfig,
|
||||
});
|
||||
initWidgetKeywordRefresh();
|
||||
}
|
||||
|
||||
@@ -465,133 +455,5 @@
|
||||
});
|
||||
}
|
||||
|
||||
function initWidgetSearchHistory(ctx) {
|
||||
var panels = document.querySelectorAll("[data-aksrt-search-history]");
|
||||
if (!panels.length) return;
|
||||
if (!ctx.historyEnabled) {
|
||||
panels.forEach(function (el) {
|
||||
el.hidden = true;
|
||||
});
|
||||
return;
|
||||
}
|
||||
var activePanel = null;
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s == null ? "" : s)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function close(panel) {
|
||||
if (!panel) return;
|
||||
panel.hidden = true;
|
||||
var input = panel.parentElement ? panel.parentElement.querySelector('input[name="s"]') : null;
|
||||
if (input) input.setAttribute("aria-expanded", "false");
|
||||
if (activePanel === panel) activePanel = null;
|
||||
}
|
||||
|
||||
function closeAll(except) {
|
||||
panels.forEach(function (panel) {
|
||||
if (panel !== except) close(panel);
|
||||
});
|
||||
}
|
||||
|
||||
function buildSearchUrl(form, keyword, type) {
|
||||
var url = new URL(form.getAttribute("action") || window.location.href, window.location.href);
|
||||
url.searchParams.set("s", keyword);
|
||||
if (type) url.searchParams.set("aksrt_search_type", type);
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function paint(panel, items) {
|
||||
var wrap = panel.querySelector("[data-aksrt-search-history-list]");
|
||||
var clearBtn = panel.querySelector("[data-aksrt-search-history-clear]");
|
||||
var form = panel.closest("form");
|
||||
var limit = parseInt(panel.getAttribute("data-history-limit"), 10);
|
||||
var list = (Array.isArray(items) ? items : []).filter(function (item) {
|
||||
return item && item.keyword;
|
||||
}).slice(0, limit > 0 ? Math.min(limit, 8) : 8);
|
||||
if (!wrap || !form || !list.length) {
|
||||
if (clearBtn) clearBtn.hidden = true;
|
||||
close(panel);
|
||||
return;
|
||||
}
|
||||
if (clearBtn) clearBtn.hidden = false;
|
||||
wrap.innerHTML = list.map(function (item) {
|
||||
var keyword = String(item.keyword);
|
||||
var type = item.type || "post";
|
||||
return (
|
||||
'<a class="aksrt-search-widget__history-item" role="listitem" href="' +
|
||||
escapeHtml(buildSearchUrl(form, keyword, type)) +
|
||||
'">' + escapeHtml(keyword) + "</a>"
|
||||
);
|
||||
}).join("");
|
||||
panel.hidden = false;
|
||||
var input = panel.parentElement ? panel.parentElement.querySelector('input[name="s"]') : null;
|
||||
if (input) input.setAttribute("aria-expanded", "true");
|
||||
}
|
||||
|
||||
function load(panel) {
|
||||
if (window.aksrtData && aksrtData.isLoggedIn) {
|
||||
ctx.requestHistorySync("get", null, function (items) {
|
||||
if (activePanel === panel) paint(panel, items);
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (activePanel === panel) paint(panel, ctx.readHistory());
|
||||
}
|
||||
|
||||
panels.forEach(function (panel) {
|
||||
var field = panel.parentElement;
|
||||
var form = panel.closest("form");
|
||||
var input = field ? field.querySelector('input[name="s"]') : null;
|
||||
if (!input || !form) return;
|
||||
|
||||
function open() {
|
||||
closeAll(panel);
|
||||
activePanel = panel;
|
||||
load(panel);
|
||||
}
|
||||
|
||||
input.addEventListener("focus", open);
|
||||
input.addEventListener("click", open);
|
||||
input.addEventListener("keydown", function (e) {
|
||||
if (e.key === "Escape") {
|
||||
close(panel);
|
||||
input.focus();
|
||||
}
|
||||
});
|
||||
form.addEventListener("submit", function () {
|
||||
close(panel);
|
||||
});
|
||||
panel.addEventListener("click", function (e) {
|
||||
if (e.target.closest(".aksrt-search-widget__history-item")) close(panel);
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelectorAll("[data-aksrt-search-history-clear]").forEach(function (btn) {
|
||||
btn.addEventListener("click", function (e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
var panel = btn.closest("[data-aksrt-search-history]");
|
||||
if (!panel) return;
|
||||
if (window.aksrtData && aksrtData.isLoggedIn) {
|
||||
ctx.requestHistorySync("save", [], function (items) {
|
||||
paint(panel, items);
|
||||
});
|
||||
return;
|
||||
}
|
||||
ctx.writeHistory([]);
|
||||
paint(panel, []);
|
||||
});
|
||||
});
|
||||
|
||||
document.addEventListener("pointerdown", function (e) {
|
||||
if (activePanel && !activePanel.parentElement.contains(e.target)) close(activePanel);
|
||||
});
|
||||
}
|
||||
|
||||
window.AksrtFrontend.register("aksrt-search-panel", function () { initSearchPanel(); });
|
||||
})();
|
||||
Reference in new issue
Block a user