All files / src/dom scroll-content.ts

0% Statements 0/34
100% Branches 0/0
0% Functions 0/10
0% Lines 0/34

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                                                                                                                                                                                                                                                                       
import {DomWatcher} from '../dom/dom-watcher';
 
/**
 *
 * Given a "scrollable" div, this class allows you to drag the
 * content area on desktop (non-touch devices).
 *
 * For example:
 * ```
 * [scroll-content]
 *    max-height: 400px
 *    overflow: scroll
 *
 * <div scroll-content>
 *    ... bunch of content that is very tall.
 * </div>
 * ```
 * The above would present a div with a scrollbar.  But
 * the user can't "drag" within the content and move it.
 *
 * This class will automatically set that up.
 *
 *
 * ```
 *   Array.from(document.querySelectorAll('[scroll-content]')).forEach(el => {
 *     new ScrollContent(el as HTMLElement);
 *   });
 * ```
 *
 */
export class ScrollContent {
  private container: HTMLElement;
  private watcher: DomWatcher;
  private links: HTMLElement[];
 
  private position: {
    x: number;
    y: number;
    scrollLeft: number;
    scrollTop: number;
  };
 
  constructor(container: HTMLElement) {
    this.container = container;
    this.watcher = new DomWatcher();
    this.links = [];
 
    this.position = {
      x: 0,
      y: 0,
      scrollLeft: 0,
      scrollTop: 0,
    };
 
    this.watcher.add({
      element: this.container,
      on: 'mousedown',
      eventOptions: {passive: true},
      callback: (e: MouseEvent) => this.handleMouseDown(e),
    });
 
    this.removeLinkDrag();
  }
 
  protected removeLinkDrag(): void {
    const links = Array.from(this.container.querySelectorAll('a'));
    for (const link of links) {
      link.draggable = false;
      // https://stackoverflow.com/questions/26356877/html5-draggable-false-not-working-in-firefox-browser
      // This is primarily required for firefox where draggble=false
      // doesn't work.
      this.watcher.add({
        element: link,
        on: ['dragstart'],
        callback: (e: MouseEvent) => {
          e.preventDefault();
        },
      });
      this.links.push(link);
    }
  }
 
  private handleMouseMove(e: MouseEvent) {
    const dx = e.clientX - this.position.x;
    const dy = e.clientY - this.position.y;
    this.container.scrollLeft = this.position.scrollLeft - dx;
    this.container.scrollTop = this.position.scrollTop - dy;
 
    for (const link of this.links) {
      link.style.pointerEvents = 'none';
    }
  }
 
  private handleMouseUp(e: MouseEvent) {
    this.container.classList.remove('active');
    this.watcher.removeById('mousemove');
    this.watcher.removeById('mouseup');
 
    for (const link of this.links) {
      link.style.removeProperty('pointer-events');
    }
  }
 
  private handleMouseDown(e: MouseEvent) {
    this.position.x = e.clientX;
    this.position.y = e.clientY;
    this.position.scrollLeft = this.container.scrollLeft;
    this.position.scrollTop = this.container.scrollTop;
    this.container.classList.add('active');
 
    this.watcher.add({
      element: this.container,
      on: 'mousemove',
      id: 'mousemove',
      eventOptions: {passive: true},
      callback: (e: MouseEvent) => this.handleMouseMove(e),
    });
 
    this.watcher.add({
      element: document,
      on: 'mouseup',
      id: 'mouseup',
      eventOptions: {passive: true},
      callback: (e: MouseEvent) => this.handleMouseUp(e),
    });
  }
 
  dispose() {
    this.watcher.dispose();
  }
}