11var $ = require ( 'jquery' ) ;
22var 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 ;
311
412var TourView = View . extend ( {
5- props : {
13+ session : {
14+ body : 'any' ,
15+ numFeatures : {
16+ type : 'number' ,
17+ default : 5
18+ } ,
619 tourCount : {
720 type : 'number' ,
821 default : 0
@@ -21,6 +34,26 @@ var TourView = View.extend({
2134 'click .tour-close-button' : 'tourRemove' ,
2235 'click #tour-bg' : 'tourRemove'
2336 } ,
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+ } ,
2457 render : function ( ) {
2558 this . renderWithTemplate ( this ) ;
2659 this . $featuresUL = this . query ( '#features ul' ) ;
@@ -71,6 +104,9 @@ var TourView = View.extend({
71104 this . showHidePreviousNextButtons ( ) ;
72105 } ,
73106 showPreviousFeature : function ( ) {
107+ if ( this . tourCount <= 0 ) {
108+ return ;
109+ }
74110 var previousFeature = this . tourCount - 1 ;
75111 var that = this ;
76112
@@ -93,6 +129,9 @@ var TourView = View.extend({
93129 this . showHidePreviousNextButtons ( ) ;
94130 } ,
95131 showNextFeature : function ( ) {
132+ if ( this . tourCount >= this . numFeatures - 1 ) {
133+ return ;
134+ }
96135 var nextFeature = this . tourCount + 1 ;
97136 var that = this ;
98137
@@ -115,6 +154,7 @@ var TourView = View.extend({
115154 this . showHidePreviousNextButtons ( ) ;
116155 } ,
117156 tourRemove : function ( ) {
157+ this . body . removeEventListener ( 'keydown' , this . onKeyPress ) ;
118158 this . remove ( ) ;
119159 }
120160} ) ;
0 commit comments