64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
/**
|
|
* AKSRT Page Admin Set — 前端页面编辑弹窗提交(后台「扩展&增强 → 系统工具」)。
|
|
*
|
|
* 弹窗内容由 PageAdminSet::ajax_modal() 经 aksrt-modal-runtime 注入,
|
|
* 这里只负责表单提交:序列化 → admin-ajax → 成功后刷新页面。
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function (window, document) {
|
|
'use strict';
|
|
|
|
function notify(message, type) {
|
|
if (typeof window.notyf === 'function') {
|
|
window.notyf(message, type || 'error', 4, 'aksrt-page-admin-set-toast');
|
|
}
|
|
}
|
|
|
|
document.addEventListener('submit', function (event) {
|
|
var form = event.target.closest('form.aksrt-admin-set-form');
|
|
if (!form) return;
|
|
event.preventDefault();
|
|
|
|
var submitButton = form.querySelector('.aksrt-admin-set-submit');
|
|
if (submitButton && submitButton.disabled) return;
|
|
if (submitButton) submitButton.disabled = true;
|
|
|
|
var action = (form.querySelector('input[name="action"]') || {}).value || '';
|
|
var ajaxUrl = (window.aksrtData && window.aksrtData.ajaxUrl) || (window._win && window._win.ajax_url) || '';
|
|
if (!ajaxUrl) {
|
|
notify('请求地址缺失', 'error');
|
|
if (submitButton) submitButton.disabled = false;
|
|
return;
|
|
}
|
|
|
|
var url = ajaxUrl + (ajaxUrl.indexOf('?') === -1 ? '?' : '&') + 'action=' + encodeURIComponent(action);
|
|
fetch(url, {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
body: new FormData(form),
|
|
})
|
|
.then(function (response) {
|
|
return response.json().catch(function () { return {}; }).then(function (payload) {
|
|
if (!response.ok || !payload || !payload.success) {
|
|
throw new Error((payload && payload.data && payload.data.msg) || '保存失败,请重试');
|
|
}
|
|
return payload;
|
|
});
|
|
})
|
|
.then(function (payload) {
|
|
var data = payload.data || {};
|
|
notify(data.msg || '保存成功', 'success');
|
|
if (data.reload !== false) {
|
|
window.setTimeout(function () { window.location.reload(); }, 600);
|
|
} else if (submitButton) {
|
|
submitButton.disabled = false;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
notify(error.message || '保存失败,请重试', 'error');
|
|
if (submitButton) submitButton.disabled = false;
|
|
});
|
|
});
|
|
})(window, document);
|