0.1.7 • Published 5 years ago

ng3-tour-test v0.1.7

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

ng3-Tour

An Angular Tour (ng3-tour) light library is built entirely in Angular and allows you easily master guide for your users through your site showing them all the sections and features including lazily loaded.

For Angular 2+ (2, 4, 5, 6, 7, 8)

Installation

npm install --save ng3-tour

Usage

  1. Import the NgTourModule in your AppModule:
import {AngularTourModule} from 'ng3-tour';

@NgModule({
    declarations: [AppComponent],
    imports: [
            RouterModule.forRoot([]),
            AngularTourModule.forRoot(),
            BrowserModule
    ],
    providers: [],
    bootstrap: [AppComponent]
    })
export class AppModule { }
  1. Import the NgTourModule in your Feature Modules:
    @NgModule({
        declarations: [FeatureComponent],
        imports: [
            NgTourModule.forChild(),
        ],
        providers: [],
        })
    export class FeatureModule { }
  1. Use ngIfTour directive inside app.component.html. A good choice is marking <router-outlet> with it.
    <route-outlet ngIfTour></route-outlet>
  1. Mark your target HTML elements with the ngTourStep directive with a unique name.
    <div ngTourStep="first">...</div>
    <span ngTourStep="second">...</span>
  1. Inject NgTourService in your Component (where Tour is being started).
    @Component({
        selector: 'component-of-your-choice',
        templateUrl: './your.component.html'
    })
    export class ComponentOfYourChoice {
        constructor(private readonly tourService: TourService) { }
    }
  1. Create a configuration object and pass it as an argument to the startTour method
const tour = {
    steps: [
        {
            stepName: "first",
            route: "home",
            title: 'My first lazily loaded feature',
            description: "My first feature description", 
            options: {backdrop: true}
        },
        {
            stepName: "nextStep",
            route: "about",
            title: "My second lazily loaded feature",
            description: "My second feature description",
            options:{placement: 'top', smoothScroll: true}
        }
    ],
    tourOptions: {
        backdrop: false,
    }
}

Keep in mind if you implement your own Step template, you can use your own Step properties besides required ('stepName' and 'route')

ngOnInit() {
    this.tourService.startTour(tour);
}

or

onClick() {
    this.tourService.startTour(tour);
}

Customizing

If you want to use tour own Step template wrap it with <ng-tour-step-template> and place in app.component.html. Mark the provided template with reference with assigned value 'step$' that gives you access to steps Stream. To handle controls event you could use tourEvent directive with corresponded input value (one of 'next', 'prev', 'close')

<ng-tour-step-template #refToExportAs="step$" tourStepData (next)="onNext($event)" (done)="onDone($event)">
    <div  class="tour-step-modal__content" *ngIf="refToExportAs.step$ | async as step">
        <div >
            <div class="tour-step-modal__header">
                <h3 class="tour-step-modal__title"> 
                {{step.title}}
                </h3>
                <button class="tour-btn-close" type="button" stepEvent="close" (break)="onBreak($event)" (done)="onDone($event)">
                &times;
                </button>
            </div>
            <div class="tour-step-modal__body">
                <p class="tour-step-modal__description">
                {{step.description}}
                </p>
                <p class="tour-step-modal__adds">
                {{step.adds}}
                </p>
                <p class="tour-step-modal__adds">
                {{step.customData}}
                </p>
                <p *ngIf="step.index===3" class="tour-step-modal__adds"> 
                StepName of this step is {{step.StepName}}
                </p>
            </div>
            <div class="tour-step-modal__footer">
                <div *ngIf="!step.options.withoutCounter" class="tour-step-modal__counter">
                {{step.index + 1}} of {{step.total}}
                </div>
                <button
                *ngIf="!step.options.withoutPrev && step.index" 
                type="button" 
                class="tour-btn"
                stepEvent="prev"
                >
                {{step.ctrlBtns.prev}}
                </button>
                <button
                *ngIf="step.index + 1 !== step.total"
                type="button"
                class="tour-btn tour-btn-next"
                stepEvent="next"  
                (next)="onNext($event)"
                >
                {{step.ctrlBtns.next}}
                </button>
                <button
                *ngIf="step.index + 1 === step.total"
                type="button"
                class="tour-btn tour-btn-done"
                stepEvent="close"
                (done)="onDone($event)"
                >
                {{step.ctrlBtns.done}}
                </button>
            </div>
        </div>
    </div>
