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 | 93x 93x 93x 93x 93x 93x 93x 259x 259x 259x 259x 259x 206x 206x 1x 1x 1x 206x 21x 228x 1x 1x 1x 17x 13x 6x 206x 206x 353x 206x 206x 206x 206x 259x 379x 38x 21x 1x 20x 4x 4x 4x 4x 4x 9x 9x 9x 9x 5x 4x 1x 20x 27x 5x 27x 12x 14x 14x 4x 3x 28x 7x 900x 4x 134x 1273x 8881x 1x 8880x 5580x 3300x 1774x 1775x 1774x 1774x 5834x 1774x 1x 1775x 1x | import { Accessor, Config, Datasource, DatasourceFactory, I18n, IDictionary, Loader } from '@zeedhi/core';
import clonedeep from 'lodash.clonedeep';
import { ComponentRender } from '../zd-component/component-render';
import { IComponentRender } from '../zd-component/interfaces';
import { Column } from './column';
import { ColumnNotFoundError } from './column-not-found';
import { ConditionsManager } from './conditions-manager';
import { IColumn, IConditionsManager, IIterable, IIterableEvents, IIterableProps } from './interfaces';
import { IterableController } from './iterable-controller';
/**
* Base class for Iterable components.
*/
export abstract class Iterable<T extends Column = Column>
extends ComponentRender<IIterableProps>
implements IIterable<T>
{
/**
* Iterable datasource
*/
public datasource!: Datasource;
// Iterable columns
public columns: T[] = [];
// Define page Sizes
public pageSizes: string[] = ['5', '10', '25', '50'];
// Defines grid events.
declare events: IIterableEvents;
// Uses virtual scrolling to render data.
public virtualScroll = false;
// Number of lines outside the viewport to cache when using virtual scroll.
public virtualScrollCache = 5;
// Defines if searchIn should consider only visible columns or not
public searchVisibleOnly!: boolean;
public defaultSearchVisibleOnly: boolean = Config.iterableSearchVisibleOnly ?? true;
private conditionsManager!: IConditionsManager;
/* istanbul ignore next */
/**
* Creates a new Iterable.
* @param props Iterable properties
*/
constructor(props: IIterableProps) {
super(props);
this.columns = this.createColumns(props);
this.pageSizes = this.getInitValue('pageSizes', props.pageSizes, this.pageSizes);
this.searchVisibleOnly = this.getInitValue(
'searchVisibleOnly',
props.searchVisibleOnly,
this.defaultSearchVisibleOnly,
);
this.virtualScroll = this.getInitValue('virtualScroll', props.virtualScroll, this.virtualScroll);
this.virtualScrollCache = this.getInitValue(
'virtualScrollCache',
props.virtualScrollCache,
this.virtualScrollCache,
);
}
protected initializeDatasource(props: IIterableProps) {
let datasourceProps = clonedeep(props.datasource);
// if using accessor, get props from the controller
if (typeof props.datasource === 'string' && Accessor.isAccessorDefinition(props.datasource)) {
const [controller, accessor] = Accessor.getAccessor(props.datasource);
const instance = Loader.getInstance(controller);
datasourceProps = instance[accessor];
}
this.datasource = DatasourceFactory.factory({
...datasourceProps,
searchIn: props.datasource?.searchIn || (`{{IterableController_${this.name}.searchIn}}` as any),
});
}
updateData(data: IDictionary[]): Promise<any> {
return this.datasource.updateData(data);
}
getAppliedConditions(args: { row: IDictionary; column: Column; path?: string }): Partial<IComponentRender> {
return this.conditionsManager.getAppliedConditions(args);
}
getCurrentRowIndex(): number {
const { uniqueKey, currentRow } = this.datasource;
const data = this.getData();
return data.findIndex((row) => row[uniqueKey] === currentRow[uniqueKey]);
}
getPage(): number {
return this.datasource.page;
}
setPage(page: number): Promise<any> {
return this.datasource.setPage(page);
}
getLastPage(): number {
return Math.ceil(this.datasource.total / this.datasource.limit);
}
protected createController() {
Loader.addController(`IterableController_${this.name}`, IterableController, [this]);
}
public onCreated(): void {
super.onCreated();
this.columns.forEach((column) => column.initialize());
this.createController();
this.initializeDatasource(this.props);
this.datasource.initialize();
this.conditionsManager = new ConditionsManager(this.datasource.uniqueKey, this.columns);
}
/**
* Retrieves columns instances
* @param columns Columns property
*/
protected createColumns(props: IIterableProps): T[] {
if (!props.columns) return [];
return props.columns?.map((columnProps) => this.createColumn(columnProps));
}
public abstract createColumn(columnProps: IColumn): T;
/**
* Retrieves a columns by name
* @param name Column name
*/
public getColumn(name: string) {
const column = this.columns.find((item) => item.name === name);
if (!column) {
throw new ColumnNotFoundError(name, this.name);
}
return column;
}
// Retrieves text for page items
get pageText() {
const last = this.datasource.page * this.datasource.limit;
const lastRow = last > this.datasource.total ? this.datasource.total : last;
const firstRow = lastRow === 0 ? 0 : last - this.datasource.limit + 1;
const translatedText = I18n.instance.t('gridPageText', { firstRow, lastRow, total: this.datasource.total });
return translatedText === 'gridPageText' ? `${firstRow} - ${lastRow} of ${this.datasource.total}` : translatedText;
}
// Get pagination length
get paginationLength() {
let { limit, total } = this.datasource;
limit = Number(limit);
total = Number(total);
if (!limit || Number.isNaN(total) || Number.isNaN(limit)) {
return 0;
}
return Math.ceil(total / limit);
}
/**
* Return cells with conditions applied for each row
* @param columns Iterable columns
* @param row Datasource row
*/
public getCellWithAppliedConditions(columns: Column[], row: IDictionary<any>) {
return this.reapplyConditions(row, columns);
}
public reapplyConditions(row: IDictionary<any>, columns: Column[] = this.columns) {
return this.conditionsManager.reapplyConditions(row, columns);
}
// Causes a reload of the datasource and clear columns lookup data
public reload() {
this.columns.forEach((column: Column) => {
column.clearLookupData();
});
return this.datasource.get();
}
// Called when data changes (shallow reactivity)
public changeData(data: IDictionary[]) {
data.forEach((row) => this.reapplyConditions(row, this.columns));
}
public onBeforeDestroy() {
super.onBeforeDestroy();
this.datasource?.destroy();
}
/**
* Dispatches change layout Event
* @param event DOM event
* @param element DOM Element
*/
public changeLayout(event?: Event, element?: any) {
this.callEvent('changeLayout', {
event,
element,
component: this,
});
}
public async setSearch(search: string): Promise<any> {
return this.datasource.setSearch(search);
}
public setCurrentRow(row: IDictionary) {
this.datasource.currentRow = row;
}
public setOrder(order: string[]) {
return this.datasource.setOrder(order);
}
public getRowKey(row: IDictionary) {
return row[this.datasource.uniqueKey];
}
public delete(row: IDictionary) {
return this.datasource.delete(row);
}
getData(): IDictionary[] {
return this.datasource.data;
}
protected changeDefaultSlotNames(slot: IComponentRender[]) {
return this.recursiveReplace(slot, /<<NAME>>/g, this.name);
}
/**
* Recursively replaces a pattern in all string values within an object/array tree
* @param obj The object or array to process
* @param pattern The regex pattern to search for
* @param replacement The replacement string
* @returns A new object/array with replaced values
*/
private recursiveReplace(obj: unknown, pattern: RegExp, replacement: string): any {
// Handle null or undefined
if (obj === null || obj === undefined) {
return obj;
}
// Handle string values - perform replacement
if (typeof obj === 'string') {
return obj.replace(pattern, replacement);
}
// Handle arrays - recursively process each element
if (Array.isArray(obj)) {
return obj.map((item) => this.recursiveReplace(item, pattern, replacement));
}
// Handle objects - recursively process each property value
if (this.isObject(obj)) {
const newObj: IDictionary = {};
for (const key in obj) {
newObj[key] = this.recursiveReplace(obj[key], pattern, replacement);
}
return newObj;
}
// For all other types (functions, numbers, booleans, etc.), return as-is
return obj;
}
private isObject(value: unknown): value is IDictionary {
return typeof value === 'object';
}
public findRow(key: string | number) {
return this.getData().find((row) => this.getRowKey(row) === key);
}
}
|