All files / src/lib/services NgxMediaService.ts

32.29% Statements 31/96
19.35% Branches 6/31
21.95% Functions 9/41
31.32% Lines 26/83

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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446                          11x   11x 11x     11x 11x                                                     11x             33x               33x                     33x                             33x               33x               33x                   66x                     139x       139x                                                                                                                 64x                                                                                                                                                                                                                                                                                                                                         44x                                                                                                                             8x 8x 8x 8x 7x         7x 7x                                 32x 32x      
/**
 * @module NgxMediaService
 * @description Provides utilities for managing media-related features such as color scheme detection,
 * window resize observation, and SVG injection.
 * @summary
 * This module exposes the `NgxMediaService` class, which includes methods for observing the
 * application's color scheme, handling window resize events, and dynamically injecting SVG content
 * into the DOM. It leverages Angular's dependency injection system and RxJS for reactive programming.
 *
 * Key exports:
 * - {@link NgxMediaService}: The main service class providing media-related utilities.
 */
 
import { ElementRef, Injectable, NgZone, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { fromEvent, Subject, Observable, BehaviorSubject, merge, of, timer } from 'rxjs';
import { distinctUntilChanged, map, shareReplay, takeUntil, tap, switchMap } from 'rxjs/operators';
import { IWindowResizeEvent } from '../engine/interfaces';
import { WindowColorScheme } from '../engine/types';
import { getWindow, getWindowDocument } from '../utils/helpers';
import { AngularEngineKeys, WindowColorSchemes } from '../engine/constants';
 
/**
 * @description Service for managing media-related features in an Angular application.
 * @summary
 * The `NgxMediaService` provides utilities for observing and managing media-related features
 * such as color scheme detection, window resize events, and dynamic SVG injection. It leverages
 * Angular's dependency injection system and RxJS for reactive programming.
 *
 * @class NgxMediaService
 * @example
 * // Inject the service into a component
 * constructor(private mediaService: NgxMediaService) {}
 *
 * // Observe the current color scheme
 * this.mediaService.colorScheme$.subscribe(scheme => {
 *   console.log('Current color scheme:', scheme);
 * });
 *
 * // Observe window resize events
 * this.mediaService.resize$.subscribe(dimensions => {
 *   console.log('Window dimensions:', dimensions);
 * });
 */
@Injectable({
  providedIn: 'root',
})
export class NgxMediaService {
  /**
   * @description Subject used to signal the destruction of the service.
   * @summary
   * This subject is used to complete observables and clean up resources when the service is destroyed.
   * It is utilized with the `takeUntil` operator to manage subscriptions.
   */
  private readonly destroy$ = new Subject<void>();
 
  /**
   * @description BehaviorSubject to track the window's dimensions.
   * @summary
   * This subject holds the current dimensions of the window (width and height) and emits updates
   * whenever the window is resized. It is used to provide the `resize$` observable.
   */
  private resizeSubject = new BehaviorSubject<IWindowResizeEvent>({
    width: this._window.innerWidth,
    height: this._window.innerHeight,
  });
 
  /**
   * @description Angular's NgZone service for running code outside Angular's zone.
   * @summary
   * This service is used to optimize performance by running certain operations outside
   * Angular's zone and bringing updates back into the zone when necessary.
   */
  private readonly angularZone: NgZone = inject(NgZone);
 
  /**
   * @description Angular's HttpClient service for making HTTP requests.
   * @summary
   * This service is used to fetch resources such as SVG files for injection into the DOM.
   */
  // private http = inject(HttpClient);
 
  /**
   * @description Tracks the current color scheme of the application.
   * @summary
   * This property holds the current color scheme (light, dark, or undefined) and is updated
   * whenever the color scheme changes.
   */
  private currentSchema: WindowColorScheme = WindowColorSchemes.undefined;
 
  /**
   * @description Observable that emits the current color scheme of the application.
   * @summary
   * This observable emits the current color scheme (light or dark) and updates whenever
   * the system's color scheme preference changes or the `DARK_PALETTE_CLASS` is toggled.
   */
  readonly colorScheme$: Observable<WindowColorScheme> = this.colorSchemeObserver();
 
  /**
   * @description Observable that emits the current dimensions of the window.
   * @summary
   * This observable emits the current dimensions of the window (width and height) and updates
   * whenever the window is resized.
   */
  readonly resize$: Observable<IWindowResizeEvent> = this.resizeSubject.asObservable();
 
  /**
   * @description Retrieves the global `window` object.
   * @summary
   * This getter provides access to the global `window` object, ensuring it is properly typed.
   *
   * @return {Window} The global `window` object.
   */
  private get _window(): Window {
    return getWindow() as Window;
  }
 
  /**
   * @description Retrieves the global `document` object.
   * @summary
   * This getter provides access to the global `document` object, ensuring it is properly typed.
   *
   * @return {Document} The global `document` object.
   */
  private get _document(): Document {
    return getWindowDocument() as Document;
  }
 
  darkModeEnabled(): boolean {
    return this._document.documentElement.classList.contains('has-dark-mode');
  }
 
  /**
   * @description Observes window resize events and emits the updated dimensions.
   * @summary
   * This method listens for window resize events and emits the new dimensions
   * (width and height) through the `resize$` observable. The resize events are
   * processed outside Angular's zone to improve performance, and updates are
   * brought back into the Angular zone to ensure change detection.
   *
   * @return {Observable<IWindowResizeEvent>} An observable that emits the window dimensions on resize.
   * @function windowResizeObserver
   * @example
   * mediaService.windowResizeObserver().subscribe(dimensions => {
   *   console.log('Window dimensions:', dimensions);
   * });
   */
  windowResizeObserver(): Observable<IWindowResizeEvent> {
    const win = this._window;
    this.angularZone.runOutsideAngular(() => {
      fromEvent(win, 'resize')
        .pipe(distinctUntilChanged(), takeUntil(this.destroy$), shareReplay(1))
        .subscribe(() => {
          const dimensions: IWindowResizeEvent = {
            width: win.innerWidth,
            height: win.innerHeight,
          };
          // update within the zone to reflect in Angular
          this.angularZone.run(() => this.resizeSubject.next(dimensions));
        });
    });
 
    return this.resize$;
  }
 
  /**
   * @description Observes the color scheme of the application.
   * @summary
   * This method observes changes in the system's color scheme preference (light or dark)
   * and the presence of the `DARK_PALETTE_CLASS` on the document's root element.
   * Optionally, it can toggle the dark mode class on a specific component.
   *
   * @param {ElementRef | HTMLElement} [component] - Optional component to toggle the dark mode class on.
   * @return {Observable<WindowColorScheme>} An observable that emits the current color scheme (`dark` or `light`).
   * @function colorSchemeObserver
   * @example
   * // Observe the color scheme globally
   * mediaService.colorSchemeObserver().subscribe(scheme => {
   *   console.log('Current color scheme:', scheme);
   * });
   *
   * // Observe and toggle dark mode on a specific component
   * const component = document.querySelector('.my-component');
   * mediaService.colorSchemeObserver(component).subscribe();
   */
  colorSchemeObserver(component?: ElementRef | HTMLElement): Observable<WindowColorScheme> {
    if (!this.darkModeEnabled()) return of(WindowColorSchemes.light);
    const win = this._window;
    const doc = this._document;
    const documentElement = doc.documentElement;
    const mediaFn = win.matchMedia(`(prefers-color-scheme: ${WindowColorSchemes.dark})`);
 
    Iif (component) {
      this.colorScheme$.subscribe((schema: WindowColorScheme) => {
        this.toggleClass(
          component,
          AngularEngineKeys.DARK_PALETTE_CLASS,
          schema === WindowColorSchemes.dark ? true : false,
        );
      });
    }
 
    return merge(
      of(mediaFn.matches ? WindowColorSchemes.dark : WindowColorSchemes.light),
      // observe media query changes
      new Observable<WindowColorScheme>((observer) => {
        this.angularZone.runOutsideAngular(() => {
          const mediaQuery = mediaFn;
          const subscription = fromEvent<MediaQueryListEvent>(mediaQuery, 'change')
            .pipe(
              map((event) => (event.matches ? WindowColorSchemes.dark : WindowColorSchemes.light)),
            )
            .subscribe((value) => {
              this.angularZone.run(() => observer.next(value));
            });
          return () => subscription.unsubscribe();
        });
      }),
      // observe ngx-dcf-palettedark class changes
      new Observable<WindowColorScheme>((observer) => {
        this.angularZone.runOutsideAngular(() => {
          const observerConfig = { attributes: true, attributeFilter: ['class'] };
          const mutationObserver = new MutationObserver(() => {
            const theme = documentElement.classList.contains(AngularEngineKeys.DARK_PALETTE_CLASS)
              ? WindowColorSchemes.dark
              : WindowColorSchemes.light;
            this.angularZone.run(() => observer.next(theme));
          });
          mutationObserver.observe(documentElement, observerConfig);
          // this.angularZone.run(() => observer.next(theme));
 
          // this.angularZone.run(() => observer.next(theme));
          return () => mutationObserver.disconnect();
        });
      }),
    ).pipe(
      distinctUntilChanged(),
      tap((scheme) => {
        const shouldAdd = scheme === WindowColorSchemes.dark;
        if (shouldAdd) {
          // always add when the emitted scheme is dark
          this.toggleClass(documentElement, AngularEngineKeys.DARK_PALETTE_CLASS, true);
        } else {
          // remove the class only if the previously stored schema was dark
          Iif (this.currentSchema === WindowColorSchemes.dark) {
            this.toggleClass(documentElement, AngularEngineKeys.DARK_PALETTE_CLASS, false);
          }
        }
        // store the latest schema value
        this.currentSchema = scheme;
      }),
      takeUntil(this.destroy$),
      shareReplay(1),
    );
  }
 
  /**
   * @description Observes the scroll state of the active page.
   * @summary
   * This method observes the scroll position of the active page's content and emits a boolean
   * indicating whether the scroll position exceeds the specified offset. It waits for a delay
   * before starting the observation to allow page transitions to complete.
   *
   * @param {number} offset - The scroll offset to compare against.
   * @param {number} [awaitDelay=500] - The delay in milliseconds before starting the observation.
   * @return {Observable<boolean>} An observable that emits `true` if the scroll position exceeds the offset, otherwise `false`.
   * @function observePageScroll
   * @example
   * mediaService.observePageScroll(100).subscribe(isScrolled => {
   *   console.log('Page scrolled past 100px:', isScrolled);
   * });
   */
  observePageScroll(offset: number, awaitDelay: number = 500): Observable<boolean> {
    // await delay for page change to complete
    return timer(awaitDelay).pipe(
      switchMap(
        () =>
          new Observable<boolean>((observer) => {
            const activeContent = this._document.querySelector(
              'ion-router-outlet .ion-page:not(.ion-page-hidden) ion-content',
            ) as HTMLIonContentElement;
            Iif (
              !(
                activeContent &&
                typeof (activeContent as HTMLIonContentElement).getScrollElement === 'function'
              )
            )
              return this.angularZone.run(() => observer.next(false));
 
            (activeContent as HTMLIonContentElement)
              .getScrollElement()
              .then((element: HTMLElement) => {
                const emitScrollState = () => {
                  const scrollTop = element.scrollTop || 0;
                  this.angularZone.run(() => observer.next(scrollTop > offset));
                };
                element.addEventListener('scroll', emitScrollState);
                emitScrollState();
                return () => element.removeEventListener('scroll', emitScrollState);
              });
          }),
      ),
      distinctUntilChanged(),
      takeUntil(this.destroy$),
      shareReplay(1),
    );
  }
 
  /**
   * @description Loads an SVG file and injects it into a target element.
   * @summary
   * This method fetches an SVG file from the specified path and injects its content
   * into the provided target element. The operation is performed outside Angular's
   * zone to improve performance, and the DOM update is brought back into the Angular
   * zone to ensure change detection.
   *
   * @param {string} path - The path to the SVG file.
   * @param {HTMLElement} target - The target element to inject the SVG content into.
   * @return {void}
   * @function loadSvgObserver
   * @example
   * const targetElement = document.getElementById('svg-container');
   * mediaService.loadSvgObserver('/assets/icons/icon.svg', targetElement);
   */
  loadSvgObserver(http: HttpClient, path: string, target: HTMLElement): void {
    this.angularZone.runOutsideAngular(() => {
      const svg$ = http
        .get(path, { responseType: 'text' })
        .pipe(takeUntil(this.destroy$), shareReplay(1));
      svg$.subscribe((svg) => {
        this.angularZone.run(() => {
          target.innerHTML = svg;
        });
      });
    });
  }
 
  /**
   * @description Determines if the current theme is dark mode.
   * @summary
   * Observes the `colorScheme$` observable and checks if the `DARK_PALETTE_CLASS` is present
   * on the document's root element or if the emitted color scheme is `dark`.
   *
   * @return {Observable<boolean>} An observable that emits `true` if dark mode is active, otherwise `false`.
   * @function isDarkMode
   * @example
   * mediaService.isDarkMode().subscribe(isDark => {
   *   console.log('Dark mode active:', isDark);
   * });
   */
  isDarkMode(): Observable<boolean> {
    if (!this.darkModeEnabled()) return of(false);
    const documentElement = this._document.documentElement;
    return this.colorScheme$.pipe(
      map(
        (scheme) =>
          documentElement.classList.contains(AngularEngineKeys.DARK_PALETTE_CLASS) ||
          scheme === WindowColorSchemes.dark,
      ),
      distinctUntilChanged(),
      shareReplay(1),
      takeUntil(this.destroy$),
    );
  }
 
  /**
   * @description Toggles dark mode for a specific component.
   * @summary
   * Subscribes to the `colorScheme$` observable and adds or removes the `DARK_PALETTE_CLASS`
   * on the provided component based on the emitted color scheme.
   *
   * @param {ElementRef | HTMLElement} component - The target component to toggle dark mode on.
   * @return {void}
   * @function setDarkMode
   * @example
   * const component = document.querySelector('.my-component');
   * mediaService.setDarkMode(component);
   */
  setDarkMode(component: ElementRef | HTMLElement): void {
    this.colorScheme$.subscribe((scheme) => {
      this.toggleClass(
        component,
        AngularEngineKeys.DARK_PALETTE_CLASS,
        scheme === WindowColorSchemes.dark ? true : false,
      );
    });
  }
 
  /**
   * @description Add or remove a CSS class on one or multiple elements.
   * @summary
   * Accepts an ElementRef, HTMLElement, or array of mixed elements and will add
   * or remove the provided `className` depending on the `add` flag. Operates
   * defensively: resolves ElementRef.nativeElement when needed and ignores
   * falsy entries.
   *
   * @param {(ElementRef | HTMLElement | unknown[])} component - Single element,
   * ElementRef, or array of elements to update.
   * @param {string} className - CSS class name to add or remove.
   * @param {boolean} [add=true] - Whether to add (true) or remove (false) the class.
   * @return {void}
   * @function toggleClass
   * @example
   * // Add a class to a single element
   * mediaService.toggleClass(element, 'active', true);
   *
   * // Remove a class from multiple elements
   * mediaService.toggleClass([element1, element2], 'hidden', false);
   */
  toggleClass(
    component: ElementRef | HTMLElement | unknown[],
    className: string,
    add: boolean = true,
  ): void {
    const components = Array.isArray(component) ? component : [component];
    components.forEach((element) => {
      if ((element as ElementRef)?.nativeElement) element = (element as ElementRef).nativeElement;
      if (element instanceof HTMLElement) {
        switch (add) {
          case true:
            (element as HTMLElement).classList.add(className);
            break;
          case false:
            (element as HTMLElement).classList.remove(className);
            break;
        }
      }
    });
  }
 
  /**
   * @description Clean up internal subscriptions and observers used by the service.
   * @summary
   * Triggers completion of the internal `destroy$` subject so that any
   * Observables created with `takeUntil(this.destroy$)` will complete and
   * resources are released.
   *
   * @return {void}
   * @function destroy
   */
  destroy(): void {
    this.destroy$.next();
    this.destroy$.complete();
  }
}