All files / app/directive resize.directive.ts

12.12% Statements 12/99
4% Branches 3/75
11.76% Functions 2/17
12.82% Lines 10/78

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 15868x     68x                                           1x 1x   1x     1x 1x 1x     1x 1x                                                                                                                                                                                                                                                
import { Directive, ElementRef, HostBinding, HostListener, Input, NgZone } from '@angular/core';
 
@Directive({ selector: '[appResize]' })
export class ResizeDirective {
 
  @Input('appResize')
  enabled?: boolean;
 
  @HostBinding('style.z-index')
  get zIndex() {
    return this.dirty ? 1 : 0;
  }
 
  @HostBinding('style.width')
  get width() {
    if (!this.enabled || !this.dim) return this.el.nativeElement.style.width;
    return this.dim.x + 'px'
  }
 
  @HostBinding('style.height')
  get height() {
    if (!this.enabled || !this.dim) return this.el.nativeElement.style.height;
    return this.dim.y + 'px';
  }
 
  minPx = 2;
  zoom = 1;
  dim?: {x: number, y: number};
  oldZoom = 1;
  dragStart?: {x: number, y: number};
  startDim?: {x: number, y: number};
  dragging = false;
  wasDragging = false;
  dirty = false;
 
  constructor(
    private el: ElementRef,
    private zone: NgZone,
  ) { }
 
  @HostListener('mousedown', ['$event'])
  onMousedown(e: MouseEvent) {
    if (this.enabled === false) return;
    if (e.button) return;
    e.preventDefault();
    this.oldZoom = this.zoom;
    this.dragStart = {
      x: e.clientX,
      y: e.clientY,
    };
    this.startDim = {
      x: Math.floor(this.el.nativeElement.offsetWidth),
      y: Math.floor(this.el.nativeElement.offsetHeight),
    };
  }
 
  @HostListener('touchstart', ['$event'])
  onTouchstart(e: TouchEvent) {
    if (this.enabled === false) return;
    if (e.touches.length != 2) return;
    this.zone.run(() => {
      e.preventDefault();
      this.oldZoom = this.zoom;
      const t1x = e.touches.item(0)!.clientX;
      const t1y = e.touches.item(0)!.clientY;
      const t2x = e.touches.item(1)?.clientX || 2 * t1x;
      const t2y = e.touches.item(1)?.clientY || 2 * t1y;
      this.dragStart = {
        x: Math.abs(t1x - t2x),
        y: Math.abs(t1y - t2y),
      };
      this.startDim = {
        x: Math.floor(this.el.nativeElement.offsetWidth),
        y: Math.floor(this.el.nativeElement.offsetHeight),
      };
    });
  }
 
  @HostListener('click', ['$event'])
  onClick(e: MouseEvent) {
    if (this.enabled === false) return;
    if (this.wasDragging) {
      e.preventDefault();
    }
    this.wasDragging = false;
  }
 
  @HostListener('window:mousemove', ['$event'])
  onMousemove(e: MouseEvent) {
    if (this.enabled === false) return;
    if (!this.dragStart || !this.startDim) return;
    if (!this.dragging) {
      if (Math.abs(e.clientX - this.dragStart.x) < this.minPx &&
          Math.abs(e.clientY - this.dragStart.y) < this.minPx) {
        return;
      }
      this.zone.run(() => {
        this.dragging = true;
        this.wasDragging = true;
      });
    }
    this.zone.run(() => {
      if (!this.dragStart || !this.startDim) return;
      e.preventDefault();
      const dx = (e.clientX - this.dragStart.x) / this.startDim.x;
      const dy = (e.clientY - this.dragStart.y) / this.startDim.y;
      const l = (dx + dy) / 2;
      this.dim ??= { ...this.startDim };
      this.dim.x = Math.floor(this.startDim.x * (1 + l));
      this.dim.y = this.dim.x * this.startDim.y / this.startDim.x;
      this.dirty = true;
    });
  }
 
  @HostListener('window:touchmove', ['$event'])
  onTouchmove(e: TouchEvent) {
    if (this.enabled === false) return;
    if (!this.dragStart || !this.startDim) return;
    this.zone.run(() => {
      if (!this.dragStart || !this.startDim) return;
      if (!this.dragging) {
        this.dragging = true;
        this.wasDragging = true;
      }
      e.preventDefault();
      const t1 = e.touches.item(0)!;
      const t2 = e.touches.item(1) || t1;
      const dims = {
        w: Math.abs(t1.clientX - t2.clientX),
        h: Math.abs(t1.clientY - t2.clientY),
      };
      const dx = (dims.w - this.dragStart.x) / this.startDim.x;
      const dy = (dims.h - this.dragStart.y) / this.startDim.y;
      const l = (dx + dy) / 2;
      this.dim ??= { ...this.startDim };
      this.dim.x = Math.floor(this.startDim.x * (1 + l));
      this.dim.y = this.dim.x * this.startDim.y / this.startDim.x;
      this.dirty = true;
    });
  }
 
  @HostListener('window:contextmenu', ['$event'])
  @HostListener('window:mouseup', ['$event'])
  @HostListener('window:touchend', ['$event'])
  @HostListener('window:touchcancel', ['$event'])
  onCancel(e: Event) {
    if (this.enabled === false) return;
    delete this.dragStart;
    if (this.dragging) {
      this.zone.run(() => {
        this.dragging = false;
        e.preventDefault();
      });
    }
  }
 
}