From 61cafb90541c45db370c65e4b074b3c60e31f973 Mon Sep 17 00:00:00 2001 From: alpacawren <66525823+alpacawren@users.noreply.github.com> Date: Sat, 6 Jun 2020 17:58:55 -0400 Subject: [PATCH] Poll tabs bug (#129) * poll tabs hiding comment tabs fix * scope change to hiding tabs specifically * selectorCbChildren readability --- assets/js/boorujs.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/assets/js/boorujs.js b/assets/js/boorujs.js index 7968f10f..666c3005 100644 --- a/assets/js/boorujs.js +++ b/assets/js/boorujs.js @@ -4,7 +4,7 @@ * Apply event-based actions through data-* attributes. The attributes are structured like so: [data-event-action] */ -import { $ } from './utils/dom'; +import { $, $$ } from './utils/dom'; import { fetchHtml, handleError } from './utils/requests'; import { showBlock } from './utils/image'; import { addTag } from './tagsinput'; @@ -21,6 +21,8 @@ const types = { const actions = { hide(data) { selectorCb(data.base, data.value, el => el.classList.add('hidden')); }, + tabHide(data) { selectorCbChildren(data.base, data.value, el => el.classList.add('hidden')); }, + show(data) { selectorCb(data.base, data.value, el => el.classList.remove('hidden')); }, toggle(data) { selectorCb(data.base, data.value, el => el.classList.toggle('hidden')); }, @@ -57,7 +59,7 @@ const actions = { data.el.classList.add('selected'); // Switch contents - this.hide({ base: block, value: '.block__tab' }); + this.tabHide({ base: block, value: '.block__tab' }); this.show({ base: block, value: `.block__tab[data-tab="${data.value}"]` }); // If the tab has a 'data-load-tab' attribute, load and insert the content @@ -81,6 +83,16 @@ function selectorCb(base = document, selector, cb) { [].forEach.call(base.querySelectorAll(selector), cb); } +function selectorCbChildren(base = document, selector, cb) { + const sel = $$(selector, base); + + for (const el of base.children) { + if (!sel.includes(el)) continue; + + cb(el); + } +} + function matchAttributes(event) { if (!types[event.type](event)) { for (const action in actions) { @@ -102,4 +114,4 @@ function registerEvents() { for (const type in types) document.addEventListener(type, matchAttributes); } -export { registerEvents }; \ No newline at end of file +export { registerEvents };