Provide mechanism to get tab click (Fixes #4)

This commit is contained in:
A McIlwraith 2025-01-30 22:57:18 -05:00
parent 67c857bbed
commit 93800c2706
6 changed files with 207 additions and 56 deletions

View File

@ -30,7 +30,7 @@ String.prototype.toPath = function () {
var font = {
size: 0
};
var copyColourFallback = function copyColourFallback(copyInfo, attr) {
var copyFallback = function copyFallback(copyInfo, attr) {
console.log("fallback");
var textArea = document.createElement('textarea');
textArea.value = copyInfo;
@ -135,7 +135,6 @@ module.exports = {
oneClickSelect: function oneClickSelect(e) {
var t = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : e.currentTarget;
// In here, "this" is the element
alert("here");
var range, selection;
if (window.getSelection) {
selection = window.getSelection();
@ -157,10 +156,10 @@ module.exports = {
navigator.clipboard.writeText(c).then(function () {
showMessage("Copied ".concat(w, "."));
}, function (e) {
copyColourFallback(c, w);
copyFallback(c, w);
});
} else {
copyColourFallback(c, w);
copyFallback(c, w);
}
},
positionTooltip: function positionTooltip() {
@ -179,6 +178,21 @@ module.exports = {
var b = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return window.getComputedStyle(el, null).getPropertyValue(prop);
},
getTabPath: function getTabPath(t) {
var url = window.location.toString();
url = url.indexOf("?") > 0 ? url.substring(0, url.indexOf("?")) : url;
url = "".concat(url, "?p=").concat(t.closest("article").getAttribute("data-path"), "#").concat(t.getAttribute("id"));
var type = "URL";
if (navigator.clipboard) {
navigator.clipboard.writeText(url).then(function () {
showMessage("Copied ".concat(type, "."));
}, function (e) {
copyFallback(url, type);
});
} else {
copyFallback(url, type);
}
},
init: function init() {
var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var url = getURLVars();
@ -3193,10 +3207,30 @@ var click = new MouseEvent('click', {
bubbles: false,
cancelable: true
});
var waitForElement = function waitForElement(selector) {
return new Promise(function (resolve) {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
var observer = new MutationObserver(function (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
});
});
};
function init() {
var p = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
var s = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
p.querySelectorAll(".tab-group, tabset").forEach(function (tabGroup) {
var container = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
var spacer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
container.querySelectorAll(".tab-group, tabset").forEach(function (tabGroup) {
if (tabGroup.querySelector("[role=tablist]") === null) {
var tabgroup = tabGroup.getAttribute("id");
var tablist = "";
@ -3219,10 +3253,17 @@ function init() {
});
var ul = document.createElement('ul');
ul.setAttribute("role", "tablist");
ul.innerHTML = s != true ? "".concat(tablist) : "".concat(tablist, "<li role=\"separator\" class=\"separator\"></li>");
ul.innerHTML = spacer != true ? "".concat(tablist) : "".concat(tablist, "<li role=\"separator\" class=\"separator\"></li>");
tabGroup.insertBefore(ul, tabGroup.firstChild);
tabGroup.querySelectorAll('[role="tab"]').forEach(function (tab) {
tab.addEventListener("click", function () {
tab.addEventListener("click", function (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 {
var siblings = Array.from(tab.parentNode.children);
siblings.forEach(function (sibling) {
return sibling.classList.remove("selected");
@ -3236,6 +3277,7 @@ function init() {
});
var tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel");
document.getElementById(tabPanelId).classList.add("open");
}
});
tab.addEventListener("keypress", function (e) {
e.preventDefault();
@ -3246,7 +3288,10 @@ function init() {
});
}
if (document.location.hash != "" && document.location.hash.substring(0, 5) == "#tab-") {
document.querySelector(document.location.hash).dispatchEvent(click);
waitForElement(document.location.hash).then(function (el) {
el.scrollIntoView();
el.dispatchEvent(click);
});
}
});
}
@ -3369,10 +3414,12 @@ __webpack_require__.r(__webpack_exports__);
// init core
// init c
_core_core_js__WEBPACK_IMPORTED_MODULE_0__.init({
success: function success(a) {
_pg_patterns_core_tabs_tabs_js__WEBPACK_IMPORTED_MODULE_9__.init(a);
_pg_patterns_core_tabs_tabs_js__WEBPACK_IMPORTED_MODULE_9__.init(a, true, {
shiftModifier: _core_core_js__WEBPACK_IMPORTED_MODULE_0__.getTabPath
});
_pg_patterns_core_switch_switch_js__WEBPACK_IMPORTED_MODULE_8__.init(a);
_pg_patterns_core_sticky_note_sticky_note_js__WEBPACK_IMPORTED_MODULE_7__.init(a);
},

View File

@ -24,6 +24,14 @@
<p>Users should not need to see content of multiple tabs simultaneously and the user should be able to easily recognise where they are within the content. </p>
<h2>How to use it</h2>
<p>The structure of the tab set is defined in html. There are two forms supported. Adding a class of <code class="inline">.tab-group</code> to the container element will work in place of the <code class="inline">tabset</code> tag, and the tab panels can be defined using either <code class="inline">tab=""</code> or <code class="inline">data-tab=""</code>. Passing an optional element to the init function will initialise tabs within that element. </p>
<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: </p>
<ul>
<li>altModifer (When the altKey is used)</li>
<li>shiftModifier (When the shift key is used)</li>
<li>metaModifier (When the Windows key or Apple key is used)</li>
</ul>
<p>You can use these callbacks to create a custom event to get the tab information. </p>
<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. </p>
<tabset id="tabs">
<pre class="language-html" tab="html">
<tabset id="uniqueID">
@ -176,9 +184,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");
@ -204,11 +234,18 @@ 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", () => {
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");
@ -219,6 +256,7 @@ export function init(p = document, s = true) {
const tabPanelId = tab.getAttribute("id").replace("tab", "tab-panel");
document.getElementById(tabPanelId).classList.add("open");
}
});
@ -232,7 +270,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);
});
}
});
}

View File

@ -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();

View File

@ -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);
},

View File

@ -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,11 +59,18 @@ 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", () => {
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");
@ -52,6 +81,7 @@ export function init(p = document, s = true) {
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);
});
}
});
}

View File

@ -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")