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); .forEach(t => t.onclick = selectAll);
// toggle between tabs // toggle between tabs
Array.from(document.getElementsByClassName('tab-change-nested'))
.forEach(t => t.onclick = tabChangeNested);
Array.from(document.getElementsByClassName('tab-change')) Array.from(document.getElementsByClassName('tab-change'))
.forEach(t => t.onclick = tabChange); .forEach(t => t.onclick = tabChange);
@ -50,6 +48,7 @@ function setDisplay(el) {
} }
} }
function toggleAction(e) { function toggleAction(e) {
var el = e.currentTarget; var el = e.currentTarget;
var pressed = el.getAttribute('aria-pressed') == 'false'; var pressed = el.getAttribute('aria-pressed') == 'false';
@ -92,7 +91,11 @@ function addRemoveClass(el, classname, bool) {
} }
function addClass(el, classname) { 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) { function removeClass(el, className) {
@ -128,35 +131,32 @@ function selectAll(e) {
.forEach(t => t.checked=true); .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) { function tabChange(e) {
var target = e.target.closest('li') var el = e.currentTarget;
var parentElement = target.parentElement; var parentElement = el.closest('[role="tablist"]');
handleTabChange(target, parentElement)
}
function handleTabChange(target, parentElement) {
parentElement.querySelectorAll('[aria-selected="true"]') parentElement.querySelectorAll('[aria-selected="true"]')
.forEach(t => t.setAttribute("aria-selected", false)); .forEach(t => t.setAttribute("aria-selected", false));
target.querySelector('[role="tab"]').setAttribute("aria-selected", true); el.setAttribute("aria-selected", true);
parentElement.querySelectorAll('li') parentElement.querySelectorAll('li')
.forEach(t => t.className=''); .forEach(t => removeClass(t, 'is-active'));
target.className = '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) { function toggleMenu(e) {
var el = e.currentTarget; var el = e.currentTarget;
var expanded = el.getAttribute('aria-expanded') == 'false'; var expanded = el.getAttribute('aria-expanded') == 'false';
el.setAttribute('aria-expanded', expanded); el.setAttribute('aria-expanded', expanded);
var target = document.getElementById(el.getAttribute('data-controls')); var targetId = el.getAttribute('data-controls');
if (targetId) {
var target = document.getElementById(targetId);
addRemoveClass(target, 'is-active', expanded); addRemoveClass(target, 'is-active', expanded);
}
} }
function ajaxPost(form) { function ajaxPost(form) {

View file

@ -175,11 +175,11 @@
{% endif %} {% endif %}
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}
<div class="box"> <section class="box">
{% include 'snippets/create_status.html' with book=book hide_cover=True %} {% 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"> <form name="tag" action="/tag/" method="post">
<label for="tags" class="is-3">Tags</label> <label for="tags" class="is-3">Tags</label>
{% csrf_token %} {% csrf_token %}
@ -187,7 +187,7 @@
<input id="tags" class="input" type="text" name="name"> <input id="tags" class="input" type="text" name="name">
<button class="button" type="submit">Add tag</button> <button class="button" type="submit">Add tag</button>
</form> </form>
</div> </section>
{% endif %} {% endif %}
<div class="block"> <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> <p>There are no books here right now! Try searching for a book to get started</p>
{% else %} {% else %}
<div class="tabs is-small"> <div class="tabs is-small">
<ul> <ul role="tablist">
{% for shelf in suggested_books %} {% for shelf in suggested_books %}
{% if shelf.books %} {% if shelf.books %}
{% with shelf_counter=forloop.counter %} {% with shelf_counter=forloop.counter %}
@ -17,17 +17,13 @@
<p> <p>
{{ shelf.name }} {{ shelf.name }}
</p> </p>
<div class="tabs is-small is-toggle" role="tablist"> <div class="tabs is-small is-toggle">
<ul> <ul>
{% for book in shelf.books %} {% for book in shelf.books %}
<li class="{% if shelf_counter == 1 and forloop.first %}is-active{% endif %}" data-id="tab-book-{{ book.id }}"> <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">
<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> <a>
{% include 'snippets/book_cover.html' with book=book size="medium" %} {% include 'snippets/book_cover.html' with book=book size="medium" %}
</a> </a>
</div>
</label>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
@ -41,10 +37,7 @@
{% for shelf in suggested_books %} {% for shelf in suggested_books %}
{% with shelf_counter=forloop.counter %} {% with shelf_counter=forloop.counter %}
{% for book in shelf.books %} {% for book in shelf.books %}
<div> <div class="suggested-tabs card{% if shelf_counter != 1 or not forloop.first %} hidden{% endif %}" role="tabpanel" id="book-{{ book.id }}">
<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"> <div class="card-header">
<p class="card-header-title"> <p class="card-header-title">
<span>{% include 'snippets/book_titleby.html' with book=book %}</span> <span>{% include 'snippets/book_titleby.html' with book=book %}</span>
@ -58,8 +51,6 @@
{% include 'snippets/create_status.html' with book=book %} {% include 'snippets/create_status.html' with book=book %}
</div> </div>
</div> </div>
</div>
</div>
{% endfor %} {% endfor %}
{% endwith %} {% endwith %}
{% endfor %} {% endfor %}

View file

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

View file

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