2018-06-07 16:00:53 +08:00
|
|
|
(function(global, document) {
|
2018-06-24 10:01:21 +08:00
|
|
|
"use strict";
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
// Do not use other files like utils.js and http.js in the gulpfile to build
|
2018-06-07 16:00:53 +08:00
|
|
|
// commento.js for the following reasons:
|
|
|
|
// - We don't use jQuery in the actual JavaScript payload because we need
|
|
|
|
// to be lightweight.
|
|
|
|
// - They pollute the global/window namespace (with global.post, etc.).
|
2018-06-24 10:01:21 +08:00
|
|
|
// That's NOT fine when we expect them to source our JavaScript. For example,
|
2018-06-07 16:00:53 +08:00
|
|
|
// the user may have their own window.post defined. We don't want to
|
|
|
|
// override that.
|
|
|
|
|
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_ROOT = "commento";
|
|
|
|
var ID_MAIN_AREA = "commento-main-area";
|
2019-02-19 05:54:13 +08:00
|
|
|
var ID_LOGIN = "commento-login";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_LOGIN_BOX_CONTAINER = "commento-login-box-container";
|
|
|
|
var ID_LOGIN_BOX = "commento-login-box";
|
2019-02-13 15:08:00 +08:00
|
|
|
var ID_LOGIN_BOX_EMAIL_SUBTITLE = "commento-login-box-email-subtitle";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_LOGIN_BOX_EMAIL_INPUT = "commento-login-box-email-input";
|
|
|
|
var ID_LOGIN_BOX_PASSWORD_INPUT = "commento-login-box-password-input";
|
|
|
|
var ID_LOGIN_BOX_NAME_INPUT = "commento-login-box-name-input";
|
|
|
|
var ID_LOGIN_BOX_WEBSITE_INPUT = "commento-login-box-website-input";
|
|
|
|
var ID_LOGIN_BOX_EMAIL_BUTTON = "commento-login-box-email-button";
|
|
|
|
var ID_LOGIN_BOX_LOGIN_LINK_CONTAINER = "commento-login-box-login-link-container";
|
|
|
|
var ID_LOGIN_BOX_LOGIN_LINK = "commento-login-box-login-link";
|
|
|
|
var ID_LOGIN_BOX_HR = "commento-login-box-hr";
|
|
|
|
var ID_LOGIN_BOX_OAUTH_PRETEXT = "commento-login-box-oauth-pretext";
|
|
|
|
var ID_LOGIN_BOX_OAUTH_BUTTONS_CONTAINER = "commento-login-box-oauth-buttons-container";
|
2018-07-05 13:06:52 +08:00
|
|
|
var ID_MOD_TOOLS = "commento-mod-tools";
|
|
|
|
var ID_MOD_TOOLS_LOCK_BUTTON = "commento-mod-tools-lock-button";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_ERROR = "commento-error";
|
2018-06-11 16:48:10 +08:00
|
|
|
var ID_LOGGED_CONTAINER = "commento-logged-container";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_COMMENTS_AREA = "commento-comments-area";
|
|
|
|
var ID_SUPER_CONTAINER = "commento-textarea-super-container-";
|
|
|
|
var ID_TEXTAREA_CONTAINER = "commento-textarea-container-";
|
|
|
|
var ID_TEXTAREA = "commento-textarea-";
|
2019-02-13 13:53:42 +08:00
|
|
|
var ID_ANONYMOUS_CHECKBOX = "commento-anonymous-checkbox-";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_CARD = "commento-comment-card-";
|
|
|
|
var ID_BODY = "commento-comment-body-";
|
2019-01-31 11:19:16 +08:00
|
|
|
var ID_TEXT = "commento-comment-text-";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_SUBTITLE = "commento-comment-subtitle-";
|
2018-12-19 08:10:12 +08:00
|
|
|
var ID_TIMEAGO = "commento-comment-timeago-";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_SCORE = "commento-comment-score-";
|
|
|
|
var ID_OPTIONS = "commento-comment-options-";
|
|
|
|
var ID_EDIT = "commento-comment-edit-";
|
|
|
|
var ID_REPLY = "commento-comment-reply-";
|
|
|
|
var ID_COLLAPSE = "commento-comment-collapse-";
|
|
|
|
var ID_UPVOTE = "commento-comment-upvote-";
|
|
|
|
var ID_DOWNVOTE = "commento-comment-downvote-";
|
|
|
|
var ID_APPROVE = "commento-comment-approve-";
|
|
|
|
var ID_REMOVE = "commento-comment-remove-";
|
2018-12-19 07:57:32 +08:00
|
|
|
var ID_STICKY = "commento-comment-sticky-";
|
2018-12-20 14:27:55 +08:00
|
|
|
var ID_CHILDREN = "commento-comment-children-";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_CONTENTS = "commento-comment-contents-";
|
2018-12-20 11:57:02 +08:00
|
|
|
var ID_NAME = "commento-comment-name-";
|
2018-06-11 01:15:56 +08:00
|
|
|
var ID_SUBMIT_BUTTON = "commento-submit-button-";
|
2019-04-21 10:04:56 +08:00
|
|
|
var ID_MARKDOWN_BUTTON = "commento-markdown-button-";
|
|
|
|
var ID_MARKDOWN_HELP = "commento-markdown-help-";
|
2018-06-11 16:48:10 +08:00
|
|
|
var ID_FOOTER = "commento-footer";
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
|
2018-12-25 10:49:53 +08:00
|
|
|
var origin = "[[[.Origin]]]";
|
|
|
|
var cdn = "[[[.CdnPrefix]]]";
|
2018-06-07 16:00:53 +08:00
|
|
|
var root = null;
|
2018-06-24 10:01:21 +08:00
|
|
|
var cssOverride;
|
2019-05-02 06:10:07 +08:00
|
|
|
var noFonts;
|
2018-06-24 10:01:21 +08:00
|
|
|
var autoInit;
|
2018-06-07 16:00:53 +08:00
|
|
|
var isAuthenticated = false;
|
|
|
|
var comments = [];
|
2019-01-31 11:19:16 +08:00
|
|
|
var commenters = {};
|
2018-06-07 16:00:53 +08:00
|
|
|
var requireIdentification = true;
|
|
|
|
var isModerator = false;
|
|
|
|
var isFrozen = false;
|
2018-07-05 13:06:52 +08:00
|
|
|
var chosenAnonymous = false;
|
|
|
|
var isLocked = false;
|
2018-12-19 07:57:32 +08:00
|
|
|
var stickyCommentHex = "none";
|
2018-06-07 16:00:53 +08:00
|
|
|
var shownReply = {};
|
2019-04-20 07:03:34 +08:00
|
|
|
var configuredOauths = {};
|
2019-02-13 14:46:46 +08:00
|
|
|
var popupBoxType = "login";
|
2018-08-13 13:20:23 +08:00
|
|
|
var oauthButtonsShown = false;
|
2019-01-31 11:19:16 +08:00
|
|
|
var selfHex = undefined;
|
2019-02-20 23:55:51 +08:00
|
|
|
var mobileView = null;
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
function $(id) {
|
|
|
|
return document.getElementById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
function tags(tag) {
|
|
|
|
return document.getElementsByTagName(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
function prepend(root, el) {
|
|
|
|
root.prepend(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function append(root, el) {
|
|
|
|
root.appendChild(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
function insertAfter(el1, el2) {
|
|
|
|
el1.parentNode.insertBefore(el2, el1.nextSibling);
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function classAdd(el, cls) {
|
|
|
|
el.classList.add("commento-" + cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function classRemove(el, cls) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (el !== null) {
|
2018-12-19 08:46:43 +08:00
|
|
|
el.classList.remove("commento-" + cls);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function create(el) {
|
|
|
|
return document.createElement(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function remove(el) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (el !== null) {
|
2018-12-19 08:46:43 +08:00
|
|
|
el.parentNode.removeChild(el);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
function attrGet(node, a) {
|
|
|
|
var attr = node.attributes[a];
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (attr === undefined) {
|
2018-06-16 21:08:24 +08:00
|
|
|
return undefined;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-16 21:08:24 +08:00
|
|
|
|
|
|
|
return attr.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-03 02:28:08 +08:00
|
|
|
function removeAllEventListeners(node) {
|
|
|
|
if (node !== null) {
|
|
|
|
var replacement = node.cloneNode(true);
|
|
|
|
if (node.parentNode !== null) {
|
|
|
|
node.parentNode.replaceChild(replacement, node);
|
|
|
|
return replacement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
function onclick(node, f, arg) {
|
2018-12-20 13:48:43 +08:00
|
|
|
node.addEventListener("click", function() {
|
2018-06-24 10:01:21 +08:00
|
|
|
f(arg);
|
2018-12-20 13:48:43 +08:00
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
function attrSet(node, a, value) {
|
2018-06-07 16:00:53 +08:00
|
|
|
node.setAttribute(a, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function post(url, data, callback) {
|
|
|
|
var xmlDoc = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xmlDoc.open("POST", url, true);
|
|
|
|
xmlDoc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
|
xmlDoc.onload = function() {
|
|
|
|
callback(JSON.parse(xmlDoc.response));
|
|
|
|
};
|
|
|
|
|
|
|
|
xmlDoc.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get(url, callback) {
|
|
|
|
var xmlDoc = new XMLHttpRequest();
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
xmlDoc.open("GET", url, true);
|
2018-06-07 16:00:53 +08:00
|
|
|
xmlDoc.onload = function() {
|
|
|
|
callback(JSON.parse(xmlDoc.response));
|
|
|
|
};
|
|
|
|
|
|
|
|
xmlDoc.send(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function call(callback) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (typeof(callback) === "function") {
|
2018-06-07 16:00:53 +08:00
|
|
|
callback();
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cookieGet(name) {
|
|
|
|
var c = "; " + document.cookie;
|
|
|
|
var x = c.split("; " + name + "=");
|
2018-06-24 10:01:21 +08:00
|
|
|
if (x.length === 2) {
|
2018-06-07 16:00:53 +08:00
|
|
|
return x.pop().split(";").shift();
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cookieSet(name, value) {
|
|
|
|
var expires = "";
|
|
|
|
var date = new Date();
|
2018-06-24 10:01:21 +08:00
|
|
|
date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
|
2018-06-07 16:00:53 +08:00
|
|
|
expires = "; expires=" + date.toUTCString();
|
|
|
|
|
|
|
|
document.cookie = name + "=" + value + expires + "; path=/";
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
function commenterTokenGet() {
|
2018-06-20 11:33:40 +08:00
|
|
|
var commenterToken = cookieGet("commentoCommenterToken");
|
2018-06-24 10:01:21 +08:00
|
|
|
if (commenterToken === undefined) {
|
2018-06-07 16:00:53 +08:00
|
|
|
return "anonymous";
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
return commenterToken;
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
global.logout = function() {
|
2018-06-20 11:33:40 +08:00
|
|
|
cookieSet("commentoCommenterToken", "anonymous");
|
2018-06-07 16:00:53 +08:00
|
|
|
refreshAll();
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
function selfLoad(commenter) {
|
|
|
|
commenters[commenter.commenterHex] = commenter;
|
|
|
|
selfHex = commenter.commenterHex;
|
|
|
|
|
|
|
|
var loggedContainer = create("div");
|
|
|
|
var loggedInAs = create("div");
|
2019-04-21 09:05:23 +08:00
|
|
|
var name;
|
|
|
|
if (commenter.link !== "undefined") {
|
|
|
|
name = create("a");
|
|
|
|
} else {
|
|
|
|
name = create("div");
|
|
|
|
}
|
2019-02-13 13:53:42 +08:00
|
|
|
var avatar;
|
|
|
|
var logout = create("div");
|
|
|
|
var color = colorGet(commenter.commenterHex + "-" + commenter.name);
|
|
|
|
|
|
|
|
loggedContainer.id = ID_LOGGED_CONTAINER;
|
|
|
|
|
|
|
|
classAdd(loggedContainer, "logged-container");
|
|
|
|
classAdd(loggedInAs, "logged-in-as");
|
|
|
|
classAdd(name, "name");
|
|
|
|
classAdd(logout, "logout");
|
|
|
|
|
|
|
|
name.innerText = commenter.name;
|
|
|
|
logout.innerText = "Logout";
|
|
|
|
|
|
|
|
onclick(logout, global.logout);
|
|
|
|
|
|
|
|
attrSet(loggedContainer, "style", "display: none");
|
2019-04-21 09:05:23 +08:00
|
|
|
if (commenter.link !== "undefined") {
|
|
|
|
attrSet(name, "href", commenter.link);
|
|
|
|
}
|
2019-02-13 13:53:42 +08:00
|
|
|
if (commenter.photo === "undefined") {
|
|
|
|
avatar = create("div");
|
|
|
|
avatar.style["background"] = color;
|
|
|
|
avatar.innerHTML = commenter.name[0].toUpperCase();
|
|
|
|
classAdd(avatar, "avatar");
|
|
|
|
} else {
|
|
|
|
avatar = create("img");
|
2019-02-23 11:20:55 +08:00
|
|
|
attrSet(avatar, "src", cdn + "/api/commenter/photo?commenterHex=" + commenter.commenterHex);
|
2019-02-13 13:53:42 +08:00
|
|
|
classAdd(avatar, "avatar-img");
|
|
|
|
}
|
|
|
|
|
|
|
|
append(loggedInAs, avatar);
|
|
|
|
append(loggedInAs, name);
|
|
|
|
append(loggedContainer, loggedInAs);
|
|
|
|
append(loggedContainer, logout);
|
|
|
|
prepend(root, loggedContainer);
|
|
|
|
|
|
|
|
isAuthenticated = true;
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function selfGet(callback) {
|
2018-06-20 11:29:55 +08:00
|
|
|
var commenterToken = commenterTokenGet();
|
2018-06-24 10:01:21 +08:00
|
|
|
if (commenterToken === "anonymous") {
|
2018-06-07 16:00:53 +08:00
|
|
|
isAuthenticated = false;
|
|
|
|
call(callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"commenterToken": commenterTokenGet(),
|
2018-06-07 16:00:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
post(origin + "/api/commenter/self", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
2018-06-20 11:33:40 +08:00
|
|
|
cookieSet("commentoCommenterToken", "anonymous");
|
2018-06-07 16:00:53 +08:00
|
|
|
call(callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
selfLoad(resp.commenter);
|
2019-04-14 09:29:06 +08:00
|
|
|
global.allShow();
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
call(callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
function cssLoad(file, onload) {
|
2018-06-07 16:00:53 +08:00
|
|
|
var link = create("link");
|
2018-06-24 10:01:21 +08:00
|
|
|
var head = document.getElementsByTagName("head")[0];
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
link.type = "text/css";
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(link, "href", file);
|
|
|
|
attrSet(link, "rel", "stylesheet");
|
|
|
|
attrSet(link, "onload", onload);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
append(head, link);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function footerLoad() {
|
|
|
|
var footer = create("div");
|
|
|
|
var aContainer = create("div");
|
|
|
|
var a = create("a");
|
|
|
|
var text = create("span");
|
|
|
|
|
2018-06-11 16:48:10 +08:00
|
|
|
footer.id = ID_FOOTER;
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(footer, "footer");
|
|
|
|
classAdd(aContainer, "logo-container");
|
|
|
|
classAdd(a, "logo");
|
|
|
|
classAdd(text, "logo-text");
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(a, "href", "https://commento.io");
|
|
|
|
attrSet(a, "target", "_blank");
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-03-03 03:09:11 +08:00
|
|
|
text.innerText = "Commento";
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
append(a, text);
|
|
|
|
append(aContainer, a);
|
|
|
|
append(footer, aContainer);
|
2019-03-03 03:09:11 +08:00
|
|
|
|
|
|
|
return footer;
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function commentsGet(callback) {
|
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"commenterToken": commenterTokenGet(),
|
2019-02-13 15:35:33 +08:00
|
|
|
"domain": parent.location.host,
|
|
|
|
"path": parent.location.pathname,
|
2018-06-07 16:00:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
post(origin + "/api/comment/list", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
|
|
|
return;
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
requireIdentification = resp.requireIdentification;
|
|
|
|
isModerator = resp.isModerator;
|
|
|
|
isFrozen = resp.isFrozen;
|
2018-07-05 13:06:52 +08:00
|
|
|
|
|
|
|
isLocked = resp.attributes.isLocked;
|
2018-12-19 07:57:32 +08:00
|
|
|
stickyCommentHex = resp.attributes.stickyCommentHex;
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
comments = resp.comments;
|
2019-01-31 11:19:16 +08:00
|
|
|
commenters = Object.assign({}, commenters, resp.commenters)
|
2018-06-11 01:15:56 +08:00
|
|
|
configuredOauths = resp.configuredOauths;
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
call(callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function errorShow(text) {
|
|
|
|
var el = $(ID_ERROR);
|
|
|
|
|
|
|
|
el.innerText = text;
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(el, "style", "display: block;");
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-04-14 09:34:27 +08:00
|
|
|
function errorHide() {
|
|
|
|
var el = $(ID_ERROR);
|
|
|
|
|
|
|
|
attrSet(el, "style", "display: none;");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-10 18:51:18 +08:00
|
|
|
function errorElementCreate() {
|
2018-06-07 16:00:53 +08:00
|
|
|
var el = create("div");
|
|
|
|
|
|
|
|
el.id = ID_ERROR;
|
|
|
|
|
|
|
|
classAdd(el, "error-box");
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(el, "style", "display: none;");
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
append(root, el);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function autoExpander(el) {
|
|
|
|
return function() {
|
|
|
|
el.style.height = "";
|
|
|
|
el.style.height = Math.min(Math.max(el.scrollHeight, 75), 400) + "px";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-04-21 10:04:56 +08:00
|
|
|
function markdownHelpShow(id) {
|
|
|
|
var textareaSuperContainer = $(ID_SUPER_CONTAINER + id);
|
|
|
|
var markdownButton = $(ID_MARKDOWN_BUTTON + id);
|
|
|
|
var markdownHelp = create("table");
|
|
|
|
var italicsContainer = create("tr");
|
|
|
|
var italicsLeft = create("td");
|
|
|
|
var italicsRight = create("td");
|
|
|
|
var boldContainer = create("tr");
|
|
|
|
var boldLeft = create("td");
|
|
|
|
var boldRight = create("td");
|
|
|
|
var codeContainer = create("tr");
|
|
|
|
var codeLeft = create("td");
|
|
|
|
var codeRight = create("td");
|
|
|
|
var strikethroughContainer = create("tr");
|
|
|
|
var strikethroughLeft = create("td");
|
|
|
|
var strikethroughRight = create("td");
|
|
|
|
var hyperlinkContainer = create("tr");
|
|
|
|
var hyperlinkLeft = create("td");
|
|
|
|
var hyperlinkRight = create("td");
|
|
|
|
var quoteContainer = create("tr");
|
|
|
|
var quoteLeft = create("td");
|
|
|
|
var quoteRight = create("td");
|
|
|
|
|
|
|
|
markdownHelp.id = ID_MARKDOWN_HELP + id;
|
|
|
|
|
|
|
|
classAdd(markdownHelp, "markdown-help");
|
|
|
|
|
|
|
|
boldLeft.innerHTML = "<b>bold</b>";
|
|
|
|
boldRight.innerHTML = "surround text with <pre>**two asterisks**</pre>";
|
|
|
|
italicsLeft.innerHTML = "<i>italics</i>";
|
|
|
|
italicsRight.innerHTML = "surround text with <pre>*asterisks*</pre>";
|
|
|
|
codeLeft.innerHTML = "<pre>code</pre>";
|
|
|
|
codeRight.innerHTML = "surround text with <pre>`backticks`</pre>";
|
|
|
|
strikethroughLeft.innerHTML = "<strike>strikethrough</strike>";
|
|
|
|
strikethroughRight.innerHTML = "surround text with <pre>~~two tilde characters~~</pre>";
|
|
|
|
hyperlinkLeft.innerHTML = "<a href=\"https://example.com\">hyperlink</a>";
|
|
|
|
hyperlinkRight.innerHTML = "<pre>[hyperlink](https://example.com)</pre> or just a bare URL";
|
|
|
|
quoteLeft.innerHTML = "<blockquote>quote</blockquote>";
|
|
|
|
quoteRight.innerHTML = "prefix with <pre>></pre>";
|
|
|
|
|
|
|
|
markdownButton = removeAllEventListeners(markdownButton);
|
|
|
|
onclick(markdownButton, markdownHelpHide, id);
|
|
|
|
|
|
|
|
append(italicsContainer, italicsLeft);
|
|
|
|
append(italicsContainer, italicsRight);
|
|
|
|
append(markdownHelp, italicsContainer);
|
|
|
|
append(boldContainer, boldLeft);
|
|
|
|
append(boldContainer, boldRight);
|
|
|
|
append(markdownHelp, boldContainer);
|
|
|
|
append(hyperlinkContainer, hyperlinkLeft);
|
|
|
|
append(hyperlinkContainer, hyperlinkRight);
|
|
|
|
append(markdownHelp, hyperlinkContainer);
|
|
|
|
append(codeContainer, codeLeft);
|
|
|
|
append(codeContainer, codeRight);
|
|
|
|
append(markdownHelp, codeContainer);
|
|
|
|
append(strikethroughContainer, strikethroughLeft);
|
|
|
|
append(strikethroughContainer, strikethroughRight);
|
|
|
|
append(markdownHelp, strikethroughContainer);
|
|
|
|
append(quoteContainer, quoteLeft);
|
|
|
|
append(quoteContainer, quoteRight);
|
|
|
|
append(markdownHelp, quoteContainer);
|
|
|
|
append(textareaSuperContainer, markdownHelp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function markdownHelpHide(id) {
|
|
|
|
var markdownButton = $(ID_MARKDOWN_BUTTON + id);
|
|
|
|
var markdownHelp = $(ID_MARKDOWN_HELP + id);
|
|
|
|
|
|
|
|
markdownButton = removeAllEventListeners(markdownButton);
|
|
|
|
onclick(markdownButton, markdownHelpShow, id);
|
|
|
|
|
|
|
|
remove(markdownHelp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function textareaCreate(id) {
|
|
|
|
var textareaSuperContainer = create("div");
|
|
|
|
var textareaContainer = create("div");
|
|
|
|
var textarea = create("textarea");
|
2019-02-13 13:53:42 +08:00
|
|
|
var anonymousCheckboxContainer = create("div");
|
|
|
|
var anonymousCheckbox = create("input");
|
|
|
|
var anonymousCheckboxLabel = create("label");
|
|
|
|
var submitButton = create("button");
|
2019-04-21 10:04:56 +08:00
|
|
|
var markdownButton = create("a");
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
textareaSuperContainer.id = ID_SUPER_CONTAINER + id;
|
|
|
|
textareaContainer.id = ID_TEXTAREA_CONTAINER + id;
|
|
|
|
textarea.id = ID_TEXTAREA + id;
|
2019-02-13 13:53:42 +08:00
|
|
|
anonymousCheckbox.id = ID_ANONYMOUS_CHECKBOX + id;
|
|
|
|
submitButton.id = ID_SUBMIT_BUTTON + id;
|
2019-04-21 10:04:56 +08:00
|
|
|
markdownButton.id = ID_MARKDOWN_BUTTON + id;
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
classAdd(textareaContainer, "textarea-container");
|
2019-02-13 13:53:42 +08:00
|
|
|
classAdd(anonymousCheckboxContainer, "round-check");
|
|
|
|
classAdd(anonymousCheckboxContainer, "anonymous-checkbox-container");
|
|
|
|
classAdd(submitButton, "button");
|
|
|
|
classAdd(submitButton, "submit-button");
|
2019-04-21 10:04:56 +08:00
|
|
|
classAdd(markdownButton, "markdown-button");
|
2019-02-13 13:53:42 +08:00
|
|
|
classAdd(textareaSuperContainer, "button-margin");
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
attrSet(textarea, "placeholder", "Add a comment");
|
|
|
|
attrSet(anonymousCheckbox, "type", "checkbox");
|
|
|
|
attrSet(anonymousCheckboxLabel, "for", ID_ANONYMOUS_CHECKBOX + id);
|
2018-12-20 13:48:43 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
anonymousCheckboxLabel.innerText = "Comment anonymously";
|
|
|
|
submitButton.innerText = "Add Comment";
|
2019-04-21 10:04:56 +08:00
|
|
|
markdownButton.innerHTML = "<b>M ↓</b> Markdown";
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
textarea.oninput = autoExpander(textarea);
|
2019-02-13 13:53:42 +08:00
|
|
|
onclick(submitButton, submitAccountDecide, id);
|
2019-04-21 10:04:56 +08:00
|
|
|
onclick(markdownButton, markdownHelpShow, id);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
append(textareaContainer, textarea);
|
|
|
|
append(textareaSuperContainer, textareaContainer);
|
2019-02-13 13:53:42 +08:00
|
|
|
append(anonymousCheckboxContainer, anonymousCheckbox);
|
|
|
|
append(anonymousCheckboxContainer, anonymousCheckboxLabel);
|
|
|
|
append(textareaSuperContainer, submitButton);
|
|
|
|
if (!requireIdentification) {
|
|
|
|
append(textareaSuperContainer, anonymousCheckboxContainer);
|
|
|
|
}
|
2019-04-21 10:04:56 +08:00
|
|
|
append(textareaSuperContainer, markdownButton);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
return textareaSuperContainer;
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function rootCreate(callback) {
|
2019-02-19 05:54:13 +08:00
|
|
|
var login = create("div");
|
|
|
|
var loginText = create("div");
|
2018-06-11 01:15:56 +08:00
|
|
|
var mainArea = $(ID_MAIN_AREA);
|
2018-06-07 16:00:53 +08:00
|
|
|
var commentsArea = create("div");
|
|
|
|
|
2019-02-19 05:54:13 +08:00
|
|
|
login.id = ID_LOGIN;
|
2018-06-07 16:00:53 +08:00
|
|
|
commentsArea.id = ID_COMMENTS_AREA;
|
|
|
|
|
2019-02-19 05:54:13 +08:00
|
|
|
classAdd(login, "login");
|
|
|
|
classAdd(loginText, "login-text");
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(commentsArea, "comments");
|
|
|
|
|
2019-02-19 05:54:13 +08:00
|
|
|
loginText.innerText = "Login";
|
2018-06-07 16:00:53 +08:00
|
|
|
commentsArea.innerHTML = "";
|
|
|
|
|
2019-02-19 05:54:13 +08:00
|
|
|
onclick(loginText, global.loginBoxShow, null);
|
|
|
|
|
|
|
|
append(login, loginText);
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (isLocked || isFrozen) {
|
2019-01-23 13:26:22 +08:00
|
|
|
if (isAuthenticated || chosenAnonymous) {
|
2018-12-19 07:57:32 +08:00
|
|
|
append(mainArea, messageCreate("This thread is locked. You cannot add new comments."));
|
2019-02-19 05:54:13 +08:00
|
|
|
remove($(ID_LOGIN));
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2019-02-19 05:54:13 +08:00
|
|
|
append(mainArea, login);
|
2018-12-19 07:57:32 +08:00
|
|
|
append(mainArea, textareaCreate("root"));
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-19 05:54:13 +08:00
|
|
|
if (!isAuthenticated) {
|
|
|
|
append(mainArea, login);
|
|
|
|
} else {
|
|
|
|
remove($(ID_LOGIN));
|
|
|
|
}
|
2018-12-19 07:57:32 +08:00
|
|
|
append(mainArea, textareaCreate("root"));
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
append(mainArea, commentsArea);
|
|
|
|
append(root, mainArea);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
call(callback);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function messageCreate(text) {
|
|
|
|
var msg = create("div");
|
|
|
|
|
|
|
|
classAdd(msg, "moderation-notice");
|
|
|
|
|
|
|
|
msg.innerText = text;
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.commentNew = function(id, commenterToken, callback) {
|
2019-01-31 11:19:16 +08:00
|
|
|
var textareaSuperContainer = $(ID_SUPER_CONTAINER + id);
|
2018-06-07 16:00:53 +08:00
|
|
|
var textarea = $(ID_TEXTAREA + id);
|
2019-01-31 11:19:16 +08:00
|
|
|
var replyButton = $(ID_REPLY + id);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
var markdown = textarea.value;
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
if (markdown === "") {
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(textarea, "red-border");
|
|
|
|
return;
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-06-07 16:00:53 +08:00
|
|
|
classRemove(textarea, "red-border");
|
|
|
|
}
|
|
|
|
|
|
|
|
var json = {
|
2019-02-23 07:20:12 +08:00
|
|
|
"commenterToken": commenterToken,
|
2019-02-13 15:35:33 +08:00
|
|
|
"domain": parent.location.host,
|
|
|
|
"path": parent.location.pathname,
|
2018-06-07 16:00:53 +08:00
|
|
|
"parentHex": id,
|
2019-01-31 11:19:16 +08:00
|
|
|
"markdown": markdown,
|
2018-06-07 16:00:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
post(origin + "/api/comment/new", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
|
|
|
return;
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
var message = "";
|
|
|
|
if (resp.state === "unapproved") {
|
|
|
|
message = "Your comment is under moderation.";
|
|
|
|
} else if (resp.state === "flagged") {
|
|
|
|
message = "Your comment was flagged as spam and is under moderation.";
|
|
|
|
}
|
2018-12-20 11:57:02 +08:00
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
if (message !== "") {
|
2019-02-13 11:15:06 +08:00
|
|
|
prepend($(ID_SUPER_CONTAINER + id), messageCreate(message));
|
2019-01-31 11:19:16 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 11:15:06 +08:00
|
|
|
var commenterHex = selfHex;
|
2019-02-13 13:53:42 +08:00
|
|
|
if (commenterHex === undefined || commenterToken === "anonymous") {
|
2019-02-13 11:15:06 +08:00
|
|
|
commenterHex = "anonymous";
|
|
|
|
}
|
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
var newCard = commentsRecurse({
|
|
|
|
"root": [{
|
|
|
|
"commentHex": resp.commentHex,
|
2019-02-13 11:15:06 +08:00
|
|
|
"commenterHex": commenterHex,
|
2019-01-31 11:19:16 +08:00
|
|
|
"markdown": markdown,
|
|
|
|
"html": resp.html,
|
|
|
|
"parentHex": "root",
|
|
|
|
"score": 0,
|
|
|
|
"state": "approved",
|
|
|
|
"direction": 0,
|
2019-04-14 09:45:04 +08:00
|
|
|
"creationDate": new Date(),
|
2019-01-31 11:19:16 +08:00
|
|
|
}],
|
|
|
|
}, "root")
|
|
|
|
|
|
|
|
if (id !== "root") {
|
|
|
|
textareaSuperContainer.replaceWith(newCard);
|
|
|
|
|
|
|
|
shownReply[id] = false;
|
|
|
|
|
|
|
|
classAdd(replyButton, "option-reply");
|
|
|
|
classRemove(replyButton, "option-cancel");
|
|
|
|
|
|
|
|
replyButton.title = "Reply to this comment";
|
|
|
|
|
|
|
|
onclick(replyButton, global.replyShow, id)
|
|
|
|
} else {
|
|
|
|
textarea.value = "";
|
|
|
|
insertAfter(textareaSuperContainer, newCard);
|
|
|
|
}
|
2019-02-13 13:53:42 +08:00
|
|
|
|
|
|
|
call(callback);
|
2018-06-07 16:00:53 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function colorGet(name) {
|
|
|
|
var colors = [
|
2018-12-19 08:02:01 +08:00
|
|
|
"#396ab1",
|
|
|
|
"#da7c30",
|
|
|
|
"#3e9651",
|
|
|
|
"#cc2529",
|
|
|
|
"#922428",
|
2019-04-14 09:14:49 +08:00
|
|
|
"#6b4c9a",
|
|
|
|
"#535154",
|
2018-06-07 16:00:53 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
var total = 0;
|
2018-06-24 10:01:21 +08:00
|
|
|
for (var i = 0; i < name.length; i++) {
|
2018-06-07 16:00:53 +08:00
|
|
|
total += name.charCodeAt(i);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
var color = colors[total % colors.length];
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function timeDifference(current, previous) { // thanks stackoverflow
|
|
|
|
var msJustNow = 5000;
|
|
|
|
var msPerMinute = 60000;
|
|
|
|
var msPerHour = 3600000;
|
|
|
|
var msPerDay = 86400000;
|
|
|
|
var msPerMonth = 2592000000;
|
|
|
|
var msPerYear = 946080000000;
|
|
|
|
|
|
|
|
var elapsed = current - previous;
|
|
|
|
|
|
|
|
if (elapsed < msJustNow) {
|
2018-06-24 10:01:21 +08:00
|
|
|
return "just now";
|
|
|
|
} else if (elapsed < msPerMinute) {
|
|
|
|
return Math.round(elapsed / 1000) + " seconds ago";
|
|
|
|
} else if (elapsed < msPerHour) {
|
|
|
|
return Math.round(elapsed / msPerMinute) + " minutes ago";
|
|
|
|
} else if (elapsed < msPerDay ) {
|
|
|
|
return Math.round(elapsed / msPerHour ) + " hours ago";
|
|
|
|
} else if (elapsed < msPerMonth) {
|
|
|
|
return Math.round(elapsed / msPerDay) + " days ago";
|
|
|
|
} else if (elapsed < msPerYear) {
|
|
|
|
return Math.round(elapsed / msPerMonth) + " months ago";
|
|
|
|
} else {
|
|
|
|
return Math.round(elapsed / msPerYear ) + " years ago";
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function scorify(score) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (score !== 1) {
|
2018-06-07 16:00:53 +08:00
|
|
|
return score + " points";
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-06-07 16:00:53 +08:00
|
|
|
return score + " point";
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function commentsRecurse(parentMap, parentHex) {
|
|
|
|
var cur = parentMap[parentHex];
|
|
|
|
if (!cur || !cur.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-23 14:30:00 +08:00
|
|
|
cur.sort(function(a, b) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (a.commentHex === stickyCommentHex) {
|
2018-12-19 07:57:32 +08:00
|
|
|
return -Infinity;
|
2019-04-14 09:45:04 +08:00
|
|
|
} else if (b.commentHex === stickyCommentHex) {
|
2018-12-19 07:57:32 +08:00
|
|
|
return Infinity;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2019-04-14 09:45:04 +08:00
|
|
|
|
|
|
|
if (a.score !== b.score) {
|
|
|
|
return b.score - a.score;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.creationDate < b.creationDate) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-23 14:30:00 +08:00
|
|
|
});
|
|
|
|
|
2019-04-14 09:45:04 +08:00
|
|
|
var curTime = (new Date()).getTime();
|
2018-06-07 16:00:53 +08:00
|
|
|
var cards = create("div");
|
|
|
|
cur.forEach(function(comment) {
|
|
|
|
var commenter = commenters[comment.commenterHex];
|
|
|
|
var avatar;
|
|
|
|
var card = create("div");
|
|
|
|
var header = create("div");
|
|
|
|
var subtitle = create("div");
|
2018-12-19 08:10:12 +08:00
|
|
|
var timeago = create("div");
|
2018-06-07 16:00:53 +08:00
|
|
|
var score = create("div");
|
|
|
|
var body = create("div");
|
2019-01-31 11:19:16 +08:00
|
|
|
var text = create("div");
|
2018-06-07 16:00:53 +08:00
|
|
|
var options = create("div");
|
|
|
|
var edit = create("button");
|
|
|
|
var reply = create("button");
|
|
|
|
var collapse = create("button");
|
2018-09-23 14:32:28 +08:00
|
|
|
var upvote = create("button");
|
|
|
|
var downvote = create("button");
|
2018-06-07 16:00:53 +08:00
|
|
|
var approve = create("button");
|
|
|
|
var remove = create("button");
|
2018-12-19 07:57:32 +08:00
|
|
|
var sticky = create("button");
|
2018-06-07 16:00:53 +08:00
|
|
|
var children = commentsRecurse(parentMap, comment.commentHex);
|
|
|
|
var contents = create("div");
|
2018-12-19 08:02:01 +08:00
|
|
|
var color = colorGet(comment.commenterHex + "-" + commenter.name);
|
2018-06-07 16:00:53 +08:00
|
|
|
var name;
|
2018-06-24 10:01:21 +08:00
|
|
|
if (commenter.link !== "undefined" && commenter.link !== "https://undefined" && commenter.link !== "") {
|
2018-06-07 16:00:53 +08:00
|
|
|
name = create("a");
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-06-07 16:00:53 +08:00
|
|
|
name = create("div");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
card.id = ID_CARD + comment.commentHex;
|
|
|
|
body.id = ID_BODY + comment.commentHex;
|
2019-01-31 11:19:16 +08:00
|
|
|
text.id = ID_TEXT + comment.commentHex;
|
2018-06-07 16:00:53 +08:00
|
|
|
subtitle.id = ID_SUBTITLE + comment.commentHex;
|
2018-12-19 08:10:12 +08:00
|
|
|
timeago.id = ID_TIMEAGO + comment.commentHex;
|
2018-06-07 16:00:53 +08:00
|
|
|
score.id = ID_SCORE + comment.commentHex;
|
|
|
|
options.id = ID_OPTIONS + comment.commentHex;
|
|
|
|
edit.id = ID_EDIT + comment.commentHex;
|
|
|
|
reply.id = ID_REPLY + comment.commentHex;
|
|
|
|
collapse.id = ID_COLLAPSE + comment.commentHex;
|
|
|
|
upvote.id = ID_UPVOTE + comment.commentHex;
|
|
|
|
downvote.id = ID_DOWNVOTE + comment.commentHex;
|
|
|
|
approve.id = ID_APPROVE + comment.commentHex;
|
|
|
|
remove.id = ID_REMOVE + comment.commentHex;
|
2018-12-19 07:57:32 +08:00
|
|
|
sticky.id = ID_STICKY + comment.commentHex;
|
2018-06-24 10:01:21 +08:00
|
|
|
if (children) {
|
2018-12-20 14:27:55 +08:00
|
|
|
children.id = ID_CHILDREN + comment.commentHex;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
contents.id = ID_CONTENTS + comment.commentHex;
|
2018-12-20 11:57:02 +08:00
|
|
|
name.id = ID_NAME + comment.commentHex;
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-12-20 14:27:55 +08:00
|
|
|
collapse.title = "Collapse children";
|
2018-06-07 16:00:53 +08:00
|
|
|
upvote.title = "Upvote";
|
|
|
|
downvote.title = "Downvote";
|
|
|
|
edit.title = "Edit";
|
|
|
|
reply.title = "Reply";
|
|
|
|
approve.title = "Approve";
|
|
|
|
remove.title = "Remove";
|
2018-06-24 10:01:21 +08:00
|
|
|
if (stickyCommentHex === comment.commentHex) {
|
|
|
|
if (isModerator) {
|
2018-12-19 07:57:32 +08:00
|
|
|
sticky.title = "Unsticky";
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-12-19 07:57:32 +08:00
|
|
|
sticky.title = "This comment has been stickied";
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-19 07:57:32 +08:00
|
|
|
sticky.title = "Sticky";
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2019-04-14 09:45:04 +08:00
|
|
|
timeago.title = comment.creationDate.toString();
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
card.style["borderLeft"] = "2px solid " + color;
|
|
|
|
name.innerText = commenter.name;
|
2019-01-31 11:19:16 +08:00
|
|
|
text.innerHTML = comment.html;
|
2019-04-14 09:45:04 +08:00
|
|
|
timeago.innerHTML = timeDifference(curTime, comment.creationDate);
|
2018-06-07 16:00:53 +08:00
|
|
|
score.innerText = scorify(comment.score);
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (commenter.photo === "undefined") {
|
2018-06-07 16:00:53 +08:00
|
|
|
avatar = create("div");
|
|
|
|
avatar.style["background"] = color;
|
2019-04-14 09:18:28 +08:00
|
|
|
|
|
|
|
if (comment.commenterHex === "anonymous") {
|
|
|
|
avatar.innerHTML = "?";
|
|
|
|
avatar.style["font-weight"] = "bold";
|
|
|
|
} else {
|
|
|
|
avatar.innerHTML = commenter.name[0].toUpperCase();
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(avatar, "avatar");
|
|
|
|
} else {
|
|
|
|
avatar = create("img");
|
2019-02-23 11:20:55 +08:00
|
|
|
attrSet(avatar, "src", cdn + "/api/commenter/photo?commenterHex=" + commenter.commenterHex);
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(avatar, "avatar-img");
|
|
|
|
}
|
|
|
|
|
|
|
|
classAdd(card, "card");
|
2018-06-24 10:01:21 +08:00
|
|
|
if (isModerator && comment.state !== "approved") {
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(card, "dark-card");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2019-02-23 11:43:25 +08:00
|
|
|
if (commenter.isModerator) {
|
|
|
|
classAdd(name, "moderator");
|
|
|
|
}
|
2018-06-24 10:01:21 +08:00
|
|
|
if (comment.state === "flagged") {
|
2018-12-20 11:57:02 +08:00
|
|
|
classAdd(name, "flagged");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(header, "header");
|
|
|
|
classAdd(name, "name");
|
|
|
|
classAdd(subtitle, "subtitle");
|
2018-12-19 08:10:12 +08:00
|
|
|
classAdd(timeago, "timeago");
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(score, "score");
|
|
|
|
classAdd(body, "body");
|
|
|
|
classAdd(options, "options");
|
2018-06-24 10:01:21 +08:00
|
|
|
if (mobileView) {
|
2018-12-20 14:27:55 +08:00
|
|
|
classAdd(options, "options-mobile");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(edit, "option-button");
|
|
|
|
classAdd(edit, "option-edit");
|
|
|
|
classAdd(reply, "option-button");
|
|
|
|
classAdd(reply, "option-reply");
|
|
|
|
classAdd(collapse, "option-button");
|
|
|
|
classAdd(collapse, "option-collapse");
|
|
|
|
classAdd(upvote, "option-button");
|
|
|
|
classAdd(upvote, "option-upvote");
|
|
|
|
classAdd(downvote, "option-button");
|
|
|
|
classAdd(downvote, "option-downvote");
|
|
|
|
classAdd(approve, "option-button");
|
|
|
|
classAdd(approve, "option-approve");
|
|
|
|
classAdd(remove, "option-button");
|
|
|
|
classAdd(remove, "option-remove");
|
2018-12-19 07:57:32 +08:00
|
|
|
classAdd(sticky, "option-button");
|
2018-06-24 10:01:21 +08:00
|
|
|
if (stickyCommentHex === comment.commentHex) {
|
2018-12-19 07:57:32 +08:00
|
|
|
classAdd(sticky, "option-unsticky");
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-12-19 07:57:32 +08:00
|
|
|
classAdd(sticky, "option-sticky");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
if (isAuthenticated) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (comment.direction > 0) {
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(upvote, "upvoted");
|
2018-06-24 10:01:21 +08:00
|
|
|
} else if (comment.direction < 0) {
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(downvote, "downvoted");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(collapse, global.commentCollapse, comment.commentHex);
|
|
|
|
onclick(approve, global.commentApprove, comment.commentHex);
|
|
|
|
onclick(remove, global.commentDelete, comment.commentHex);
|
|
|
|
onclick(sticky, global.commentSticky, comment.commentHex);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (isAuthenticated) {
|
2019-03-03 02:28:08 +08:00
|
|
|
var upDown = upDownOnclickSet(upvote, downvote, comment.commentHex, comment.direction);
|
|
|
|
upvote = upDown[0];
|
|
|
|
downvote = upDown[1];
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2019-02-13 13:53:42 +08:00
|
|
|
onclick(upvote, global.loginBoxShow, null);
|
|
|
|
onclick(downvote, global.loginBoxShow, null);
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
onclick(reply, global.replyShow, comment.commentHex);
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (commenter.link !== "undefined" && commenter.link !== "https://undefined" && commenter.link !== "") {
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(name, "href", commenter.link);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-07-24 14:27:25 +08:00
|
|
|
append(options, collapse);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-07-24 14:27:25 +08:00
|
|
|
// append(options, edit); // uncomment when implemented
|
2018-06-11 01:15:56 +08:00
|
|
|
append(options, downvote);
|
2018-07-24 14:27:25 +08:00
|
|
|
append(options, upvote);
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-12-19 07:57:32 +08:00
|
|
|
append(options, reply);
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
if (isModerator) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (parentHex === "root") {
|
2018-12-19 07:57:32 +08:00
|
|
|
append(options, sticky);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-07-24 14:27:25 +08:00
|
|
|
append(options, remove);
|
2018-06-24 10:01:21 +08:00
|
|
|
if (comment.state !== "approved") {
|
2018-06-07 16:00:53 +08:00
|
|
|
append(options, approve);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (stickyCommentHex === comment.commentHex) {
|
2018-12-19 07:57:32 +08:00
|
|
|
append(options, sticky);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-12-19 07:57:32 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-07-24 14:27:25 +08:00
|
|
|
attrSet(options, "style", "width: " + ((options.childNodes.length+1)*32) + "px;");
|
2018-06-24 10:01:21 +08:00
|
|
|
for (var i = 0; i < options.childNodes.length; i++) {
|
2018-07-24 14:27:25 +08:00
|
|
|
attrSet(options.childNodes[i], "style", "right: " + (i*32) + "px;");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-07-24 14:27:25 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
append(subtitle, score);
|
2018-12-19 08:10:12 +08:00
|
|
|
append(subtitle, timeago);
|
2018-07-24 14:27:25 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (!mobileView) {
|
2018-12-20 14:27:55 +08:00
|
|
|
append(header, options);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
append(header, avatar);
|
|
|
|
append(header, name);
|
|
|
|
append(header, subtitle);
|
2019-01-31 11:19:16 +08:00
|
|
|
append(body, text);
|
2018-06-07 16:00:53 +08:00
|
|
|
append(contents, body);
|
2018-12-20 14:27:55 +08:00
|
|
|
if (mobileView) {
|
|
|
|
append(contents, options);
|
|
|
|
var optionsClearfix = create("div");
|
|
|
|
classAdd(optionsClearfix, "options-clearfix");
|
|
|
|
append(contents, optionsClearfix);
|
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
if (children) {
|
|
|
|
classAdd(children, "body");
|
|
|
|
append(contents, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
append(card, header);
|
|
|
|
append(card, contents);
|
|
|
|
append(cards, card);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cards;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
global.commentApprove = function(commentHex) {
|
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"commenterToken": commenterTokenGet(),
|
2018-06-07 16:00:53 +08:00
|
|
|
"commentHex": commentHex,
|
|
|
|
}
|
|
|
|
|
|
|
|
post(origin + "/api/comment/approve", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
|
|
|
return
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var card = $(ID_CARD + commentHex);
|
2018-12-20 11:57:02 +08:00
|
|
|
var name = $(ID_NAME + commentHex);
|
2018-06-07 16:00:53 +08:00
|
|
|
var tick = $(ID_APPROVE + commentHex);
|
|
|
|
|
|
|
|
classRemove(card, "dark-card");
|
2018-12-20 11:57:02 +08:00
|
|
|
classRemove(name, "flagged");
|
2018-06-07 16:00:53 +08:00
|
|
|
remove(tick);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
global.commentDelete = function(commentHex) {
|
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"commenterToken": commenterTokenGet(),
|
2018-06-07 16:00:53 +08:00
|
|
|
"commentHex": commentHex,
|
|
|
|
}
|
|
|
|
|
|
|
|
post(origin + "/api/comment/delete", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
|
|
|
return
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var card = $(ID_CARD + commentHex);
|
|
|
|
remove(card);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function nameWidthFix() {
|
|
|
|
var els = document.getElementsByClassName("commento-name");
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
for (var i = 0; i < els.length; i++) {
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(els[i], "style", "max-width: " + (els[i].getBoundingClientRect()["width"] + 20) + "px;")
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-20 13:48:43 +08:00
|
|
|
function upDownOnclickSet(upvote, downvote, commentHex, direction) {
|
2019-03-03 02:28:08 +08:00
|
|
|
upvote = removeAllEventListeners(upvote);
|
|
|
|
downvote = removeAllEventListeners(downvote);
|
|
|
|
|
2018-12-20 13:48:43 +08:00
|
|
|
if (direction > 0) {
|
2019-01-17 12:00:24 +08:00
|
|
|
onclick(upvote, global.vote, [commentHex, [1, 0]]);
|
|
|
|
onclick(downvote, global.vote, [commentHex, [1, -1]]);
|
2018-06-24 10:01:21 +08:00
|
|
|
} else if (direction < 0) {
|
2019-01-17 12:00:24 +08:00
|
|
|
onclick(upvote, global.vote, [commentHex, [-1, 1]]);
|
|
|
|
onclick(downvote, global.vote, [commentHex, [-1, 0]]);
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2019-01-17 12:00:24 +08:00
|
|
|
onclick(upvote, global.vote, [commentHex, [0, 1]]);
|
|
|
|
onclick(downvote, global.vote, [commentHex, [0, -1]]);
|
2018-12-20 13:48:43 +08:00
|
|
|
}
|
2019-03-03 02:28:08 +08:00
|
|
|
|
|
|
|
return [upvote, downvote];
|
2018-12-20 13:48:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-17 12:00:24 +08:00
|
|
|
global.vote = function(data) {
|
|
|
|
var commentHex = data[0];
|
|
|
|
var oldDirection = data[1][0];
|
|
|
|
var newDirection = data[1][1];
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
var upvote = $(ID_UPVOTE + commentHex);
|
|
|
|
var downvote = $(ID_DOWNVOTE + commentHex);
|
|
|
|
var score = $(ID_SCORE + commentHex);
|
|
|
|
|
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"commenterToken": commenterTokenGet(),
|
2018-06-07 16:00:53 +08:00
|
|
|
"commentHex": commentHex,
|
2019-01-17 12:00:24 +08:00
|
|
|
"direction": newDirection,
|
2018-06-07 16:00:53 +08:00
|
|
|
};
|
|
|
|
|
2019-03-03 02:28:08 +08:00
|
|
|
var upDown = upDownOnclickSet(upvote, downvote, commentHex, newDirection);
|
|
|
|
upvote = upDown[0];
|
|
|
|
downvote = upDown[1];
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
classRemove(upvote, "upvoted");
|
|
|
|
classRemove(downvote, "downvoted");
|
2019-01-17 12:00:24 +08:00
|
|
|
if (newDirection > 0) {
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(upvote, "upvoted");
|
2019-01-17 12:00:24 +08:00
|
|
|
} else if (newDirection < 0) {
|
2018-06-07 16:00:53 +08:00
|
|
|
classAdd(downvote, "downvoted");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-01-17 12:00:24 +08:00
|
|
|
score.innerText = scorify(parseInt(score.innerText.replace(/[^\d-.]/g, "")) + newDirection - oldDirection);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
post(origin + "/api/comment/vote", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
2019-03-03 03:30:23 +08:00
|
|
|
classRemove(upvote, "upvoted");
|
|
|
|
classRemove(downvote, "downvoted");
|
|
|
|
score.innerText = scorify(parseInt(score.innerText.replace(/[^\d-.]/g, "")) - newDirection + oldDirection);
|
|
|
|
upDownOnclickSet(upvote, downvote, commentHex, oldDirection);
|
2018-06-24 10:01:21 +08:00
|
|
|
return;
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
|
|
|
});
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
global.replyShow = function(id) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (id in shownReply && shownReply[id]) {
|
2018-06-07 16:00:53 +08:00
|
|
|
return;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-01-31 11:19:16 +08:00
|
|
|
var text = $(ID_TEXT + id);
|
|
|
|
insertAfter(text, textareaCreate(id));
|
2018-06-07 16:00:53 +08:00
|
|
|
shownReply[id] = true;
|
|
|
|
|
|
|
|
var replyButton = $(ID_REPLY + id);
|
|
|
|
|
|
|
|
classRemove(replyButton, "option-reply");
|
|
|
|
classAdd(replyButton, "option-cancel");
|
|
|
|
|
|
|
|
replyButton.title = "Cancel reply";
|
|
|
|
|
2019-04-14 09:11:00 +08:00
|
|
|
replyButton = removeAllEventListeners(replyButton);
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(replyButton, global.replyCollapse, id);
|
2018-06-07 16:00:53 +08:00
|
|
|
};
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
global.replyCollapse = function(id) {
|
|
|
|
var replyButton = $(ID_REPLY + id);
|
|
|
|
var el = $(ID_SUPER_CONTAINER + id);
|
|
|
|
|
|
|
|
el.remove();
|
|
|
|
shownReply[id] = false;
|
|
|
|
|
|
|
|
classAdd(replyButton, "option-reply");
|
|
|
|
classRemove(replyButton, "option-cancel");
|
|
|
|
|
|
|
|
replyButton.title = "Reply to this comment";
|
|
|
|
|
2019-04-14 09:11:00 +08:00
|
|
|
replyButton = removeAllEventListeners(replyButton);
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(replyButton, global.replyShow, id)
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
global.commentCollapse = function(id) {
|
2018-12-20 14:27:55 +08:00
|
|
|
var children = $(ID_CHILDREN + id);
|
2018-06-07 16:00:53 +08:00
|
|
|
var button = $(ID_COLLAPSE + id);
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (children) {
|
2018-12-20 14:27:55 +08:00
|
|
|
classAdd(children, "hidden");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
classRemove(button, "option-collapse");
|
|
|
|
classAdd(button, "option-uncollapse");
|
|
|
|
|
2018-12-20 14:27:55 +08:00
|
|
|
button.title = "Expand children";
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-04-14 09:11:00 +08:00
|
|
|
button = removeAllEventListeners(button);
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(button, global.commentUncollapse, id);
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
global.commentUncollapse = function(id) {
|
2018-12-20 14:27:55 +08:00
|
|
|
var children = $(ID_CHILDREN + id);
|
2018-06-07 16:00:53 +08:00
|
|
|
var button = $(ID_COLLAPSE + id);
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (children) {
|
2018-12-20 14:27:55 +08:00
|
|
|
classRemove(children, "hidden");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
classRemove(button, "option-uncollapse");
|
|
|
|
classAdd(button, "option-collapse");
|
|
|
|
|
2018-12-20 14:27:55 +08:00
|
|
|
button.title = "Collapse children";
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-04-14 09:11:00 +08:00
|
|
|
button = removeAllEventListeners(button);
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(button, global.commentCollapse, id);
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function commentsRender() {
|
|
|
|
var parentMap = {};
|
|
|
|
var parentHex;
|
|
|
|
|
|
|
|
var commentsArea = $(ID_COMMENTS_AREA);
|
|
|
|
|
|
|
|
comments.forEach(function(comment) {
|
|
|
|
parentHex = comment.parentHex;
|
|
|
|
if (!(parentHex in parentMap)) {
|
|
|
|
parentMap[parentHex] = [];
|
|
|
|
}
|
2019-04-14 09:45:04 +08:00
|
|
|
|
|
|
|
comment.creationDate = new Date(comment.creationDate);
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
parentMap[parentHex].push(comment);
|
|
|
|
});
|
|
|
|
|
|
|
|
var cards = commentsRecurse(parentMap, "root");
|
|
|
|
if (cards) {
|
|
|
|
append(commentsArea, cards);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
function submitAuthenticated(id) {
|
|
|
|
if (isAuthenticated) {
|
|
|
|
global.commentNew(id, commenterTokenGet());
|
2018-06-07 16:00:53 +08:00
|
|
|
return;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.loginBoxShow(id);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
function submitAnonymous(id) {
|
|
|
|
chosenAnonymous = true;
|
|
|
|
global.commentNew(id, "anonymous");
|
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
function submitAccountDecide(id) {
|
|
|
|
if (requireIdentification) {
|
|
|
|
submitAuthenticated(id);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
var anonymousCheckbox = $(ID_ANONYMOUS_CHECKBOX + id);
|
2019-02-13 14:46:46 +08:00
|
|
|
var textarea = $(ID_TEXTAREA + id);
|
|
|
|
var markdown = textarea.value;
|
|
|
|
|
|
|
|
if (markdown === "") {
|
|
|
|
classAdd(textarea, "red-border");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
classRemove(textarea, "red-border");
|
|
|
|
}
|
2018-06-24 10:01:21 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
if (!anonymousCheckbox.checked) {
|
|
|
|
submitAuthenticated(id);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
submitAnonymous(id);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-19 08:46:43 +08:00
|
|
|
}
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.commentoAuth = function(data) {
|
|
|
|
var provider = data.provider;
|
|
|
|
var id = data.id;
|
2018-06-07 16:00:53 +08:00
|
|
|
var popup = window.open("", "_blank");
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
get(origin + "/api/commenter/token/new", function(resp) {
|
2018-06-07 16:00:53 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
|
|
|
return;
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:33:40 +08:00
|
|
|
cookieSet("commentoCommenterToken", resp.commenterToken);
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
popup.location = origin + "/api/oauth/" + provider + "/redirect?commenterToken=" + resp.commenterToken;
|
2018-06-07 16:00:53 +08:00
|
|
|
|
|
|
|
var interval = setInterval(function() {
|
|
|
|
if (popup.closed) {
|
|
|
|
clearInterval(interval);
|
2019-02-13 13:53:42 +08:00
|
|
|
selfGet(function() {
|
|
|
|
var loggedContainer = $(ID_LOGGED_CONTAINER);
|
|
|
|
if (loggedContainer) {
|
|
|
|
attrSet(loggedContainer, "style", "");
|
|
|
|
}
|
|
|
|
|
2019-02-19 05:58:10 +08:00
|
|
|
remove($(ID_LOGIN));
|
|
|
|
if (id !== null) {
|
|
|
|
global.commentNew(id, resp.commenterToken, function() {
|
|
|
|
global.loginBoxClose();
|
|
|
|
});
|
|
|
|
} else {
|
2019-02-13 13:53:42 +08:00
|
|
|
global.loginBoxClose();
|
2019-02-19 05:58:10 +08:00
|
|
|
}
|
2019-02-13 13:53:42 +08:00
|
|
|
});
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
function refreshAll(callback) {
|
2018-06-11 01:15:56 +08:00
|
|
|
$(ID_ROOT).innerHTML = "";
|
2018-06-07 16:00:53 +08:00
|
|
|
shownReply = {};
|
2018-12-20 13:48:43 +08:00
|
|
|
global.main(callback);
|
2018-06-07 16:00:53 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
function loginBoxCreate() {
|
|
|
|
var loginBoxContainer = create("div");
|
|
|
|
|
|
|
|
loginBoxContainer.id = ID_LOGIN_BOX_CONTAINER;
|
|
|
|
|
|
|
|
append(root, loginBoxContainer);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.popupRender = function(id) {
|
2018-06-11 01:15:56 +08:00
|
|
|
var loginBoxContainer = $(ID_LOGIN_BOX_CONTAINER);
|
|
|
|
var loginBox = create("div");
|
2019-04-21 08:34:25 +08:00
|
|
|
var ssoSubtitle = create("div");
|
|
|
|
var ssoButtonContainer = create("div");
|
|
|
|
var ssoButton = create("div");
|
|
|
|
var hr1 = create("hr");
|
2019-02-13 15:08:00 +08:00
|
|
|
var oauthSubtitle = create("div");
|
|
|
|
var oauthButtonsContainer = create("div");
|
|
|
|
var oauthButtons = create("div");
|
2019-04-21 08:34:25 +08:00
|
|
|
var hr2 = create("hr");
|
2019-02-13 15:08:00 +08:00
|
|
|
var emailSubtitle = create("div");
|
2018-06-11 01:15:56 +08:00
|
|
|
var emailContainer = create("div");
|
|
|
|
var email = create("div");
|
|
|
|
var emailInput = create("input");
|
|
|
|
var emailButton = create("button");
|
|
|
|
var loginLinkContainer = create("div");
|
|
|
|
var loginLink = create("a");
|
|
|
|
var close = create("div");
|
|
|
|
|
|
|
|
loginBox.id = ID_LOGIN_BOX;
|
2019-02-13 15:08:00 +08:00
|
|
|
emailSubtitle.id = ID_LOGIN_BOX_EMAIL_SUBTITLE;
|
2018-06-11 01:15:56 +08:00
|
|
|
emailInput.id = ID_LOGIN_BOX_EMAIL_INPUT;
|
|
|
|
emailButton.id = ID_LOGIN_BOX_EMAIL_BUTTON;
|
|
|
|
loginLink.id = ID_LOGIN_BOX_LOGIN_LINK;
|
|
|
|
loginLinkContainer.id = ID_LOGIN_BOX_LOGIN_LINK_CONTAINER;
|
2019-04-21 08:34:25 +08:00
|
|
|
hr2.id = ID_LOGIN_BOX_HR;
|
2019-02-13 15:08:00 +08:00
|
|
|
oauthSubtitle.id = ID_LOGIN_BOX_OAUTH_PRETEXT;
|
2018-06-11 01:15:56 +08:00
|
|
|
oauthButtonsContainer.id = ID_LOGIN_BOX_OAUTH_BUTTONS_CONTAINER;
|
|
|
|
|
|
|
|
classAdd(loginBoxContainer, "login-box-container");
|
|
|
|
classAdd(loginBox, "login-box");
|
2019-02-13 15:08:00 +08:00
|
|
|
classAdd(emailSubtitle, "login-box-subtitle");
|
2018-06-11 01:15:56 +08:00
|
|
|
classAdd(emailContainer, "email-container");
|
|
|
|
classAdd(email, "email");
|
|
|
|
classAdd(emailInput, "input");
|
|
|
|
classAdd(emailButton, "email-button");
|
|
|
|
classAdd(loginLinkContainer, "login-link-container");
|
|
|
|
classAdd(loginLink, "login-link");
|
2019-04-21 08:34:25 +08:00
|
|
|
classAdd(ssoSubtitle, "login-box-subtitle");
|
|
|
|
classAdd(ssoButtonContainer, "oauth-buttons-container");
|
|
|
|
classAdd(ssoButton, "oauth-buttons");
|
2019-02-13 15:08:00 +08:00
|
|
|
classAdd(oauthSubtitle, "login-box-subtitle");
|
2018-06-11 01:15:56 +08:00
|
|
|
classAdd(oauthButtonsContainer, "oauth-buttons-container");
|
|
|
|
classAdd(oauthButtons, "oauth-buttons");
|
|
|
|
classAdd(close, "login-box-close");
|
2018-09-23 12:52:34 +08:00
|
|
|
classAdd(root, "root-min-height");
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
loginLink.innerText = "Don't have an account? Sign up.";
|
2019-02-13 15:08:00 +08:00
|
|
|
emailSubtitle.innerText = "Login with your email address";
|
2018-06-11 01:15:56 +08:00
|
|
|
emailButton.innerText = "Continue";
|
2019-02-13 15:08:00 +08:00
|
|
|
oauthSubtitle.innerText = "Proceed with social login";
|
2019-04-21 08:34:25 +08:00
|
|
|
ssoSubtitle.innerText = "Proceed with " + parent.location.host + " authentication";
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
onclick(emailButton, global.passwordAsk, id);
|
2019-02-13 14:46:46 +08:00
|
|
|
onclick(loginLink, global.popupSwitch);
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(close, global.loginBoxClose);
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(loginBoxContainer, "style", "display: none; opacity: 0;");
|
|
|
|
attrSet(emailInput, "name", "email");
|
|
|
|
attrSet(emailInput, "placeholder", "Email address");
|
|
|
|
attrSet(emailInput, "type", "text");
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
var numOauthConfigured = 0;
|
|
|
|
var oauthProviders = ["google", "twitter", "github", "gitlab"];
|
|
|
|
oauthProviders.forEach(function(provider) {
|
|
|
|
if (configuredOauths[provider]) {
|
|
|
|
var button = create("button");
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
classAdd(button, "button");
|
2019-04-21 08:34:25 +08:00
|
|
|
classAdd(button, provider + "-button");
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
button.innerText = provider;
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
onclick(button, global.commentoAuth, {"provider": provider, "id": id});
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
append(oauthButtons, button);
|
|
|
|
numOauthConfigured++;
|
|
|
|
}
|
|
|
|
});
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-04-21 08:34:25 +08:00
|
|
|
if (configuredOauths["sso"]) {
|
|
|
|
var button = create("button");
|
|
|
|
|
|
|
|
classAdd(button, "button");
|
|
|
|
classAdd(button, "sso-button");
|
|
|
|
|
|
|
|
button.innerText = "Login with Single Sign-On";
|
|
|
|
|
|
|
|
onclick(button, global.commentoAuth, {"provider": "sso", "id": id});
|
|
|
|
|
|
|
|
append(ssoButton, button);
|
|
|
|
append(ssoButtonContainer, ssoButton);
|
|
|
|
append(loginBox, ssoSubtitle);
|
|
|
|
append(loginBox, ssoButtonContainer);
|
|
|
|
|
|
|
|
if (numOauthConfigured > 0 || configuredOauths["commento"]) {
|
|
|
|
append(loginBox, hr1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
if (numOauthConfigured > 0) {
|
2019-02-13 15:08:00 +08:00
|
|
|
append(loginBox, oauthSubtitle);
|
|
|
|
append(oauthButtonsContainer, oauthButtons);
|
|
|
|
append(loginBox, oauthButtonsContainer);
|
|
|
|
oauthButtonsShown = true;
|
|
|
|
} else {
|
|
|
|
oauthButtonsShown = false;
|
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
append(email, emailInput);
|
|
|
|
append(email, emailButton);
|
|
|
|
append(emailContainer, email);
|
|
|
|
|
|
|
|
append(loginLinkContainer, loginLink);
|
2019-04-20 07:03:34 +08:00
|
|
|
|
|
|
|
if (numOauthConfigured > 0 && configuredOauths["commento"]) {
|
2019-04-21 08:34:25 +08:00
|
|
|
append(loginBox, hr2);
|
2019-04-20 07:03:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (configuredOauths["commento"]) {
|
|
|
|
append(loginBox, emailSubtitle);
|
|
|
|
append(loginBox, emailContainer);
|
|
|
|
append(loginBox, loginLinkContainer);
|
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
append(loginBox, close);
|
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
popupBoxType = "login";
|
2018-06-11 01:15:56 +08:00
|
|
|
loginBoxContainer.innerHTML = "";
|
|
|
|
append(loginBoxContainer, loginBox);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
global.popupSwitch = function() {
|
2019-02-13 15:08:00 +08:00
|
|
|
var emailSubtitle = $(ID_LOGIN_BOX_EMAIL_SUBTITLE);
|
2018-06-11 01:15:56 +08:00
|
|
|
var loginLink = $(ID_LOGIN_BOX_LOGIN_LINK);
|
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
if (popupBoxType === "login") {
|
|
|
|
loginLink.innerText = "Already have an account? Log in.";
|
2019-02-13 15:08:00 +08:00
|
|
|
emailSubtitle.innerText = "Create an account";
|
2019-02-13 14:46:46 +08:00
|
|
|
popupBoxType = "signup";
|
|
|
|
} else {
|
|
|
|
loginLink.innerText = "Don't have an account? Sign up.";
|
2019-02-13 15:08:00 +08:00
|
|
|
emailSubtitle.innerText = "Login with your email address";
|
2019-02-13 14:46:46 +08:00
|
|
|
popupBoxType = "login";
|
2018-06-11 03:10:34 +08:00
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
function loginUP(username, password, id) {
|
2018-06-11 01:15:56 +08:00
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"email": username,
|
|
|
|
"password": password,
|
2018-06-11 01:15:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
post(origin + "/api/commenter/login", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
2018-12-20 13:48:43 +08:00
|
|
|
global.loginBoxClose();
|
2018-06-11 01:15:56 +08:00
|
|
|
errorShow(resp.message);
|
|
|
|
return
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:33:40 +08:00
|
|
|
cookieSet("commentoCommenterToken", resp.commenterToken);
|
2019-02-13 13:53:42 +08:00
|
|
|
|
|
|
|
selfLoad(resp.commenter);
|
2019-04-14 09:29:06 +08:00
|
|
|
global.allShow();
|
2019-02-13 13:53:42 +08:00
|
|
|
|
2019-02-19 05:58:10 +08:00
|
|
|
remove($(ID_LOGIN));
|
|
|
|
if (id !== null) {
|
|
|
|
global.commentNew(id, resp.commenterToken, function() {
|
|
|
|
global.loginBoxClose();
|
|
|
|
});
|
|
|
|
} else {
|
2019-02-13 13:53:42 +08:00
|
|
|
global.loginBoxClose();
|
2019-02-19 05:58:10 +08:00
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.login = function(id) {
|
2018-06-11 01:15:56 +08:00
|
|
|
var email = $(ID_LOGIN_BOX_EMAIL_INPUT);
|
|
|
|
var password = $(ID_LOGIN_BOX_PASSWORD_INPUT);
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
loginUP(email.value, password.value, id);
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.signup = function(id) {
|
2018-06-11 01:15:56 +08:00
|
|
|
var email = $(ID_LOGIN_BOX_EMAIL_INPUT);
|
|
|
|
var name = $(ID_LOGIN_BOX_NAME_INPUT);
|
|
|
|
var website = $(ID_LOGIN_BOX_WEBSITE_INPUT);
|
|
|
|
var password = $(ID_LOGIN_BOX_PASSWORD_INPUT);
|
|
|
|
|
|
|
|
var json = {
|
2018-06-20 11:29:55 +08:00
|
|
|
"email": email.value,
|
|
|
|
"name": name.value,
|
|
|
|
"website": website.value,
|
|
|
|
"password": password.value,
|
2018-06-11 01:15:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
post(origin + "/api/commenter/new", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
2018-12-20 13:48:43 +08:00
|
|
|
global.loginBoxClose();
|
2018-06-11 01:15:56 +08:00
|
|
|
errorShow(resp.message);
|
|
|
|
return
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
loginUP(email.value, password.value, id);
|
2018-06-11 01:15:56 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.passwordAsk = function(id) {
|
2018-06-11 01:15:56 +08:00
|
|
|
var loginBox = $(ID_LOGIN_BOX);
|
2019-02-13 15:08:00 +08:00
|
|
|
var subtitle = $(ID_LOGIN_BOX_EMAIL_SUBTITLE);
|
2018-06-11 01:15:56 +08:00
|
|
|
var emailButton = $(ID_LOGIN_BOX_EMAIL_BUTTON);
|
|
|
|
var loginLinkContainer = $(ID_LOGIN_BOX_LOGIN_LINK_CONTAINER);
|
|
|
|
var hr = $(ID_LOGIN_BOX_HR);
|
|
|
|
var oauthButtonsContainer = $(ID_LOGIN_BOX_OAUTH_BUTTONS_CONTAINER);
|
|
|
|
var oauthPretext = $(ID_LOGIN_BOX_OAUTH_PRETEXT);
|
|
|
|
|
|
|
|
remove(emailButton);
|
|
|
|
remove(loginLinkContainer);
|
2018-08-13 13:20:23 +08:00
|
|
|
if (oauthButtonsShown) {
|
2018-06-11 03:53:37 +08:00
|
|
|
if (configuredOauths.length > 0) {
|
2018-06-11 03:10:34 +08:00
|
|
|
remove(hr);
|
|
|
|
remove(oauthPretext);
|
|
|
|
remove(oauthButtonsContainer);
|
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
var order, fid, type, placeholder;
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
if (popupBoxType === "signup") {
|
2019-02-13 13:53:42 +08:00
|
|
|
order = ["name", "website", "password"];
|
|
|
|
fid = [ID_LOGIN_BOX_NAME_INPUT, ID_LOGIN_BOX_WEBSITE_INPUT, ID_LOGIN_BOX_PASSWORD_INPUT];
|
|
|
|
type = ["text", "text", "password"];
|
|
|
|
placeholder = ["Real Name", "Website (Optional)", "Password"];
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2019-02-13 13:53:42 +08:00
|
|
|
order = ["password"];
|
|
|
|
fid = [ID_LOGIN_BOX_PASSWORD_INPUT];
|
|
|
|
type = ["password"];
|
|
|
|
placeholder = ["Password"];
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
if (popupBoxType === "signup") {
|
2018-08-13 13:22:04 +08:00
|
|
|
subtitle.innerText = "Finish the rest of your profile to complete."
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-08-13 13:22:04 +08:00
|
|
|
subtitle.innerText = "Enter your password to log in."
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < order.length; i++) {
|
|
|
|
var fieldContainer = create("div");
|
|
|
|
var field = create("div");
|
|
|
|
var fieldInput = create("input");
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
fieldInput.id = fid[i];
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
classAdd(fieldContainer, "email-container");
|
|
|
|
classAdd(field, "email");
|
|
|
|
classAdd(fieldInput, "input");
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(fieldInput, "name", order[i]);
|
|
|
|
attrSet(fieldInput, "type", type[i]);
|
|
|
|
attrSet(fieldInput, "placeholder", placeholder[i]);
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
append(field, fieldInput);
|
|
|
|
append(fieldContainer, field);
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (order[i] === "password") {
|
2018-06-11 01:15:56 +08:00
|
|
|
var fieldButton = create("button");
|
|
|
|
classAdd(fieldButton, "email-button");
|
2019-02-13 14:46:46 +08:00
|
|
|
fieldButton.innerText = popupBoxType;
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
if (popupBoxType === "signup") {
|
2019-02-13 13:53:42 +08:00
|
|
|
onclick(fieldButton, global.signup, id);
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2019-02-13 13:53:42 +08:00
|
|
|
onclick(fieldButton, global.login, id);
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
append(field, fieldButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
append(loginBox, fieldContainer);
|
|
|
|
}
|
2018-08-13 13:22:19 +08:00
|
|
|
|
2019-02-13 14:46:46 +08:00
|
|
|
if (popupBoxType === "signup") {
|
2018-12-20 13:48:43 +08:00
|
|
|
$(ID_LOGIN_BOX_NAME_INPUT).focus();
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-08-13 13:23:15 +08:00
|
|
|
$(ID_LOGIN_BOX_PASSWORD_INPUT).focus();
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-07-05 13:06:52 +08:00
|
|
|
function pageUpdate(callback) {
|
|
|
|
var attributes = {
|
|
|
|
"isLocked": isLocked,
|
2018-12-19 07:57:32 +08:00
|
|
|
"stickyCommentHex": stickyCommentHex,
|
2018-07-05 13:06:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var json = {
|
|
|
|
"commenterToken": commenterTokenGet(),
|
2019-02-13 15:35:33 +08:00
|
|
|
"domain": parent.location.host,
|
|
|
|
"path": parent.location.pathname,
|
2018-07-05 13:06:52 +08:00
|
|
|
"attributes": attributes,
|
|
|
|
};
|
|
|
|
|
|
|
|
post(origin + "/api/page/update", json, function(resp) {
|
|
|
|
if (!resp.success) {
|
|
|
|
errorShow(resp.message);
|
|
|
|
return
|
2019-04-14 09:34:27 +08:00
|
|
|
} else {
|
|
|
|
errorHide();
|
2018-07-05 13:06:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
call(callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
global.threadLockToggle = function() {
|
|
|
|
var lock = $(ID_MOD_TOOLS_LOCK_BUTTON);
|
|
|
|
|
|
|
|
isLocked = !isLocked;
|
|
|
|
|
|
|
|
lock.disabled = true;
|
2019-01-23 13:26:22 +08:00
|
|
|
pageUpdate(function() {
|
|
|
|
lock.disabled = false;
|
|
|
|
refreshAll();
|
2018-07-05 13:06:52 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-19 07:57:32 +08:00
|
|
|
global.commentSticky = function(commentHex) {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (stickyCommentHex !== "none") {
|
2018-12-19 07:57:32 +08:00
|
|
|
var sticky = $(ID_STICKY + stickyCommentHex);
|
|
|
|
classRemove(sticky, "option-unsticky");
|
|
|
|
classAdd(sticky, "option-sticky");
|
|
|
|
}
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (stickyCommentHex === commentHex) {
|
2018-12-19 07:57:32 +08:00
|
|
|
stickyCommentHex = "none";
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-12-19 07:57:32 +08:00
|
|
|
stickyCommentHex = commentHex;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-12-19 07:57:32 +08:00
|
|
|
|
2019-01-23 13:26:22 +08:00
|
|
|
pageUpdate(function() {
|
|
|
|
var sticky = $(ID_STICKY + commentHex);
|
|
|
|
if (stickyCommentHex === commentHex) {
|
|
|
|
classRemove(sticky, "option-sticky");
|
|
|
|
classAdd(sticky, "option-unsticky");
|
|
|
|
} else {
|
|
|
|
classRemove(sticky, "option-unsticky");
|
|
|
|
classAdd(sticky, "option-sticky");
|
2018-12-19 07:57:32 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
function mainAreaCreate() {
|
|
|
|
var mainArea = create("div");
|
|
|
|
|
|
|
|
mainArea.id = ID_MAIN_AREA;
|
|
|
|
|
|
|
|
classAdd(mainArea, "main-area");
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(mainArea, "style", "display: none");
|
2018-06-11 16:48:10 +08:00
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
append(root, mainArea);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-07-05 13:06:52 +08:00
|
|
|
function modToolsCreate() {
|
|
|
|
var modTools = create("div");
|
|
|
|
var lock = create("button");
|
|
|
|
|
|
|
|
modTools.id = ID_MOD_TOOLS;
|
|
|
|
lock.id = ID_MOD_TOOLS_LOCK_BUTTON;
|
|
|
|
|
|
|
|
classAdd(modTools, "mod-tools");
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (isLocked) {
|
2018-07-05 13:06:52 +08:00
|
|
|
lock.innerHTML = "Unlock Thread";
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2018-07-05 13:06:52 +08:00
|
|
|
lock.innerHTML = "Lock Thread";
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-12-20 13:48:43 +08:00
|
|
|
onclick(lock, global.threadLockToggle);
|
|
|
|
|
2018-07-05 13:06:52 +08:00
|
|
|
attrSet(modTools, "style", "display: none");
|
|
|
|
|
|
|
|
append(modTools, lock);
|
|
|
|
append(root, modTools);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
global.loadCssOverride = function() {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (cssOverride === undefined) {
|
2018-06-16 21:08:24 +08:00
|
|
|
global.allShow();
|
2018-06-24 10:01:21 +08:00
|
|
|
} else {
|
2019-02-23 07:22:49 +08:00
|
|
|
cssLoad(cssOverride, "window.commento.allShow()");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-16 21:08:24 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-11 16:48:10 +08:00
|
|
|
global.allShow = function() {
|
|
|
|
var mainArea = $(ID_MAIN_AREA);
|
2018-07-05 13:06:52 +08:00
|
|
|
var modTools = $(ID_MOD_TOOLS);
|
2018-06-11 16:48:10 +08:00
|
|
|
var loggedContainer = $(ID_LOGGED_CONTAINER);
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(mainArea, "style", "");
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (isModerator) {
|
2018-07-05 13:06:52 +08:00
|
|
|
attrSet(modTools, "style", "");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-07-05 13:06:52 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (loggedContainer) {
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(loggedContainer, "style", "");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-11 16:48:10 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
global.loginBoxClose = function() {
|
|
|
|
var mainArea = $(ID_MAIN_AREA);
|
|
|
|
var loginBoxContainer = $(ID_LOGIN_BOX_CONTAINER);
|
|
|
|
|
|
|
|
classRemove(mainArea, "blurred");
|
2018-09-23 12:52:34 +08:00
|
|
|
classRemove(root, "root-min-height");
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(loginBoxContainer, "style", "display: none");
|
2018-06-11 01:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.loginBoxShow = function(id) {
|
2018-06-11 01:15:56 +08:00
|
|
|
var mainArea = $(ID_MAIN_AREA);
|
|
|
|
var loginBoxContainer = $(ID_LOGIN_BOX_CONTAINER);
|
|
|
|
|
2019-02-13 13:53:42 +08:00
|
|
|
global.popupRender(id);
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
classAdd(mainArea, "blurred");
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
attrSet(loginBoxContainer, "style", "");
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
window.location.hash = ID_LOGIN_BOX_CONTAINER;
|
|
|
|
|
|
|
|
$(ID_LOGIN_BOX_EMAIL_INPUT).focus();
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
function dataTagsLoad() {
|
|
|
|
var scripts = tags("script")
|
|
|
|
for (var i = 0; i < scripts.length; i++) {
|
|
|
|
if (scripts[i].src.match(/\/js\/commento\.js$/)) {
|
|
|
|
cssOverride = attrGet(scripts[i], "data-css-override");
|
2018-06-16 21:22:46 +08:00
|
|
|
|
|
|
|
autoInit = attrGet(scripts[i], "data-auto-init");
|
|
|
|
|
|
|
|
ID_ROOT = attrGet(scripts[i], "data-id-root");
|
2018-06-24 10:01:21 +08:00
|
|
|
if (ID_ROOT === undefined) {
|
2018-06-16 21:22:46 +08:00
|
|
|
ID_ROOT = "commento";
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2019-05-02 06:10:07 +08:00
|
|
|
|
|
|
|
noFonts = attrGet(scripts[i], "data-no-fonts");
|
2018-06-16 21:08:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2019-02-18 06:45:05 +08:00
|
|
|
function loadHash() {
|
2019-03-03 04:16:56 +08:00
|
|
|
if (window.location.hash) {
|
|
|
|
if (window.location.hash.startsWith("#commento-")) {
|
|
|
|
var el = $(ID_CARD + window.location.hash.split("-")[1]);
|
|
|
|
if (el === null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-18 06:45:05 +08:00
|
|
|
|
2019-03-03 04:16:56 +08:00
|
|
|
classAdd(el, "highlighted-card");
|
|
|
|
el.scrollIntoView(true);
|
|
|
|
} else if (window.location.hash.startsWith("#commento")) {
|
|
|
|
root.scrollIntoView(true);
|
|
|
|
}
|
2019-02-18 06:45:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-16 21:22:46 +08:00
|
|
|
global.main = function(callback) {
|
2018-06-11 01:15:56 +08:00
|
|
|
root = $(ID_ROOT);
|
2018-12-29 00:59:02 +08:00
|
|
|
if (root === null) {
|
|
|
|
console.log("[commento] error: no root element with ID '" + ID_ROOT + "' found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:55:51 +08:00
|
|
|
if (mobileView === null) {
|
|
|
|
mobileView = root.getBoundingClientRect()["width"] < 450;
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:08:24 +08:00
|
|
|
classAdd(root, "root");
|
2019-05-04 07:38:23 +08:00
|
|
|
if (noFonts !== "true") {
|
|
|
|
classAdd(root, "root-font");
|
|
|
|
}
|
2018-06-16 21:08:24 +08:00
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
loginBoxCreate();
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-10 18:51:18 +08:00
|
|
|
errorElementCreate();
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
mainAreaCreate();
|
|
|
|
|
2019-03-03 03:09:29 +08:00
|
|
|
var footer = footerLoad();
|
|
|
|
cssLoad(cdn + "/css/commento.css", "window.commento.loadCssOverride()");
|
|
|
|
|
2018-06-07 16:00:53 +08:00
|
|
|
selfGet(function() {
|
|
|
|
commentsGet(function() {
|
2018-07-05 13:06:52 +08:00
|
|
|
modToolsCreate();
|
2018-06-07 16:00:53 +08:00
|
|
|
rootCreate(function() {
|
|
|
|
commentsRender();
|
2019-03-03 03:09:29 +08:00
|
|
|
append(root, footer);
|
2019-03-03 04:24:16 +08:00
|
|
|
loadHash();
|
2019-04-14 09:29:06 +08:00
|
|
|
global.allShow();
|
2019-05-02 06:15:23 +08:00
|
|
|
nameWidthFix();
|
2018-06-07 16:00:53 +08:00
|
|
|
call(callback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-16 22:36:30 +08:00
|
|
|
var initted = false;
|
2018-06-24 10:01:21 +08:00
|
|
|
|
|
|
|
|
2018-06-16 22:36:30 +08:00
|
|
|
function init() {
|
2018-06-24 10:01:21 +08:00
|
|
|
if (initted) {
|
2018-06-14 16:57:57 +08:00
|
|
|
return;
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-16 22:36:30 +08:00
|
|
|
initted = true;
|
2018-06-14 16:57:57 +08:00
|
|
|
|
2018-06-16 21:22:46 +08:00
|
|
|
dataTagsLoad();
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (autoInit === "true" || autoInit === undefined) {
|
2018-12-20 13:48:43 +08:00
|
|
|
global.main(undefined);
|
2018-06-24 10:01:21 +08:00
|
|
|
} else if (autoInit !== "false") {
|
2018-06-16 21:22:46 +08:00
|
|
|
console.log("[commento] error: invalid value for data-auto-init; allowed values: true, false");
|
2018-06-24 10:01:21 +08:00
|
|
|
}
|
2018-06-14 16:57:57 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:28:17 +08:00
|
|
|
|
2018-06-16 22:36:30 +08:00
|
|
|
var readyLoad = function() {
|
|
|
|
var readyState = document.readyState;
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
if (readyState === "loading") {
|
2018-06-16 22:36:30 +08:00
|
|
|
// The document is still loading. The div we need to fill might not have
|
|
|
|
// been parsed yet, so let's wait and retry when the readyState changes.
|
|
|
|
// If there is more than one state change, we aren't affected because we
|
|
|
|
// have a double-call protection in init().
|
|
|
|
document.addEventListener("readystatechange", readyLoad);
|
2018-06-24 10:01:21 +08:00
|
|
|
} else if (readyState === "interactive") {
|
2018-06-16 22:36:30 +08:00
|
|
|
// The document has been parsed and DOM objects are now accessible. While
|
|
|
|
// JS, CSS, and images are still loading, we don't need to wait.
|
|
|
|
init();
|
2018-06-24 10:01:21 +08:00
|
|
|
} else if (readyState === "complete") {
|
2018-06-16 22:36:30 +08:00
|
|
|
// The page has fully loaded (including JS, CSS, and images). From our
|
|
|
|
// point of view, this is practically no different from interactive.
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
|
2018-06-16 22:36:30 +08:00
|
|
|
readyLoad();
|
2018-06-07 16:00:53 +08:00
|
|
|
|
2018-06-24 10:01:21 +08:00
|
|
|
|
2019-03-19 13:23:37 +08:00
|
|
|
}(window.commento, document));
|