1
1
var $ = require ( 'jquery' ) ;
2
2
var View = require ( 'ampersand-view' ) ;
3
+ // var debug = require('debug')('scout:tour:index');
4
+
5
+ var ESC_KEY = 27 ;
6
+ var LEFT_ARROW_KEY = 37 ;
7
+ var RIGHT_ARROW_KEY = 39 ;
8
+ var TAB_KEY = 9 ;
9
+ var ENTER_KEY = 13 ;
10
+ var SPACE_KEY = 32 ;
3
11
4
12
var TourView = View . extend ( {
5
- props : {
13
+ session : {
14
+ body : 'any' ,
15
+ numFeatures : {
16
+ type : 'number' ,
17
+ default : 5
18
+ } ,
6
19
tourCount : {
7
20
type : 'number' ,
8
21
default : 0
@@ -21,6 +34,26 @@ var TourView = View.extend({
21
34
'click .tour-close-button' : 'tourRemove' ,
22
35
'click #tour-bg' : 'tourRemove'
23
36
} ,
37
+ onKeyPress : function ( evt ) {
38
+ if ( evt . keyCode === ESC_KEY ) {
39
+ evt . preventDefault ( ) ;
40
+ evt . stopPropagation ( ) ;
41
+ this . tourRemove ( ) ;
42
+ } else if ( [
43
+ RIGHT_ARROW_KEY ,
44
+ TAB_KEY ,
45
+ ENTER_KEY ,
46
+ SPACE_KEY ] . indexOf ( evt . keyCode ) !== - 1 ) {
47
+ this . showNextFeature ( ) ;
48
+ } else if ( evt . keyCode === LEFT_ARROW_KEY ) {
49
+ this . showPreviousFeature ( ) ;
50
+ }
51
+ } ,
52
+ initialize : function ( ) {
53
+ this . onKeyPress = this . onKeyPress . bind ( this ) ;
54
+ this . body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
55
+ this . body . addEventListener ( 'keydown' , this . onKeyPress ) ;
56
+ } ,
24
57
render : function ( ) {
25
58
this . renderWithTemplate ( this ) ;
26
59
this . $featuresUL = this . query ( '#features ul' ) ;
@@ -71,6 +104,9 @@ var TourView = View.extend({
71
104
this . showHidePreviousNextButtons ( ) ;
72
105
} ,
73
106
showPreviousFeature : function ( ) {
107
+ if ( this . tourCount <= 0 ) {
108
+ return ;
109
+ }
74
110
var previousFeature = this . tourCount - 1 ;
75
111
var that = this ;
76
112
@@ -93,6 +129,9 @@ var TourView = View.extend({
93
129
this . showHidePreviousNextButtons ( ) ;
94
130
} ,
95
131
showNextFeature : function ( ) {
132
+ if ( this . tourCount >= this . numFeatures - 1 ) {
133
+ return ;
134
+ }
96
135
var nextFeature = this . tourCount + 1 ;
97
136
var that = this ;
98
137
@@ -115,6 +154,7 @@ var TourView = View.extend({
115
154
this . showHidePreviousNextButtons ( ) ;
116
155
} ,
117
156
tourRemove : function ( ) {
157
+ this . body . removeEventListener ( 'keydown' , this . onKeyPress ) ;
118
158
this . remove ( ) ;
119
159
}
120
160
} ) ;
0 commit comments