]>
Commit | Line | Data |
---|---|---|
a4b75251 TL |
1 | import { Injectable } from '@angular/core'; |
2 | ||
3 | import { BehaviorSubject, Observable } from 'rxjs'; | |
4 | ||
5 | import { WizardStepModel } from '~/app/shared/models/wizard-steps'; | |
6 | ||
7 | const initialStep = [{ stepIndex: 1, isComplete: false }]; | |
8 | ||
9 | @Injectable({ | |
10 | providedIn: 'root' | |
11 | }) | |
12 | export class WizardStepsService { | |
13 | steps$: BehaviorSubject<WizardStepModel[]>; | |
14 | currentStep$: BehaviorSubject<WizardStepModel> = new BehaviorSubject<WizardStepModel>(null); | |
15 | ||
16 | constructor() { | |
17 | this.steps$ = new BehaviorSubject<WizardStepModel[]>(initialStep); | |
18 | this.currentStep$.next(this.steps$.value[0]); | |
19 | } | |
20 | ||
21 | setTotalSteps(step: number) { | |
22 | const steps: WizardStepModel[] = []; | |
23 | for (let i = 1; i <= step; i++) { | |
24 | steps.push({ stepIndex: i, isComplete: false }); | |
25 | } | |
26 | this.steps$ = new BehaviorSubject<WizardStepModel[]>(steps); | |
27 | } | |
28 | ||
29 | setCurrentStep(step: WizardStepModel): void { | |
30 | this.currentStep$.next(step); | |
31 | } | |
32 | ||
33 | getCurrentStep(): Observable<WizardStepModel> { | |
34 | return this.currentStep$.asObservable(); | |
35 | } | |
36 | ||
37 | getSteps(): Observable<WizardStepModel[]> { | |
38 | return this.steps$.asObservable(); | |
39 | } | |
40 | ||
41 | moveToNextStep(): void { | |
42 | const index = this.currentStep$.value.stepIndex; | |
43 | this.currentStep$.next(this.steps$.value[index]); | |
44 | } | |
45 | ||
46 | moveToPreviousStep(): void { | |
47 | const index = this.currentStep$.value.stepIndex - 1; | |
48 | this.currentStep$.next(this.steps$.value[index - 1]); | |
49 | } | |
50 | ||
51 | isLastStep(): boolean { | |
52 | return this.currentStep$.value.stepIndex === this.steps$.value.length; | |
53 | } | |
54 | ||
55 | isFirstStep(): boolean { | |
56 | return this.currentStep$.value?.stepIndex - 1 === 0; | |
57 | } | |
58 | } |