All files / app/component/action/action-list action-list.component.ts

40.65% Statements 37/91
45.94% Branches 17/37
38.09% Functions 8/21
41.33% Lines 31/75

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  77x                                     77x 77x       77x 77x 77x 77x                   77x             9x   9x   9x             9x       9x 9x     9x 9x 9x 9x 9x 9x 9x       7x       8x 8x       77x 7x     9x                               8x                   77x 8x       77x                                                                                                                                              
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { FakeLinkDirective } from '../../../directive/fake-link.directive';
import { TemplatePortal } from '@angular/cdk/portal';
import { KeyValuePipe } from '@angular/common';
import {
  AfterViewInit,
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  HostListener,
  Input,
  NgZone,
  OnChanges,
  SimpleChanges,
  TemplateRef,
  ViewChild,
  ViewContainerRef
} from '@angular/core';
import { defer } from 'lodash-es';
import { Subscription } from 'rxjs';
import { TitleDirective } from '../../../directive/title.directive';
import { Ref, writeRef } from '../../../model/ref';
import { Action } from '../../../model/tag';
import { ActionService } from '../../../service/action.service';
import { ConfigService } from '../../../service/config.service';
import { downloadRef, downloadUrl } from '../../../util/download';
import { memo, MemoCache } from '../../../util/memo';
import { ConfirmActionComponent } from '../confirm-action/confirm-action.component';
import { InlineButtonComponent } from '../inline-button/inline-button.component';
import { ProxyService } from '../../../service/api/proxy.service';
 
@Component({
  selector: 'app-action-list',
  templateUrl: './action-list.component.html',
  styleUrl: './action-list.component.scss',
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [FakeLinkDirective, ConfirmActionComponent, TitleDirective, InlineButtonComponent, KeyValuePipe]
})
export class ActionListComponent implements AfterViewInit, OnChanges {
 
  @Input()
  ref!: Ref;
  @Input()
  repostRef?: Ref;
  @Input()
  showDownload = true;
  @Input()
  mediaAttachment = '';
  @Input()
  groupedActions?: { [key: string]: Action[] } = {};
  @Input()
  groupedAdvancedActions?: { [key: string]: Action[] };
 
  @ViewChild('actionsMenu')
  actionsMenu!: TemplateRef<any>;
 
  hiddenActions = 0;
  overlayRef?: OverlayRef;
 
  private overlayEvents?: Subscription;
  private overlayResizeObserver? = window.ResizeObserver && new ResizeObserver(() => this.overlayRef?.updatePosition()) || undefined;
  private resizeObserver? = window.ResizeObserver && new ResizeObserver(() => this.onResize()) || undefined;
 
  constructor(
    private config: ConfigService,
    private proxy: ProxyService,
    private acts: ActionService,
    private overlay: Overlay,
    private el: ElementRef<HTMLElement>,
    private viewContainerRef: ViewContainerRef,
    private zone: NgZone,
  ) { }
 
  ngAfterViewInit() {
    this.resizeObserver?.observe(this.el.nativeElement!.parentElement!);
  }
 
  ngOnChanges(changes: SimpleChanges) {
    MemoCache.clear(this);
    defer(() => this.onResize());
  }
 
  @memo
  get advanced() {
    return this.groupedAdvancedActions && Object.keys(this.groupedAdvancedActions as any).length > 0;
  }
 
  apply$ = (actions: Action[]) => () => {
    this.closeAdvanced();
    return this.acts.apply$(actions, this.ref, this.repostRef);
  }
 
  download() {
    downloadRef(writeRef(this.ref));
  }
 
  downloadMedia() {
    if (!this.mediaAttachment) return;
    downloadUrl(this.proxy, this.mediaAttachment);
  }
 
  @HostListener('window:resize')
  onResize() {
    Eif (!this.actions) return;
    this.measureVisible();
  }
 
  measureVisible() {
    if (!this.actions) return;
    this.hiddenActions = this.actions - this.visible;
  }
 
  @memo
  get actions() {
    return Object.keys(this.groupedActions as any).length;
  }
 
  @memo
  get actionWidths() {
    const el = this.el.nativeElement;
    const result: number[] = [];
    for (let i = 0; i < el.children.length; i++) {
      const e = el.children[i] as HTMLElement;
      const s = getComputedStyle(e);
      result.push(e.offsetWidth + parseInt(s.marginLeft) + parseInt(s.marginRight));
    }
    return result;
  }
 
  get visible() {
    if (this.config.mobile) return this.actions;
    const el = this.el.nativeElement;
    const parentWidth = el.parentElement!.offsetWidth;
    let result = 0;
    let childWidth = 0;
    for (let i = 0; i < el.parentElement!.children.length - 1; i++) {
      const e = el.parentElement!.children[i] as HTMLElement;
      const s = getComputedStyle(e);
      childWidth += e.offsetWidth + parseInt(s.marginLeft) + parseInt(s.marginRight);
    }
    for (const w of this.actionWidths) {
      childWidth += w;
      if (childWidth < parentWidth) result++;
    }
    return result;
  }
 
  showAdvanced(event: MouseEvent) {
    this.closeAdvanced();
    const origin = event.detail === 0
      ? event.currentTarget as HTMLElement
      : {x: event.x, y: event.y};
    defer(() => {
      const positionStrategy = this.overlay.position()
        .flexibleConnectedTo(origin)
        .withPositions([{
          originX: 'center',
          originY: 'center',
          overlayX: 'start',
          overlayY: 'top',
        }]);
      this.overlayRef = this.overlay.create({
        positionStrategy,
        scrollStrategy: this.overlay.scrollStrategies.close(),
      });
      this.overlayRef.attach(new TemplatePortal(this.actionsMenu, this.viewContainerRef));
      this.overlayEvents = this.overlayRef.outsidePointerEvents().subscribe((event: MouseEvent) => {
        switch (event.type) {
          case 'click':
          case 'pointerdown':
          case 'touchstart':
          case 'mousedown':
          case 'contextmenu':
            this.zone.run(() => this.closeAdvanced());
        }
      });
      this.overlayResizeObserver?.observe(this.overlayRef.overlayElement);
    });
  }
 
  closeAdvanced() {
    if (this.overlayRef?.overlayElement) this.overlayResizeObserver?.unobserve(this.overlayRef?.overlayElement);
    this.overlayRef?.dispose();
    this.overlayEvents?.unsubscribe();
    this.overlayRef = undefined;
    this.overlayEvents = undefined;
    return false;
  }
}