diff --git a/bookwyrm/static/css/format.css b/bookwyrm/static/css/format.css index e99513b04..f10549eda 100644 --- a/bookwyrm/static/css/format.css +++ b/bookwyrm/static/css/format.css @@ -14,20 +14,16 @@ } /* --- TOGGLES --- */ -input.toggle-control { +.toggle-button[aria-pressed=true], .toggle-button[aria-pressed=true]:hover { + background-color: hsl(171, 100%, 41%); + color: white; +} +.hide-active[aria-pressed=true], .hide-inactive[aria-pressed=false] { display: none; } .hidden { - display: none; -} - -input.toggle-control:checked ~ .toggle-content { - display: block; -} - -input.toggle-control:checked ~ .modal.toggle-content { - display: flex; + display: none !important; } /* --- STARS --- */ diff --git a/bookwyrm/static/js/shared.js b/bookwyrm/static/js/shared.js index b2de57368..55a5c1b20 100644 --- a/bookwyrm/static/js/shared.js +++ b/bookwyrm/static/js/shared.js @@ -1,7 +1,7 @@ // set up javascript listeners window.onload = function() { - // let buttons set keyboard focus - Array.from(document.getElementsByClassName('toggle-control')) + // buttons that display or hide content + document.querySelectorAll('[data-controls]') .forEach(t => t.onclick = toggleAction); // javascript interactions (boost/fav) @@ -13,8 +13,6 @@ window.onload = function() { .forEach(t => t.onclick = selectAll); // toggle between tabs - Array.from(document.getElementsByClassName('tab-change-nested')) - .forEach(t => t.onclick = tabChangeNested); Array.from(document.getElementsByClassName('tab-change')) .forEach(t => t.onclick = tabChange); @@ -29,9 +27,22 @@ window.onload = function() { // update localstorage Array.from(document.getElementsByClassName('set-display')) .forEach(t => t.onclick = updateDisplay); + + // hidden submit button in a form + document.querySelectorAll('.hidden-form input') + .forEach(t => t.onchange = revealForm); }; +function revealForm(e) { + var hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0]; + if (hidden) { + removeClass(hidden, 'hidden'); + } +} + + function updateDisplay(e) { + // used in set reading goal var key = e.target.getAttribute('data-id'); var value = e.target.getAttribute('data-value'); window.localStorage.setItem(key, value); @@ -41,37 +52,52 @@ function updateDisplay(e) { } function setDisplay(el) { + // used in set reading goal var key = el.getAttribute('data-hide'); - var value = window.localStorage.getItem(key) - if (!value) { - el.className = el.className.replace('hidden', ''); - } else if (value != null && !!value) { - el.className += ' hidden'; - } + var value = window.localStorage.getItem(key); + addRemoveClass(el, 'hidden', value); } + function toggleAction(e) { - // set hover, if appropriate - var hover = e.target.getAttribute('data-hover-target'); - if (hover) { - document.getElementById(hover).focus(); + 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'))); + + if (targetId) { + var target = document.getElementById(targetId); + addRemoveClass(target, 'hidden', !pressed); + addRemoveClass(target, 'is-active', pressed); + } + + // show/hide container + var container = document.getElementById('hide-' + targetId); + if (!!container) { + addRemoveClass(container, 'hidden', pressed); + } + + // set checkbox, if appropriate + var checkbox = el.getAttribute('data-controls-checkbox'); + if (checkbox) { + document.getElementById(checkbox).checked = !!pressed; + } + + // set focus, if appropriate + var focus = el.getAttribute('data-focus-target'); + if (focus) { + document.getElementById(focus).focus(); } } - function interact(e) { e.preventDefault(); ajaxPost(e.target); var identifier = e.target.getAttribute('data-id'); - var elements = document.getElementsByClassName(identifier); - for (var i = 0; i < elements.length; i++) { - if (elements[i].className.includes('hidden')) { - elements[i].className = elements[i].className.replace('hidden', ''); - } else { - elements[i].className += ' hidden'; - } - } - return true; + Array.from(document.getElementsByClassName(identifier)) + .forEach(t => addRemoveClass(t, 'hidden', t.className.indexOf('hidden') == -1)); } function selectAll(e) { @@ -79,32 +105,32 @@ function selectAll(e) { .forEach(t => t.checked=true); } -function tabChangeNested(e) { - var target = e.target.closest('li') - var parentElement = target.parentElement.closest('li').parentElement; - handleTabChange(target, parentElement) -} - function tabChange(e) { - var target = e.target.closest('li') - var parentElement = target.parentElement; - handleTabChange(target, parentElement) -} + var el = e.currentTarget; + var parentElement = el.closest('[role="tablist"]'); - -function handleTabChange(target, parentElement) { parentElement.querySelectorAll('[aria-selected="true"]') .forEach(t => t.setAttribute("aria-selected", false)); - target.querySelector('[role="tab"]').setAttribute("aria-selected", true); + el.setAttribute("aria-selected", true); parentElement.querySelectorAll('li') - .forEach(t => t.className=''); - target.className = 'is-active'; + .forEach(t => removeClass(t, 'is-active')); + addClass(el, 'is-active'); + + var tabId = el.getAttribute('data-tab'); + Array.from(document.getElementsByClassName(el.getAttribute('data-category'))) + .forEach(t => addRemoveClass(t, 'hidden', t.id != tabId)); } function toggleMenu(e) { - var el = e.target.closest('.pulldown-menu'); - el.setAttribute('aria-expanded', el.getAttribute('aria-expanded') == 'false'); + var el = e.currentTarget; + var expanded = el.getAttribute('aria-expanded') == 'false'; + el.setAttribute('aria-expanded', expanded); + var targetId = el.getAttribute('data-controls'); + if (targetId) { + var target = document.getElementById(targetId); + addRemoveClass(target, 'is-active', expanded); + } } function ajaxPost(form) { @@ -113,3 +139,31 @@ function ajaxPost(form) { body: new FormData(form) }); } + +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(' '); +} diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index 4e65ecf84..4a95237cf 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -39,14 +39,28 @@ {% if request.user.is_authenticated and not book.cover %}