Files

2 lines
2.3 KiB
JavaScript

function d({isSkippable:n,isStepPersistedInQueryString:i,key:h,livewireId:l,schemaKey:c,startStep:r,stepQueryStringKey:o}){return{boundResetHandler:null,step:null,init(){let t=this.getSteps(),e=new URLSearchParams(window.location.search).get(o);this.step=i&&t.includes(e)?e:t.at(r-1),this.$watch("step",()=>{this.updateQueryString(),this.autofocusFields()}),this.autofocusFields(!0),this.boundResetHandler=s=>{s.detail.livewireId!==l||s.detail.schemaKey!==c||i||this.$nextTick(()=>{this.step=this.getSteps().at(r-1)??this.step})},window.addEventListener("reset-schema-component-state",this.boundResetHandler)},async requestNextStep(){await this.$wire.callSchemaComponentMethod(h,"nextStep",{currentStepIndex:this.getStepIndex(this.step),currentStepKey:this.step})},goToNextStep(t=null,e=null){if(!(e!==null&&this.step!==e)){if(t===null){let s=this.getStepIndex(this.step);if(s===-1)return;t=this.getSteps()[s+1]}this.getSteps().includes(t)&&(this.step=t,this.scroll())}},goToPreviousStep(){let t=this.getStepIndex(this.step)-1;t<0||(this.step=this.getSteps()[t],this.scroll())},goToStep(t){let e=this.getStepIndex(t);e<=-1||!n&&e>this.getStepIndex(this.step)||(this.step=t,this.scroll())},scroll(){this.$nextTick(()=>{this.$refs.header?.children[this.getStepIndex(this.step)].scrollIntoView({behavior:"smooth",block:"start"})})},autofocusFields(t=!1){this.$nextTick(()=>{if(t&&document.activeElement&&document.activeElement!==document.body&&this.$el.compareDocumentPosition(document.activeElement)&Node.DOCUMENT_POSITION_PRECEDING)return;let e=this.$refs[`step-${this.step}`]?.querySelectorAll("[autofocus]")??[];for(let s of e)if(s.focus(),document.activeElement===s)break})},getStepIndex(t){return this.getSteps().findIndex(e=>e===t)},getSteps(){return JSON.parse(this.$refs.stepsData.value)},isFirstStep(){return this.getStepIndex(this.step)<=0},isLastStep(){return this.getStepIndex(this.step)+1>=this.getSteps().length},isStepAccessible(t){return this.getStepIndex(t)!==-1&&(n||this.getStepIndex(this.step)>this.getStepIndex(t))},updateQueryString(){if(!i)return;let t=new URL(window.location.href);t.searchParams.set(o,this.step),history.replaceState(null,document.title,t.toString())},destroy(){this.boundResetHandler&&window.removeEventListener("reset-schema-component-state",this.boundResetHandler)}}}export{d as default};