Skip to content
Snippets Groups Projects
Verified Commit 34f4683e authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix(tabs): Only activate the closing behaviour if the closed tab was previously active. #264

parent 7d13ee9b
No related branches found
No related tags found
No related merge requests found
...@@ -16,11 +16,11 @@ ...@@ -16,11 +16,11 @@
<main> <main>
<monster-tabs data-monster-option-features-removeBehavior="auto"> <monster-tabs data-monster-option-features-removeBehavior="auto">
<div data-monster-button-label="A1" data-monster-removable>test</div> <div data-monster-button-label="A1" data-monster-removable>test1</div>
<div data-monster-button-label="A2" data-monster-removable>test</div> <div data-monster-button-label="A2" data-monster-removable>test2</div>
<div data-monster-button-label="A3" class="active" data-monster-removable>test</div> <div data-monster-button-label="A3" class="active" data-monster-removable>test3</div>
<div data-monster-button-label="A4" data-monster-removable>test</div> <div data-monster-button-label="A4" data-monster-removable>test44</div>
<div data-monster-button-label="A5" data-monster-removable>test</div> <div data-monster-button-label="A5" data-monster-removable>test55</div>
</monster-tabs> </monster-tabs>
</main> </main>
......
...@@ -442,7 +442,8 @@ function showPopper() { ...@@ -442,7 +442,8 @@ function showPopper() {
this[popperElementSymbol].style.removeProperty("visibility"); this[popperElementSymbol].style.removeProperty("visibility");
}) })
.run(undefined) .run(undefined)
.then(() => {}) .then(() => {
})
.catch((e) => { .catch((e) => {
addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, e.message); addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, e.message);
}); });
...@@ -653,26 +654,18 @@ function show(element) { ...@@ -653,26 +654,18 @@ function show(element) {
* @private * @private
*/ */
function initEventHandler() { function initEventHandler() {
const self = this;
if (!this.shadowRoot) { if (!this.shadowRoot) {
throw new Error("no shadow-root is defined"); throw new Error("no shadow-root is defined");
} }
/**
* @param {Event} event
*/
this[changeTabEventHandler] = (event) => {
const element = findTargetElementFromEvent(event, ATTRIBUTE_ROLE, "button");
if (element instanceof HTMLButtonElement && element.disabled !== true) {
show.call(this, element);
}
};
/** /**
* @param {Event} event * @param {Event} event
* @fires monster-tab-remove * @fires monster-tab-remove
*/ */
this[removeTabEventHandler] = (event) => { this[removeTabEventHandler] = (event) => {
const element = findTargetElementFromEvent( const element = findTargetElementFromEvent(
event, event,
ATTRIBUTE_ROLE, ATTRIBUTE_ROLE,
...@@ -691,39 +684,50 @@ function initEventHandler() { ...@@ -691,39 +684,50 @@ function initEventHandler() {
`${ATTRIBUTE_PREFIX}tab-reference`, `${ATTRIBUTE_PREFIX}tab-reference`,
); );
const previous = button.previousElementSibling; let doChange = false;
const next = button.nextElementSibling; let nextName = null
let previousName = null;
const btn = this.getOption("buttons")
for (let i = 0; i < btn.standard.length; i++) {
if (btn.standard[i].reference === reference) {
if (btn.standard[i].state === "active") {
doChange = i
if (i < btn.standard.length - 1) {
nextName = btn.standard[i + 1]?.reference
}
if (i > 0) {
previousName = btn.standard[i - 1]?.reference
}
}
break;
}
}
if (reference) { if (reference) {
const container = this.querySelector(`[id=${reference}]`); const container = this.querySelector(`[id=${reference}]`);
if (container instanceof HTMLElement) { if (container instanceof HTMLElement) {
container.remove();
initTabButtons.call(this); if (doChange) {
fireCustomEvent(this, "monster-tab-remove", {
reference,
});
}
}
switch (this.getOption("features.removeBehavior")) { switch (this.getOption("features.removeBehavior")) {
case "auto": case "auto":
if (next instanceof HTMLButtonElement) { if (nextName !== null) {
next.click(); self.activeTab(nextName);
} else { } else {
// get previous button if (previousName !== null) {
if (previous instanceof HTMLButtonElement) { self.activeTab(previousName);
previous.click();
} }
} }
break; break;
case "next": case "next":
if (next instanceof HTMLButtonElement) { if (nextName !== null) {
next.click(); self.activeTab(nextName);
} }
break; break;
case "previous": case "previous":
if (previous instanceof HTMLButtonElement) { if (previousName !== null) {
previous.click(); self.activeTab(previousName);
} }
break; break;
...@@ -731,14 +735,30 @@ function initEventHandler() { ...@@ -731,14 +735,30 @@ function initEventHandler() {
break; break;
} }
} }
container.remove();
initTabButtons.call(this);
fireCustomEvent(this, "monster-tab-remove", {
reference,
});
}
}
}
} }
}; };
this[navElementSymbol].addEventListener("touch", this[changeTabEventHandler]); /**
this[navElementSymbol].addEventListener("click", this[changeTabEventHandler]); * @param {Event} event
*/
this[changeTabEventHandler] = (event) => {
this[navElementSymbol].addEventListener("touch", this[removeTabEventHandler]); const element = findTargetElementFromEvent(event, ATTRIBUTE_ROLE, "button");
this[navElementSymbol].addEventListener("click", this[removeTabEventHandler]);
if (element instanceof HTMLButtonElement && element.disabled !== true) {
show.call(this, element);
}
};
/** /**
* @param {Event} event * @param {Event} event
...@@ -755,6 +775,14 @@ function initEventHandler() { ...@@ -755,6 +775,14 @@ function initEventHandler() {
hidePopper.call(this); hidePopper.call(this);
}; };
// the order is important, because the remove must be before the change
this[navElementSymbol].addEventListener("touch", this[removeTabEventHandler]);
this[navElementSymbol].addEventListener("click", this[removeTabEventHandler]);
this[navElementSymbol].addEventListener("touch", this[changeTabEventHandler]);
this[navElementSymbol].addEventListener("click", this[changeTabEventHandler]);
return this; return this;
} }
...@@ -906,7 +934,8 @@ function initTabButtons() { ...@@ -906,7 +934,8 @@ function initTabButtons() {
} }
}) })
.run(undefined) .run(undefined)
.then(() => {}) .then(() => {
})
.catch((e) => { .catch((e) => {
addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, e.message); addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, e.message);
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment