Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

245 строки
8.8 KiB

  1. import {AfterViewInit, Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
  2. import {FormGroup} from "@angular/forms";
  3. import {ListComponent} from "@app/_components/list/list.component";
  4. import {ListColDefinition} from "@app/_components/list/list-col-definition";
  5. import {Observable} from "rxjs";
  6. import {Sort} from "@angular/material/sort";
  7. import {FilterBarComponent} from "@app/_components/filter-bar/filter-bar.component";
  8. @Component({
  9. selector: 'app-search-select',
  10. templateUrl: './search-select.component.html',
  11. styleUrl: './search-select.component.scss'
  12. })
  13. export class SearchSelectComponent implements OnInit, AfterViewInit {
  14. @Input() public formId!: string;
  15. @Input() public resultField!: string;
  16. @Input() public formLabelLangKey!: string;
  17. @Input() public documentForm!: FormGroup;
  18. @Input() public documentFormField!: string;
  19. @Input() public getDataFunction!: (index: number, pageSize: number, term?: string) => Observable<any>;
  20. @Input() public onRowSelectedFunction!: Function;
  21. @Input() public dataSet!: any;
  22. @Input() public displayedDataField!: string;
  23. @Input() public displayedDataSubResource!: string;
  24. @Input() public listColDefinitions!: ListColDefinition[];
  25. @Input() public displayAsButton: boolean;
  26. @Input() public showHeader: boolean;
  27. @Output() rowSelected = new EventEmitter<any>();
  28. @ViewChild('paragraphRef', {static: false}) paragraphRef!: ElementRef;
  29. @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;
  30. protected readonly SearchSelectComponent = SearchSelectComponent;
  31. protected selectedRowIndex: number | null = null;
  32. protected searchBoxOpen: boolean;
  33. protected searchBoxInitialized: boolean;
  34. protected searchBoxFilled: boolean;
  35. constructor() {
  36. this.searchBoxOpen = false;
  37. this.searchBoxInitialized = false;
  38. this.searchBoxFilled = false;
  39. this.displayAsButton = false;
  40. this.showHeader = false;
  41. }
  42. ngOnInit(): void {
  43. if (this.dataSet !== undefined) {
  44. this.searchBoxFilled = true;
  45. }
  46. // Add this line to create a deep copy of the listColDefinitions
  47. this.listColDefinitions = JSON.parse(JSON.stringify(this.listColDefinitions));
  48. }
  49. ngAfterViewInit(): void {
  50. if (this.dataSet !== undefined) {
  51. this.paragraphRef.nativeElement.textContent = this.dataSet[this.displayedDataField];
  52. }
  53. }
  54. onRowSelected = (row: any, index: number) => {
  55. if (this.onRowSelectedFunction !== undefined) {
  56. this.onRowSelectedFunction(row, index);
  57. } else {
  58. this.selectedRowIndex = index;
  59. const value = this.resultField !== undefined ? row[this.resultField] : row.id;
  60. this.documentForm.get(this.formId)?.setValue(value);
  61. if (this.displayedDataSubResource !== undefined) {
  62. this.paragraphRef.nativeElement.textContent = row[this.displayedDataSubResource][this.displayedDataField];
  63. } else {
  64. this.paragraphRef.nativeElement.textContent = row[this.displayedDataField];
  65. }
  66. this.searchBoxFilled = true;
  67. this.searchBoxOpen = false;
  68. }
  69. }
  70. openSearchBox() {
  71. this.searchBoxOpen = !this.searchBoxOpen;
  72. if (this.searchBoxOpen && !this.searchBoxInitialized) {
  73. this.listComponent.getData();
  74. this.searchBoxInitialized = true;
  75. }
  76. }
  77. clearSearch() {
  78. this.paragraphRef.nativeElement.textContent = '';
  79. this.searchBoxFilled = false;
  80. this.documentForm.get(this.formId)?.setValue(null);
  81. }
  82. public getPageIndex() {
  83. return this.listComponent.getPageIndex();
  84. }
  85. public getPageSize() {
  86. return this.listComponent.getPageSize();
  87. }
  88. public static getDefaultColDefLocations(subResource?: string): ListColDefinition[] {
  89. return [
  90. {
  91. name: 'name',
  92. text: 'common.name',
  93. type: ListComponent.COLUMN_TYPE_TEXT,
  94. field: 'name',
  95. sortable: true,
  96. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  97. } as ListColDefinition,
  98. {
  99. name: 'code',
  100. text: 'common.code',
  101. type: ListComponent.COLUMN_TYPE_TEXT,
  102. field: 'code',
  103. sortable: true,
  104. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  105. } as ListColDefinition,
  106. {
  107. name: 'zone',
  108. text: 'model.zone',
  109. type: ListComponent.COLUMN_TYPE_TEXT,
  110. subResource: 'zone',
  111. field: 'name',
  112. sortable: true,
  113. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  114. } as ListColDefinition,
  115. ];
  116. }
  117. public static getDefaultColDefZones(subResource?: string): ListColDefinition[] {
  118. return [
  119. {
  120. name: 'name',
  121. text: 'common.name',
  122. type: ListComponent.COLUMN_TYPE_TEXT,
  123. field: 'name',
  124. sortable: true,
  125. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  126. } as ListColDefinition,
  127. {
  128. name: 'createdAt',
  129. text: 'common.created_at',
  130. type: ListComponent.COLUMN_TYPE_DATE,
  131. field: 'createdAt',
  132. sortable: true,
  133. filterType: FilterBarComponent.FILTER_TYPE_DATE,
  134. } as ListColDefinition,
  135. ];
  136. }
  137. public static getDefaultColDefShippingCompanies(subResource?: string): ListColDefinition[] {
  138. return [
  139. {
  140. name: 'name',
  141. text: 'common.name',
  142. type: ListComponent.COLUMN_TYPE_TEXT,
  143. field: 'name',
  144. sortable: true,
  145. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  146. } as ListColDefinition,
  147. {
  148. name: 'code',
  149. text: 'common.code',
  150. type: ListComponent.COLUMN_TYPE_TEXT,
  151. field: 'code',
  152. sortable: true,
  153. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  154. } as ListColDefinition,
  155. {
  156. name: 'createdAt',
  157. text: 'common.created_at',
  158. type: ListComponent.COLUMN_TYPE_DATE,
  159. field: 'createdAt',
  160. sortable: true,
  161. filterType: FilterBarComponent.FILTER_TYPE_DATE,
  162. } as ListColDefinition,
  163. ];
  164. }
  165. public static getDefaultColDefVessels(subResource?: string): ListColDefinition[] {
  166. return [
  167. {
  168. name: 'name',
  169. text: 'common.name',
  170. type: ListComponent.COLUMN_TYPE_TEXT,
  171. field: 'name',
  172. sortable: true,
  173. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  174. } as ListColDefinition,
  175. {
  176. name: 'code',
  177. text: 'common.code',
  178. type: ListComponent.COLUMN_TYPE_TEXT,
  179. field: 'code',
  180. sortable: true,
  181. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  182. } as ListColDefinition,
  183. {
  184. name: 'shippingCompanyName',
  185. text: 'model.shipping_company',
  186. type: ListComponent.COLUMN_TYPE_TEXT,
  187. subResource: 'company',
  188. field: 'name',
  189. sortable: true,
  190. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  191. } as ListColDefinition,
  192. {
  193. name: 'createdAt',
  194. text: 'common.created_at',
  195. type: ListComponent.COLUMN_TYPE_DATE,
  196. field: 'createdAt',
  197. sortable: true,
  198. filterType: FilterBarComponent.FILTER_TYPE_DATE,
  199. } as ListColDefinition,
  200. ];
  201. }
  202. public static getDefaultColDefUsers(subResource?: string): ListColDefinition[] {
  203. return [
  204. {
  205. name: 'fullName',
  206. text: 'users.full_name',
  207. type: ListComponent.COLUMN_TYPE_TEXT,
  208. field: 'fullName',
  209. sortable: true,
  210. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  211. },
  212. {
  213. name: 'email',
  214. text: 'users.email',
  215. type: ListComponent.COLUMN_TYPE_TEXT,
  216. field: 'email',
  217. sortable: true,
  218. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  219. },
  220. {
  221. name: 'referenceId',
  222. text: 'users.pilotIdNo',
  223. type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
  224. field: 'referenceId',
  225. } as ListColDefinition,
  226. ];
  227. }
  228. }