</ng-tour-step-template>

Keep in mind if you use <ng-tour-step-template> and ngIfTour at the same time 'customTemplate' option is set to true programmatically. If you omit ngIfTour and want to use your own Step template set this option to true manually.

Internalization

If you implement a custom Step template you can implement any Internalization you want, but if you use the provided Step template you can not do so. There is an embedded one for this case.

You can pass an object with the required translations to the Step properties the title and the description.

title: {
    'en-EN': 'My first feature',
    'ru-RU': 'Моя первая фича',
    'fr-FR': 'Mon premier caractéristique',
}

You also can define t he required translations for the Step controls. Simply pass the corresponded object to the Tour option ctrlBtns. There is default value below.

ctrlBtns: {
  done: {
   'en-EN': 'done',
   'ru-RU': 'закр',
   'fr-FR': 'fini',
  },
  prev: {
    'en-EN': 'prev',
    'ru-RU': 'пред',
    'fr-FR': 'préc'
  },
  next: {
    'en-EN': 'next',
    'ru-RU': 'след',
    'fr-FR': 'proch',
  },
}

API reference

Tour Properties

Tour configuration object contains steps array and could include common options, event handlers and set of control button names Name | Required | Type | Destination -----|----|----|--------| steps | yes | TourStep[] | This option define Tour Steps and its order | tourEvents | no | TourEventsI | define event handlers for 'tour start', 'tour break', 'tour end', 'next step', 'prev step' | tourOption | no | TourStepI | Set common options. Could be redefined with Step options ctrlBtns | no | CtrlBtnsI | Set translations of the control buttons for any languages you want

Tour Events

These events are functions witch is called within corresponded tourService methods. You can pass your own implementation of these functions within tourEvent property for example to collect some statistical data about user behavior. Keep in mind Tour Events unlike Step Events could fire without user activity in case continueIfTargetAbsent property is set to true (default value).

NameRequiredPropsDescription
tourStartno{tourEvent: string, tour?: TourI}Add logic executed before the Tour will be started
tourBreakno{tourEvent: string, step?: number, history?: number[]}Add logic executing before the Tour will be stoped untimely
tourEndno{tourEvent: string, step?: number, history?: number[]}Add logic executing before the Tour will be stopped after finishing (latest step was visited)
nextno{tourEvent: string, step?: number, history?: number[]}Add logic executing before the next step will be initiated
prevno{tourEvent: string, step?: number, history?: number[]}Add logic executing before the previous step will be initiated

Step Properties (StepOptions)

NameRequiredTypeDestinationDefault value
stepNameyesstringDefine unique name of the Step
routeyesstringDefine route corresponded to the Step
indexnonumber'Index' value is set by TourService service according to the position of the Step in the Steps array
titlenostringSet title of the current Step""
descriptionnostringSet description of the current Step""
ctrlBtnsnoCtrlBtnsISet translations of the control buttons for any languages you wantsee below
optionsnoStepOptionsITo customize separate Stepdescribed below
options:
classNamenostringSet custom class to the Step component""
themeColornostringDefine theme color'rgb(20, 60, 60)'
backdropnobooleanAdd backdrop if option set truetrue
opacitynonumberDefine the backdrop opacity.6
placementnostringThis option define position of step modal relative to target. Possible values: 'down', 'top', 'left', 'right', 'center', 'right-center', 'left-center', right-top', 'left-top' ( case no matter )"Down"
customTemplatenobooleanThis option has by default value true if you used <ng-tour-step-template> and directive ngIfTour simultaneously, otherwise false.true/false
withoutCounternobooleanIf true remove counter including a number of the step and total number of steps from the Step templatefalse
withoutPrevnobooleanIf true remove 'Prev' control button from the Step templatefalse
arrowToTargetnobooleanIf true add arrow in direction corresponded location of the Step targettrue
scrollTonobooleanIf value equals true, the window is scrolled to show Step target and modal (popup)true
scrollSmoothnobooleanIf true option behavior of the window scroll is set to smoothfalse
continueIfTargetAbsentnobooleanIf true and the Step target is absent will initialize the next Steptrue
animatedStepnobooleanIf true add classes to control css animationtrue
fixednobooleanIf value equals true position css property of the Step Component and Backdrop Component is set to the 'fixed', otherwise 'absolute'false
animationDelaynonumberRequired in case of routing with lazy loaded Feature Modules. Delay defined in ms500
targetResizenonumber[]Change size in px of the Step target window. Number with index 0 define Width. Number with index 1 if exist define Height differ from the Width0
minWidthnostringDefine min-width of the Step modal'200px'
minHeightnostringDefine min-height of the Step modal'200px'
maxWidthnostringDefine max-width of the Step modal'400px'
maxHeightnostringDefine max-height of the Step modal'400px'
autofocusnobooleanIf true 'next' and 'done' buttons obtain focustrue
closeOnClickOutsidenobooleanIf true any click outside step modal will cause closing the Tour. This is maybe useful if you set backdrop options to false and user have access to your site controls (for example navigation menu)false

