Advertisement
psi_mmobile

Untitled

Nov 5th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.39 KB | None | 0 0
  1. import { Button, TextField } from '@bryntum/scheduler/scheduler.umd.js';
  2. import { Inject, Injectable, OnDestroy } from '@angular/core';
  3. import { I18NEXT_SERVICE, ITranslationService } from 'angular-i18next';
  4. import { Workbook } from 'exceljs/dist/exceljs.min.js';
  5. import { saveAs } from 'file-saver';
  6. import { MatSnackBar } from '@angular/material/snack-bar';
  7. import { NotificationService } from '../core/services/notification.service';
  8. import { TranslateService } from '@ngx-translate/core';
  9. import { MatDialog } from '@angular/material/dialog';
  10. import { ColumnSelectionToastComponent } from './toasts/column-selection-toast/column-selection.toast.component';
  11. import { UserDataService } from '../core/services/user-data.service';
  12. import { ServiceLocator } from '../core/services/locator.service';
  13. import { SaveColumnStateFormComponent } from './toasts/save-column-state-toast/save.column-state.form';
  14. import { DialogOperations } from './enum/enum.dialog.operations';
  15. import { UltraBaseGridComponent } from './ultra.base.grid.component';
  16. import { CustomGridState } from './interface/CustomGridState.interface';
  17. import { of, Subject, Subscription } from 'rxjs';
  18. import { delay, switchMap } from 'rxjs/operators';
  19.  
  20. @Injectable()
  21. export class BaseGridComponent
  22. extends UltraBaseGridComponent
  23. implements OnDestroy
  24. {
  25. counterElem;
  26. counterSearch;
  27. countFound;
  28. setColumnsWidth = false;
  29. reordering = false;
  30. settingDefaultColumnsState;
  31. searchField;
  32. config;
  33. filterItems;
  34. filterItemsTwoState;
  35.  
  36. readonly _saveGrid = new Subject<boolean>();
  37. saveGridNow$ = this._saveGrid
  38. .asObservable()
  39. .pipe(switchMap(() => of(true).pipe(delay(2000))));
  40. saveGridSubscription: Subscription;
  41.  
  42. defaultState: CustomGridState;
  43. mode: number;
  44.  
  45.  
  46.  
  47. @Inject(MatDialog) protected dialog: MatDialog;
  48. @Inject(I18NEXT_SERVICE) protected i18NextService: ITranslationService;
  49. @Inject(MatSnackBar) protected snackBar: MatSnackBar;
  50. @Inject(NotificationService)
  51. protected notificationService: NotificationService;
  52. @Inject(TranslateService) protected translate: TranslateService;
  53.  
  54. protected userDataService: UserDataService;
  55.  
  56. constructor() {
  57. super();
  58. this.userDataService = ServiceLocator.injector.get(UserDataService);
  59. this.filterItems = [
  60. { value: 'Y', text: 'Yes', icon: 'yes_checkbox_state.png' },
  61. { value: 'N', text: 'No', icon: 'no_checkbox_state.png' },
  62. { value: 'null_value', text: 'Null', icon: 'null_checkbox_state.png' }
  63. ];
  64.  
  65. this.filterItemsTwoState = [
  66. { value: 'Y', text: 'Yes', icon: 'yes_checkbox_state.png' },
  67. { value: 'N', text: 'No', icon: 'null_checkbox_state.png' }
  68. ];
  69. }
  70. ngOnDestroy(): void {
  71. this.saveGridSubscription?.unsubscribe();
  72. }
  73.  
  74. setSavedColumnsWidth() {
  75. if (this.grid.columns) {
  76. const state = this.grid.getState();
  77. this.grid.columns.forEach((column) => {
  78. let width;
  79. // bug bryntum resizeToFitContent with checkbox column
  80. if (
  81. column.type === 'check' ||
  82. column.type === 'widget' ||
  83. column.type === 'template'
  84. ) {
  85. width = localStorage[column.id + 'width']
  86. ? +localStorage[column.id + 'width']
  87. : 60;
  88. } else if (
  89. !column.children &&
  90. !column.hidden &&
  91. column.type !== 'timeAxis'
  92. ) {
  93. if (localStorage[column.id + 'width']) {
  94. width = +localStorage[column.id + 'width'];
  95. } else {
  96. if (!column.flex) {
  97. const margin = 40; // margin to have enough space for the column filter
  98. const contentWidth = column.resizeToFitContent() + margin;
  99. if (contentWidth > 300) {
  100. width = 300;
  101. } else if (contentWidth) {
  102. width = contentWidth >= 60 ? contentWidth : 30;
  103. }
  104. }
  105. }
  106. }
  107. state.columns.find((columnState) => {
  108. if (columnState.id === column.id) {
  109. columnState.width = width;
  110. return true;
  111. } else if (columnState.children) {
  112. columnState.children.forEach((childState) => {
  113. if (childState.id === column.id) {
  114. childState.width = width;
  115. return true;
  116. }
  117. });
  118. } else {
  119. return false;
  120. }
  121. });
  122. });
  123. this.grid.applyState(state);
  124. }
  125. }
  126.  
  127. saveColumnsWidth(data) {
  128. if (data.action === 'update' && data.changes.width) {
  129. Object.values(this.grid._columnStore.modified.idMap).forEach(
  130. (column: any) => {
  131. if (column.data.width) {
  132. localStorage[column.id + 'width'] = column.data.width;
  133. }
  134. }
  135. );
  136. }
  137. }
  138.  
  139. // Deprecated : use handleGridState instead
  140. handleColumnsWidth() {
  141. // set width of columns on first row added
  142. this.grid.store.addListener({
  143. add: () => {
  144. if (!this.setColumnsWidth) {
  145. this.setSavedColumnsWidth();
  146. this.setColumnsWidth = true;
  147. }
  148. }
  149. });
  150.  
  151. // save width of columns
  152. this.grid._columnStore.addListener({
  153. change: (data) => {
  154. if (this.setColumnsWidth) {
  155. this.saveColumnsWidth(data);
  156. }
  157. }
  158. });
  159. }
  160.  
  161. reorderColumns() {
  162. this.reordering = true;
  163. const state = { columns: [] };
  164. let index = 0;
  165. let columnId = localStorage['column' + index + this.gridName];
  166. while (columnId) {
  167. let columnState;
  168. const column = this.grid._columnStore.idRegister[columnId];
  169. if (column && column.childLevel === 0) {
  170. // parent state
  171. columnState = {
  172. id: column.data.id,
  173. children: []
  174. };
  175.  
  176. // children state
  177. if (localStorage['columnChildren' + column.data.id]) {
  178. localStorage['columnChildren' + column.data.id]
  179. .split(';')
  180. .forEach((childId) => {
  181. if (column.data.children.find((child) => child.id === childId)) {
  182. const child = this.grid._columnStore.idRegister[childId];
  183. columnState.children.push({
  184. id: child.data.id
  185. });
  186. }
  187. });
  188. }
  189.  
  190. state.columns.push(columnState);
  191. }
  192. index++;
  193. columnId = localStorage['column' + index + this.gridName];
  194. }
  195.  
  196. this.grid.applyState(state);
  197. this.reordering = false;
  198. }
  199.  
  200. saveColumnsOrder(action) {
  201. if (!this.reordering && action === 'add') {
  202. let index = 0;
  203. let parentId;
  204. this.grid.columns.forEach((column) => {
  205. if (column.childLevel === 0) {
  206. localStorage['column' + index + this.gridName] = column.data.id;
  207. index++;
  208. parentId = column.data.id;
  209. localStorage.removeItem('columnChildren' + parentId);
  210. } else {
  211. if (localStorage['columnChildren' + parentId]) {
  212. localStorage['columnChildren' + parentId] += ';' + column.data.id;
  213. } else {
  214. localStorage['columnChildren' + parentId] = column.data.id;
  215. }
  216. }
  217. });
  218. }
  219. }
  220.  
  221. // Deprecated : use handleGridState instead
  222. handleColumnsOrder() {
  223. // reorder columns
  224. this.reorderColumns();
  225.  
  226. // save order of columns
  227. this.grid._columnStore.addListener({
  228. change: ({ action }) => {
  229. this.saveColumnsOrder(action);
  230. }
  231. });
  232. }
  233.  
  234. hideColumn(columnId) {
  235. const column = this.grid.columns.getById(columnId);
  236. if (column) {
  237. column.hide();
  238. }
  239. }
  240.  
  241. hideColumns(defaultColumnsId?) {
  242. const hiddenColumns = localStorage.hiddenColumns
  243. ? JSON.parse(localStorage.hiddenColumns)
  244. : {};
  245.  
  246. // default hidden columns
  247. if (
  248. defaultColumnsId &&
  249. !Object.keys(hiddenColumns).find(
  250. (hiddenColumnId) => hiddenColumnId === defaultColumnsId[0]
  251. )
  252. ) {
  253. defaultColumnsId.forEach((id) => {
  254. hiddenColumns[id] = 'hidden';
  255. });
  256. localStorage.hiddenColumns = JSON.stringify(hiddenColumns);
  257. }
  258.  
  259. this.grid.columns.forEach((column) => {
  260. if (hiddenColumns[column.id] === 'hidden') {
  261. column.hide();
  262. }
  263.  
  264. if (column.children) {
  265. column.children.forEach((child) => {
  266. if (hiddenColumns[child.id] === 'hidden') {
  267. child.hide();
  268. }
  269. });
  270. }
  271. });
  272. }
  273.  
  274. saveColumnsVisibility(item, column) {
  275. // {nameColumn1: hidden, nameColumn2: nothidden, ...}
  276. const hiddenColumns = localStorage.hiddenColumns
  277. ? JSON.parse(localStorage.hiddenColumns)
  278. : {};
  279. if (item._ref === 'hideColumn') {
  280. const columnId = column.id;
  281. hiddenColumns[columnId] = 'hidden';
  282. } else if (item.column) {
  283. const columnId = item.column.id;
  284. hiddenColumns[columnId] = item.column.hidden ? 'hidden' : 'nothidden';
  285. if (item.column.children) {
  286. item.column.children.forEach((childColumn) => {
  287. hiddenColumns[childColumn.id] = item.column.hidden
  288. ? 'hidden'
  289. : 'nothidden';
  290. });
  291. }
  292. }
  293. localStorage.hiddenColumns = JSON.stringify(hiddenColumns);
  294. }
  295.  
  296. // Deprecated : use handleGridState instead
  297. handleColumnsVisibility(defaultColumnsId?) {
  298. this.hideColumns(defaultColumnsId);
  299.  
  300. // save visibility of columns
  301. this.grid.addListener({
  302. contextmenuitem: ({ column, item }) => {
  303. if (item._ref === 'hideColumn' || item.column) {
  304. this.saveColumnsVisibility(item, column);
  305. }
  306. }
  307. });
  308. }
  309.  
  310. // must be call before new grid(config) as it modifies the config
  311. // must add "headerMenu : {items: {saveColumns: null, resetColumns: null}}" in features of the grid
  312. handleDefaultColumnsState(config) {
  313. this.addButtonDefaultColumnsHeaderMenu(config);
  314. // const savedState = localStorage['state' + this.gridName];
  315. // if (
  316. // !Object.keys(localStorage).find((key) => key.includes(this.gridName)) &&
  317. // !savedState &&
  318. // this.defaultState?.grid_state
  319. // ) {
  320. // this.setDefaultColumnsState(this.defaultState.grid_state);
  321. // }
  322. }
  323.  
  324. addButtonDefaultColumnsHeaderMenu(config) {
  325. if (localStorage.preferences.includes('system')) {
  326. config.features.headerMenu.items.saveColumns = {
  327. text: this.translate.instant('grid.saveDefaultColumnsState'),
  328. icon: 'b-fa b-fa-save',
  329. onItem: () => {
  330. this.openSaveColumnStateDialog();
  331. },
  332. weight: 250
  333. };
  334. }
  335. config.features.headerMenu.items.resetColumns = {
  336. text: this.translate.instant('grid.defaultColumnsState'),
  337. icon: 'b-fa b-fa-undo',
  338. onItem: () => {
  339. this.resetColumnState();
  340. },
  341. weight: 250
  342. };
  343. }
  344.  
  345. setDefaultColumnsState(columnsState) {
  346. this.settingDefaultColumnsState = true;
  347. if (columnsState.columns) {
  348. this.applyState(columnsState);
  349. } else {
  350. // Deprecated localstorage values
  351. Object.entries(columnsState).forEach(([key, value]) => {
  352. localStorage[key] = value;
  353. });
  354.  
  355. // add a filter to optimize the reorder (bryntum very slow)
  356. this.grid.store.addFilter({
  357. id: 'boost',
  358. filterBy: () => false
  359. });
  360. this.grid._columnStore.forEach((column) => column.show());
  361. this.hideColumns();
  362. this.reorderColumns();
  363. this.grid.store.removeFilter('boost');
  364. }
  365. this.saveGridState();
  366. this.settingDefaultColumnsState = false;
  367. }
  368.  
  369. handleGridState(defaultHiddenColumnIds?) {
  370. if (this.defaultState?.allow_column_selection === 'N') {
  371. const savedState = localStorage['state' + this.gridName];
  372. let orderedState;
  373. if (savedState) {
  374. const parsedSavedState = JSON.parse(savedState)?.columns.map(
  375. (c) => c.id
  376. );
  377. if (parsedSavedState) {
  378. orderedState = { ...this.defaultState.grid_state };
  379. orderedState.columns = [...orderedState.columns];
  380. orderedState.columns.sort((a, b) => {
  381. return (
  382. parsedSavedState.indexOf(a.id) - parsedSavedState.indexOf(b.id)
  383. );
  384. });
  385. }
  386. } else {
  387. orderedState = this.defaultState.grid_state;
  388. }
  389. this.applyState(orderedState);
  390. } else {
  391. const savedState = localStorage['state' + this.gridName]
  392. ? JSON.parse(localStorage['state' + this.gridName])
  393. : null;
  394. if (savedState) {
  395. this.applyState(savedState);
  396. } else {
  397. // Convert old localstorage values to state
  398. this.setSavedColumnsWidth();
  399. this.reorderColumns();
  400. this.hideColumns(defaultHiddenColumnIds);
  401. this.saveGridState();
  402. }
  403. }
  404.  
  405. // save columns state
  406. this.grid._columnStore.addListener({
  407. update: () => {
  408. this._saveGrid.next(true);
  409. }
  410. });
  411. this.saveGridSubscription = this.saveGridNow$.subscribe(() =>
  412. this.saveGridState()
  413. );
  414. }
  415.  
  416. saveGridState() {
  417. const state = this.grid.getState();
  418. state.scroll.scrollLeft.normal = 0;
  419. state.store.filters = [];
  420.  
  421. // delete stuff from scheduler (or we get weird behavior)
  422. delete state.zoomLevelOptions;
  423. delete state.startDate;
  424. delete state.endDate;
  425. delete state.barMargin;
  426. delete state.zoomLevel;
  427. delete state.zoomLevelOptions;
  428. delete state.eventLayout;
  429. delete state.mode;
  430. delete state.tickSize;
  431. delete state.eventColor;
  432. delete state.eventStyle;
  433. delete state.fillTicks;
  434. localStorage['state' + this.gridName] = JSON.stringify(state);
  435. }
  436.  
  437. // export by default the field of the column if no function excelValue defined
  438. // excelValue must return a string or false if the column don't need to be exported
  439. exportExcel(name) {
  440. this.notificationService.setIsLoadingExcel(true);
  441. const workbook = new Workbook();
  442. const worksheet = workbook.addWorksheet(name);
  443.  
  444. // set columns
  445. const excelColumns = [];
  446. this.grid.columns.visibleColumns
  447. .filter((column) => column.data.excelValue !== false)
  448. .forEach((column) => {
  449. const excelColumn = { header: '', key: '', width: 0 };
  450. if (column.childLevel === 1) {
  451. excelColumn.header =
  452. this.grid.columns.getById(column.parentId).text +
  453. ' - ' +
  454. column.text;
  455. } else {
  456. excelColumn.header = column.text;
  457. }
  458.  
  459. excelColumn.width = this.getActualColumnWidth(column);
  460. if (excelColumn.width < excelColumn.header.length) {
  461. excelColumn.width = excelColumn.header.length;
  462. }
  463. excelColumn.key = excelColumn.header;
  464. excelColumns.push(excelColumn);
  465. });
  466. worksheet.columns = excelColumns;
  467.  
  468. // set header style
  469. worksheet.getRow(1).fill = {
  470. type: 'pattern',
  471. pattern: 'solid',
  472. fgColor: { argb: '103A60' }
  473. };
  474. worksheet.getRow(1).font = {
  475. color: { argb: 'FFFFFF' }
  476. };
  477. worksheet.getRow(1).border = {
  478. left: { style: 'thin', color: { argb: 'FFFFFF' } },
  479. right: { style: 'thin', color: { argb: 'FFFFFF' } }
  480. };
  481. worksheet.getRow(1).alignement = {
  482. vertical: 'middle'
  483. };
  484.  
  485. // add rows
  486. const rows = [];
  487. this.grid.store.records.forEach((record) => {
  488. const row = [];
  489. this.grid.columns.visibleColumns.forEach((column) => {
  490. if (column.data.excelValue !== false) {
  491. if (
  492. column.data.excelColor !== undefined &&
  493. column.data.excelValue !== undefined
  494. ) {
  495. row.push({
  496. color: column.data.excelColor(record),
  497. excelValue: column.data.excelValue(record)
  498. });
  499. } else if (column.data.excelColor !== undefined) {
  500. row.push({
  501. color: column.data.excelColor(record)
  502. });
  503. }
  504. if (column.data.excelValue !== undefined) {
  505. row.push(column.data.excelValue(record));
  506. } else {
  507. if (column.data.excelColor !== undefined) {
  508. } else {
  509. row.push(record.data[column.data.field]);
  510. }
  511. }
  512. }
  513. });
  514. rows.push(row);
  515. });
  516. worksheet.addRows(rows);
  517.  
  518. worksheet.eachRow(function (row) {
  519. row.eachCell(function (cell, colNumber) {
  520. if (cell?.value?.color) {
  521. row.getCell(colNumber).fill = {
  522. type: 'pattern',
  523. pattern: 'solid',
  524. fgColor: { argb: cell.value.color.replace(/^#/, '') }
  525. };
  526. row.getCell(colNumber).value = cell?.value?.excelValue ?? '';
  527. }
  528. });
  529. });
  530.  
  531. // save xlsx
  532. workbook.xlsx.writeBuffer().then((data) => {
  533. const blob = new Blob([data], {
  534. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  535. });
  536. saveAs(blob, name + '.xlsx');
  537. this.notificationService.setIsLoadingExcel(false);
  538. });
  539. }
  540.  
  541. handleColumnSelection(config) {
  542. if (
  543. !this.defaultState ||
  544. this.defaultState.allow_column_selection === 'Y'
  545. ) {
  546. config.features.headerMenu.items.filterColumns = {
  547. text: this.translate.instant('columnsSelection'),
  548. icon: 'b-fa b-fa-columns',
  549. onItem: () => {
  550. // get selected columns
  551. const gridState = this.grid.getState();
  552. const selectedColumns = gridState.columns.filter((column) => {
  553. column.text = this.grid.columns.allRecords.find(
  554. (record) => record.id === column.id
  555. ).text;
  556. return (
  557. !column.hidden ||
  558. (!column.parentId &&
  559. gridState.columns.some(
  560. (c) => c.parentId === column.id && !c.hidden
  561. ))
  562. );
  563. });
  564.  
  565. // get available columns
  566. const availableColumns = gridState.columns.filter((column) => {
  567. column.text = this.grid.columns.allRecords.find(
  568. (record) => record.id === column.id
  569. ).text;
  570. return (
  571. column.hidden ||
  572. (!column.parentId &&
  573. gridState.columns.some(
  574. (c) => c.parentId === column.id && c.hidden
  575. ))
  576. );
  577. });
  578.  
  579. const dialogRef = this.dialog.open(ColumnSelectionToastComponent, {
  580. width: '85%',
  581. height: '85%',
  582. data: { availableColumns, selectedColumns }
  583. });
  584.  
  585. dialogRef.afterClosed().subscribe((selectedColumns) => {
  586. if (selectedColumns && selectedColumns.length > 0) {
  587. const state = this.grid.getState();
  588. this.sortStateColumns(
  589. state.columns,
  590. selectedColumns.map((sc) => sc.data.id)
  591. );
  592. state.columns.forEach((stateColumn) => {
  593. const selectedColumn = selectedColumns.find(
  594. (sc) => sc.data.id === stateColumn.id
  595. );
  596. if (!selectedColumn) {
  597. stateColumn.hidden = true;
  598. /** Not needed anymore because of update bryntum 5.6.12 */
  599. // if (stateColumn.children) {
  600. // stateColumn.children.forEach(
  601. // (child) => (child.hidden = true)
  602. // );
  603. // }
  604. } else {
  605. stateColumn.hidden = false;
  606. /** Not needed anymore because of update bryntum 5.6.12 */
  607. // if (stateColumn.children) {
  608. // this.sortStateColumns(
  609. // stateColumn.children,
  610. // selectedColumn.children.map((scc) => scc.id)
  611. // );
  612. // stateColumn.children.forEach((child) => {
  613. // child.hidden =
  614. // selectedColumn.children.find(
  615. // (scc) => scc.id === child.id
  616. // ) === undefined;
  617. // });
  618. // }
  619. }
  620. });
  621. this.applyState(state);
  622. this.saveGridState();
  623. }
  624. });
  625. },
  626. weight: 150
  627. };
  628. }
  629. }
  630.  
  631. sortStateColumns(stateColumns, sortedColumnIds) {
  632. stateColumns.sort((c1, c2) => {
  633. const c1Index = sortedColumnIds.findIndex((id) => id === c1.id);
  634. const c2Index = sortedColumnIds.findIndex((id) => id === c2.id);
  635. if (c1Index && c2Index) {
  636. return c1Index - c2Index;
  637. } else if (c1Index) {
  638. return 1;
  639. } else if (c2Index) {
  640. return -1;
  641. } else {
  642. return 0;
  643. }
  644. });
  645. }
  646.  
  647. applyState(state) {
  648. // hide new columns
  649. this.hideNewColumns(state);
  650.  
  651. this.grid.features.filterBar.toggleFilterBar(); // Bryntum bug : duplicate filter bar
  652. this.grid.applyState(state);
  653. this.grid.features.filterBar.toggleFilterBar();
  654. }
  655.  
  656. hideNewColumns(state) {
  657. this.grid.columns.data.forEach((column) => {
  658. const stateColumn = state.columns.find(
  659. (stateColumn) => stateColumn.id === column.id
  660. );
  661. if (!stateColumn) {
  662. state.columns.push({ id: column.id, hidden: true });
  663. } else {
  664. if (column.children) {
  665. column.children.forEach((child) => {
  666. if (!stateColumn.children) {
  667. stateColumn.children = [];
  668. }
  669. if (
  670. !stateColumn.children.find(
  671. (stateChild) => stateChild.id === child.id
  672. )
  673. ) {
  674. stateColumn.children.push({ id: child.id, hidden: true });
  675. }
  676. });
  677. }
  678. }
  679. });
  680. }
  681.  
  682. // make a free search for the grid in the element appendToID
  683. makeFreeSearch(appendToID) {
  684. this.counterElem = document.createElement('div');
  685. this.counterElem.style =
  686. 'color: rgb(97, 97, 97, 0.8); font-size: 11px; padding-right:5px; padding-top:1px;';
  687. this.counterSearch = 1;
  688. this.countFound = 0;
  689. this.searchField = new TextField({
  690. appendTo: appendToID,
  691. //icon : 'b-icon b-icon-search',
  692. id: appendToID + 'field',
  693. style: 'width: 200px; color: black',
  694. placeholder: 'Search',
  695. listeners: {
  696. input: () => {
  697. this.grid.features.search.search(this.searchField.value);
  698. this.searchField.focus(); // grid search deselect searchfield
  699. if (this.grid.features.search.foundCount > 0) {
  700. if (this.searchField.value !== '') {
  701. this.counterSearch = 1;
  702. this.countFound = 0;
  703.  
  704. // count total rows found
  705. let previousHitId;
  706. this.grid.features.search.found.forEach((hit) => {
  707. if (previousHitId !== hit.id) {
  708. this.countFound++;
  709. previousHitId = hit.id;
  710. }
  711. });
  712.  
  713. this.counterElem.textContent =
  714. this.counterSearch + '/' + this.countFound;
  715. } else {
  716. this.counterElem.textContent = '';
  717. }
  718. } else {
  719. this.counterElem.textContent = '0/0';
  720. }
  721. }
  722. }
  723. });
  724. this.searchField.element.firstElementChild.insertAdjacentElement(
  725. 'beforeend',
  726. this.counterElem
  727. );
  728. new Button({
  729. icon: 'b-fa-arrow-up',
  730. appendTo: this.searchField.element.childNodes[0] as HTMLElement,
  731. cls: 'b-icon twinkle-grey previousNextButton',
  732. listeners: {
  733. click: () => {
  734. if (this.searchField.value !== '' && this.counterSearch !== 1) {
  735. this.grid.features.search.gotoPrevHit();
  736. this.counterSearch--;
  737. this.counterElem.textContent =
  738. this.counterSearch + '/' + this.countFound;
  739. }
  740. }
  741. }
  742. });
  743. new Button({
  744. icon: 'b-fa-arrow-down',
  745. appendTo: this.searchField.element.childNodes[0] as HTMLElement,
  746. cls: 'b-icon twinkle-grey previousNextButton',
  747. style: 'margin-right: 6px',
  748. listeners: {
  749. click: () => {
  750. if (
  751. this.searchField.value !== '' &&
  752. this.counterSearch !== this.countFound
  753. ) {
  754. this.grid.features.search.gotoNextHit();
  755. this.counterSearch++;
  756. this.counterElem.textContent =
  757. this.counterSearch + '/' + this.countFound;
  758. }
  759. }
  760. }
  761. });
  762. }
  763.  
  764. parentColumnFilterFn(property, record, value) {
  765. if (record.isLeaf) {
  766. const parentRecord = this.grid.store.getById(record.parentId);
  767. return (
  768. value !== null &&
  769. parentRecord.data[property] !== null &&
  770. parentRecord.data[property]
  771. .toString()
  772. .toLowerCase()
  773. .includes(value.toString().toLowerCase())
  774. );
  775. } else {
  776. return (
  777. value !== null &&
  778. record.data[property] !== null &&
  779. record.data[property]
  780. .toString()
  781. .toLowerCase()
  782. .includes(value.toString().toLowerCase())
  783. );
  784. }
  785. }
  786.  
  787. removeColumnsById(ColumnsIds) {
  788. this.config.columns = this.config.columns.filter(
  789. (column) => !ColumnsIds.find((element) => column.id === element)
  790. );
  791.  
  792. this.config.columns = this.config.columns.map((column) => {
  793. if (column.children) {
  794. column.children = column.children.filter(
  795. (columnChildren) =>
  796. !ColumnsIds.find((element) => columnChildren.id === element)
  797. );
  798. }
  799. return column;
  800. });
  801. }
  802.  
  803. openSaveColumnStateDialog() {
  804. const saveColumnDialogRef = this.dialog.open(SaveColumnStateFormComponent, {
  805. width: '300px',
  806. height: '325px'
  807. });
  808.  
  809. saveColumnDialogRef.afterClosed().subscribe(({ operation, formResult }) => {
  810. if (operation === DialogOperations.confirm) {
  811. this.userDataService
  812. .postGridState(JSON.stringify(this.grid.getState()), this.gridName, {
  813. vo_role_id: formResult.vo_role_id,
  814. vo_legal_entity_id: formResult.vo_legal_entity_id,
  815. allow_column_selection: formResult.allowColumnSelection,
  816. is_default: formResult.isDefaultState,
  817. screen_mode: this.mode
  818. })
  819. .subscribe(
  820. () => {
  821. this.snackBar.open(
  822. this.translate.instant('grid.defaultColumnsStateSaved'),
  823. undefined,
  824. { panelClass: ['snackBar', 'savedSnackBar'], duration: 2000 }
  825. );
  826. },
  827. (error) => {
  828. this.notificationService.errorToast(
  829. 'save payroll lines default columns state',
  830. error
  831. );
  832. }
  833. );
  834. }
  835. });
  836. }
  837.  
  838. resetColumnState() {
  839. if (this.defaultState) {
  840. this.setDefaultColumnsState(this.defaultState.grid_state);
  841. this.snackBar.open(
  842. this.translate.instant('grid.defaultColumnsStateSet'),
  843. undefined,
  844. { panelClass: 'snackBar', duration: 2000 }
  845. );
  846. } else {
  847. this.snackBar.open(
  848. this.translate.instant('grid.noDefaultColumnsState'),
  849. undefined,
  850. { panelClass: 'snackBar', duration: 2000 }
  851. );
  852. }
  853. }
  854.  
  855. applyDefaultConfig(config) {
  856. super.applyDefaultConfig(config);
  857. this.defaultState = this.userDataService.getGridState(
  858. this.gridName,
  859. this.mode
  860. );
  861. if (this.defaultState?.allow_column_selection === 'N') {
  862. config.features.headerMenu.items.columnPicker = false;
  863. config.features.headerMenu.items.hideColumn = false;
  864. }
  865. }
  866.  
  867. getActualColumnWidth(column) {
  868. const ratioExcelBryntumWidth = 5;
  869. if (typeof column.width === 'number') {
  870. return column.data.width / ratioExcelBryntumWidth;
  871. } else {
  872. let columnWidth: string = column.width;
  873. let endsWith: string = columnWidth.charAt(columnWidth.length - 1);
  874. let actualSize: number = 0;
  875. if (endsWith === 'x') {
  876. actualSize = +columnWidth.slice(0, columnWidth.length - 2);
  877. } else if (endsWith === 'm') {
  878. actualSize = +columnWidth.slice(0, columnWidth.length - 2);
  879. actualSize = Math.ceil(actualSize * 2);
  880. } else if (endsWith === '%') {
  881. actualSize = +window.screen.width;
  882. let factor = +columnWidth.slice(0, columnWidth.length - 1);
  883. actualSize = Math.ceil(actualSize * (factor / 100));
  884. }
  885. return actualSize / ratioExcelBryntumWidth;
  886. }
  887. }
  888.  
  889. getCheckboxFilterable(attribute,threeState : boolean) {
  890. return {
  891. filterField: {
  892. type: 'combo',
  893. editable: false,
  894. items: threeState ? this.filterItems : this.filterItemsTwoState,
  895. autoComplete: 'new-password',
  896. listItemTpl: (item) => {
  897. return `<img style='padding-right:10px; min-width:26px; width:26px' src='../assets/images/${
  898. item.data.icon
  899. }'> ${this.translate.instant(
  900. 'personList.' + item.data.text
  901. )}`;
  902. },
  903. displayValueRenderer: (record) => {
  904. return record
  905. ? this.translate.instant('personList.' + record.text)
  906. : '';
  907. },
  908. pickerWidth: this.pickerWidthCombo
  909. },
  910. filterFn: ({ record, value }) => {
  911. const filterValue = value === 'null_value' ? null : value;
  912. return filterValue === record[attribute];
  913. }
  914. }
  915. }
  916. }
  917.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement