Skip to content

Commit

Permalink
refactor(grid): mark lastSearchInfo readonly/getter-only (#14388)
Browse files Browse the repository at this point in the history
  • Loading branch information
damyanpetev authored Jun 14, 2024
1 parent fd3e0ee commit 88d3b3d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ export interface GridType extends IGridDataBindable {
/** Represents the last search in the grid
* It contains the search text (the user has entered), the match and some settings for the search
*/
lastSearchInfo: ISearchInfo;
readonly lastSearchInfo: ISearchInfo;
/** @hidden @internal */
page: number;
/** @hidden @internal */
Expand Down
75 changes: 39 additions & 36 deletions projects/igniteui-angular/src/lib/grids/grid-base.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2864,15 +2864,9 @@ export abstract class IgxGridBaseDirective implements GridType,
/**
* Represents the last search information.
*/
public lastSearchInfo: ISearchInfo = {
searchText: '',
caseSensitive: false,
exactMatch: false,
activeMatchIndex: 0,
matchInfoCache: [],
matchCount: 0,
content: ''
};
public get lastSearchInfo(): ISearchInfo {
return this._lastSearchInfo;
}

/**
* @hidden @internal
Expand Down Expand Up @@ -3040,6 +3034,15 @@ export abstract class IgxGridBaseDirective implements GridType,
protected _filterStrategy: IFilteringStrategy = new FilteringStrategy();
protected _autoGeneratedCols = [];
protected _dataView = [];
protected _lastSearchInfo: ISearchInfo = {
searchText: '',
caseSensitive: false,
exactMatch: false,
activeMatchIndex: 0,
matchInfoCache: [],
matchCount: 0,
content: ''
};
protected gridComputedStyles;

/** @hidden @internal */
Expand Down Expand Up @@ -5097,25 +5100,25 @@ export abstract class IgxGridBaseDirective implements GridType,
* @param updateActiveInfo
*/
public refreshSearch(updateActiveInfo?: boolean, endEdit = true): number {
if (this.lastSearchInfo.searchText) {
if (this._lastSearchInfo.searchText) {
this.rebuildMatchCache();

if (updateActiveInfo) {
const activeInfo = this.textHighlightService.highlightGroupsMap.get(this.id);
this.lastSearchInfo.matchInfoCache.forEach((match, i) => {
this._lastSearchInfo.matchInfoCache.forEach((match, i) => {
if (match.column === activeInfo.column &&
match.row === activeInfo.row &&
match.index === activeInfo.index &&
compareMaps(match.metadata, activeInfo.metadata)) {
this.lastSearchInfo.activeMatchIndex = i;
this._lastSearchInfo.activeMatchIndex = i;
}
});
}

return this.find(this.lastSearchInfo.searchText,
return this.find(this._lastSearchInfo.searchText,
0,
this.lastSearchInfo.caseSensitive,
this.lastSearchInfo.exactMatch,
this._lastSearchInfo.caseSensitive,
this._lastSearchInfo.exactMatch,
false,
endEdit);
} else {
Expand All @@ -5132,7 +5135,7 @@ export abstract class IgxGridBaseDirective implements GridType,
* ```
*/
public clearSearch() {
this.lastSearchInfo = {
this._lastSearchInfo = {
searchText: '',
caseSensitive: false,
exactMatch: false,
Expand Down Expand Up @@ -7524,10 +7527,10 @@ export abstract class IgxGridBaseDirective implements GridType,
const exactMatchResolved = exactMatch ? true : false;
let rebuildCache = false;

if (this.lastSearchInfo.searchText !== text ||
this.lastSearchInfo.caseSensitive !== caseSensitiveResolved ||
this.lastSearchInfo.exactMatch !== exactMatchResolved) {
this.lastSearchInfo = {
if (this._lastSearchInfo.searchText !== text ||
this._lastSearchInfo.caseSensitive !== caseSensitiveResolved ||
this._lastSearchInfo.exactMatch !== exactMatchResolved) {
this._lastSearchInfo = {
searchText: text,
activeMatchIndex: 0,
caseSensitive: caseSensitiveResolved,
Expand All @@ -7539,7 +7542,7 @@ export abstract class IgxGridBaseDirective implements GridType,

rebuildCache = true;
} else {
this.lastSearchInfo.activeMatchIndex += increment;
this._lastSearchInfo.activeMatchIndex += increment;
}

if (rebuildCache) {
Expand All @@ -7554,15 +7557,15 @@ export abstract class IgxGridBaseDirective implements GridType,
this.rebuildMatchCache();
}

if (this.lastSearchInfo.activeMatchIndex >= this.lastSearchInfo.matchCount) {
this.lastSearchInfo.activeMatchIndex = 0;
} else if (this.lastSearchInfo.activeMatchIndex < 0) {
this.lastSearchInfo.activeMatchIndex = this.lastSearchInfo.matchCount - 1;
if (this._lastSearchInfo.activeMatchIndex >= this._lastSearchInfo.matchCount) {
this._lastSearchInfo.activeMatchIndex = 0;
} else if (this._lastSearchInfo.activeMatchIndex < 0) {
this._lastSearchInfo.activeMatchIndex = this._lastSearchInfo.matchCount - 1;
}

if (this.lastSearchInfo.matchCount > 0) {
const matchInfo = this.lastSearchInfo.matchInfoCache[this.lastSearchInfo.activeMatchIndex];
this.lastSearchInfo = { ...this.lastSearchInfo };
if (this._lastSearchInfo.matchCount > 0) {
const matchInfo = this._lastSearchInfo.matchInfoCache[this._lastSearchInfo.activeMatchIndex];
this._lastSearchInfo = { ...this._lastSearchInfo };

if (scroll !== false) {
this.scrollTo(matchInfo.row, matchInfo.column);
Expand All @@ -7579,15 +7582,15 @@ export abstract class IgxGridBaseDirective implements GridType,
this.textHighlightService.clearActiveHighlight(this.id);
}

return this.lastSearchInfo.matchCount;
return this._lastSearchInfo.matchCount;
}

private rebuildMatchCache() {
this.lastSearchInfo.matchInfoCache = [];
this._lastSearchInfo.matchInfoCache = [];

const caseSensitive = this.lastSearchInfo.caseSensitive;
const exactMatch = this.lastSearchInfo.exactMatch;
const searchText = caseSensitive ? this.lastSearchInfo.searchText : this.lastSearchInfo.searchText.toLowerCase();
const caseSensitive = this._lastSearchInfo.caseSensitive;
const exactMatch = this._lastSearchInfo.exactMatch;
const searchText = caseSensitive ? this._lastSearchInfo.searchText : this._lastSearchInfo.searchText.toLowerCase();
const data = this.filteredSortedData;
const columnItems = this.visibleColumns.filter((c) => !c.columnGroup).sort((c1, c2) => c1.visibleIndex - c2.visibleIndex);

Expand All @@ -7611,7 +7614,7 @@ export abstract class IgxGridBaseDirective implements GridType,
metadata: new Map<string, boolean>([['pinned', this.isRecordPinnedByIndex(rowIndex)]])
};

this.lastSearchInfo.matchInfoCache.push(mic);
this._lastSearchInfo.matchInfoCache.push(mic);
}
} else {
let occurrenceIndex = 0;
Expand All @@ -7625,7 +7628,7 @@ export abstract class IgxGridBaseDirective implements GridType,
metadata: new Map<string, boolean>([['pinned', this.isRecordPinnedByIndex(rowIndex)]])
};

this.lastSearchInfo.matchInfoCache.push(mic);
this._lastSearchInfo.matchInfoCache.push(mic);

searchValue = searchValue.substring(searchIndex + searchText.length);
searchIndex = searchValue.indexOf(searchText);
Expand All @@ -7635,7 +7638,7 @@ export abstract class IgxGridBaseDirective implements GridType,
});
});

this.lastSearchInfo.matchCount = this.lastSearchInfo.matchInfoCache.length;
this._lastSearchInfo.matchCount = this._lastSearchInfo.matchInfoCache.length;
}

private updateDefaultRowHeight() {
Expand Down

0 comments on commit 88d3b3d

Please sign in to comment.