Replace hybrid site-nav/drawer shell with zibll navbar-top layouts 1/2/3, body.mobile-navbar-show drawer, wired NavWalker + aksrt_menu_options meta, nav_folding/chevron, VIP switches, and SPA/PHP schema parity. No dual-class compat paths.
176 lines
5.7 KiB
JavaScript
176 lines
5.7 KiB
JavaScript
/**
|
|
* AKSRT messages center interactions.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var nonce = typeof aksrtData !== 'undefined' ? aksrtData.nonce : '';
|
|
var ajaxUrl = typeof aksrtData !== 'undefined' ? aksrtData.ajaxUrl : '/wp-admin/admin-ajax.php';
|
|
var pollTimer = null;
|
|
var pollInterval = 60000;
|
|
|
|
function post(action, data) {
|
|
var body = new URLSearchParams({ action: action, nonce: nonce });
|
|
if (data) {
|
|
Object.keys(data).forEach(function (key) {
|
|
var value = data[key];
|
|
if (Array.isArray(value)) {
|
|
value.forEach(function (item) {
|
|
body.append(key + '[]', item);
|
|
});
|
|
return;
|
|
}
|
|
body.append(key, value);
|
|
});
|
|
}
|
|
|
|
return fetch(ajaxUrl, { method: 'POST', body: body, credentials: 'same-origin' })
|
|
.then(function (response) { return response.json(); });
|
|
}
|
|
|
|
document.addEventListener('click', function (event) {
|
|
var item = event.target.closest('.messages-nav__item[data-type]');
|
|
if (!item) return;
|
|
|
|
event.preventDefault();
|
|
var url = new URL(window.location.href);
|
|
url.searchParams.set('msg_type', item.dataset.type);
|
|
url.searchParams.delete('msg_page');
|
|
window.location.href = url.toString();
|
|
});
|
|
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('.js-msg-mark-read');
|
|
if (!button) return;
|
|
|
|
var id = button.dataset.id;
|
|
var msgItem = button.closest('.message-item');
|
|
post('aksrt_mark_notification_read', { notification_id: id }).then(function (result) {
|
|
if (!result.success) return;
|
|
|
|
if (msgItem) {
|
|
msgItem.classList.remove('is-unread');
|
|
}
|
|
button.remove();
|
|
updateBadges(result.data && result.data.unread !== undefined ? result.data.unread : countUnread());
|
|
});
|
|
});
|
|
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('.js-msg-delete');
|
|
if (!button) return;
|
|
|
|
var id = button.dataset.id;
|
|
var msgItem = button.closest('.message-item');
|
|
post('aksrt_delete_notification', { notification_id: id }).then(function (result) {
|
|
if (!result.success) return;
|
|
|
|
if (msgItem) {
|
|
msgItem.style.opacity = '0';
|
|
msgItem.style.transform = 'translateX(20px)';
|
|
msgItem.style.transition = 'all 0.2s';
|
|
setTimeout(function () { msgItem.remove(); }, 200);
|
|
}
|
|
|
|
updateBadges(result.data && result.data.unread !== undefined ? result.data.unread : countUnread());
|
|
checkEmpty();
|
|
});
|
|
});
|
|
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('.js-msg-mark-all-read');
|
|
if (!button) return;
|
|
|
|
post('aksrt_mark_notification_read', { mark_all: '1' }).then(function (result) {
|
|
if (!result.success) return;
|
|
|
|
document.querySelectorAll('.message-item.is-unread').forEach(function (item) {
|
|
item.classList.remove('is-unread');
|
|
});
|
|
document.querySelectorAll('.js-msg-mark-read').forEach(function (item) {
|
|
item.remove();
|
|
});
|
|
updateBadges(0);
|
|
});
|
|
});
|
|
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('.js-msg-clear-read');
|
|
if (!button) return;
|
|
|
|
var readItems = document.querySelectorAll('.message-item:not(.is-unread)');
|
|
var ids = [];
|
|
readItems.forEach(function (item) {
|
|
var id = item.dataset.notificationId;
|
|
if (id) ids.push(id);
|
|
});
|
|
if (!ids.length) return;
|
|
|
|
post('aksrt_delete_notifications', { notification_ids: ids }).then(function (result) {
|
|
if (!result.success) return;
|
|
|
|
readItems.forEach(function (item) {
|
|
item.style.opacity = '0';
|
|
item.style.transform = 'translateX(20px)';
|
|
item.style.transition = 'all 0.2s';
|
|
setTimeout(function () { item.remove(); }, 200);
|
|
});
|
|
setTimeout(checkEmpty, 250);
|
|
updateBadges(result.data && result.data.unread !== undefined ? result.data.unread : countUnread());
|
|
});
|
|
});
|
|
|
|
function countUnread() {
|
|
return document.querySelectorAll('.message-item.is-unread').length;
|
|
}
|
|
|
|
function updateBadges(unread) {
|
|
var nextUnread = unread === undefined ? countUnread() : unread;
|
|
|
|
document.querySelectorAll('.messages-nav__badge[data-type="unread"]').forEach(function (badge) {
|
|
badge.textContent = String(nextUnread);
|
|
badge.dataset.count = String(nextUnread);
|
|
badge.style.display = nextUnread > 0 ? '' : 'none';
|
|
});
|
|
|
|
// Numeric badge inside the user dropdown only — never target the pure avatar
|
|
// .nav-user__dot.aksrt-notification-badge (no textContent on a dot indicator).
|
|
var headerBadge = document.querySelector('.aksrt-notification-badge');
|
|
if (headerBadge) {
|
|
headerBadge.textContent = String(nextUnread);
|
|
headerBadge.style.display = nextUnread > 0 ? '' : 'none';
|
|
}
|
|
|
|
var avatarDot = document.querySelector('.nav-user__dot');
|
|
if (avatarDot) {
|
|
avatarDot.style.display = nextUnread > 0 ? '' : 'none';
|
|
}
|
|
}
|
|
|
|
function checkEmpty() {
|
|
var list = document.querySelector('.messages-list');
|
|
if (list && list.children.length === 0) {
|
|
list.outerHTML = '<div class="messages-empty"><span class="messages-empty__icon"><i class="fas fa-bell-slash"></i></span><p class="messages-empty__text">暂无消息</p></div>';
|
|
}
|
|
}
|
|
|
|
function startPolling() {
|
|
pollTimer = setInterval(function () {
|
|
post('aksrt_check_new_notifications', {}).then(function (result) {
|
|
if (!result.success || !result.data) return;
|
|
if (result.data.unread !== undefined) {
|
|
updateBadges(result.data.unread);
|
|
return;
|
|
}
|
|
if (result.data.count !== undefined) {
|
|
updateBadges(result.data.count);
|
|
}
|
|
}).catch(function () {});
|
|
}, pollInterval);
|
|
}
|
|
|
|
startPolling();
|
|
})();
|