101 lines
3.2 KiB
JavaScript
101 lines
3.2 KiB
JavaScript
/**
|
|
* AKSRT Follow — 前台关注交互
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var API_ROOT = (function () {
|
|
var data = window.aksrtData || {};
|
|
var raw = data.apiUrl || data.restUrl || data.rest_url || '/index.php?rest_route=/aksrt/v1';
|
|
return String(raw).replace(/\/$/, '');
|
|
})();
|
|
var NONCE = (window.aksrtData && window.aksrtData.restNonce) || '';
|
|
|
|
function buttonsFor(userId) {
|
|
return Array.prototype.slice.call(document.querySelectorAll('[data-aksrt-follow="' + userId + '"]'));
|
|
}
|
|
|
|
function setLoading(buttons, loading) {
|
|
buttons.forEach(function (button) {
|
|
button.disabled = loading;
|
|
button.classList.toggle('is-loading', loading);
|
|
});
|
|
}
|
|
|
|
function updateButton(button, isFollowing) {
|
|
var icon = button.querySelector('i');
|
|
var label = button.querySelector('[data-aksrt-follow-label]');
|
|
if (!label) return;
|
|
|
|
button.classList.toggle('is-following', isFollowing);
|
|
if (icon) {
|
|
icon.className = isFollowing ? 'fa fas fa-heart em09' : 'fa far fa-heart em09';
|
|
}
|
|
label.textContent = isFollowing ? '已关注' : '关注';
|
|
button.title = isFollowing ? '点击取消关注' : '关注该用户';
|
|
}
|
|
|
|
function updateFollowerCount(count) {
|
|
var countEl = document.querySelector('.aksrt-user-home__stat-value[data-followers]');
|
|
if (countEl && count !== undefined) {
|
|
countEl.textContent = count;
|
|
}
|
|
}
|
|
|
|
function initFollowButtons() {
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('[data-aksrt-follow]');
|
|
if (!button) return;
|
|
event.preventDefault();
|
|
|
|
var targetUserId = parseInt(button.getAttribute('data-aksrt-follow'), 10);
|
|
if (!targetUserId || button.disabled) return;
|
|
|
|
var matchingButtons = buttonsFor(targetUserId);
|
|
setLoading(matchingButtons, true);
|
|
|
|
fetch(API_ROOT + '/follow/toggle', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-WP-Nonce': NONCE,
|
|
},
|
|
body: JSON.stringify({ target_user_id: targetUserId }),
|
|
})
|
|
.then(function (response) { return response.json(); })
|
|
.then(function (result) {
|
|
if (!result.success || !result.data) {
|
|
notyf(result.message || '操作失败', 'danger');
|
|
return;
|
|
}
|
|
|
|
var isFollowing = Boolean(result.data.is_following);
|
|
matchingButtons.forEach(function (matchingButton) {
|
|
updateButton(matchingButton, isFollowing);
|
|
});
|
|
updateFollowerCount(result.data.follower_count);
|
|
|
|
if (!isFollowing) {
|
|
var managementItem = button.closest('.uc-follow-management__item');
|
|
if (managementItem) managementItem.remove();
|
|
}
|
|
notyf(isFollowing ? '关注成功' : '已取消关注', 'success');
|
|
})
|
|
.catch(function () {
|
|
notyf('网络错误,请重试', 'danger');
|
|
})
|
|
.finally(function () {
|
|
setLoading(matchingButtons, false);
|
|
});
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', initFollowButtons);
|
|
} else {
|
|
initFollowButtons();
|
|
}
|
|
})();
|