修复了一些问题
This commit is contained in:
+59
-3
@@ -13,7 +13,12 @@
|
||||
var ajaxUrl = typeof aksrtData !== 'undefined' ? aksrtData.ajaxUrl : '/wp-admin/admin-ajax.php';
|
||||
var content = root.querySelector('[data-message-content]');
|
||||
var nav = root.querySelector('[data-message-nav]');
|
||||
var state = { view: root.dataset.view || 'unread', page: 1, detail: 0 };
|
||||
var state = {
|
||||
view: root.dataset.view || 'unread',
|
||||
page: 1,
|
||||
detail: 0,
|
||||
paginateType: root.dataset.paginateType || 'ajax_lists',
|
||||
};
|
||||
var pollInterval = 60000;
|
||||
|
||||
function post(action, data) {
|
||||
@@ -51,6 +56,34 @@
|
||||
content.querySelector('.messages-empty__text').textContent = message || '加载失败,请稍后重试';
|
||||
}
|
||||
|
||||
function appendMessagePage(markup) {
|
||||
var incoming = document.createElement('div');
|
||||
incoming.innerHTML = markup;
|
||||
var currentList = content.querySelector('.messages-list');
|
||||
var incomingList = incoming.querySelector('.messages-list');
|
||||
if (!currentList || !incomingList) {
|
||||
content.innerHTML = markup;
|
||||
return null;
|
||||
}
|
||||
|
||||
var existingIds = new Set();
|
||||
currentList.querySelectorAll('[data-notification-id]').forEach(function (item) {
|
||||
existingIds.add(item.dataset.notificationId);
|
||||
});
|
||||
var firstAdded = null;
|
||||
incomingList.querySelectorAll('.message-item').forEach(function (item) {
|
||||
if (existingIds.has(item.dataset.notificationId)) return;
|
||||
if (!firstAdded) firstAdded = item;
|
||||
currentList.appendChild(item);
|
||||
});
|
||||
|
||||
var currentPagination = content.querySelector('.messages-pagination');
|
||||
var incomingPagination = incoming.querySelector('.messages-pagination');
|
||||
if (currentPagination) currentPagination.remove();
|
||||
if (incomingPagination) content.appendChild(incomingPagination);
|
||||
return firstAdded;
|
||||
}
|
||||
|
||||
function load(next, options) {
|
||||
var requested = {
|
||||
view: next.view || state.view,
|
||||
@@ -69,10 +102,17 @@
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.data && result.data.message ? result.data.message : '加载失败,请稍后重试');
|
||||
}
|
||||
requested.paginateType = result.data.paginate_type || state.paginateType;
|
||||
state = requested;
|
||||
root.dataset.view = state.view;
|
||||
root.dataset.paginateType = state.paginateType;
|
||||
nav.innerHTML = result.data.nav;
|
||||
content.innerHTML = result.data.content;
|
||||
var firstAdded = null;
|
||||
if (settings.append && state.paginateType === 'ajax_lists') {
|
||||
firstAdded = appendMessagePage(result.data.content);
|
||||
} else {
|
||||
content.innerHTML = result.data.content;
|
||||
}
|
||||
updateHeaderBadge(result.data.unread);
|
||||
|
||||
if (settings.history === 'push') {
|
||||
@@ -80,7 +120,12 @@
|
||||
} else if (settings.history === 'replace') {
|
||||
window.history.replaceState(state, '', routeFor(state.view));
|
||||
}
|
||||
content.focus({ preventScroll: true });
|
||||
if (firstAdded) {
|
||||
firstAdded.setAttribute('tabindex', '-1');
|
||||
firstAdded.focus({ preventScroll: true });
|
||||
} else {
|
||||
content.focus({ preventScroll: true });
|
||||
}
|
||||
}).catch(function (error) {
|
||||
renderError(error.message);
|
||||
}).finally(function () {
|
||||
@@ -116,9 +161,20 @@
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-message-page]');
|
||||
if (!button || !root.contains(button)) return;
|
||||
event.preventDefault();
|
||||
load({ view: state.view, page: button.dataset.messagePage, detail: 0 });
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-message-next-page]');
|
||||
if (!button || !root.contains(button)) return;
|
||||
event.preventDefault();
|
||||
load(
|
||||
{ view: state.view, page: button.dataset.messageNextPage, detail: 0 },
|
||||
{ append: true }
|
||||
);
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('.js-msg-mark-read');
|
||||
if (!button || !root.contains(button)) return;
|
||||
|
||||
Reference in New Issue
Block a user