2023-09-03 16:30:20 +12:00
|
|
|
// fires after DOM is ready for manipulation
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
|
|
setupCommunityNameInput();
|
2023-10-15 19:36:14 +13:00
|
|
|
setupShowMoreLinks();
|
2023-10-21 15:49:01 +13:00
|
|
|
setupConfirmFirst();
|
2023-09-03 16:30:20 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// fires after all resources have loaded, including stylesheets and js files
|
|
|
|
window.addEventListener("load", function () {
|
2023-09-17 21:19:51 +12:00
|
|
|
setupPostTypeTabs(); // when choosing the type of your new post, store the chosen tab in a hidden field so the backend knows which fields to check
|
2023-10-15 19:36:14 +13:00
|
|
|
setupHideButtons();
|
2023-09-03 16:30:20 +12:00
|
|
|
});
|
|
|
|
|
2023-10-23 22:54:11 +13:00
|
|
|
function collapseReply(comment_id) {
|
|
|
|
const reply = document.getElementById('comment_' + comment_id);
|
|
|
|
let isHidden = false;
|
|
|
|
if(reply) {
|
|
|
|
const hidables = parentElement.querySelectorAll('.hidable');
|
|
|
|
|
|
|
|
hidables.forEach(hidable => {
|
|
|
|
hidable.style.display = isHidden ? 'block' : 'none';
|
|
|
|
});
|
|
|
|
|
|
|
|
const moreHidables = parentElement.parentElement.querySelectorAll('.hidable');
|
|
|
|
moreHidables.forEach(hidable => {
|
|
|
|
hidable.style.display = isHidden ? 'block' : 'none';
|
|
|
|
});
|
|
|
|
|
|
|
|
// Toggle the content of hideEl
|
|
|
|
if (isHidden) {
|
|
|
|
hideEl.innerHTML = "<a href='#'>[-] hide</a>";
|
|
|
|
} else {
|
|
|
|
hideEl.innerHTML = "<a href='#'>[+] show</a>";
|
|
|
|
}
|
|
|
|
|
|
|
|
isHidden = !isHidden; // Toggle the state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-21 15:49:01 +13:00
|
|
|
// every element with the 'confirm_first' class gets a popup confirmation dialog
|
|
|
|
function setupConfirmFirst() {
|
|
|
|
const show_first = document.querySelectorAll('.confirm_first');
|
|
|
|
show_first.forEach(element => {
|
|
|
|
element.addEventListener("click", function(event) {
|
|
|
|
if (!confirm("Are you sure?")) {
|
|
|
|
event.preventDefault(); // As the user clicked "Cancel" in the dialog, prevent the default action.
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2023-10-15 19:36:14 +13:00
|
|
|
function setupShowMoreLinks() {
|
|
|
|
const comments = document.querySelectorAll('.comment');
|
|
|
|
|
|
|
|
comments.forEach(comment => {
|
2023-10-15 21:13:32 +13:00
|
|
|
const content = comment.querySelector('.limit_height');
|
2023-10-15 19:36:14 +13:00
|
|
|
if (content && content.clientHeight > 400) {
|
|
|
|
content.style.overflow = 'hidden';
|
|
|
|
content.style.maxHeight = '400px';
|
|
|
|
const showMoreLink = document.createElement('a');
|
|
|
|
showMoreLink.classList.add('show-more');
|
|
|
|
showMoreLink.classList.add('hidable');
|
|
|
|
showMoreLink.innerHTML = '<i class="fe fe-angles-down" title="Read more"></i>';
|
|
|
|
showMoreLink.href = '#';
|
|
|
|
showMoreLink.addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
content.classList.toggle('expanded');
|
|
|
|
if (content.classList.contains('expanded')) {
|
|
|
|
content.style.overflow = 'visible';
|
|
|
|
content.style.maxHeight = '';
|
|
|
|
showMoreLink.innerHTML = '<i class="fe fe-angles-up" title="Collapse"></i>';
|
|
|
|
} else {
|
|
|
|
content.style.overflow = 'hidden';
|
2023-10-15 21:13:32 +13:00
|
|
|
content.style.maxHeight = '400px';
|
2023-10-15 19:36:14 +13:00
|
|
|
showMoreLink.innerHTML = '<i class="fe fe-angles-down" title="Read more"></i>';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
content.insertAdjacentElement('afterend', showMoreLink);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-09-03 16:30:20 +12:00
|
|
|
|
|
|
|
function setupCommunityNameInput() {
|
|
|
|
var communityNameInput = document.getElementById('community_name');
|
|
|
|
|
|
|
|
if (communityNameInput) {
|
|
|
|
communityNameInput.addEventListener('keyup', function() {
|
|
|
|
var urlInput = document.getElementById('url');
|
|
|
|
urlInput.value = titleToURL(communityNameInput.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 21:19:51 +12:00
|
|
|
function setupPostTypeTabs() {
|
|
|
|
|
|
|
|
const tabEl = document.querySelector('#discussion-tab')
|
|
|
|
if(tabEl) {
|
|
|
|
tabEl.addEventListener('show.bs.tab', event => {
|
|
|
|
document.getElementById('type').value = 'discussion';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const tabE2 = document.querySelector('#link-tab')
|
|
|
|
if(tabE2) {
|
|
|
|
tabE2.addEventListener('show.bs.tab', event => {
|
|
|
|
document.getElementById('type').value = 'link';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const tabE3 = document.querySelector('#image-tab')
|
|
|
|
if(tabE3) {
|
|
|
|
tabE3.addEventListener('show.bs.tab', event => {
|
|
|
|
document.getElementById('type').value = 'image';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const tabE4 = document.querySelector('#poll-tab')
|
|
|
|
if(tabE4) {
|
|
|
|
tabE4.addEventListener('show.bs.tab', event => {
|
|
|
|
document.getElementById('type').value = 'poll';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 16:30:20 +12:00
|
|
|
|
2023-10-15 19:36:14 +13:00
|
|
|
function setupHideButtons() {
|
|
|
|
const hideEls = document.querySelectorAll('.hide_button');
|
|
|
|
hideEls.forEach(hideEl => {
|
|
|
|
let isHidden = false;
|
|
|
|
|
|
|
|
hideEl.addEventListener('click', event => {
|
|
|
|
event.preventDefault();
|
|
|
|
const parentElement = hideEl.parentElement;
|
|
|
|
const hidables = parentElement.querySelectorAll('.hidable');
|
|
|
|
|
|
|
|
hidables.forEach(hidable => {
|
|
|
|
hidable.style.display = isHidden ? 'block' : 'none';
|
|
|
|
});
|
|
|
|
|
2023-10-15 21:13:32 +13:00
|
|
|
const moreHidables = parentElement.parentElement.querySelectorAll('.hidable');
|
|
|
|
moreHidables.forEach(hidable => {
|
|
|
|
hidable.style.display = isHidden ? 'block' : 'none';
|
|
|
|
});
|
|
|
|
|
2023-10-15 19:36:14 +13:00
|
|
|
// Toggle the content of hideEl
|
|
|
|
if (isHidden) {
|
|
|
|
hideEl.innerHTML = "<a href='#'>[-] hide</a>";
|
|
|
|
} else {
|
|
|
|
hideEl.innerHTML = "<a href='#'>[+] show</a>";
|
|
|
|
}
|
|
|
|
|
|
|
|
isHidden = !isHidden; // Toggle the state
|
|
|
|
});
|
|
|
|
});
|
2023-10-23 22:54:11 +13:00
|
|
|
|
|
|
|
if(toBeHidden) {
|
|
|
|
toBeHidden.forEach((arrayElement) => {
|
|
|
|
// Build the ID of the outer div
|
|
|
|
const divId = "comment_" + arrayElement;
|
|
|
|
|
|
|
|
// Access the outer div by its ID
|
|
|
|
const commentDiv = document.getElementById(divId);
|
|
|
|
|
|
|
|
if (commentDiv) {
|
|
|
|
// Access the inner div with class "hide_button" inside the outer div
|
|
|
|
const hideButton = commentDiv.getElementsByClassName("hide_button")[0];
|
|
|
|
|
|
|
|
if (hideButton) {
|
|
|
|
// Programmatically trigger a click event on the "hide_button" div
|
|
|
|
hideButton.click();
|
|
|
|
} else {
|
|
|
|
console.log(`"hide_button" not found in ${divId}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log(`Div with ID ${divId} not found`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-10-15 19:36:14 +13:00
|
|
|
}
|
|
|
|
|
2023-09-03 16:30:20 +12:00
|
|
|
function titleToURL(title) {
|
|
|
|
// Convert the title to lowercase and replace spaces with hyphens
|
2023-11-16 22:31:14 +13:00
|
|
|
return title.toLowerCase().replace(/\s+/g, '_');
|
2023-09-03 16:30:20 +12:00
|
|
|
}
|