Telemachy

Table of contents

Getting started

Installation

yarn add telemachy npm install --save telemachy

Import TelemachyModule in your root module (make sure the RouterModule is loaded, Telemachy uses it to automatically start the tour for any compontent that a user navigates to):

import { NgModule } from '@angular/core' ; import { BrowserModule } from '@angular/platform-browser' ; import { RouterModule } from '@angular/router' ; import { TelemachyModule } from 'telemachy' ; import { AppComponent } from './app.component' ; ({ imports: [BrowserModule, RouterModule.forRoot([]), TelemachyModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {}

Finally, add the <telemachy-tour> component to your root app component (or one that is always loaded when you want to display a tour):

import { Component } from '@angular/core' ; ({ selector: 'app-root' , template: `<telemachy-tour></telemachy-tour><router-outlet></router-outlet>` }) export class RootComponent {}

Usage

In its easiest form, you can use Telemachy just by implementing the HasGuidedTour interface:

({ selector: 'app-complex-component' , templateUrl: './complex-component.component.html' }) export class ComplexComponent implements HasGuidedTour { tourAutoStart() { return true ;} getTour():TourStep[] { return [ new YoutubeTourStep( 'l9xU1WpdmVY' ), new ElementTourStep( '#complex-thing' , 'Here, I will explain this.' ), new ElementTourStep( '#second-complex-thing' , 'Then, I will explain this.' ), new HTMLTourStep( `Finally, we are <em>done</em>!` ) ]; } }

By implementing the optional tourAutoStart() method (and having it return true ), Telemachy will automatically start this component's tour whenever it becomes active through the angular router. You can also explicitly say it should be started by calling TelemachyService.startTour(this) . In both cases, Telemachy will only show the tour if the persistency layer says the user should see it.

Persistency

By default, Telemachy will always show a user the tour on each visit. If you want to save if a user has already seen a tour, you should provide an alternate implementation for the TourPersistency service.

Telemachy comes with an alternate implementation that uses localstorage to save this state. This implementation depends on phenomnomnominal/angular-2-local-storage. You can use this as follows:

import { LocalStorageModule, LocalStorageService } from 'angular-2-local-storage' ; import { TelemachyModule, TourPersistency, LocalstorageTourPersistencyFactory } from 'telemachy' ; ({ imports: [BrowserModule, LocalStorageModule.withConfig(), TelemachyModule], declarations: [AppComponent], bootstrap: [AppComponent], providers: [ {provide: TourPersistency, useFactory: LocalstorageTourPersistencyFactory, deps: [LocalStorageService]} ] }) export class AppModule {}

We recommend you replace the TourPersistency service with an implementation that saves the user's state in a remote system, for instance through a REST API.

Restarting a tour

You can restart the tour for a currently visible component by calling TelemachyService.restartTour() . To check if there is a currently visible component that has a tour, use TelemachyService.canRestart() .

API

TelemachyService

class TelemachyService { startTour(component: HasGuidedTour): void ; canRestart(): boolean ; restartTour(): void ; addEventListener( type : string , listener: () => void ): void ; removeEventListener( type : string , listener: () => void ): void ; canGoBack(): boolean ; canFinish(): boolean ; finish(): void ; skip(): void ; next(): void ; previous(): void ; private reset(); private emit(); subscribeStep(generatorOrNext?: any , error?: any , complete?: any ): Subscription; readonly progress: number ; readonly total: number ; }

TourPersistency

class TourPersistency { shouldStart(componentName: string ): Observable< boolean >; finish(componentName: string ): void ; skip(componentName: string ): void ; }

YoutubeTourStep

class YoutubeTourStep extends TourStep { video: string ; constructor ( video: string ); }

HTMLTourStep

class HTMLTourStep extends TourStep { body: string ; constructor ( body: string ); }

ElementTourStep

class ElementTourStep extends TourStep { domElement: Element; body: string ; constructor ( selector: string , body: string , async ?: boolean ); readonly top: string; readonly bottom: string; readonly left: string; readonly right: string; readonly documentHeight: string; readonly documentWidth: string; readonly toBottom: string; readonly toRight: string; readonly fromLeft: string; readonly fromTop: string; readonly height: string; readonly width: string; }

If async is true , the ElementTourStep will attempt to find the element on the page after it has been initialized. If the element isn't available by the time the step should be rendered, the step will be skipped.

Event Listeners

It is possible to add event listeners that are fired add the start of the tour or at the end of the tour when the finished/skipped button is clicked. Possible types: start or end