bookwyrm/bookwyrm/static/js/bookwyrm.js

163 lines
4.6 KiB
JavaScript
Raw Normal View History

/* globals setDisplay TabGroup updateDisplay */
// set up javascript listeners
2021-01-14 21:02:28 +00:00
window.onload = function() {
// buttons that display or hide content
document.querySelectorAll('[data-controls]')
2021-01-14 23:16:18 +00:00
.forEach(t => t.onclick = toggleAction);
2021-01-14 23:16:18 +00:00
// javascript interactions (boost/fav)
Array.from(document.getElementsByClassName('interaction'))
.forEach(t => t.onsubmit = interact);
2021-01-14 23:16:18 +00:00
// handle aria settings on menus
Array.from(document.getElementsByClassName('pulldown-menu'))
.forEach(t => t.onclick = toggleMenu);
2021-01-18 17:57:44 +00:00
// hidden submit button in a form
document.querySelectorAll('.hidden-form input')
.forEach(t => t.onchange = revealForm);
2021-01-19 00:32:02 +00:00
// polling
document.querySelectorAll('[data-poll]')
2021-01-19 22:59:46 +00:00
.forEach(el => polling(el));
2021-01-29 18:25:31 +00:00
// browser back behavior
document.querySelectorAll('[data-back]')
.forEach(t => t.onclick = back);
2021-03-19 22:59:28 +00:00
Array.from(document.getElementsByClassName('tab-group'))
.forEach(t => new TabGroup(t));
// display based on localstorage vars
document.querySelectorAll('[data-hide]')
.forEach(t => setDisplay(t));
// update localstorage
Array.from(document.getElementsByClassName('set-display'))
.forEach(t => t.onclick = updateDisplay);
2021-01-14 21:02:28 +00:00
};
2021-01-29 18:25:31 +00:00
function back(e) {
e.preventDefault();
history.back();
}
2021-02-09 18:38:43 +00:00
function polling(el, delay) {
delay = delay || 10000;
delay += (Math.random() * 1000);
2021-01-19 22:59:46 +00:00
setTimeout(function() {
fetch('/api/updates/' + el.getAttribute('data-poll'))
.then(response => response.json())
.then(data => updateCountElement(el, data));
2021-02-09 18:38:43 +00:00
polling(el, delay * 1.25);
2021-01-19 22:59:46 +00:00
}, delay, el);
2021-01-19 00:32:02 +00:00
}
2021-01-19 22:59:46 +00:00
2021-01-19 00:32:02 +00:00
function updateCountElement(el, data) {
2021-01-19 22:59:46 +00:00
const currentCount = el.innerText;
2021-03-23 19:52:38 +00:00
const count = data.count;
if (count != currentCount) {
2021-03-23 19:52:38 +00:00
addRemoveClass(el.closest('[data-poll-wrapper]'), 'hidden', count < 1);
2021-01-19 22:59:46 +00:00
el.innerText = count;
2021-01-19 00:32:02 +00:00
}
}
2021-01-18 17:57:44 +00:00
function revealForm(e) {
var hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0];
if (hidden) {
removeClass(hidden, 'hidden');
}
}
2021-01-14 23:16:18 +00:00
function toggleAction(e) {
var el = e.currentTarget;
var pressed = el.getAttribute('aria-pressed') == 'false';
var targetId = el.getAttribute('data-controls');
document.querySelectorAll('[data-controls="' + targetId + '"]')
.forEach(t => t.setAttribute('aria-pressed', (t.getAttribute('aria-pressed') == 'false')));
2021-01-17 03:57:20 +00:00
if (targetId) {
var target = document.getElementById(targetId);
2021-01-17 18:10:59 +00:00
addRemoveClass(target, 'hidden', !pressed);
addRemoveClass(target, 'is-active', pressed);
}
// show/hide container
var container = document.getElementById('hide-' + targetId);
if (container) {
2021-01-17 18:10:59 +00:00
addRemoveClass(container, 'hidden', pressed);
2021-01-17 03:57:20 +00:00
}
// set checkbox, if appropriate
var checkbox = el.getAttribute('data-controls-checkbox');
2021-01-17 03:57:20 +00:00
if (checkbox) {
document.getElementById(checkbox).checked = !!pressed;
}
// set focus, if appropriate
var focus = el.getAttribute('data-focus-target');
if (focus) {
var focusEl = document.getElementById(focus);
focusEl.focus();
setTimeout(function(){ focusEl.selectionStart = focusEl.selectionEnd = 10000; }, 0);
}
2021-01-14 21:02:28 +00:00
}
2020-03-16 01:12:45 +00:00
function interact(e) {
e.preventDefault();
ajaxPost(e.target);
var identifier = e.target.getAttribute('data-id');
Array.from(document.getElementsByClassName(identifier))
.forEach(t => addRemoveClass(t, 'hidden', t.className.indexOf('hidden') == -1));
2020-03-16 01:12:45 +00:00
}
function toggleMenu(e) {
var el = e.currentTarget;
var expanded = el.getAttribute('aria-expanded') == 'false';
el.setAttribute('aria-expanded', expanded);
2021-01-18 04:19:09 +00:00
var targetId = el.getAttribute('data-controls');
if (targetId) {
var target = document.getElementById(targetId);
addRemoveClass(target, 'is-active', expanded);
}
2020-11-09 19:58:19 +00:00
}
function ajaxPost(form) {
fetch(form.action, {
method : "POST",
body: new FormData(form)
});
2020-03-15 21:15:36 +00:00
}
function addRemoveClass(el, classname, bool) {
if (bool) {
addClass(el, classname);
} else {
removeClass(el, classname);
}
}
function addClass(el, classname) {
var classes = el.className.split(' ');
if (classes.indexOf(classname) > -1) {
return;
}
el.className = classes.concat(classname).join(' ');
}
function removeClass(el, className) {
var classes = [];
if (el.className) {
classes = el.className.split(' ');
}
const idx = classes.indexOf(className);
if (idx > -1) {
classes.splice(idx, 1);
}
el.className = classes.join(' ');
}