234 lines
7.7 KiB
JavaScript
234 lines
7.7 KiB
JavaScript
/**
|
|
* Comment moveForm for reply/edit (zibll parity) — REST-backed edit fetch.
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var respondId = 'respond';
|
|
var tempId = 'aksrt-wp-temp-form-div';
|
|
var editingId = 0;
|
|
|
|
function getRespond() {
|
|
return document.getElementById(respondId);
|
|
}
|
|
|
|
function restRoot() {
|
|
var d = window.aksrtData || {};
|
|
return (d.restUrl || d.rest_url || '/wp-json/aksrt/v1').replace(/\/$/, '');
|
|
}
|
|
|
|
function nonce() {
|
|
var d = window.aksrtData || {};
|
|
return d.restNonce || d.nonce || '';
|
|
}
|
|
|
|
function setActionText(text) {
|
|
var el = document.querySelector('#' + respondId + ' .action-text');
|
|
if (!el) return;
|
|
if (text) {
|
|
el.style.display = '';
|
|
el.textContent = text;
|
|
} else {
|
|
el.style.display = 'none';
|
|
el.textContent = '';
|
|
}
|
|
}
|
|
|
|
function ensureParentField(respond) {
|
|
var form = respond.querySelector('#commentform') || respond;
|
|
var parent = form.querySelector('[name="comment_parent"]');
|
|
if (!parent) {
|
|
parent = document.createElement('input');
|
|
parent.type = 'hidden';
|
|
parent.name = 'comment_parent';
|
|
parent.id = 'comment_parent';
|
|
parent.value = '0';
|
|
form.appendChild(parent);
|
|
}
|
|
return parent;
|
|
}
|
|
|
|
function ensureEditField(respond, commentId) {
|
|
var form = respond.querySelector('#commentform') || respond;
|
|
var existing = form.querySelector('[name="edit_comment_ID"]');
|
|
if (commentId) {
|
|
if (!existing) {
|
|
existing = document.createElement('input');
|
|
existing.type = 'hidden';
|
|
existing.name = 'edit_comment_ID';
|
|
existing.id = 'edit_comment_ID';
|
|
form.appendChild(existing);
|
|
}
|
|
existing.value = String(commentId);
|
|
// Hide parent while editing.
|
|
var parent = form.querySelector('[name="comment_parent"]');
|
|
if (parent) parent.name = 'comment_parent_bak';
|
|
} else if (existing) {
|
|
existing.remove();
|
|
var bak = form.querySelector('[name="comment_parent_bak"]');
|
|
if (bak) bak.name = 'comment_parent';
|
|
}
|
|
}
|
|
|
|
function moveForm(commId, parentId, isEdit) {
|
|
var respond = getRespond();
|
|
if (!respond) return false;
|
|
var comm = document.getElementById(commId);
|
|
var parent = ensureParentField(respond);
|
|
var cancel = document.getElementById('cancel-comment-reply-link');
|
|
var submit = respond.querySelector('#submit');
|
|
var textarea = respond.querySelector('#comment');
|
|
|
|
if (!document.getElementById(tempId)) {
|
|
var temp = document.createElement('div');
|
|
temp.id = tempId;
|
|
temp.style.display = 'none';
|
|
respond.parentNode.insertBefore(temp, respond);
|
|
}
|
|
|
|
if (comm && comm.parentNode) {
|
|
comm.parentNode.insertBefore(respond, comm.nextSibling);
|
|
}
|
|
|
|
parent.value = isEdit ? '0' : String(parentId || '0');
|
|
editingId = isEdit ? parseInt(parentId, 10) || 0 : 0;
|
|
|
|
if (cancel) {
|
|
cancel.style.display = '';
|
|
cancel.onclick = function () {
|
|
var t = document.getElementById(tempId);
|
|
var r = getRespond();
|
|
if (t && r && t.parentNode) {
|
|
t.parentNode.insertBefore(r, t);
|
|
t.parentNode.removeChild(t);
|
|
}
|
|
ensureEditField(r, 0);
|
|
if (parent) parent.value = '0';
|
|
setActionText('');
|
|
if (textarea) textarea.value = '';
|
|
editingId = 0;
|
|
cancel.style.display = 'none';
|
|
if (window.AksrtFixedInput) window.AksrtFixedInput(r, 'hide');
|
|
else if (r) r.classList.remove('show');
|
|
return false;
|
|
};
|
|
}
|
|
|
|
if (isEdit && editingId) {
|
|
setActionText('编辑评论');
|
|
ensureEditField(respond, editingId);
|
|
if (textarea) {
|
|
textarea.disabled = true;
|
|
textarea.value = '加载中…';
|
|
}
|
|
if (submit) submit.disabled = true;
|
|
fetch(restRoot() + '/comment/get?comment_id=' + encodeURIComponent(String(editingId)), {
|
|
headers: { 'X-WP-Nonce': nonce() },
|
|
credentials: 'same-origin',
|
|
})
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (json) {
|
|
var content = (json && (json.content || (json.data && json.data.content))) || '';
|
|
if (textarea) {
|
|
textarea.disabled = false;
|
|
textarea.value = content;
|
|
textarea.dispatchEvent(new Event('input'));
|
|
}
|
|
if (submit) submit.disabled = false;
|
|
})
|
|
.catch(function () {
|
|
notyf('加载评论失败', 'danger');
|
|
if (textarea) {
|
|
textarea.disabled = false;
|
|
textarea.value = '';
|
|
}
|
|
if (submit) submit.disabled = false;
|
|
});
|
|
} else {
|
|
setActionText('回复评论');
|
|
ensureEditField(respond, 0);
|
|
if (textarea) {
|
|
textarea.focus();
|
|
}
|
|
}
|
|
|
|
if (window.AksrtFixedInput) {
|
|
window.AksrtFixedInput(respond, 'show');
|
|
} else {
|
|
respond.classList.add('show');
|
|
}
|
|
|
|
if (comm && comm.scrollIntoView) {
|
|
comm.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
}
|
|
return false;
|
|
}
|
|
|
|
document.addEventListener('click', function (e) {
|
|
var reply = e.target && e.target.closest ? e.target.closest('.comment-reply-link') : null;
|
|
var edit = e.target && e.target.closest ? e.target.closest('.comment-edit-link, [data-comment-action="edit"]') : null;
|
|
if (reply) {
|
|
e.preventDefault();
|
|
var cid = reply.getAttribute('data-commentid') || reply.getAttribute('data-comment-id') || '';
|
|
var postid = reply.getAttribute('data-postid') || '';
|
|
if (!cid) {
|
|
var li = reply.closest('[data-comment-id], #comment-' + (cid || ''));
|
|
if (li && li.getAttribute('data-comment-id')) cid = li.getAttribute('data-comment-id');
|
|
}
|
|
if (!cid) {
|
|
var m = (reply.closest('li[id^="comment-"]') || {}).id || '';
|
|
var mm = /comment-(\d+)/.exec(m);
|
|
if (mm) cid = mm[1];
|
|
}
|
|
moveForm('div-comment-' + cid, cid, false);
|
|
return;
|
|
}
|
|
if (edit) {
|
|
e.preventDefault();
|
|
var eid = edit.getAttribute('data-commentid') || edit.getAttribute('data-comment-id') || '';
|
|
if (!eid) {
|
|
var row = edit.closest('[data-comment-id]');
|
|
if (row) eid = row.getAttribute('data-comment-id');
|
|
}
|
|
if (!eid) return;
|
|
moveForm('div-comment-' + eid, eid, true);
|
|
}
|
|
});
|
|
|
|
// Dropup toggles (guest / expand).
|
|
document.addEventListener('click', function (e) {
|
|
var t = e.target && e.target.closest ? e.target.closest('[data-toggle-class="open"]') : null;
|
|
if (!t) return;
|
|
var targetSel = t.getAttribute('data-target');
|
|
if (!targetSel) return;
|
|
e.preventDefault();
|
|
var wrap = t.closest('.dropup, .input-expand-wrap, #comment-user-info') || t.parentElement;
|
|
var panel = targetSel.charAt(0) === '#' || targetSel.charAt(0) === '.'
|
|
? (wrap && wrap.querySelector(targetSel)) || document.querySelector(targetSel)
|
|
: null;
|
|
if (wrap) wrap.classList.toggle('open');
|
|
if (panel && panel !== wrap) panel.classList.toggle('open');
|
|
});
|
|
|
|
// Close dropups on outside click.
|
|
document.addEventListener('click', function (e) {
|
|
if (e.target.closest && e.target.closest('.dropup, .input-expand-wrap, [data-toggle-class="open"]')) return;
|
|
document.querySelectorAll('.dropup.open, .input-expand-wrap.open, #comment-user-info.open').forEach(function (n) {
|
|
n.classList.remove('open');
|
|
});
|
|
});
|
|
|
|
// Scroll helper for @parent.
|
|
document.addEventListener('click', function (e) {
|
|
var a = e.target && e.target.closest ? e.target.closest('[data-scroll-to]') : null;
|
|
if (!a) return;
|
|
e.preventDefault();
|
|
var sel = a.getAttribute('data-scroll-to');
|
|
var el = sel ? document.querySelector(sel) : null;
|
|
if (el) el.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
});
|
|
|
|
window.aksrtCommentMoveForm = moveForm;
|
|
window.aksrtGetEditingCommentId = function () { return editingId; };
|
|
})();
|