-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.ts
60 lines (52 loc) · 2.02 KB
/
driver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { globalConfig } from "@/config"
import type { ILocalSteps } from "@/models/"
import "@/styles/Driver.css"
import { getFromSessionStorage, setToSessionStorage } from "@/utils/"
import { driver, type DriveStep, type Driver } from "driver.js"
import "driver.js/dist/driver.css"
export class DriverManager {
private driver: Driver
private globalSteps: DriveStep[] // for steps that are in all pages
private localSteps: ILocalSteps[] // for steps for each page
private wasGlobalStepsShowed: string | null
constructor(globalSteps: DriveStep[], localSteps: ILocalSteps[]) {
this.globalSteps = globalSteps
this.localSteps = localSteps
this.wasGlobalStepsShowed = getFromSessionStorage(globalConfig.localStorage.wasGlobalStepsShowed)
this.driver = driver(
{
showProgress: true,
animate: true,
showButtons: ["next", "previous"],
popoverClass: "driverjs-theme",
})
}
private determinateStepsForPage() {
if (this.driver == null) {
console.error('DriverManager found an error: driver is not defined')
return
}
let stepsToShow = this.globalSteps
const path = window.location.pathname
const foundLocalSteps = this.localSteps.find(
(localStep) => localStep.path.includes(path as string)
)?.steps
if (foundLocalSteps != null) {
if (this.wasGlobalStepsShowed === null) {
stepsToShow = [...stepsToShow, ...foundLocalSteps]
setToSessionStorage(globalConfig.localStorage.wasGlobalStepsShowed, 'yes')
} else {
stepsToShow = foundLocalSteps
}
}
this.driver.setSteps(stepsToShow)
}
public async showSteps() {
if (this.driver == null) {
console.error('DriverManager found an error: driver is not defined')
return
}
this.determinateStepsForPage()
this.driver.drive()
}
}