fix(single): restore author follow controls
This commit is contained in:
1 parent
71444b139a
commit
8eb62e712b
19 files changed
+261
-615
No files matched your search
+58
-59
@@ -4508,31 +4508,60 @@ tbquire.config({
|
||||
/**
|
||||
* AKSRT Follow — 前台关注交互
|
||||
*
|
||||
* 监听 [data-aksrt-follow] 按钮点击,切换关注状态。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var API_ROOT = (function () {
|
||||
var d = window.aksrtData || {};
|
||||
var raw = d.apiUrl || d.restUrl || d.rest_url || '/index.php?rest_route=/aksrt/v1';
|
||||
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) || '';
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 初始关注状态由服务端渲染(is-following class + 文案),无需逐按钮查询。
|
||||
function initFollowButtons() {
|
||||
document.addEventListener('click', function (e) {
|
||||
var btn = e.target.closest('[data-aksrt-follow]');
|
||||
if (!btn) return;
|
||||
e.preventDefault();
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-aksrt-follow]');
|
||||
if (!button) return;
|
||||
event.preventDefault();
|
||||
|
||||
var targetUserId = parseInt(btn.getAttribute('data-aksrt-follow'));
|
||||
if (!targetUserId) return;
|
||||
var targetUserId = parseInt(button.getAttribute('data-aksrt-follow'), 10);
|
||||
if (!targetUserId || button.disabled) return;
|
||||
|
||||
btn.disabled = true;
|
||||
var matchingButtons = buttonsFor(targetUserId);
|
||||
setLoading(matchingButtons, true);
|
||||
|
||||
fetch(API_ROOT + '/follow/toggle', {
|
||||
method: 'POST',
|
||||
@@ -4542,64 +4571,34 @@ tbquire.config({
|
||||
},
|
||||
body: JSON.stringify({ target_user_id: targetUserId }),
|
||||
})
|
||||
.then(function (res) { return res.json(); })
|
||||
.then(function (response) { return response.json(); })
|
||||
.then(function (result) {
|
||||
if (result.success && result.data) {
|
||||
var isFollowing = result.data.is_following;
|
||||
updateFollowButton(btn, isFollowing, result.data.follower_count);
|
||||
notyf(isFollowing ? '关注成功' : '已取消关注', 'success');
|
||||
} else {
|
||||
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 () {
|
||||
btn.disabled = false;
|
||||
setLoading(matchingButtons, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function updateFollowButton(btn, isFollowing, count) {
|
||||
var icon = btn.querySelector('i');
|
||||
var text = btn.querySelector('span');
|
||||
function setLabel(label) {
|
||||
if (text) {
|
||||
text.textContent = label;
|
||||
return;
|
||||
}
|
||||
// Fallback when markup has bare text nodes next to the icon.
|
||||
var nodes = Array.prototype.slice.call(btn.childNodes);
|
||||
var textNode = nodes.filter(function (n) { return n.nodeType === 3 && n.textContent.trim() !== ''; })[0];
|
||||
if (textNode) {
|
||||
textNode.textContent = ' ' + label + ' ';
|
||||
}
|
||||
}
|
||||
|
||||
if (isFollowing) {
|
||||
btn.classList.add('is-following');
|
||||
if (icon) icon.className = 'fa fas fa-heart';
|
||||
setLabel('已关注');
|
||||
btn.title = '点击取消关注';
|
||||
} else {
|
||||
btn.classList.remove('is-following');
|
||||
if (icon) icon.className = 'fa fas fa-heart';
|
||||
setLabel('关注');
|
||||
btn.title = '关注该用户';
|
||||
}
|
||||
|
||||
if (!isFollowing && btn.closest('.uc-follow-management__item')) {
|
||||
btn.closest('.uc-follow-management__item').remove();
|
||||
}
|
||||
|
||||
// Update follower count display if exists
|
||||
var countEl = document.querySelector('.aksrt-user-home__stat-value[data-followers]');
|
||||
if (countEl && count !== undefined) {
|
||||
countEl.textContent = count;
|
||||
}
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initFollowButtons);
|
||||
} else {
|
||||
|
||||
+58
-59
@@ -1,31 +1,60 @@
|
||||
/**
|
||||
* AKSRT Follow — 前台关注交互
|
||||
*
|
||||
* 监听 [data-aksrt-follow] 按钮点击,切换关注状态。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var API_ROOT = (function () {
|
||||
var d = window.aksrtData || {};
|
||||
var raw = d.apiUrl || d.restUrl || d.rest_url || '/index.php?rest_route=/aksrt/v1';
|
||||
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) || '';
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 初始关注状态由服务端渲染(is-following class + 文案),无需逐按钮查询。
|
||||
function initFollowButtons() {
|
||||
document.addEventListener('click', function (e) {
|
||||
var btn = e.target.closest('[data-aksrt-follow]');
|
||||
if (!btn) return;
|
||||
e.preventDefault();
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-aksrt-follow]');
|
||||
if (!button) return;
|
||||
event.preventDefault();
|
||||
|
||||
var targetUserId = parseInt(btn.getAttribute('data-aksrt-follow'));
|
||||
if (!targetUserId) return;
|
||||
var targetUserId = parseInt(button.getAttribute('data-aksrt-follow'), 10);
|
||||
if (!targetUserId || button.disabled) return;
|
||||
|
||||
btn.disabled = true;
|
||||
var matchingButtons = buttonsFor(targetUserId);
|
||||
setLoading(matchingButtons, true);
|
||||
|
||||
fetch(API_ROOT + '/follow/toggle', {
|
||||
method: 'POST',
|
||||
@@ -35,64 +64,34 @@
|
||||
},
|
||||
body: JSON.stringify({ target_user_id: targetUserId }),
|
||||
})
|
||||
.then(function (res) { return res.json(); })
|
||||
.then(function (response) { return response.json(); })
|
||||
.then(function (result) {
|
||||
if (result.success && result.data) {
|
||||
var isFollowing = result.data.is_following;
|
||||
updateFollowButton(btn, isFollowing, result.data.follower_count);
|
||||
notyf(isFollowing ? '关注成功' : '已取消关注', 'success');
|
||||
} else {
|
||||
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 () {
|
||||
btn.disabled = false;
|
||||
setLoading(matchingButtons, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function updateFollowButton(btn, isFollowing, count) {
|
||||
var icon = btn.querySelector('i');
|
||||
var text = btn.querySelector('span');
|
||||
function setLabel(label) {
|
||||
if (text) {
|
||||
text.textContent = label;
|
||||
return;
|
||||
}
|
||||
// Fallback when markup has bare text nodes next to the icon.
|
||||
var nodes = Array.prototype.slice.call(btn.childNodes);
|
||||
var textNode = nodes.filter(function (n) { return n.nodeType === 3 && n.textContent.trim() !== ''; })[0];
|
||||
if (textNode) {
|
||||
textNode.textContent = ' ' + label + ' ';
|
||||
}
|
||||
}
|
||||
|
||||
if (isFollowing) {
|
||||
btn.classList.add('is-following');
|
||||
if (icon) icon.className = 'fa fas fa-heart';
|
||||
setLabel('已关注');
|
||||
btn.title = '点击取消关注';
|
||||
} else {
|
||||
btn.classList.remove('is-following');
|
||||
if (icon) icon.className = 'fa fas fa-heart';
|
||||
setLabel('关注');
|
||||
btn.title = '关注该用户';
|
||||
}
|
||||
|
||||
if (!isFollowing && btn.closest('.uc-follow-management__item')) {
|
||||
btn.closest('.uc-follow-management__item').remove();
|
||||
}
|
||||
|
||||
// Update follower count display if exists
|
||||
var countEl = document.querySelector('.aksrt-user-home__stat-value[data-followers]');
|
||||
if (countEl && count !== undefined) {
|
||||
countEl.textContent = count;
|
||||
}
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initFollowButtons);
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user