You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

146 lines
5.5 KiB

  1. import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
  2. import {MatSort, MatSortModule, Sort} from "@angular/material/sort";
  3. import {MatPaginator, MatPaginatorIntl, MatPaginatorModule, PageEvent} from "@angular/material/paginator";
  4. import {Subscription} from "rxjs";
  5. import {DocumentJsonld, DocumentService, ProductJsonld} from "@app/core/api/v1";
  6. import {Router, RouterLink, RouterLinkActive} from "@angular/router";
  7. import {MatTableDataSource, MatTableModule} from "@angular/material/table";
  8. import {OrderFilter} from "@app/_models/orderFilter";
  9. import {DatePipe, NgIf} from "@angular/common";
  10. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  11. import {NewDocumentComponent} from "@app/documents/new-document/new-document.component";
  12. import {TranslateModule} from "@ngx-translate/core";
  13. import {ModalStatus} from "@app/_helpers/modal.states";
  14. import {NewCommentComponent} from "@app/postings/new-comment/new-comment.component";
  15. @Component({
  16. selector: 'app-documents',
  17. templateUrl: './documents.component.html',
  18. styleUrl: './documents.component.scss',
  19. standalone: true,
  20. imports: [MatTableModule, MatSortModule, MatPaginatorModule, RouterLink, RouterLinkActive, NgIf, TranslateModule, DatePipe],
  21. })
  22. export class DocumentsComponent {
  23. @ViewChild(MatSort) sort;
  24. @ViewChild(MatPaginator) documentsPaginator: MatPaginator;
  25. protected displayedColumns: string[];
  26. protected documentsSub: Subscription;
  27. protected documents: Array<DocumentJsonld>;
  28. protected documentsDataSource;
  29. protected documentsLength: number;
  30. protected documentsPageEvent: PageEvent;
  31. protected documentsPageSize: number;
  32. protected documentsPageIndex: number;
  33. protected modalOptions: NgbModalOptions = {
  34. centered: true
  35. };
  36. constructor(
  37. private router: Router,
  38. private modalService: NgbModal,
  39. private documentService: DocumentService
  40. ) {
  41. this.sort = new MatSort();
  42. this.displayedColumns = ['pos', 'name', 'description', 'partnerName', 'productName', 'createdAt', 'createdByName', 'download', 'edit'];
  43. this.documentsSub = new Subscription();
  44. this.documents = [];
  45. this.documentsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  46. this.documentsDataSource = new MatTableDataSource<DocumentJsonld>(this.documents);
  47. this.documentsLength = 0;
  48. this.documentsPageEvent = new PageEvent();
  49. this.documentsPageSize = 10;
  50. this.documentsPageIndex = 0;
  51. }
  52. ngOnInit() {
  53. this.getDocumentsData();
  54. }
  55. ngAfterViewInit() {
  56. this.documentsDataSource.sort = this.sort;
  57. this.documentsDataSource.paginator = this.documentsPaginator;
  58. }
  59. getDocumentsData() {
  60. this.documentsSub = this.documentService.documentsGetCollection(
  61. this.documentsPageIndex + 1,
  62. this.documentsPageSize
  63. ).subscribe(
  64. data => {
  65. this.documents = data["hydra:member"];
  66. this.documentsDataSource = new MatTableDataSource<DocumentJsonld>(this.documents);
  67. this.documentsLength = Number(data["hydra:totalItems"]);
  68. this.documentsPaginator.length = this.documentsLength;
  69. }
  70. );
  71. }
  72. /** Announce the change in sort state for assistive technology. */
  73. onSortChange(sortState: Sort) {
  74. // Reset page index to first page
  75. this.documentsPageIndex = 0;
  76. let order: OrderFilter;
  77. if (sortState.direction === "") {
  78. order = OrderFilter.Undefined;
  79. } else {
  80. order = sortState.direction;
  81. }
  82. // this.nameOrderAsc = OrderFilter.Undefined;
  83. // this.cityOrderAsc = OrderFilter.Undefined;
  84. // this.websiteOrderAsc = OrderFilter.Undefined;
  85. // switch (sortState.active) {
  86. // case "name":
  87. // this.nameOrderAsc = order;
  88. // break;
  89. // case "address":
  90. // this.cityOrderAsc = order;
  91. // break;
  92. // case "website":
  93. // this.websiteOrderAsc = order;
  94. // break;
  95. // }
  96. this.getDocumentsData();
  97. }
  98. handlePageEvent(e: PageEvent) {
  99. this.documentsPageEvent = e;
  100. this.documentsLength = e.length;
  101. this.documentsPageIndex = e.pageIndex.valueOf();
  102. this.documentsPageSize = e.pageSize.valueOf();
  103. this.getDocumentsData();
  104. }
  105. navigateToDocumentFile(element: DocumentJsonld) {
  106. window.open(element.documentUrl?.toString(), '_blank');
  107. }
  108. openModalNewDocument() {
  109. const modalRefDocument = this.modalService.open(NewDocumentComponent, this.modalOptions);
  110. let document: DocumentJsonld = {} as DocumentJsonld;
  111. modalRefDocument.componentInstance.document = document;
  112. modalRefDocument.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  113. if (modalStatus === ModalStatus.Submitted) {
  114. modalRefDocument.dismiss();
  115. this.getDocumentsData();
  116. }
  117. });
  118. }
  119. openModalEditDocument(document: DocumentJsonld) {
  120. const modalRefDocument = this.modalService.open(NewDocumentComponent, this.modalOptions);
  121. modalRefDocument.componentInstance.document = document;
  122. modalRefDocument.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  123. if (modalStatus === ModalStatus.Submitted) {
  124. modalRefDocument.dismiss();
  125. this.getDocumentsData();
  126. }
  127. });
  128. }
  129. }