- rehydrate.js 新增 boot()/aksrt:ready 事件,支持 boot 后晚注册立即以 init 触发 - aksrt-runtime.js 初始加载改走 AksrtFrontend.boot,tooltip/倒计时注册为组件 - 约 30 个前端脚本(首页、评论、支付、路由、页面叶子、widgets) 从直接 DOMContentLoaded 迁移为 AksrtFrontend.register,并加幂等标记 - section-navs 依赖改为 aksrt-runtime,保证注册时序 - 清理死代码:删除孤儿 loader.js/main.js 及 runtime/page-navs 遗留副本 - loader.js 移除已迁移模块的 tbquire 路径映射,main.js 相应瘦身 - build-public-assets.mjs 增加 runtime/main.min.js 自动压缩步骤 - 新增 4 个契约测试(homepage-dom/rehydrate-runtime/global-dom-init/ public-bundle-parity),3 个既有 harness 适配注册表初始化方式
67 lines
2.4 KiB
JavaScript
67 lines
2.4 KiB
JavaScript
/**
|
|
* Server-rendered user medal actions.
|
|
*
|
|
* Mirrors the legacy admin-ajax lifecycle: mutate, close the dialog, then
|
|
* reload the page so every server-rendered medal surface is in sync.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function (window, document) {
|
|
'use strict';
|
|
|
|
var actions = {
|
|
wear: 'aksrt_user_medal_wear',
|
|
grant: 'aksrt_user_medal_manually_add',
|
|
revoke: 'aksrt_user_medal_manually_remove'
|
|
};
|
|
|
|
function notice(message, type) {
|
|
if (typeof window.notyf === 'function') window.notyf(message, type);
|
|
}
|
|
|
|
function bindOnce() {
|
|
if (document.__aksrtUserBadgeActionsBound) return;
|
|
document.__aksrtUserBadgeActionsBound = true;
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('[data-aksrt-badge-action]');
|
|
if (!button || button.classList.contains('is-loading')) return;
|
|
|
|
var type = button.getAttribute('data-aksrt-badge-action') || '';
|
|
var action = actions[type];
|
|
var slug = button.getAttribute('data-slug') || '';
|
|
var nonce = (window.aksrtData && window.aksrtData.nonce) || '';
|
|
if (!action || !slug || !nonce) return;
|
|
|
|
event.preventDefault();
|
|
button.classList.add('is-loading');
|
|
button.setAttribute('aria-busy', 'true');
|
|
|
|
var form = new URLSearchParams();
|
|
form.set('action', action);
|
|
form.set('nonce', nonce);
|
|
form.set('slug', slug);
|
|
if (type !== 'wear') form.set('id', button.getAttribute('data-user-id') || '0');
|
|
|
|
window.fetch((window.aksrtData && window.aksrtData.ajaxUrl) || '/wp-admin/admin-ajax.php', {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
|
body: form.toString()
|
|
}).then(function (response) {
|
|
return response.json();
|
|
}).then(function (json) {
|
|
if (!json || !json.success) throw new Error((json && json.data && json.data.message) || '操作失败');
|
|
notice((json.data && json.data.message) || '操作成功', 'success');
|
|
window.AksrtModalRuntime.close('badge-action');
|
|
window.setTimeout(function () { window.location.reload(); }, 120);
|
|
}).catch(function (error) {
|
|
button.classList.remove('is-loading');
|
|
button.removeAttribute('aria-busy');
|
|
notice(error.message || '网络错误', 'danger');
|
|
});
|
|
});
|
|
}
|
|
|
|
window.AksrtFrontend.register('aksrt-user-badge-actions', bindOnce);
|
|
})(window, document);
|