/** * Server-rendered user medal actions. * * Badge mutations close only the active dialog, matching the reference theme. * * @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, '', 'badge-action'); } 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(); var modal = button.closest('.aksrt-modal-runtime'); var label = button.innerHTML; button.classList.add('is-loading'); button.setAttribute('aria-busy', 'true'); button.setAttribute('aria-disabled', 'true'); button.setAttribute('disabled', ''); button.innerHTML = '请稍候'; notice('正在处理,请稍候…', 'load'); function restoreButton() { button.classList.remove('is-loading'); button.removeAttribute('aria-busy'); button.removeAttribute('aria-disabled'); button.removeAttribute('disabled'); button.innerHTML = label; } 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) || '操作失败'); restoreButton(); notice((json.data && json.data.message) || '操作成功', 'success'); if (window.AksrtModalRuntime.getActive() === modal) window.AksrtModalRuntime.close('badge-action'); }).catch(function (error) { restoreButton(); notice(error.message || '网络错误', 'danger'); }); }); })(window, document);