All files / components/tek-grid layout-options.ts

3.92% Statements 4/102
0% Branches 0/52
0% Functions 0/19
4.04% Lines 4/99

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 2481x 1x 1x                     1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
import { ComponentRender, Grid } from '@zeedhi/common';
import { Config, Http, IDictionary } from '@zeedhi/core';
import {
  IDynamicFilterItem, ITekGridLayoutColumn, TekGrid, TekGridColumn,
} from '..';
import { ITekConfig } from '../../utils';
import {
  ITekGridLayoutOptions,
  ITekGridLayout,
  ITekGridLayoutInfo,
  ITekGridLayoutOptionsEvents,
} from './interfaces';
 
export class TekGridLayoutOptions
  extends ComponentRender
  implements ITekGridLayoutOptions {
  public currentLayoutName: string = '';
 
  public layoutEdited: boolean = false;
 
  public layouts: IDictionary<ITekGridLayout> = {};
 
  public layoutNames: string[] = [];
 
  public viewApplyLayout?: Function;
 
  public originalColumnProps: ITekGridLayoutColumn[] = [];
 
  public originalDatasourceOrder: string[] = [];
 
  public originalDatasourceDynamicFilter: IDictionary<IDynamicFilterItem[]> = {};
 
  public originalDatasourceFilter: IDictionary<any> = {};
 
  /* Grid events */
  public events!: ITekGridLayoutOptionsEvents;
 
  public grid!: Grid;
 
  private getParentGrid() {
    let { parent } = this;
    while (
      parent
      && !(
        parent.constructor === Grid
        || parent.constructor.prototype instanceof Grid
      )
    ) {
      parent = parent.parent;
    }
    return parent as Grid;
  }
 
  public async onMounted(element: HTMLElement) {
    super.onMounted(element);
    this.grid = this.getParentGrid();
 
    this.originalDatasourceOrder = [...this.grid.datasource.order];
    this.originalDatasourceDynamicFilter = {
      ...(this.grid.datasource as any).dynamicFilter,
    };
    this.originalDatasourceFilter = { ...this.grid.datasource.filter };
    this.originalColumnProps = this.grid.columns.map((column: any) => ({
      name: column.name,
      label: column.label,
      align: column.align,
      isVisible: column.isVisible,
      minWidth: column.minWidth,
      maxWidth: column.maxWidth,
      width: column.width,
      fixed: column.fixed,
      grouped: column.grouped,
      groupOpened: column.groupOpened,
      aggregation: column.aggregation,
      filterHelperValue: this.getHelperValue(column),
    }));
 
    let layoutsInfo: ITekGridLayoutInfo = {};
 
    const promise = this.loadLayoutsInfo();
 
    Iif (this.grid instanceof TekGrid) {
      this.grid.registerTask(promise);
    }
 
    layoutsInfo = await promise;
    Iif (layoutsInfo.layouts) {
      this.layouts = layoutsInfo.layouts;
      this.layoutNames = Object.keys(this.layouts);
      Iif (layoutsInfo.currentLayoutName) {
        this.currentLayoutName = layoutsInfo.currentLayoutName;
        this.applyLayout(this.currentLayoutName, false);
      }
    }
  }
 
  protected async loadLayoutsInfo() {
    const eventFunction = this.events.loadLayouts || this.grid.events.loadLayouts;
 
    Iif (eventFunction && typeof eventFunction === 'function') {
      return eventFunction({ component: this.grid });
    }
 
    Iif ((Config as ITekConfig).loadGridLayoutsEndPoint) {
      const route = (Config as ITekConfig).loadGridLayoutsEndPoint!;
      const response = await Http.get(`${route}?id=${this.grid.name}`);
      const responseData = response.data.data;
      return responseData.length && responseData[0]
        ? responseData[0].layouts
        : responseData.layouts || {};
    }
 
    return JSON.parse(localStorage.getItem(this.grid.name) || '{}');
  }
 
  public getHelperValue(column: any) {
    Iif (column instanceof TekGridColumn) {
      Iif (!Array.isArray(column.filterProps)) return column.filterProps.helperValue;
      return column.filterProps.map((prop) => prop.helperValue);
    }
    return '';
  }
 
  public newLayout(layout: ITekGridLayout) {
    this.fixColumns(layout);
    this.currentLayoutName = layout.name;
    this.addLayout(layout);
    this.layoutEdited = false;
  }
 
  public addLayout(layout: ITekGridLayout) {
    this.fixColumns(layout);
    this.layouts[layout.name] = layout;
 
    this.saveLayouts();
  }
 
  public applyLayout(name: string, save: boolean = true) {
    this.currentLayoutName = name;
    const layoutSelected = this.layouts[name];
 
    Iif (this.viewApplyLayout) {
      this.viewApplyLayout(layoutSelected);
    }
 
    Iif (save) {
      this.saveLayouts();
    }
 
    this.layoutEdited = false;
  }
 
  private saveLayouts() {
    this.layoutNames = Object.keys(this.layouts);
 
    const layoutInfo: ITekGridLayoutInfo = {
      currentLayoutName: this.currentLayoutName,
      layouts: this.layouts,
    };
 
    const eventFunction = this.events.saveLayouts || this.grid.events.saveLayouts;
    if (eventFunction && typeof eventFunction === 'function') {
      eventFunction({ component: this.grid, layouts: layoutInfo });
    } else if ((Config as ITekConfig).saveGridLayoutsEndPoint) {
      const route = (Config as ITekConfig).saveGridLayoutsEndPoint!;
      Http.post(route, { id: this.grid.name, layouts: layoutInfo });
    } else {
      localStorage.setItem(this.grid.name, JSON.stringify(layoutInfo));
    }
  }
 
  public deleteLayout(name: string) {
    Iif (this.currentLayoutName === name) {
      this.currentLayoutName = '';
      this.applyLayout(this.currentLayoutName, false);
    }
 
    delete this.layouts[name];
 
    this.saveLayouts();
  }
 
  public updateLayout(name: string, layout: ITekGridLayout) {
    this.fixColumns(layout);
    this.currentLayoutName = name;
    layout.name = name;
    this.layouts[layout.name] = layout;
 
    this.saveLayouts();
 
    this.layoutEdited = false;
  }
 
  public updateDefaultLayout(layout: ITekGridLayout) {
    this.fixColumns(layout);
    this.originalDatasourceOrder = layout.order || this.originalDatasourceOrder;
    this.originalDatasourceDynamicFilter = layout.dynamicFilter || this.originalDatasourceDynamicFilter;
    this.originalDatasourceFilter = layout.filter || this.originalDatasourceFilter;
    Iif (layout.columns) {
      this.originalColumnProps = layout.columns.map((column: any) => {
        const originalColumnIdx = this.originalColumnProps.findIndex(
          (item) => item.name === column.name,
        );
        const originalColumn = originalColumnIdx !== -1
          ? this.originalColumnProps[originalColumnIdx]
          : {};
        return {
          ...originalColumn,
          ...column,
        };
      });
    }
 
    Iif (this.currentLayoutName === '') {
      this.applyLayout(this.currentLayoutName, false);
    }
  }
 
  private fixColumns(layout: ITekGridLayout) {
    const hasLayoutColumns = !!layout.columns;
    layout.columns = layout.columns || [];
    const layoutColumnNames = layout.columns.map(
      (layoutColumn) => layoutColumn.name,
    );
    this.grid.columns.forEach((gridColumn: any) => {
      Iif (
        !hasLayoutColumns
        || layoutColumnNames.indexOf(gridColumn.name) === -1
      ) {
        layout.columns!.push({
          name: gridColumn.name,
          label: gridColumn.label,
          align: gridColumn.align,
          isVisible: !hasLayoutColumns && gridColumn.isVisible,
          width: gridColumn.width,
          minWidth: gridColumn.minWidth,
          maxWidth: gridColumn.maxWidth,
          fixed: gridColumn.fixed,
          grouped: gridColumn.grouped,
          groupOpened: gridColumn.groupOpened,
          aggregation: gridColumn.aggregation,
          filterHelperValue: this.getHelperValue(gridColumn),
        });
      }
    });
  }
}