DefaultCtrlBtns:

{
    done: {
    'en-EN': 'done',
    'ru-RU': 'закр',
    'fr-FR': 'fini',
    },
    prev: {
        'en-EN': 'prev',
        'ru-RU': 'пред',
        'fr-FR': 'préc'
    },
    next: {
        'en-EN': 'next',
        'ru-RU': 'след',
        'fr-FR': 'proch',
    },
}

Services

TourService methods:

NameArgsDescriptionReturn
main
startTourtour: TourIstart Tour (The only necessary to use this lib)void
prevStepCall initStep with previous stepNamevoid
nextStepCall initStep with next stepNamevoid
stopTourstop Tourvoid
additional
getStepStreamReturn the steps observable
resetStepstringnumber, TourStepIChange Step configuration
getHistoryreturn array of the indexes of the passed Steps
getStepByNamestringreturn the Step with the given stepName
getStepByIndexnumberreturn the Step with the given index
getLastStepreturn the last initialized Step
isRouteChangedreturn true if the route property of the current Step differs from the previous one
getTourStatusreturn true if Tour started

Directives

ngIfTour

This directive is similar to a structural one if it is used solo without <ng-tour-step-template>. In this case, ngIfTour adds provided Step template to the DOM after the Tour is started and removes it after the Tour is stoped. If ngIfTour is used along with <ng-tour-step-template> (no matter which element was marked with directive <ng-tour-step-template> or some other) it make only one thing it set 'customTemplate' property to true.

ngTourStep

This directive is required to mark Step target @Input | Required | Description | Values/Type --------|----------|-------------|------------- ngTourStep | yes | The value should be unique string | string

stepEvent

This directive binds listeners (with corresponded Tour methods) to Step's controls and emits corresponded events. It may come in handy if you want to master your own Step template. @Input | Required | Destination --------|----------|-------|------------- stepEvent | required | Possible values are 'next' , 'prev' and 'close'. Value predefines which handler will be implemented for the click event.

@OutputPropsDestination
next{tourEvent: string, step: number, history: number[]}Emit 'next' event with described props. The destination is the same as tourEvents have
prev{tourEvent: string, step: number, history number[]}Emit 'prev' event with described props.
break{tourEvent: string, step: number, history: number[]}Emit 'break' event with described props.
done{tourEvent: string, step: number, history: number[]}Emit 'done' event with described props.

Components

ng-tour-step-template

OutputPropsDescription
next{tourEvent: string, step: number, history: number[]}Emit 'next' event with described props. The destination is the same as tourEvents have
prev{tourEvent: string, step: number, history: number[]}Emit 'prev' event with described props.
break{tourEvent: string, step: number, history: number[]}Emit 'break' event with described props.
done{tourEvent: string, step: number, history: number[]}Emit 'done' event with described props.

SCSS Customizing

You can easily redefine styles of the provided Step template in any scss files importing in style.scss file of your project.

Main selectors

NameSelect
.tour-step-modalStep (includes provided template and custom template)
.tour-step-modal__contentprovided Step template
.tour-step-modal__headerprovided Step header (includes title and Close control)
.tour-btn-closeClose control
.tour-step-modal__titleprovided Step title
.tour-step-modal__bodyprovided Step body (includes Step description)
.tour-step-modal__descriptionprovided Step description
.tour-step-modal__footerprovided Step footer (includes Step controls Next, Prev, Done, and Counter)
.tour-step-modal__counterCounter
.tour-btn-nextNext control
.tour-btn-prevPrev control
.tour-btn-doneDone control
.tour-btnCommon selector for Step footer controls