Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { FlexibleConnectedPositionStrategy, Overlay, OverlayOutsideClickDispatcher, OverlayRef, PositionStrategy } from '@angular/cdk/overlay'; import { ComponentPortal } from '@angular/cdk/portal'; import { ElementRef, inject, Injectable, Injector, signal, DOCUMENT } from '@angular/core'; import { makeOverlay, makePositionStrategy } from '@siemens/element-ng/common'; import { ResizeObserverService } from '@siemens/element-ng/resize-observer'; import { map, merge, Subject, Subscription, tap, throttleTime } from 'rxjs'; import { SiTourHighlightComponent } from './si-tour-highlight.component'; import { SI_TOUR_TOKEN, TourAction, TourToken } from './si-tour-token.model'; import { SiTourComponent } from './si-tour.component'; import { TourOptions, TourStep } from './si-tour.model'; @Injectable({ providedIn: 'root' }) export class SiTourService { private injector = inject(Injector); private resizeObserver = inject(ResizeObserverService); private overlay = inject(Overlay); private outsideClickDispatcher = inject(OverlayOutsideClickDispatcher); private overlayRefHighlight?: OverlayRef; private overlayRef?: OverlayRef; private portal?: ComponentPortal<SiTourComponent>; private options: TourOptions = {}; private steps: TourStep[] = []; private currentStepIndex = -1; private currentStep?: TourStep; private active = false; private tourToken: TourToken = { currentStep: new Subject(), blocked: signal(false), positionChange: new Subject(), sizeChange: new Subject(), control: new Subject() }; private positionChangeSub?: Subscription; private resizeSub?: Subscription; private document = inject(DOCUMENT); constructor() { this.tourToken.control.subscribe(action => this.controlAction(action)); } /** * Event triggered after the tour is completed. */ readonly onTourComplete = new Subject<void>(); /** * Event triggered after the tour is cancelled, either through skip button or closing the tour dialog. */ readonly onTourCancel = new Subject<void>(); /** * Sets options for the whole tour. */ setOptions(options: TourOptions): void { Object.assign(this.options, options); } /** * Add steps to the tour */ addSteps(steps: TourStep[]): void { this.steps.push(...steps); } /** * Clear all steps */ clearSteps(): void { this.steps = []; this.currentStepIndex = -1; } /** * Start the tour */ start(): void { this.makeStep(0); } /** * Finish the tour */ complete(): void { this.controlAction('complete'); } private async controlAction(action: TourAction): Promise<void> { switch (action) { case 'next': this.makeStep(this.currentStepIndex + 1); break; case 'back': this.makeStep(this.currentStepIndex - 1); break; case 'cancel': this.hide(); this.onTourCancel.next(); break; case 'complete': this.hide(); this.onTourComplete.next(); break; } } private async makeStep(index: number): Promise<void> { Iif (index < 0 || index >= this.steps.length) { return; } this.active = true; await this.handleBeforeNextPromise(); this.currentStepIndex = index; const step = this.steps[this.currentStepIndex]; this.currentStep = Object.assign({}, this.options?.defaultStepOptions ?? {}, step); Iif (this.currentStep.beforeShowPromise) { this.tourToken.blocked.set(true); await this.currentStep.beforeShowPromise(); } Iif (!this.active) { // tour could have been cancelled before beforeShowPromise completes return; } const anchorElement = this.getElement(step.attachTo?.element); this.scrollIntoView(this.currentStep, anchorElement); const anchorElementRef = this.isElementVisible(anchorElement) ? new ElementRef(anchorElement!) : undefined; this.makeOverlay(anchorElementRef); this.handleResizeSubscription(anchorElementRef); this.tourToken.currentStep.next({ step, stepNumber: this.currentStepIndex + 1, totalSteps: this.steps.length, anchor: anchorElementRef }); this.tourToken.blocked.set(false); } private scrollIntoView(step: TourStep, element?: HTMLElement): void { if (!element || !(step.scrollTo || step.scrollToHandler)) { return; } if (step.scrollToHandler) { step.scrollToHandler(element); } else { element?.scrollIntoView(step.scrollTo); } } private async handleBeforeNextPromise(): Promise<void> { Iif (!this.active) { return; } Iif (this.currentStep?.beforeNextPromise) { this.tourToken.blocked.set(true); await this.currentStep.beforeNextPromise(); this.tourToken.blocked.set(false); } } private makeOverlay(anchorElement: ElementRef<HTMLElement> | undefined): void { const strategy = makePositionStrategy(anchorElement, this.overlay, 'auto'); this.handlePositionChangeSubscription(strategy, anchorElement); Iif (this.overlayRef) { this.overlayRef.updatePositionStrategy(strategy); // This moves the dispatcher to the top, allowing it to catch other open overlays. // Much lighter than to detach and re-attach the portal, not re-creating the si-tour // component for each step this.outsideClickDispatcher.remove(this.overlayRef); this.outsideClickDispatcher.add(this.overlayRef); return; } const componentInjector = Injector.create({ providers: [{ provide: SI_TOUR_TOKEN, useValue: this.tourToken }], parent: this.injector }); // first the highlighter, const hlPortal = new ComponentPortal(SiTourHighlightComponent, undefined, componentInjector); this.overlayRefHighlight = this.overlay.create({ positionStrategy: this.overlay.position().global() }); this.overlayRefHighlight.attach(hlPortal); // then the dialog this.portal = new ComponentPortal(SiTourComponent, undefined, componentInjector); this.overlayRef = makeOverlay(strategy, this.overlay, true); // needs a subscriber, otherwise events will be ignored and the .backdrop CSS hack doesn't help this.overlayRef.outsidePointerEvents().subscribe(); this.overlayRef.attach(this.portal); } private handleResizeSubscription(anchorElement: ElementRef<HTMLElement> | undefined): void { this.resizeSub?.unsubscribe(); this.resizeSub = undefined; if (anchorElement) { this.resizeSub = merge( // this catches edge cases e.g. with side-panel this.resizeObserver.observe(this.document.body, 20, false), this.resizeObserver.observe(anchorElement.nativeElement, 20, false) ) .pipe( throttleTime(20, undefined, { trailing: true }), map(() => undefined), tap(() => { if (!this.isElementVisible(anchorElement?.nativeElement)) { // repositions to center if anchor disappears this.makeOverlay(undefined); } else { this.overlayRef?.updatePosition(); } }) ) .subscribe(this.tourToken.sizeChange); } } private isElementVisible(element: HTMLElement | undefined): boolean { const rect = element?.getBoundingClientRect(); return !!rect?.width && !!rect.height; } private handlePositionChangeSubscription( strategy: PositionStrategy, anchor?: ElementRef<HTMLElement> ): void { this.positionChangeSub?.unsubscribe(); if (anchor && strategy instanceof FlexibleConnectedPositionStrategy) { this.positionChangeSub = strategy.positionChanges .pipe(map(change => ({ change, anchor }))) .subscribe(this.tourToken.positionChange); } else E{ this.tourToken.positionChange.next(undefined); } } private getElement( selectorOrElement?: string | HTMLElement | (() => string | HTMLElement) ): HTMLElement | undefined { Iif (typeof selectorOrElement === 'function') { selectorOrElement = selectorOrElement(); } Iif (!selectorOrElement) { return undefined; } if (typeof selectorOrElement === 'string') { const element = this.document.querySelector<HTMLElement>(selectorOrElement); return element ?? undefined; } return selectorOrElement; } private async hide(): Promise<void> { await this.handleBeforeNextPromise(); this.currentStepIndex = -1; this.currentStep = undefined; this.active = false; this.resizeSub?.unsubscribe(); this.positionChangeSub?.unsubscribe(); this.overlayRef?.dispose(); this.overlayRef = undefined; this.overlayRefHighlight?.dispose(); this.overlayRefHighlight = undefined; } } |