All files / components/tek-tree-grid tree-grid.ts

8.42% Statements 8/95
0% Branches 0/19
0% Functions 0/17
8.69% Lines 8/92

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 3911x         1x       1x 1x     1x 1x 1x                     1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
import {
  Button,
  Dropdown,
  IComponentRender, Report, Search, TreeGridEditable,
} from '@zeedhi/common';
import {
  Datasource,
  I18n, IDictionary, KeyMap, Loader, Metadata,
} from '@zeedhi/core';
import merge from 'lodash.merge';
import {
  GridBase, ReportFilter, GridController, ITekGridAtoms,
} from '../../utils';
import { TekRestDatasource } from '../tek-datasource/rest-datasource';
import { TekMemoryDatasource } from '../tek-datasource/memory-datasource';
import { TekGridColumn } from '../tek-grid/grid-column';
import {
  IModalFilterProps,
  ITekGridColumn,
  ITekGridEvents,
  ITekGridExportConfig,
} from '../tek-grid/interfaces';
import { ITekTreeGrid } from './interfaces';
import { TekGridLayoutOptions } from '..';
 
/* TekTreeGrid Class */
export class TekTreeGrid extends TreeGridEditable implements ITekTreeGrid {
  /* Grid Title */
  public title: string = '';
 
  /* Show Add button */
  public addButton: boolean = false;
 
  /* Show edit buttons */
  public showEditButtons: boolean = true;
 
  /* Show Delete button */
  public deleteButton: 'none' | 'currentRow' | 'selection' = 'none';
 
  /* Show Delete button */
  public actions: IComponentRender[] = [];
 
  /* Show Filter button */
  public filterButton: boolean = false;
 
  /* Define modal filter props */
  public modalFilterProps: IModalFilterProps = {
    name: this.name,
    height: 'auto',
    persistent: true,
    cssClass: '',
    cssStyle: '',
    dark: false,
    draggable: false,
    fullscreen: false,
    dragHandle: '',
    title: 'FILTER',
  };
 
  /* Show search Input */
  public showSearch?: boolean = true;
 
  /* Show Column Filter button */
  public columnFilterButton: boolean = false;
 
  /* Show Columns button */
  public columnsButton: boolean = false;
 
  /* Columns to be ignored on columns button */
  public columnsButtonIgnore: string[] = [];
 
  /* Grid events */
  public events!: ITekGridEvents;
 
  /**
   * Enables column dragging
   * @public
   */
  public dragColumns: boolean = true;
 
  /**
   * Defines if the grid is the unique grid on screen
   */
  public mainGrid: boolean = true;
 
  /**
   * Enables column resizing
   * @public
   */
  public resizeColumns: boolean = true;
 
  /**
   * Enables layout saving
   * @public
   */
  public showLayoutOptions: boolean = true;
 
  /**
   * Editable columns
   */
  public columns!: TekGridColumn[];
 
  /**
   * Show export button
   */
  public showExport: boolean = false;
 
  /**
   * Show refresh button
   */
  public showReload: boolean = true;
 
  protected keyShortcutKeyMapping: any;
 
  /**
   * Export config
   */
  public exportConfig: ITekGridExportConfig[] = [
    {
      type: 'pdf',
      label: I18n.translate('TEKGRID_EXPORT_AS', { type: 'PDF' }),
    },
    {
      type: 'xls',
      label: I18n.translate('TEKGRID_EXPORT_AS', { type: 'XLS' }),
    },
    {
      type: 'csv',
      label: I18n.translate('TEKGRID_EXPORT_AS', { type: 'CSV' }),
    },
  ];
 
  /**
   * Actions to overwrite the default export config
   */
  public exportActions?: IComponentRender[];
 
  private toolbarSlotProps: boolean = false;
 
  private gridBase: GridBase;
 
  public filterRelationsDatasource: Datasource;
 
  public filterOperationsDatasource: Datasource;
 
