2021-09-21 09:31:37 +00:00
|
|
|
import { t } from "@lingui/macro";
|
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { CSSResult, LitElement, TemplateResult, css, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
|
|
|
import { ifDefined } from "lit/directives/if-defined.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
|
|
|
import AKGlobal from "../authentik.css";
|
2021-03-17 16:11:39 +00:00
|
|
|
import PFTabs from "@patternfly/patternfly/components/Tabs/tabs.css";
|
|
|
|
import PFGlobal from "@patternfly/patternfly/patternfly-base.css";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2022-05-23 17:48:33 +00:00
|
|
|
import { CURRENT_CLASS, EVENT_REFRESH, ROUTE_SEPARATOR } from "../constants";
|
2021-11-18 20:01:58 +00:00
|
|
|
import { getURLParams, updateURLParams } from "./router/RouteMatch";
|
2020-11-08 21:43:46 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
@customElement("ak-tabs")
|
2020-11-20 21:08:00 +00:00
|
|
|
export class Tabs extends LitElement {
|
2021-11-26 12:30:02 +00:00
|
|
|
@property()
|
2021-11-26 13:08:45 +00:00
|
|
|
pageIdentifier = "page";
|
2021-11-26 12:30:02 +00:00
|
|
|
|
2020-11-26 22:31:56 +00:00
|
|
|
@property()
|
|
|
|
currentPage?: string;
|
2020-11-08 21:43:46 +00:00
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ type: Boolean })
|
2021-03-03 08:28:12 +00:00
|
|
|
vertical = false;
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
PFGlobal,
|
|
|
|
PFTabs,
|
|
|
|
AKGlobal,
|
|
|
|
css`
|
|
|
|
::slotted(*) {
|
|
|
|
flex-grow: 2;
|
|
|
|
}
|
|
|
|
:host([vertical]) {
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
:host([vertical]) .pf-c-tabs {
|
|
|
|
width: auto !important;
|
|
|
|
}
|
|
|
|
:host([vertical]) .pf-c-tabs__list {
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
2020-11-08 21:43:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 23:58:50 +00:00
|
|
|
observer: MutationObserver;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.observer = new MutationObserver(() => {
|
|
|
|
this.requestUpdate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
|
|
|
super.connectedCallback();
|
2021-08-03 15:52:21 +00:00
|
|
|
this.observer.observe(this, {
|
|
|
|
attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
2021-03-17 23:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
|
|
|
this.observer.disconnect();
|
|
|
|
super.disconnectedCallback();
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:19:08 +00:00
|
|
|
onClick(slot?: string): void {
|
|
|
|
this.currentPage = slot;
|
2021-11-26 12:30:02 +00:00
|
|
|
const params: { [key: string]: string | undefined } = {};
|
|
|
|
params[this.pageIdentifier] = slot;
|
|
|
|
updateURLParams(params);
|
2022-05-23 17:48:33 +00:00
|
|
|
const page = this.querySelector(`[slot='${this.currentPage}']`);
|
|
|
|
if (!page) return;
|
|
|
|
page.dispatchEvent(
|
|
|
|
new CustomEvent(EVENT_REFRESH, {
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
}),
|
|
|
|
);
|
2021-04-04 21:19:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
renderTab(page: Element): TemplateResult {
|
|
|
|
const slot = page.attributes.getNamedItem("slot")?.value;
|
|
|
|
return html` <li class="pf-c-tabs__item ${slot === this.currentPage ? CURRENT_CLASS : ""}">
|
2021-04-04 21:19:08 +00:00
|
|
|
<button class="pf-c-tabs__link" @click=${() => this.onClick(slot)}>
|
2021-08-03 15:52:21 +00:00
|
|
|
<span class="pf-c-tabs__item-text"> ${page.getAttribute("data-tab-title")} </span>
|
2020-12-01 16:27:19 +00:00
|
|
|
</button>
|
|
|
|
</li>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
render(): TemplateResult {
|
2021-01-16 22:04:08 +00:00
|
|
|
const pages = Array.from(this.querySelectorAll("[slot^='page-']"));
|
2021-11-18 20:01:58 +00:00
|
|
|
if (window.location.hash.includes(ROUTE_SEPARATOR)) {
|
|
|
|
const params = getURLParams();
|
2021-11-26 12:30:02 +00:00
|
|
|
if (this.pageIdentifier in params) {
|
|
|
|
if (this.querySelector(`[slot='${params[this.pageIdentifier]}']`) !== null) {
|
2021-11-18 20:01:58 +00:00
|
|
|
// To update the URL to match with the current slot
|
2022-05-23 17:48:33 +00:00
|
|
|
this.currentPage = params[this.pageIdentifier] as string;
|
2021-11-18 20:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 22:31:56 +00:00
|
|
|
if (!this.currentPage) {
|
|
|
|
if (pages.length < 1) {
|
2021-04-03 17:26:43 +00:00
|
|
|
return html`<h1>${t`no tabs defined`}</h1>`;
|
2020-11-26 22:31:56 +00:00
|
|
|
}
|
2021-11-18 20:01:58 +00:00
|
|
|
const wantedPage = pages[0].attributes.getNamedItem("slot")?.value;
|
2021-04-04 21:19:08 +00:00
|
|
|
this.onClick(wantedPage);
|
2020-11-08 21:43:46 +00:00
|
|
|
}
|
2021-03-03 08:28:12 +00:00
|
|
|
return html`<div class="pf-c-tabs ${this.vertical ? "pf-m-vertical pf-m-box" : ""}">
|
2020-11-26 22:31:56 +00:00
|
|
|
<ul class="pf-c-tabs__list">
|
2020-12-01 16:27:19 +00:00
|
|
|
${pages.map((page) => this.renderTab(page))}
|
2020-11-26 22:31:56 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
2021-11-05 00:05:43 +00:00
|
|
|
<slot name="header"></slot>
|
2020-12-02 14:44:40 +00:00
|
|
|
<slot name="${ifDefined(this.currentPage)}"></slot>`;
|
2020-11-08 21:43:46 +00:00
|
|
|
}
|
|
|
|
}
|