Uses js pattern for tabs

This commit is contained in:
Mouse Reeve 2021-01-17 20:19:09 -08:00
parent aba5c48af9
commit 3f136c40da
5 changed files with 61 additions and 91 deletions

View file

@ -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);
@ -50,6 +48,7 @@ function setDisplay(el) {
}
}
function toggleAction(e) {
var el = e.currentTarget;
var pressed = el.getAttribute('aria-pressed') == 'false';
@ -92,7 +91,11 @@ function addRemoveClass(el, classname, bool) {
}
function addClass(el, classname) {
el.className = el.className.split(' ').concat(classname).join(' ');
var classes = el.className.split(' ');
if (classes.indexOf(classname) > -1) {
return
}
el.className = classes.concat(classname).join(' ');
}
function removeClass(el, className) {
@ -128,35 +131,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.currentTarget;
var expanded = el.getAttribute('aria-expanded') == 'false';
el.setAttribute('aria-expanded', expanded);
var target = document.getElementById(el.getAttribute('data-controls'));
addRemoveClass(target, 'is-active', expanded);
var targetId = el.getAttribute('data-controls');
if (targetId) {
var target = document.getElementById(targetId);
addRemoveClass(target, 'is-active', expanded);
}
}
function ajaxPost(form) {

View file

@ -175,11 +175,11 @@
{% endif %}
{% if request.user.is_authenticated %}
<div class="box">
<section class="box">
{% include 'snippets/create_status.html' with book=book hide_cover=True %}
</div>
</section>
<div class="block">
<section class="block">
<form name="tag" action="/tag/" method="post">
<label for="tags" class="is-3">Tags</label>
{% csrf_token %}
@ -187,7 +187,7 @@
<input id="tags" class="input" type="text" name="name">
<button class="button" type="submit">Add tag</button>
</form>
</div>
</section>
{% endif %}
<div class="block">

View file

@ -9,7 +9,7 @@
<p>There are no books here right now! Try searching for a book to get started</p>
{% else %}
<div class="tabs is-small">
<ul>
<ul role="tablist">
{% for shelf in suggested_books %}
{% if shelf.books %}
{% with shelf_counter=forloop.counter %}
@ -17,17 +17,13 @@
<p>
{{ shelf.name }}
</p>
<div class="tabs is-small is-toggle" role="tablist">
<div class="tabs is-small is-toggle">
<ul>
{% for book in shelf.books %}
<li class="{% if shelf_counter == 1 and forloop.first %}is-active{% endif %}" data-id="tab-book-{{ book.id }}">
<label for="book-{{ book.id }}" class="tab-change-nested">
<div role="tab" tabindex="0" aria-selected="{% if shelf_counter == 1 and forloop.first %}true{% else %}false{% endif %}" aria-controls="book-{{ book.id }}-panel">
<a>
{% include 'snippets/book_cover.html' with book=book size="medium" %}
</a>
</div>
</label>
<li class="tab-change{% if shelf_counter == 1 and forloop.first %} is-active{% endif %}" data-tab="book-{{ book.id }}" data-tab="book-{{ book.id }}" role="tab" tabindex="0" aria-selected="{% if shelf_counter == 1 and forloop.first %}true{% else %}false{% endif %}" aria-controls="book-{{ book.id }}" data-category="suggested-tabs">
<a>
{% include 'snippets/book_cover.html' with book=book size="medium" %}
</a>
</li>
{% endfor %}
</ul>
@ -41,24 +37,19 @@
{% for shelf in suggested_books %}
{% with shelf_counter=forloop.counter %}
{% for book in shelf.books %}
<div>
<input class="toggle-control" type="radio" name="recent-books" id="book-{{ book.id }}" {% if shelf_counter == 1 and forloop.first %}checked{% endif %}>
<div class="toggle-content hidden" role="tabpanel" id="book-{{ book.id }}-panel">
<div class="card">
<div class="card-header">
<p class="card-header-title">
<span>{% include 'snippets/book_titleby.html' with book=book %}</span>
</p>
<div class="card-header-icon is-hidden-tablet">
{% include 'snippets/toggle/toggle_button.html' with label="close" controls_text="no-book" class="delete" %}
</div>
</div>
<div class="card-content">
{% include 'snippets/shelve_button.html' with book=book %}
{% include 'snippets/create_status.html' with book=book %}
</div>
<div class="suggested-tabs card{% if shelf_counter != 1 or not forloop.first %} hidden{% endif %}" role="tabpanel" id="book-{{ book.id }}">
<div class="card-header">
<p class="card-header-title">
<span>{% include 'snippets/book_titleby.html' with book=book %}</span>
</p>
<div class="card-header-icon is-hidden-tablet">
{% include 'snippets/toggle/toggle_button.html' with label="close" controls_text="no-book" class="delete" %}
</div>
</div>
<div class="card-content">
{% include 'snippets/shelve_button.html' with book=book %}
{% include 'snippets/create_status.html' with book=book %}
</div>
</div>
{% endfor %}
{% endwith %}

View file

@ -3,53 +3,32 @@
<div class="tabs is-boxed">
<ul role="tablist">
<li class="is-active" data-id="tab-review-{{ book.id }}" data-category="tab-option-{{ book.id }}">
<label for="review-{{ book.id }}">
<div class="tab-change" role="tab" aria-selected="true" tabindex="0">
<a>Review</a>
</div>
</label>
<li class="tab-change is-active" data-category="tab-option-{{ book.id }}" role="tab" aria-selected="true" tabindex="0" data-tab="review-{{ book.id }}">
<a>Review</a>
</li>
<li data-id="tab-comment-{{ book.id }}" data-category="tab-option-{{ book.id }}">
<label for="comment-{{ book.id}}">
<div class="tab-change" role="tab" tabindex="0">
<a>Comment</a>
</div>
</label>
<li class="tab-change" data-category="tab-option-{{ book.id }}" role="tab" tabindex="0" data-tab="comment-{{ book.id}}">
<a>Comment</a>
</li>
<li data-id="tab-quotation-{{ book.id }}" data-category="tab-option-{{ book.id }}">
<label for="quote-{{ book.id }}">
<div class="tab-change" role="tab" tabindex="0">
<a>Quote</a>
</div>
</label>
<li class="tab-change" data-category="tab-option-{{ book.id }}" role="tab" tabindex="0" data-tab="quote-{{ book.id }}">
<a>Quote</a>
</li>
</ul>
</div>
<div>
<input class="toggle-control" type="radio" name="status-tabs-{{ book.id }}" id="review-{{ book.id }}" checked>
<div class="toggle-content hidden tab-option-{{ book.id }}">
{% with 0|uuid as uuid %}
{% include 'snippets/create_status_form.html' with type='review' %}
{% endwith %}
</div>
<div class="tab-option-{{ book.id }}" id="review-{{ book.id }}">
{% with 0|uuid as uuid %}
{% include 'snippets/create_status_form.html' with type='review' %}
{% endwith %}
</div>
<div>
<input class="toggle-control" type="radio" name="status-tabs-{{ book.id }}" id="comment-{{ book.id }}">
<div class="toggle-content hidden tab-option-{{ book.id }}">
{% with 0|uuid as uuid %}
{% include 'snippets/create_status_form.html' with type="comment" placeholder="Some thoughts on '"|add:book.title|add:"'" %}
{% endwith %}
</div>
<div class="hidden tab-option-{{ book.id }}" id="comment-{{ book.id }}">
{% with 0|uuid as uuid %}
{% include 'snippets/create_status_form.html' with type="comment" placeholder="Some thoughts on '"|add:book.title|add:"'" %}
{% endwith %}
</div>
<div>
<input class="toggle-control" type="radio" name="status-tabs-{{ book.id }}" id="quote-{{ book.id }}">
<div class="toggle-content hidden tab-option-{{ book.id }}">
{% with 0|uuid as uuid %}
{% include 'snippets/create_status_form.html' with type="quotation" placeholder="An excerpt from '"|add:book.title|add:"'" %}
{% endwith %}
</div>
<div class="hidden tab-option-{{ book.id }}" id="quote-{{ book.id }}">
{% with 0|uuid as uuid %}
{% include 'snippets/create_status_form.html' with type="quotation" placeholder="An excerpt from '"|add:book.title|add:"'" %}
{% endwith %}
</div>

View file

@ -1,5 +1,5 @@
{% load humanize %}
<section class="content block">
<div class="content block">
<div id="hide-edit-readthrough-{{ readthrough.id }}">
<dl class="mb-1">
{% if readthrough.start_date %}
@ -34,5 +34,5 @@
{% include 'snippets/toggle/close_button.html' with text="Cancel" controls_text="edit-readthrough" controls_uid=readthrough.id %}
</div>
</form>
</section>
</div>
{% include 'snippets/delete_readthrough_modal.html' with controls_text="delete-readthrough" controls_uid=readthrough.id %}