  /**
   * TekTreeGrid class constructor
   * @param props TekTreeGrid properties
   */
  constructor(props: ITekTreeGrid) {
    super(props);
    this.title = this.getInitValue('title', props.title, this.title);
    this.addButton = this.getInitValue(
      'addButton',
      props.addButton,
      this.addButton,
    );
    this.deleteButton = this.getInitValue(
      'deleteButton',
      props.deleteButton,
      this.deleteButton,
    );
    this.showEditButtons = this.getInitValue(
      'showEditButtons',
      props.showEditButtons,
      this.showEditButtons,
    );
    this.filterButton = this.getInitValue(
      'filterButton',
      props.filterButton,
      this.filterButton,
    );
    this.showSearch = this.getInitValue(
      'showSearch',
      props.showSearch,
      this.showSearch,
    );
    this.columnFilterButton = this.getInitValue(
      'columnFilterButton',
      props.columnFilterButton,
      this.columnFilterButton,
    );
    this.columnsButton = this.getInitValue(
      'columnsButton',
      props.columnsButton,
      this.columnsButton,
    );
    this.columnsButtonIgnore = this.getInitValue(
      'columnsButtonIgnore',
      props.columnsButtonIgnore,
      this.columnsButtonIgnore,
    );
    this.dragColumns = this.getInitValue(
      'dragColumns',
      props.dragColumns,
      this.dragColumns,
    );
    this.resizeColumns = this.getInitValue(
      'resizeColumns',
      props.resizeColumns,
      this.resizeColumns,
    );
    this.showLayoutOptions = this.getInitValue(
      'showLayoutOptions',
      props.showLayoutOptions,
      this.showLayoutOptions,
    );
    this.showExport = this.getInitValue(
      'showExport',
      props.showExport,
      this.showExport,
    );
    this.showReload = this.getInitValue(
      'showReload',
      props.showReload,
      this.showReload,
    );
    this.exportConfig = this.getInitValue(
      'exportConfig',
      props.exportConfig,
      this.exportConfig,
    );
    this.exportActions = props.exportActions || this.exportActions;
    this.actions = props.actions || this.actions;
    this.toolbarSlotProps = props.toolbarSlot !== undefined;
 
    Iif (this.deleteButton === 'selection') {
      this.selectable = true;
    }
 
    this.keyShortcutKeyMapping = {
      'mod+f': {
        event: this.focusSearchInput.bind(this),
        stop: true,
        active: !this.mainGrid,
        input: true,
      },
      'mod+l': {
        event: ({ event, element }: any) => {
          Iif (this.filterButton) {
            const instance = Metadata.getInstance<Button>(
              `${this.name}_filterButton`,
            );
            instance.click(event, element);
          }
        },
        stop: true,
        active: !this.mainGrid,
        input: true,
      },
      'mod+enter': {
        event: () => {
          const instance = Metadata.getInstance<Dropdown>(
            `${this.name}_actions_dropdown`,
          );
          instance.setFocus();
          instance.value = !instance.value;
        },
        stop: true,
        active: !this.mainGrid,
        input: true,
      },
    };
 
    this.gridBase = new GridBase(this);
    this.filterOperationsDatasource = this.gridBase.getFilterOperationsDatasource();
    this.filterRelationsDatasource = this.gridBase.getFilterRelationsDatasource();
 
    this.createAccessors();
  }
 
  public onMounted(element: HTMLElement) {
    super.onMounted(element);
    KeyMap.bind(this.keyShortcutKeyMapping, this, element);
  }
 
  public onBeforeDestroy() {
    super.onBeforeDestroy();
    KeyMap.unbind(this.keyShortcutKeyMapping, this);
  }
 
  public onCreated() {
    super.onCreated();
    Loader.addController(
      `GridController_${this.componentId}`,
      new GridController(this),
    );
    Iif (!this.toolbarSlotProps) this.toolbarSlot = this.gridBase.createToolbarProps();
  }
 
  protected focusSearchInput() {
    const searchInput = Metadata.getInstance<Search>(`${this.name}_gridSearch`);
    searchInput.setFocus();
  }
 
  get layoutOptions() {
    Iif (!this.showLayoutOptions) return undefined;
    return Metadata.getInstance<TekGridLayoutOptions>(
      `${this.name}_layout_options`,
    );
  }
 
  /**
   * Get Grid columns objects
   * @param columns Grid columns parameter
   */
  protected getColumns(columns: ITekGridColumn[]): TekGridColumn[] {
    return columns.map((column) => new TekGridColumn(column, this as any));
  }
 
  public columnHasFilterData(column: TekGridColumn) {
    Iif (
      this.datasource instanceof TekRestDatasource
      || this.datasource instanceof TekMemoryDatasource
    ) {
      const dynamicFilter = this.datasource.dynamicFilter[column.name];
      return dynamicFilter && dynamicFilter.length > 0;
    }
    return this.datasource.filter[column.name];
  }
 
  public async getReport(type: string, portrait?: boolean, rowObj: any = {}) {
    const report = new Report(this, this.title);
    const reportGroups = {};
    const reportAggregations = {};
 
    let filter;
    Iif (
      this.datasource instanceof TekRestDatasource
      || this.datasource instanceof TekMemoryDatasource
    ) {
      const reportFilter = new ReportFilter(this.datasource.dynamicFilter);
      filter = reportFilter.build();
    }
 
    let beforeOpen;
 
    Iif (typeof this.events.beforeOpenReport === 'function') {
      beforeOpen = this.events.beforeOpenReport;
    }
 
    return report
      .getReport(
        type,
        portrait,
        {
          metaData: merge(rowObj, {
            filter,
            groups: reportGroups,
            columns: reportAggregations,
          }),
        },
        beforeOpen,
      )
      .then((reportUrl) => window.open(reportUrl));
  }
 
  public async setSearch(search: string) {
    return this.gridBase.setSearch(search);
  }
 
  /**
   * Dispatches row click event
   * @param row Grid row
   * @param event DOM event
   * @param element DOM Element
   */
  public rowClick(row: IDictionary<any>, event: Event, element: HTMLElement) {
    Iif (!this.preventRowClick) {
      this.datasource.currentRow = row;
 
      this.callEvent('rowClick', {
        event,
        element,
        row,
        component: this,
        column: undefined,
      });
    }
    this.preventRowClick = false;
  }
 
  public getAtomInstance<T>(key: keyof ITekGridAtoms) {
    return this.gridBase.getAtomInstance<T>(key);
  }
 
  public getFilterInputs(columnName?: string) {
    return this.gridBase.getFilterInputs(columnName);
  }
}