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

55.26% Statements 42/76
44.18% Branches 19/43
43.47% Functions 10/23
57.35% Lines 39/68

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 18776x                 76x       76x 76x 76x                           76x 20x             20x   20x   20x   20x   20x   20x   20x   20x       20x   20x   20x     20x 20x         20x     20x 20x 20x 20x               110x         4x 4x 4x                         4x       110x 110x             110x 110x                           121x         19x 19x 4x                         20x                                                                          
import { DestroyRef, inject, Component, forwardRef, Input, OnInit, QueryList, ViewChildren, ChangeDetectionStrategy } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Router } from '@angular/router';
import { DateTime } from 'luxon';
import { catchError, forkJoin, Observable, of } from 'rxjs';
import { HasChanges } from '../../../guard/pending-changes.guard';
import { Ext } from '../../../model/ext';
import { Page } from '../../../model/page';
import { Ref } from '../../../model/ref';
import { score } from '../../../mods/vote';
import { AccountService } from '../../../service/account.service';
import { RefService } from '../../../service/api/ref.service';
import { Store } from '../../../store/store';
import { LoadingComponent } from '../../loading/loading.component';
import { PageControlsComponent } from '../../page-controls/page-controls.component';
import { RefComponent } from '../ref.component';
 
@Component({
  selector: 'app-ref-list',
  templateUrl: './ref-list.component.html',
  styleUrls: ['./ref-list.component.scss'],
  host: { 'class': 'ref-list' },
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [
    forwardRef(() => RefComponent),
    PageControlsComponent,
    LoadingComponent,
  ],
})
export class RefListComponent implements OnInit, HasChanges {
  private destroyRef = inject(DestroyRef);
 
  @Input()
  hide?: number[];
  @Input()
  plugins?: string[];
  @Input()
  showPageLast = true;
  @Input()
  showAlarm = true;
  @Input()
  pageControls = true;
  @Input()
  emptyMessage = $localize`No results found`;
  @Input()
  showToggle = true;
  @Input()
  expandInline = false;
  @Input()
  showVotes = false;
  @Input()
  hideNewZeroVoteScores = true;
  @Input()
  newRefs$?: Observable<Ref | undefined>;
  @Input()
  insertNewAtTop = false;
  @Input()
  showPrev = true;
 
  @ViewChildren(RefComponent)
  list?: QueryList<RefComponent>;
 
  pinned: Ref[] = [];
  newRefs: Ref[] = [];
 
  private _page?: Page<Ref>;
  private _ext?: Ext;
  private _expanded?: boolean;
  private _cols = 0;
 
  constructor(
    private accounts: AccountService,
    private router: Router,
    private store: Store,
    private refs: RefService,
  ) { }
 
  saveChanges() {
    return !this.list?.find(r => !r.saveChanges());
  }
 
  get ext() {
    return this._ext;
  }
 
  @Input()
  set ext(value: Ext | undefined) {
    this._ext = value;
    if (!value?.config?.pinned?.length) {
      this.pinned = [];
    } else E{
      forkJoin((value.config.pinned as string[])
        .map(pin => this.refs.getCurrent(pin).pipe(
          catchError(err => of({ url: pin })),
          takeUntilDestroyed(this.destroyRef),
        )))
        .subscribe(pinned => this.pinned = pinned);
    }
  }
 
  @Input()
  set cols(value: number | undefined) {
    this._cols = value || 0;
  }
 
  get colStyle() {
    if (!this.cols) {
      return '';
    } else E{
      return ' 1fr'.repeat(this.cols);
    }
  }
 
  get cols() {
    Iif (this._cols) return this._cols;
    return this.ext?.config?.defaultCols;
  }
 
  get expanded(): boolean {
    if (this._expanded === undefined) return this._ext?.config?.defaultExpanded;
    return this._expanded;
  }
 
  @Input()
  set expanded(value: boolean) {
    this._expanded = value;
  }
 
  get page(): Page<Ref> | undefined {
    return this._page;
  }
 
  @Input()
  set page(value: Page<Ref> | undefined) {
    this._page = value;
    if (this._page) {
      Iif (this._page.page.number > 0 && this._page.page.number >= this._page.page.totalPages) {
        this.router.navigate([], {
          queryParams: {
            pageNumber: this._page.page.totalPages - 1,
          },
          queryParamsHandling: 'merge',
          replaceUrl: true,
        });
      }
    }
  }
 
  ngOnInit(): void {
    this.newRefs$?.pipe(
      takeUntilDestroyed(this.destroyRef),
    ).subscribe(ref => ref && this.addNewRef(ref));
  }
 
 
  getNumber(i: number) {
    if (this.showVotes) {
      const votes = score(this.page!.content[i]);
      if (votes < 100 &&
        this.hideNewZeroVoteScores &&
        DateTime.now().diff(this.page!.content[i].created!, 'minutes').minutes < 5) {
        return '•';
      }
      return votes;
    }
    return i + this.page!.page.number * this.page!.page.size + 1;
  }
 
  addNewRef(ref: Ref) {
    // TODO: verify read before clearing?
    this.accounts.clearNotificationsIfNone(ref.modified);
    if (ref.url !== this.store.view.url && !this.page?.content.find(r => r.url === ref.url)) {
      const index = this.newRefs.findIndex(r => r.url === ref.url);
      if (index !== -1) {
        this.newRefs[index] = ref;
      } else if (this.insertNewAtTop) {
        this.newRefs = [ref, ...this.newRefs];
        return;
      } else {
        this.newRefs = [...this.newRefs, ref];
        return;
      }
    }
    this.store.eventBus.refresh(ref);
  }
}