11import { Injectable } from '@angular/core' ;
2- import Shepherd from 'shepherd.js' ;
2+ import Shepherd , {
3+ type TourOptions ,
4+ type StepOptions ,
5+ type Tour
6+ } from 'shepherd.js' ;
37import { elementIsHidden } from './utils/dom' ;
48import { makeButton } from './utils/buttons' ;
5- import Step from 'shepherd.js/src/types/step' ;
9+
10+ interface RequiredElement {
11+ message : string ;
12+ selector : string ;
13+ title : string ;
14+ }
615@Injectable ( {
716 providedIn : 'root'
817} )
918export class ShepherdService {
10- confirmCancel = false ;
11- confirmCancelMessage : string = null ;
12- defaultStepOptions : Step . StepOptions = { } ;
13- errorTitle = null ;
19+ confirmCancel : TourOptions [ 'confirmCancel' ] = false ;
20+ confirmCancelMessage ?: TourOptions [ 'confirmCancelMessage' ] ;
21+ defaultStepOptions : StepOptions = { } ;
22+ errorTitle ?: string ;
23+ exitOnEsc = true ;
1424 isActive = false ;
15- keyboardNavigation = true ;
16- messageForUser : string = null ;
25+ keyboardNavigation : TourOptions [ 'keyboardNavigation' ] = true ;
26+ messageForUser : string | null = null ;
1727 modal = false ;
18- requiredElements = [ ] ;
28+ requiredElements : Array < RequiredElement > = [ ] ;
1929 tourName = undefined ;
20- tourObject : Shepherd . Tour = null ;
21- exitOnEsc = true ;
30+ tourObject : Tour | null = null ;
2231
23- constructor ( ) {
24- }
32+ constructor ( ) { }
2533
2634 /**
2735 * Get the tour object and call back
2836 */
2937 back ( ) {
30- this . tourObject . back ( ) ;
38+ this . tourObject ? .back ( ) ;
3139 }
3240
3341 /**
3442 * Cancel the tour
3543 */
3644 cancel ( ) {
37- this . tourObject . cancel ( ) ;
45+ this . tourObject ? .cancel ( ) ;
3846 }
3947
4048 /**
4149 * Complete the tour
4250 */
4351 complete ( ) {
44- this . tourObject . complete ( ) ;
52+ this . tourObject ? .complete ( ) ;
4553 }
4654
4755 /**
4856 * Hides the current step
4957 */
5058 hide ( ) {
51- this . tourObject . hide ( ) ;
59+ this . tourObject ? .hide ( ) ;
5260 }
5361
5462 /**
5563 * Advance the tour to the next step
5664 */
5765 next ( ) {
58- this . tourObject . next ( ) ;
66+ this . tourObject ? .next ( ) ;
5967 }
6068
6169 /**
6270 * Show a specific step, by passing its id
6371 * @param id The id of the step you want to show
6472 */
6573 show ( id : string | number ) {
66- this . tourObject . show ( id ) ;
74+ this . tourObject ? .show ( id ) ;
6775 }
6876
6977 /**
7078 * Start the tour
7179 */
7280 start ( ) {
7381 this . isActive = true ;
74- this . tourObject . start ( ) ;
82+ this . tourObject ? .start ( ) ;
7583 }
7684
7785 /**
@@ -86,24 +94,26 @@ export class ShepherdService {
8694 * Take a set of steps and create a tour object based on the current configuration
8795 * @param steps An array of steps
8896 */
89- addSteps ( steps : Array < Step . StepOptions > ) {
97+ addSteps ( steps : Array < StepOptions > ) {
9098 this . _initialize ( ) ;
9199 const tour = this . tourObject ;
92100
93- // Return nothing if there are no steps
94- if ( ! steps || ! Array . isArray ( steps ) || steps . length === 0 ) {
101+ // Return nothing if there are no steps or if somehow there is no tour.
102+ if ( ! tour || ! steps || ! Array . isArray ( steps ) || steps . length === 0 ) {
95103 return ;
96104 }
97105
98106 if ( ! this . requiredElementsPresent ( ) ) {
99107 tour . addStep ( {
100- buttons : [ {
101- text : 'Exit' ,
102- action : tour . cancel
103- } ] ,
108+ buttons : [
109+ {
110+ text : 'Exit' ,
111+ action : tour . cancel
112+ }
113+ ] ,
104114 id : 'error' ,
105115 title : this . errorTitle ,
106- text : [ this . messageForUser ]
116+ text : [ this . messageForUser as string ]
107117 } ) ;
108118 return ;
109119 }
@@ -128,7 +138,10 @@ export class ShepherdService {
128138 this . requiredElements . forEach ( ( element ) => {
129139 const selectedElement = document . querySelector ( element . selector ) ;
130140
131- if ( allElementsPresent && ( ! selectedElement || elementIsHidden ( selectedElement ) ) ) {
141+ if (
142+ allElementsPresent &&
143+ ( ! selectedElement || elementIsHidden ( selectedElement as HTMLElement ) )
144+ ) {
132145 allElementsPresent = false ;
133146 this . errorTitle = element . title ;
134147 this . messageForUser = element . message ;
0 commit comments