进行了一些小优化
This commit is contained in:
@@ -577,6 +577,58 @@
|
||||
}
|
||||
}
|
||||
|
||||
/* ==================== 消息通知偏好 ==================== */
|
||||
function initNotificationPreferences(root) {
|
||||
root = root || document;
|
||||
var form = root.matches && root.matches('[data-aksrt-notification-preferences]')
|
||||
? root
|
||||
: root.querySelector('[data-aksrt-notification-preferences]');
|
||||
if (!form || form.dataset.aksrtInitialized === 'true') return;
|
||||
form.dataset.aksrtInitialized = 'true';
|
||||
|
||||
form.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
var button = form.querySelector('[type="submit"]');
|
||||
if (!button || button.disabled) return;
|
||||
var toastId = 'aksrt-notification-preferences-toast';
|
||||
var restUrl = (window.aksrtData && (window.aksrtData.restUrl || window.aksrtData.apiUrl)) || cfg.restUrl || '';
|
||||
var nonce = (window.aksrtData && (window.aksrtData.restNonce || window.aksrtData.nonce)) || cfg.restNonce || '';
|
||||
var originalText = button.textContent;
|
||||
var preferences = {};
|
||||
['posts', 'like', 'system'].forEach(function(key) {
|
||||
var input = form.querySelector('[name="' + key + '"]');
|
||||
preferences[key] = Boolean(input && input.checked);
|
||||
});
|
||||
|
||||
button.disabled = true;
|
||||
button.textContent = i18n.saving || '保存中…';
|
||||
notyf(i18n.saving || '正在保存设置…', 'load', '', toastId);
|
||||
fetch(restUrl.replace(/\/$/, '') + '/user/notification-preferences', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': nonce
|
||||
},
|
||||
body: JSON.stringify({ preferences: preferences })
|
||||
}).then(function(response) {
|
||||
return response.json().then(function(payload) {
|
||||
if (!response.ok || !payload || !payload.success) {
|
||||
throw new Error(payload && payload.message ? payload.message : (i18n.saveFailed || '保存失败'));
|
||||
}
|
||||
return payload;
|
||||
});
|
||||
}).then(function(payload) {
|
||||
notyf(payload.message || i18n.saved || '设置已保存', 'success', 5, toastId);
|
||||
}).catch(function(error) {
|
||||
notyf(error.message || i18n.networkError || '网络错误,请重试', 'danger', 5, toastId);
|
||||
}).finally(function() {
|
||||
button.disabled = false;
|
||||
button.textContent = originalText;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* ==================== 账号设置:弹窗 / 密码 / 邮箱 / 绑定 ==================== */
|
||||
function initSettings(root) {
|
||||
if (!ajaxUrl || !settingsNonce) return;
|
||||
@@ -873,6 +925,9 @@
|
||||
initSettings(root);
|
||||
initBindPhone(root);
|
||||
});
|
||||
window.AksrtFrontend.register('aksrt-notification-preferences', function(root) {
|
||||
initNotificationPreferences(root);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -884,6 +939,7 @@
|
||||
initUnfavorite();
|
||||
initPostDeletion();
|
||||
initNotifications();
|
||||
initNotificationPreferences();
|
||||
initSettings();
|
||||
initBindPhone();
|
||||
initSettingsCallbackToast();
|
||||
@@ -895,6 +951,7 @@
|
||||
initUnfavorite();
|
||||
initPostDeletion();
|
||||
initNotifications();
|
||||
initNotificationPreferences();
|
||||
initSettings();
|
||||
initBindPhone();
|
||||
initSettingsCallbackToast();
|
||||
|
||||
Reference in New Issue
Block a user