All files / src/angular controller-horizontal-scroll-element.ts

0% Statements 0/28
0% Branches 0/22
0% Functions 0/11
0% Lines 0/28

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                                                                                                                                                                                                                                 
import {DomWatcher} from '../dom/dom-watcher';
import {
  HorizontalScrollElement,
  HorizontalScrollElementEvents,
} from '../dom/horizontal-scroll-element';
import type {INgDisposable} from './i-ng-disposable';
 
export interface HorizontalScrollElementControllerInitConfig {
  scrollSelector: string;
  leftAlign: boolean;
}
 
/**
 * A composition around HorizontalScrollElement.
 *
 * ```
 *
 * import { HorizontalScrollElementController } from 'degu/lib/angular/controller-horizontal-scroll-element';
 * app.controller('HorizontalScrollElementController', HorizontalScrollElementController);
 *
 * ```
 *
 *
 * @see HorizontalScrollElement for docs.
 */
export class HorizontalScrollElementController implements INgDisposable {
  private el: HTMLElement;
  private scrollElement: HTMLElement;
  private $scope: ng.IScope;
  private horizontalScroll: HorizontalScrollElement | null = null;
  private domWatcher: DomWatcher | null = null;
  static get $inject() {
    return ['$scope', '$element'];
  }
 
  constructor($scope: ng.IScope, $element: ng.IAugmentedJQuery) {
    this.$scope = $scope;
    this.el = $element[0];
    this.scrollElement = this.el;
    $scope.$on('$destroy', () => {
      this.dispose();
    });
  }
 
  public init(config: HorizontalScrollElementControllerInitConfig) {
    this.domWatcher = new DomWatcher();
 
    if (config.scrollSelector) {
      this.scrollElement = this.el.querySelector(config.scrollSelector)!;
      if (!this.scrollElement) {
        throw new Error(
          'An element with the selector ' +
            config.scrollSelector +
            ' was not found'
        );
      }
    }
 
    this.domWatcher.add({
      element: this.scrollElement,
      on: HorizontalScrollElementEvents.INDEX_CHANGE,
      callback: () => {
        if (!this.$scope.$$phase) {
          this.$scope.$apply();
        }
      },
    });
 
    window.setTimeout(() => {
      this.horizontalScroll = new HorizontalScrollElement({
        rootElement: this.scrollElement,
        leftAlign: config.leftAlign || false,
        resizeOnFirstEv: false,
        snapToClosest: true,
        slideDeltaValues: true,
        dragBounce: 0,
        delayResizeMs: 10,
      });
 
      const deltaSelector = this.el.getAttribute(
        'add-delta-value-elements-selector'
      );
      if (deltaSelector) {
        const group = Array.from(
          this.el.querySelectorAll(deltaSelector)
        ) as Array<HTMLElement>;
        this.horizontalScroll.addSlideDeltaValuesToElements([group]);
      }
    });
  }
 
  public prev(): void {
    this.horizontalScroll && this.horizontalScroll.prev();
  }
 
  public next(): void {
    this.horizontalScroll && this.horizontalScroll.next();
  }
 
  public isFirstSlide() {
    return this.horizontalScroll && this.horizontalScroll.isFirstSlide();
  }
 
  public isLastSlide() {
    return this.horizontalScroll && this.horizontalScroll.isLastSlide();
  }
 
  public dispose(): void {
    this.horizontalScroll && this.horizontalScroll.dispose();
    this.domWatcher && this.domWatcher.dispose();
  }
}