Files
interchan/board/static/board/post.js
Alek Ratzloff 647aedfc67 Add in-line ban capabilities
Anyone with the ability to create bans can now do so via the drop down
menu on all posts.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2022-06-23 22:27:26 -07:00

88 lines
2.4 KiB
JavaScript

const OPEN = "open";
const CLOSED = "closed";
function documentClick(e) {
let sender = e.target;
let id = sender.getAttribute("data-id");
if (!id) {
return;
}
switch (id) {
case "post_menu_button": {
openMenu(e);
}; break;
}
}
function doQuote(e) {
let idText = $("#id_text");
let caret = idText[0].selectionStart;
let text = idText.val();
let toAdd = ">>" + e.target.innerText + "\n";
idText.val(text.substring(0, caret) + toAdd + text.substring(caret));
}
function closeMenu(e) {
$(document).off("click", closeMenu);
$(".post_menu").remove();
}
function openMenu(e) {
e.preventDefault();
let sender = e.target;
let menuList = $("<ul></ul>")
.append($("<lh><strong>Actions</strong></lh>").addClass("post_menu_item"))
.addClass("post_menu_items");
window.menuItemFactories.forEach(factory => {
item = factory(sender.parentElement);
menuList.append(
$("<li></li>")
.addClass("post_menu_item")
.append(item)
);
});
let rect = sender.getBoundingClientRect();
let menu = $("<div></div>")
.addClass("post_menu")
.css({
top: rect.bottom + 3 + window.pageYOffset + "px",
left: rect.left + 3 + window.pageXOffset + "px",
})
.append(menuList);
$("body").append(menu);
$(document).on("click", closeMenu);
}
function openReportWindow(e, postElement) {
e.preventDefault();
// If there's already a report window open, close it and open this one.
if (window.top.reportWindow) {
window.top.reportWindow.close();
}
//let postId = sender.parentElement.getAttribute("id").substring(1);
let reportUrl = $(postElement).attr("data-report-url");
window.reportWindow = new WinBox("New Report", {
url: reportUrl,
modal: true,
onclose: function (force) {
window.top.reportWindow = null;
}
});
}
function onLoad(e) {
window.reportWindow = null;
window.menuItemFactories.push(
(postElement) =>
$("<a>")
.text("Report")
.attr("href", "#")
.on("click", (e) => { return openReportWindow(e, postElement); })
);
}
$(document).on("click", documentClick);
$(document).on("click", ".post_id", doQuote);
$(window).on("load", onLoad);
window.menuItemFactories = [];