import { ZuiBaseElement } from '@zywave/zui-base';
import { html } from 'lit';
import { property } from 'lit/decorators.js';
import { style } from './zui-step-flow-css.js';
import type { PropertyValues } from 'lit';
import type { ZuiStepFlowIndicator } from './zui-step-flow-indicator';
import type { ZuiStepFlowContent } from './zui-step-flow-content';
/**
* @element zui-step-flow
*
* @deprecated
*/
export class ZuiStepFlow extends ZuiBaseElement {
@property({ type: Number, reflect: true })
step = 0;
@property({ type: String })
orientation = 'horizontal';
stepFlowIndicator: ZuiStepFlowIndicator | undefined = undefined;
stepFlowContent: ZuiStepFlowContent | undefined = undefined;
constructor() {
super();
this.addEventListener('nextstep', () => {
this._setStep(this.step + 1);
});
this.addEventListener('previousstep', () => {
this._setStep(this.step - 1);
});
this.addEventListener('setstep', this._setStepEvent);
}
firstUpdated() {
this.stepFlowIndicator = this.querySelector('zui-step-flow-indicator');
}
static get styles() {
return [super.styles, style];
}
render() {
return html`
`;
}
updated(changedProps: PropertyValues) {
super.update(changedProps);
const currentStepIndex = this.step;
const stepFlowContentItems = Array.from(this.getElementsByTagName('zui-step-flow-content-item'));
const stepFlowContentItemsLength = stepFlowContentItems.length;
const hasIntro = stepFlowContentItems.some((contentItem) => contentItem.hasAttribute('intro'));
const hasEnd = stepFlowContentItems.some((contentItem) => contentItem.hasAttribute('end'));
if ((hasIntro && currentStepIndex === 0) || (hasEnd && currentStepIndex === stepFlowContentItemsLength - 1)) {
this._setIndicatorHidden(true);
} else {
this._setIndicatorHidden(false);
}
if (this._hasValidStepNumber(currentStepIndex)) {
this._setIndicatorStep(hasIntro ? currentStepIndex - 1 : currentStepIndex);
this._setContentStep(currentStepIndex);
this._setStep(currentStepIndex);
}
if (changedProps.has('orientation')) {
const indicator = this.querySelector('zui-step-flow-indicator');
if (indicator) {
if (this.orientation && this.orientation === 'vertical') {
indicator.setAttribute('orientation', 'vertical');
} else {
indicator.removeAttribute('orientation');
}
}
}
}
private _hasValidStepNumber(step: number) {
const stepFlowContentItems = Array.from(this.getElementsByTagName('zui-step-flow-content-item'));
const stepFlowContentItemsLength = stepFlowContentItems.length;
return step >= 0 && step < stepFlowContentItemsLength;
}
private _setStepEvent(event: CustomEvent) {
let requestedStep = event.detail;
//If intro page exists compensate off by one step error
const stepFlowContentItems = Array.from(this.getElementsByTagName('zui-step-flow-content-item'));
const hasIntro = stepFlowContentItems.some((contentItem) => contentItem.hasAttribute('intro'));
if (hasIntro) {
requestedStep += 1;
}
if (requestedStep !== this.step) {
this.step = requestedStep;
this._setStep(requestedStep);
}
}
private _setStep(step: number) {
if (this._hasValidStepNumber(step)) {
this.step = step;
}
}
private _setIndicatorStep(step: number) {
if (this.stepFlowIndicator) {
this.stepFlowIndicator.step = step;
} else {
this.stepFlowIndicator = this._getElement('zui-step-flow-indicator') as ZuiStepFlowIndicator;
this.stepFlowIndicator.step = step;
}
}
private _setContentStep(step: number) {
if (this.stepFlowContent) {
this.stepFlowContent.step = step;
} else {
this.stepFlowContent = this._getElement('zui-step-flow-content') as ZuiStepFlowContent;
this.stepFlowContent.step = step;
}
}
private _setIndicatorHidden(shouldHide: boolean) {
if (!this.stepFlowIndicator) {
this.stepFlowIndicator = this._getElement('zui-step-flow-indicator') as ZuiStepFlowIndicator;
}
if (this.stepFlowIndicator.hide !== shouldHide) {
this.stepFlowIndicator.hide = shouldHide;
}
}
private _getElement(elementName: string) {
const elements = this.getElementsByTagName(elementName);
if (elements && elements.length > 0) {
return elements[0];
}
return {};
}
}
window.customElements.define('zui-step-flow', ZuiStepFlow);
declare global {
interface HTMLElementTagNameMap {
'zui-step-flow': ZuiStepFlow;
}
}