All files / app/directive resize-handle.directive.ts

31.7% Statements 26/82
29.16% Branches 14/48
27.77% Functions 5/18
31.25% Lines 20/64

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 14487x                         87x         87x 2x     2x     2x           2x 2x 2x 2x 2x         2x 2x 2x 2x                 3x       1x 1x 1x       1x                                                                                                                                                                  
import {
  AfterViewInit,
  ChangeDetectorRef,
  Directive,
  ElementRef,
  HostBinding,
  HostListener,
  Input,
  NgZone,
  OnDestroy
} from '@angular/core';
import { defer } from 'lodash-es';
import { ConfigService } from '../service/config.service';
import { relativeX, relativeY } from '../util/math';
 
@Directive({
    selector: '[appResizeHandle]',
})
export class ResizeHandleDirective implements AfterViewInit, OnDestroy {
  @HostBinding('style.cursor') cursor = 'auto';
 
  @Input()
  hitArea = 24;
 
  @Input()
  appResizeHandle?: boolean | string = true;
 
  @Input()
  child?: HTMLElement;
 
  @HostBinding('class.resize-dragging')
  dragging = false;
  x = 0;
  y = 0;
  width = 0;
  height = 0;
 
  resizeObserver?: ResizeObserver;
 
  constructor(
    private config: ConfigService,
    private el: ElementRef,
    private zone: NgZone,
    private cd: ChangeDetectorRef,
  ) { }
 
  get resizeCursor() {
    return this.config.mobile ? 'row-resize' : 'se-resize';
  }
 
  @HostBinding('class.resize-handle')
  get enabled() {
    return this.appResizeHandle !== 'false' && this.appResizeHandle !== false;
  }
 
  ngAfterViewInit() {
    Iif (!this.enabled) return;
    this.resizeObserver = window.ResizeObserver && new ResizeObserver(() => this.shrinkContainer()) || undefined;
    Iif (this.child) this.resizeObserver?.observe(this.child);
  }
 
  ngOnDestroy() {
    this.resizeObserver?.disconnect();
  }
 
  shrinkContainer() {
    if (!this.child) return;
    this.el.nativeElement.style.width = this.child.style.width;
    this.el.nativeElement.style.height = this.child.style.height;
  }
 
  @HostListener('fullscreenchange')
  onFullscreenChange() {
    this.cd.markForCheck();
  }
 
  @HostListener('pointerdown', ['$event'])
  onPointerDown(event: PointerEvent) {
    if (!this.enabled) return;
    if (event.button) return;
    if (this.hit(event)) {
      this.dragging = true;
      this.cursor = 'grabbing';
      this.x = event.clientX;
      this.y = event.clientY;
      this.width = this.el.nativeElement.offsetWidth;
      this.height = this.el.nativeElement.offsetHeight;
      event.preventDefault();
      event.stopPropagation();
      event.stopImmediatePropagation();
    }
  }
 
  @HostListener('window:pointermove', ['$event'])
  onPointerMove(event: PointerEvent) {
    if (!this.enabled) return;
    if (this.dragging) {
      this.zone.run(() => {
        const dx = event.clientX - this.x;
        const dy = event.clientY - this.y;
        this.setWidth((this.width + dx) + 'px');
        this.setHeight((this.height + dy) + 'px');
        event.preventDefault();
        event.stopPropagation();
        event.stopImmediatePropagation();
      });
    } else {
      const cursor = this.hit(event) ? this.resizeCursor : 'auto';
      if (this.cursor !== cursor) {
        this.zone.run(() => this.cursor = cursor);
      }
    }
  }
 
  @HostListener('window:pointerup', ['$event'])
  onPointerUp(event: PointerEvent) {
    if (!this.enabled) return;
    if (this.dragging) {
      this.cursor = this.hit(event) ? this.resizeCursor : 'auto';
      event.preventDefault();
      event.stopPropagation();
      event.stopImmediatePropagation();
      defer(() => this.dragging = false);
    }
  }
 
  private hit(event: PointerEvent) {
    const x = this.el.nativeElement.offsetWidth - relativeX(event.clientX, this.el.nativeElement);
    const y = this.el.nativeElement.offsetHeight - relativeY(event.clientY, this.el.nativeElement);
    return x + y < this.hitArea;
  }
 
  private setWidth(width: string) {
    this.el.nativeElement.style.width = width;
    if (this.child) this.child.style.width = width;
  }
 
  private setHeight(height: string) {
    this.el.nativeElement.style.height = height;
    if (this.child) this.child.style.height = height;
  }
 
}