Voyage/static/voyage.js

97 lines
3.1 KiB
JavaScript

function add_tag(event) {
input_tag = event.target.closest('.tag');
post_id = input_tag.closest('.post_container').id;
tag_name = input_tag.children[0].value;
fetch(url_prefix + '/add_tag', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
'post_id': post_id,
'tag_name': tag_name,
})
}).then(function(response) {
if(!response.ok) { throw new Error('Error adding tag!'); }
return response.json();
}).then(function(json) {
if (!json.ok) { throw new Error('Could not add tag.') }
event.target.parentElement.firstElementChild.value = '';
let tag = document.createElement('span');
tag.innerText = tag_name;
tag.className = 'tag';
let span = document.createElement('span');
span.className = 'tag_button';
span.addEventListener('click', remove_tag);
span.innerText = '-';
tag.appendChild(span);
input_tag.closest('.tags').insertBefore(tag, input_tag);
});
}
function remove_tag(event) {
tag = event.target.closest('.tag');
post_id = tag.closest('.post_container').id;
tag_name = tag.innerText.replace(/ .*/, '');
fetch(url_prefix + '/remove_tag', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
'post_id': post_id,
'tag_name': tag_name,
})
}).then(function(response) {
if(!response.ok) { throw new Error('Error removing tag!'); }
return response.json();
}).then(function(json) {
if (!json.ok) { throw new Error('Could not remove tag.') }
tag.remove();
});
}
function toggle_visibility_menu(event) {
let menu = document.querySelector('#visibility_menu');
if (menu.style.display == 'block') {
menu.style.display = 'none';
} else if (menu.style.display == 'none') {
menu.style.display = 'block';
}
}
function toggle_visibility(event) {
console.log(event);
let tag = event.target.parentElement.parentElement.cells[0].innerText;
let vis_class = ['faded', 'hidden'];
if (tag == 'non-qm posts') {
for (let post of document.querySelectorAll('.post_container:not(.qm_post)')) {
if (event.target.checked) {
post.classList.add(vis_class[event.target.parentElement.cellIndex - 1]);
} else {
post.classList.remove(vis_class[event.target.parentElement.cellIndex - 1]);
}
}
} else if (tag == 'post tags') {
for (let post of document.querySelectorAll('.post_container > .tags')) {
if (event.target.checked) {
post.classList.add(vis_class[event.target.parentElement.cellIndex - 1]);
} else {
post.classList.remove(vis_class[event.target.parentElement.cellIndex - 1]);
}
}
} else if (tag == 'post header') {
for (let post of document.querySelectorAll('.post_container > .header')) {
if (event.target.checked) {
post.classList.add(vis_class[event.target.parentElement.cellIndex - 1]);
} else {
post.classList.remove(vis_class[event.target.parentElement.cellIndex - 1]);
}
}
} else {
for (let post of document.querySelectorAll('.' + tag)) {
if (event.target.checked) {
post.classList.add(vis_class[event.target.parentElement.cellIndex - 1]);
} else {
post.classList.remove(vis_class[event.target.parentElement.cellIndex - 1]);
}
}
}
}