Category accordion + AJAX cat/search posts for documentnav; archives month lists with view/comment/like meta, sidebar and comments shell.
198 lines
6.2 KiB
JavaScript
198 lines
6.2 KiB
JavaScript
/**
|
|
* Document navigation AJAX (zibll page-template documentnav subset).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function ajaxUrl() {
|
|
if (window.aksrtData && window.aksrtData.ajaxUrl) return window.aksrtData.ajaxUrl;
|
|
return '/wp-admin/admin-ajax.php';
|
|
}
|
|
|
|
function toast(msg, type) {
|
|
if (window.aksrt && typeof window.aksrt.toast === 'function') {
|
|
window.aksrt.toast(msg, type || 'info');
|
|
return;
|
|
}
|
|
// eslint-disable-next-line no-alert
|
|
window.alert(msg);
|
|
}
|
|
|
|
function qs(sel, root) {
|
|
return (root || document).querySelector(sel);
|
|
}
|
|
|
|
function qsa(sel, root) {
|
|
return Array.prototype.slice.call((root || document).querySelectorAll(sel));
|
|
}
|
|
|
|
function documentAjax(triggerEl, data, url) {
|
|
var container = qs('.document-nav-container');
|
|
var searchBox = qs('.document-search');
|
|
if (!container) return;
|
|
|
|
container.classList.add('is-loading');
|
|
if (searchBox && data && data.search) searchBox.classList.add('is-loading');
|
|
|
|
data = data || {};
|
|
data.one_cat = container.getAttribute('data-one-cat') || '';
|
|
data.action = 'aksrt_documentnav_posts';
|
|
|
|
var body = new URLSearchParams();
|
|
Object.keys(data).forEach(function (k) {
|
|
if (data[k] !== undefined && data[k] !== null) body.append(k, data[k]);
|
|
});
|
|
|
|
fetch(url || ajaxUrl(), {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
|
body: body.toString(),
|
|
})
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (n) {
|
|
if (!n || n.error || !n.html) {
|
|
toast((n && n.msg) || '加载失败,请重试', 'danger');
|
|
}
|
|
if (n && n.html) container.innerHTML = n.html;
|
|
if (n && n.history_search) {
|
|
var hist = qs('.history-search > div');
|
|
if (hist) hist.innerHTML = n.history_search;
|
|
}
|
|
})
|
|
.catch(function () {
|
|
toast('网络错误', 'danger');
|
|
})
|
|
.finally(function () {
|
|
container.classList.remove('is-loading');
|
|
if (searchBox) searchBox.classList.remove('is-loading');
|
|
if (triggerEl && triggerEl.classList) triggerEl.classList.remove('is-loading');
|
|
});
|
|
}
|
|
|
|
function initAccordion() {
|
|
var nav = qs('.document-nav');
|
|
if (!nav) return;
|
|
nav.addEventListener('click', function (e) {
|
|
var toggle = e.target.closest('.panel-toggle[data-toggle="collapse"]');
|
|
if (!toggle) return;
|
|
e.preventDefault();
|
|
var targetSel = toggle.getAttribute('data-target');
|
|
var panel = targetSel ? qs(targetSel, nav) : null;
|
|
if (!panel) return;
|
|
var parentSel = toggle.getAttribute('data-parent');
|
|
if (parentSel) {
|
|
var parent = qs(parentSel, nav);
|
|
if (parent) {
|
|
qsa('.panel-collapse.is-open, .panel-collapse.in', parent).forEach(function (p) {
|
|
if (p !== panel) {
|
|
p.classList.remove('is-open', 'in');
|
|
var t = parent.querySelector('[data-target="#' + p.id + '"]');
|
|
if (t) t.classList.add('collapsed');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
var open = panel.classList.contains('is-open') || panel.classList.contains('in');
|
|
if (open) {
|
|
panel.classList.remove('is-open', 'in');
|
|
toggle.classList.add('collapsed');
|
|
} else {
|
|
panel.classList.add('is-open', 'in');
|
|
toggle.classList.remove('collapsed');
|
|
}
|
|
});
|
|
}
|
|
|
|
function initSearch() {
|
|
var form = qs('.document-search-form');
|
|
if (!form) return;
|
|
form.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
var input = form.querySelector('[name="s"]');
|
|
var search = input ? String(input.value || '').trim() : '';
|
|
if (!search.length) {
|
|
toast('请输入搜索关键词', 'warning');
|
|
return;
|
|
}
|
|
if (search.length < 2) {
|
|
toast('关键词太短', 'warning');
|
|
return;
|
|
}
|
|
qsa('.cat-load').forEach(function (el) {
|
|
el.classList.remove('is-loading', 'active');
|
|
});
|
|
documentAjax(qs('.document-search'), { search: search });
|
|
});
|
|
|
|
document.addEventListener('click', function (e) {
|
|
var kw = e.target.closest('.document-search .search_keywords');
|
|
if (!kw) return;
|
|
e.preventDefault();
|
|
var text = (kw.textContent || '').trim();
|
|
var input = qs('.document-search-form [name="s"]');
|
|
if (input) {
|
|
input.value = text;
|
|
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
}
|
|
if (form) form.dispatchEvent(new Event('submit', { cancelable: true, bubbles: true }));
|
|
});
|
|
}
|
|
|
|
function initCatLoad() {
|
|
document.addEventListener('click', function (e) {
|
|
var a = e.target.closest('.cat-load');
|
|
if (!a) return;
|
|
e.preventDefault();
|
|
qsa('.cat-load').forEach(function (el) {
|
|
el.classList.remove('is-loading', 'active');
|
|
});
|
|
a.classList.add('is-loading', 'active');
|
|
var catId = a.getAttribute('data-cat-id') || a.getAttribute('cat-id') || '';
|
|
documentAjax(a, { cat_id: catId });
|
|
});
|
|
}
|
|
|
|
function initPager() {
|
|
document.addEventListener('click', function (e) {
|
|
var a = e.target.closest('.document-nav-container .pagenav a');
|
|
if (!a) return;
|
|
e.preventDefault();
|
|
var paged = a.getAttribute('data-paged') || '';
|
|
var href = a.getAttribute('href') || '';
|
|
if (paged) {
|
|
// Prefer current context from last active cat or search.
|
|
var active = qs('.cat-load.active');
|
|
var searchInput = qs('.document-search-form [name="s"]');
|
|
var data = { paged: paged };
|
|
if (active) {
|
|
data.cat_id = active.getAttribute('data-cat-id') || '';
|
|
} else if (searchInput && searchInput.value) {
|
|
data.search = searchInput.value.trim();
|
|
}
|
|
documentAjax(a, data);
|
|
return;
|
|
}
|
|
if (href && href.indexOf('admin-ajax.php') !== -1) {
|
|
documentAjax(a, {}, href);
|
|
}
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
if (!qs('.document-nav-container') && !qs('.document-nav')) return;
|
|
initAccordion();
|
|
initSearch();
|
|
initCatLoad();
|
|
initPager();
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|