147 lines
4.5 KiB
JavaScript
147 lines
4.5 KiB
JavaScript
/**
|
|
* Header user-box: check-in REST + signout modal.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function restUrl(path) {
|
|
path = String(path || '').replace(/^\//, '');
|
|
var base = '';
|
|
if (window.aksrtData && window.aksrtData.restUrl) base = String(window.aksrtData.restUrl);
|
|
else if (window.aksrtData && window.aksrtData.apiUrl) base = String(window.aksrtData.apiUrl);
|
|
else if (window.aksrtData && window.aksrtData.siteUrl) base = String(window.aksrtData.siteUrl).replace(/\/?$/, '/') + 'wp-json/aksrt/v1';
|
|
else base = '/wp-json/aksrt/v1';
|
|
base = base.replace(/\/?$/, '');
|
|
// Plain permalinks: ...?rest_route=/aksrt/v1 → append /path to rest_route value
|
|
if (base.indexOf('rest_route=') !== -1) {
|
|
if (base.charAt(base.length - 1) === '/') {
|
|
return base + path;
|
|
}
|
|
// rest_route=/aksrt/v1 + /signin
|
|
return base + '/' + path;
|
|
}
|
|
return base + '/' + path;
|
|
}
|
|
|
|
function restNonce() {
|
|
return (window.aksrtData && window.aksrtData.restNonce) || '';
|
|
}
|
|
|
|
function toast(msg, type) {
|
|
type = type || 'info';
|
|
// Site-wide notification widget (aksrt-runtime notyf).
|
|
if (window.aksrt && typeof window.aksrt.notyf === 'function') {
|
|
window.aksrt.notyf(msg, { type: type, duration: 2200 });
|
|
return;
|
|
}
|
|
if (typeof window.aksrtToast === 'function') {
|
|
window.aksrtToast(msg, type);
|
|
return;
|
|
}
|
|
if (typeof window.AksrtToast === 'function') {
|
|
window.AksrtToast(msg, type);
|
|
return;
|
|
}
|
|
// Last resort: non-blocking console (never native alert for UX).
|
|
if (window.console && typeof window.console.log === 'function') {
|
|
window.console.log('[aksrt]', type, msg);
|
|
}
|
|
}
|
|
|
|
function openModal(id) {
|
|
var modal = document.getElementById(id);
|
|
if (!modal) return;
|
|
modal.classList.add('is-open', 'show');
|
|
modal.setAttribute('aria-hidden', 'false');
|
|
}
|
|
|
|
function closeModal(id) {
|
|
var modal = document.getElementById(id);
|
|
if (!modal) return;
|
|
modal.classList.remove('is-open', 'show');
|
|
modal.setAttribute('aria-hidden', 'true');
|
|
}
|
|
|
|
function initSignoutModal() {
|
|
document.addEventListener('click', function (e) {
|
|
var openBtn = e.target.closest('[data-target="#modal_signout"], [href="#modal_signout"]');
|
|
if (openBtn) {
|
|
e.preventDefault();
|
|
openModal('modal_signout');
|
|
return;
|
|
}
|
|
var dismiss = e.target.closest('#modal_signout [data-dismiss="modal"]');
|
|
if (dismiss) {
|
|
e.preventDefault();
|
|
closeModal('modal_signout');
|
|
return;
|
|
}
|
|
if (e.target && e.target.id === 'modal_signout') {
|
|
closeModal('modal_signout');
|
|
}
|
|
});
|
|
document.addEventListener('keydown', function (e) {
|
|
if (e.key === 'Escape') closeModal('modal_signout');
|
|
});
|
|
}
|
|
|
|
function initCheckin() {
|
|
document.addEventListener('click', function (e) {
|
|
var btn = e.target.closest('[data-aksrt-checkin="1"]');
|
|
if (!btn || btn.disabled) return;
|
|
e.preventDefault();
|
|
btn.disabled = true;
|
|
fetch(restUrl('signin'), {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-WP-Nonce': restNonce(),
|
|
},
|
|
body: '{}',
|
|
})
|
|
.then(function (r) {
|
|
return r.json().then(function (data) {
|
|
return { ok: r.ok, data: data };
|
|
});
|
|
})
|
|
.then(function (res) {
|
|
if (!res.ok) {
|
|
var msg = (res.data && (res.data.message || res.data.msg)) || '签到失败';
|
|
toast(msg, 'danger');
|
|
btn.disabled = false;
|
|
return;
|
|
}
|
|
toast((res.data && res.data.message) || '签到成功', 'success');
|
|
btn.classList.add('is-done');
|
|
btn.disabled = true;
|
|
btn.setAttribute('disabled', 'disabled');
|
|
// Keep existing structure so CSS layout (.but.block flex) is not lost.
|
|
var signedLabel = btn.classList.contains('aksrt-header-checkin--compact')
|
|
? '已签到'
|
|
: '今日已签到';
|
|
btn.innerHTML =
|
|
'<i class="fas fa-calendar-check" aria-hidden="true"></i>' +
|
|
'<span>' + signedLabel + '</span>';
|
|
})
|
|
.catch(function () {
|
|
toast('网络错误', 'danger');
|
|
btn.disabled = false;
|
|
});
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
initSignoutModal();
|
|
initCheckin();
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|