30
30
</template >
31
31
32
32
<script >
33
+ import { DEFAULT_OPTIONS , KEYS } from ' ../shared/constants'
34
+
33
35
export default {
34
36
name: ' v-tour' ,
35
37
props: {
@@ -39,6 +41,10 @@ export default {
39
41
},
40
42
name: {
41
43
type: String
44
+ },
45
+ options: {
46
+ type: Object ,
47
+ default : () => { return DEFAULT_OPTIONS }
42
48
}
43
49
},
44
50
data () {
@@ -48,30 +54,71 @@ export default {
48
54
},
49
55
mounted () {
50
56
this .$tours [this .name ] = this
57
+
58
+ if (this .customOptions .useKeyboardNavigation ) {
59
+ window .addEventListener (' keyup' , this .handleKeyup )
60
+ }
61
+ },
62
+ beforeDestroy () {
63
+ // Remove the keyup listener if it has been defined
64
+ if (this .customOptions .useKeyboardNavigation ) {
65
+ window .removeEventListener (' keyup' , this .handleKeyup )
66
+ }
51
67
},
52
68
computed: {
69
+ // Allow us to define custom options and merge them with the default options.
70
+ // Since options is a computed property, it is reactive and can be updated during runtime.
71
+ customOptions () {
72
+ return {
73
+ ... DEFAULT_OPTIONS,
74
+ ... this .options
75
+ }
76
+ },
77
+ // Return true if the tour is active, which means that there's a VStep displayed
78
+ isRunning () {
79
+ return this .currentStep > - 1 && this .currentStep < this .numberOfSteps
80
+ },
53
81
isFirst () {
54
82
return this .currentStep === 0
55
83
},
56
84
isLast () {
57
85
return this .currentStep === this .steps .length - 1
86
+ },
87
+ numberOfSteps () {
88
+ return this .steps .length
58
89
}
59
90
},
60
91
methods: {
61
92
start () {
62
93
// Wait for the DOM to be loaded, then start the tour
63
94
setTimeout (() => {
64
95
this .currentStep = 0
65
- })
96
+ }, this . customOptions . startTimeout )
66
97
},
67
98
previousStep () {
68
- this .currentStep --
99
+ if ( this . currentStep > 0 ) this .currentStep --
69
100
},
70
101
nextStep () {
71
- this .currentStep ++
102
+ if ( this . currentStep < this . numberOfSteps - 1 && this . currentStep !== - 1 ) this .currentStep ++
72
103
},
73
104
stop () {
74
105
this .currentStep = - 1
106
+ },
107
+
108
+ handleKeyup (e ) {
109
+ // TODO: debug mode
110
+ // console.log('[Vue Tour] A keyup event occured:', e)
111
+ switch (e .keyCode ) {
112
+ case KEYS .ARROW_RIGHT :
113
+ this .nextStep ()
114
+ break
115
+ case KEYS .ARROW_LEFT :
116
+ this .previousStep ()
117
+ break
118
+ case KEYS .ESCAPE :
119
+ this .stop ()
120
+ break
121
+ }
75
122
}
76
123
}
77
124
}
0 commit comments