All files / app/component/blog blog.component.ts

51.11% Statements 23/45
29.41% Branches 10/34
26.66% Functions 4/15
47.36% Lines 18/38

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 11867x                   67x 67x 67x                         67x 1x     1x   1x   1x 1x     1x         1x     1x 1x 1x       1x 1x               2x                                                                                                                  
import { Component, Input, OnDestroy, QueryList, ViewChildren } from '@angular/core';
import { Router } from '@angular/router';
import { catchError, forkJoin, of, Subject, takeUntil } 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 { RootConfig } from '../../mods/root';
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 { BlogEntryComponent } from './blog-entry/blog-entry.component';
 
@Component({
  selector: 'app-blog',
  templateUrl: './blog.component.html',
  styleUrls: ['./blog.component.scss'],
  host: { 'class': 'blog ext' },
  imports: [
    BlogEntryComponent,
    PageControlsComponent,
    LoadingComponent,
  ],
})
export class BlogComponent implements HasChanges, OnDestroy {
  private destroy$ = new Subject<void>();
 
  @Input()
  pageControls = true;
  @Input()
  emptyMessage = $localize`No blog entries found`;
 
  pinned: Ref[] = [];
  colStyle = '';
  error: any;
 
  @ViewChildren(BlogEntryComponent)
  list?: QueryList<BlogEntryComponent>;
 
  private _page?: Page<Ref>;
  private _ext?: Ext;
  private _cols? = 0;
 
  constructor(
    private router: Router,
    private store: Store,
    private refs: RefService,
  ) { }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  saveChanges() {
    return !this.list?.find(r => !r.saveChanges());
  }
 
  get page(): Page<Ref> | undefined {
    return this._page;
  }
 
  @Input()
  set cols(value: number | undefined) {
    this._cols = value;
    if (!value) {
      this.colStyle = '';
    } else {
      this.colStyle = ' 1fr'.repeat(value);
    }
  }
 
  get cols() {
    if (this._cols) return this._cols;
    return this.config?.defaultCols;
  }
 
  get ext() {
    return this._ext;
  }
 
  get config() {
    return this.ext?.config as RootConfig | undefined;
  }
 
  @Input()
  set ext(value: Ext | undefined) {
    this._ext = value;
    if (!value?.config?.pinned?.length) {
      this.pinned = [];
    } else {
      forkJoin((value.config.pinned as string[])
        .map(pin => this.refs.getCurrent(pin).pipe(
          catchError(err => of({url: pin})),
          takeUntil(this.destroy$),
        )))
        .subscribe(pinned => this.pinned = pinned);
    }
  }
 
  @Input()
  set page(value: Page<Ref> | undefined) {
    this._page = value;
    if (this._page) {
      if (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",
        })
      }
    }
  }
 
}