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

100% Statements 107/107
100% Branches 53/53
100% Functions 19/19
100% Lines 102/102

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 249112x 112x                   112x 112x   112x 19x   19x   19x   19x                         19x   19x   19x   19x               17x 17x 16x   17x       17x 17x   17x 17x     17x 53x                             17x   17x   17x 16x     17x 17x 14x 14x 14x 4x 4x           17x   17x 2x     15x 3x 3x 3x 3x     12x       60x 57x 56x   3x       2x 2x 2x 2x       3x 3x   3x       1x   1x 1x     1x       11x 11x   11x 3x     11x 4x     11x       10x   10x         10x 10x 1x 9x 1x 1x   8x         2x 1x 1x     2x   2x       1x 1x 1x 1x   1x   1x       2x 2x 2x 2x 2x 2x 18x 8x 8x             2x 2x         8x 8x 19x 8x 24x 7x                                    
import { Config, Http, IDictionary } from '@zeedhi/core';
import { Column, ComponentRender, Iterable } from '..';
import { ITekConfig } from '../../utils/config/config';
import {
	ITekGridLayout,
	ITekGridLayoutColumn,
	ITekGridLayoutInfo,
	ITekGridLayoutOptions,
	ITekGridLayoutOptionsEvents,
} from './interfaces';
import { IDynamicFilterItem } from './tek-datasource/interfaces';
import { TekGrid } from './tek-grid';
import { TekGridColumn } from './tek-grid-column';
 
export class TekGridLayoutOptions extends ComponentRender implements ITekGridLayoutOptions {
	public currentLayoutName = '';
 
	public layoutEdited = false;
 
	public layouts: IDictionary<ITekGridLayout> = {};
 
	public layoutNames: string[] = [];
 
	public viewApplyLayout?: (layout?: ITekGridLayout) => void;
 
	public viewGetCurrentLayout?: (layoutName: string) => {
		name: string;
		gridWidth: any;
		order: any;
		filter: any;
		dynamicFilter: any;
		columns: any;
	};
 
	public originalColumnProps: ITekGridLayoutColumn[] = [];
 
	public originalDatasourceOrder: string[] = [];
 
	public originalDatasourceDynamicFilter: IDictionary<IDynamicFilterItem[]> = {};
 
	public originalDatasourceFilter: IDictionary<any> = {};
 
	/* Grid events */
	public declare events: ITekGridLayoutOptionsEvents;
 
	public grid!: Iterable<TekGridColumn>;
 
	private getParentGrid() {
		let { parent } = this;
		while (parent && !(parent.constructor === Iterable || parent.constructor.prototype instanceof Iterable)) {
			parent = parent.parent;
		}
		return parent as Iterable<TekGridColumn>;
	}
 
	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();
 
		if (this.grid instanceof TekGrid) {
			this.grid.registerTask(promise);
		}
 
		layoutsInfo = await promise;
		if (layoutsInfo.layouts) {
			this.layouts = layoutsInfo.layouts;
			this.layoutNames = Object.keys(this.layouts);
			if (layoutsInfo.currentLayoutName) {
				this.currentLayoutName = layoutsInfo.currentLayoutName;
				this.applyLayout(this.currentLayoutName, false);
			}
		}
	}
 
	protected async loadLayoutsInfo() {
		const eventFunction = this.events.loadLayouts || this.grid.events.loadLayouts;
 
		if (eventFunction && typeof eventFunction === 'function') {
			return eventFunction({ component: this.grid });
		}
 
		if ((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: Column) {
		if (column instanceof TekGridColumn) {
			if (!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 discardChanges() {
		const layoutSelected = this.layouts[this.currentLayoutName];
 
		if (this.viewApplyLayout) {
			this.viewApplyLayout(layoutSelected);
		}
 
		this.layoutEdited = false;
	}
 
	public applyLayout(name: string, save = true) {
		this.currentLayoutName = name;
		const layoutSelected = this.layouts[name];
 
		if (this.viewApplyLayout) {
			this.viewApplyLayout(layoutSelected);
		}
 
		if (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) {
		if (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;
		if (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,
				};
			});
		}
 
		if (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) => {
			if (!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),
				});
			}
		});
	}
}