Provide mechanism to get tab click (Fixes #4)
This commit is contained in:
@@ -21,7 +21,7 @@ const font = {
|
||||
size: 0
|
||||
}
|
||||
|
||||
const copyColourFallback = (copyInfo, attr) => {
|
||||
const copyFallback = (copyInfo, attr) => {
|
||||
console.log("fallback")
|
||||
var textArea = document.createElement('textarea');
|
||||
textArea.value = copyInfo;
|
||||
@@ -157,10 +157,10 @@ module.exports = {
|
||||
navigator.clipboard.writeText(c).then(function() {
|
||||
showMessage(`Copied ${w}.`);
|
||||
}, function(e) {
|
||||
copyColourFallback(c,w);
|
||||
copyFallback(c,w);
|
||||
});
|
||||
} else {
|
||||
copyColourFallback(c, w);
|
||||
copyFallback(c, w);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -178,6 +178,23 @@ module.exports = {
|
||||
},
|
||||
getCSS: (el, prop, b = false) => {
|
||||
return window.getComputedStyle(el, null).getPropertyValue(prop);
|
||||
},
|
||||
getTabPath: (t) => {
|
||||
let url = window.location.toString();
|
||||
url = url.indexOf("?") > 0 ? url.substring(0,url.indexOf("?")) : url;
|
||||
url = `${url}?p=${t.closest("article").getAttribute("data-path")}#${t.getAttribute("id")}`
|
||||
let type = "URL";
|
||||
if (navigator.clipboard) {
|
||||
navigator.clipboard.writeText(url).then(function() {
|
||||
showMessage(`Copied ${type}.`);
|
||||
}, function(e) {
|
||||
copyFallback(url,type);
|
||||
});
|
||||
} else {
|
||||
copyFallback(url, type);
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
init: (args = {}) => {
|
||||
const url = getURLVars();
|
||||
|
@@ -12,10 +12,12 @@ import * as stickynote from "../pg/patterns/core/sticky-note/_sticky-note.js";
|
||||
import * as swtch from "../pg/patterns/core/switch/_switch.js";
|
||||
import * as tabs from "../pg/patterns/core/tabs/_tabs.js";
|
||||
|
||||
// init core
|
||||
// init c
|
||||
core.init({
|
||||
success: (a) => {
|
||||
tabs.init(a);
|
||||
tabs.init(a, true, {
|
||||
shiftModifier: core.getTabPath,
|
||||
});
|
||||
swtch.init(a);
|
||||
stickynote.init(a);
|
||||
},
|
||||
|
@@ -9,9 +9,31 @@ const click = new MouseEvent('click', {
|
||||
cancelable: true
|
||||
});
|
||||
|
||||
export function init(p = document, s = true) {
|
||||
const waitForElement = (selector) => {
|
||||
return new Promise(resolve => {
|
||||
if (document.querySelector(selector)) {
|
||||
return resolve(document.querySelector(selector));
|
||||
}
|
||||
|
||||
p.querySelectorAll(".tab-group, tabset").forEach(tabGroup => {
|
||||
const observer = new MutationObserver(mutations => {
|
||||
if (document.querySelector(selector)) {
|
||||
observer.disconnect();
|
||||
resolve(document.querySelector(selector));
|
||||
}
|
||||
});
|
||||
|
||||
// If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function init(container = document, spacer = true, args = {}) {
|
||||
|
||||
container.querySelectorAll(".tab-group, tabset").forEach(tabGroup => {
|
||||
|
||||
if (tabGroup.querySelector("[role=tablist]") === null) {
|
||||
const tabgroup = tabGroup.getAttribute("id");
|
||||
@@ -37,21 +59,29 @@ export function init(p = document, s = true) {
|
||||
|
||||
const ul = document.createElement('ul');
|
||||
ul.setAttribute("role", "tablist");
|
||||
ul.innerHTML = s != true ? `${tablist}` : `${tablist}<li role="separator" class="separator"></li>`;
|
||||
ul.innerHTML = spacer != true ? `${tablist}` : `${tablist}<li role="separator" class="separator"></li>`;
|
||||
tabGroup.insertBefore(ul, tabGroup.firstChild);
|
||||
|
||||
tabGroup.querySelectorAll('[role="tab"]').forEach(tab => {
|
||||
tab.addEventListener("click", () => {
|
||||
const siblings = Array.from(tab.parentNode.children);
|
||||
siblings.forEach(sibling => sibling.classList.remove("selected"));
|
||||
tab.classList.add("selected");
|
||||
tab.addEventListener("click", (e) => {
|
||||
if (e.altKey && typeof args.altModifier == "function") {
|
||||
args.altModifier(tab);
|
||||
} else if (e.shiftKey && typeof args.shiftModifier == "function") {
|
||||
args.shiftModifier(tab);
|
||||
} else if (e.metaKey && typeof args.metaModifier == "function") {
|
||||
args.metaModifier(tab);
|
||||
} else {
|
||||
const siblings = Array.from(tab.parentNode.children);
|
||||
siblings.forEach(sibling => sibling.classList.remove("selected"));
|
||||
tab.classList.add("selected");
|
||||
|
||||
const tabPanels = Array.from(tab.parentNode.parentNode.children)
|
||||
.filter(child => child.getAttribute("role") === "tabpanel");
|
||||
tabPanels.forEach(panel => panel.classList.remove("open"));
|
||||
const tabPanels = Array.from(tab.parentNode.parentNode.children)
|
||||
.filter(child => child.getAttribute("role") === "tabpanel");
|
||||
tabPanels.forEach(panel => panel.classList.remove("open"));
|
||||
|
||||
const tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel");
|
||||
document.getElementById(tabPanelId).classList.add("open");
|
||||
const tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel");
|
||||
document.getElementById(tabPanelId).classList.add("open");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -65,7 +95,11 @@ export function init(p = document, s = true) {
|
||||
}
|
||||
|
||||
if (document.location.hash != "" && document.location.hash.substring(0,5) == "#tab-") {
|
||||
document.querySelector(document.location.hash).dispatchEvent(click);
|
||||
waitForElement(document.location.hash).then((el) => {
|
||||
el.scrollIntoView();
|
||||
el.dispatchEvent(click);
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -22,6 +22,15 @@ block content
|
||||
+h(2)
|
||||
p The structure of the tab set is defined in html. There are two forms supported. Adding a class of #[code.inline .tab-group] to the container element will work in place of the #[code.inline tabset] tag, and the tab panels can be defined using either #[code.inline tab=""] or #[code.inline data-tab=""]. Passing an optional element to the init function will initialise tabs within that element.
|
||||
|
||||
p The tab initalize function now takes a new function parameter in the form of an third argument is an object that can take the following callbacks:
|
||||
ul
|
||||
li altModifer (When the altKey is used)
|
||||
li shiftModifier (When the shift key is used)
|
||||
li metaModifier (When the Windows key or Apple key is used)
|
||||
p You can use these callbacks to create a custom event to get the tab information.
|
||||
|
||||
p Note: There is a new core function (core.getTabPath) that will generate the query string and url hash for use in DS2 Core. You may wish to update your scaffolding.js file to make use of this functionality.
|
||||
|
||||
|
||||
tabset#tabs
|
||||
pre.language-html(tab="html")
|
||||
|
Reference in New Issue
Block a user