|
1 | 1 | import { Injectable } from '@angular/core';
|
2 | 2 | import { Subject } from 'rxjs';
|
3 | 3 | import { cloneArray } from '../core/utils';
|
4 |
| -import { IFilteringExpression, FilteringLogic } from '../data-operations/filtering-expression.interface'; |
| 4 | +import { IFilteringExpression } from '../data-operations/filtering-expression.interface'; |
5 | 5 | import { ISortingExpression, SortingDirection } from '../data-operations/sorting-expression.interface';
|
6 | 6 | import { IgxGridCellComponent } from './cell.component';
|
7 | 7 | import { IgxColumnComponent } from './column.component';
|
8 | 8 | import { IgxRowComponent } from './row.component';
|
9 | 9 | import { IFilteringOperation, FilteringExpressionsTree, IFilteringExpressionsTree } from '../../public_api';
|
10 | 10 | import { IGridEditEventArgs, IGridComponent } from './grid-interfaces';
|
| 11 | +import { IgxTextHighlightDirective } from '../directives/text-highlight/text-highlight.directive'; |
| 12 | +import { IgxForOfDirective } from '../directives/for-of/for_of.directive'; |
11 | 13 |
|
12 | 14 | /**
|
13 | 15 | *@hidden
|
@@ -347,12 +349,218 @@ export class IGridAPIService <T extends IGridComponent> {
|
347 | 349 | });
|
348 | 350 | }
|
349 | 351 |
|
350 |
| - protected remove_grouping_expression(id: string, fieldName: string) { |
| 352 | + public refreshSearch(id: string, updateActiveInfo?: boolean) { |
| 353 | + const grid = this.get(id); |
| 354 | + if (grid && grid.lastSearchInfo.searchText) { |
| 355 | + this.rebuildMatchCache(id); |
| 356 | + |
| 357 | + if (updateActiveInfo) { |
| 358 | + const activeInfo = IgxTextHighlightDirective.highlightGroupsMap.get(id); |
| 359 | + grid.lastSearchInfo.matchInfoCache.forEach((match, i) => { |
| 360 | + if (match.column === activeInfo.columnID && |
| 361 | + match.row === activeInfo.rowID && |
| 362 | + match.index === activeInfo.index) { |
| 363 | + grid.lastSearchInfo.activeMatchIndex = i; |
| 364 | + } |
| 365 | + }); |
| 366 | + } |
| 367 | + |
| 368 | + return this.find( |
| 369 | + id, |
| 370 | + grid.lastSearchInfo.searchText, |
| 371 | + 0, |
| 372 | + grid.lastSearchInfo.caseSensitive, |
| 373 | + grid.lastSearchInfo.exactMatch, |
| 374 | + false |
| 375 | + ); |
| 376 | + } else { |
| 377 | + return 0; |
| 378 | + } |
| 379 | + } |
| 380 | + |
| 381 | + public find(id: string, text: string, increment: number, caseSensitive?: boolean, exactMatch?: boolean, scroll?: boolean) { |
| 382 | + const grid = this.get(id); |
| 383 | + if (!grid || !grid.rowList) { |
| 384 | + return 0; |
| 385 | + } |
| 386 | + |
| 387 | + const editModeCell = this.get_cell_inEditMode(id); |
| 388 | + if (editModeCell) { |
| 389 | + this.escape_editMode(id); |
| 390 | + } |
| 391 | + |
| 392 | + if (!text) { |
| 393 | + this.clearSearch(id); |
| 394 | + return 0; |
| 395 | + } |
| 396 | + |
| 397 | + const caseSensitiveResolved = caseSensitive ? true : false; |
| 398 | + const exactMatchResolved = exactMatch ? true : false; |
| 399 | + let rebuildCache = false; |
| 400 | + |
| 401 | + if (grid.lastSearchInfo.searchText !== text || |
| 402 | + grid.lastSearchInfo.caseSensitive !== caseSensitiveResolved || |
| 403 | + grid.lastSearchInfo.exactMatch !== exactMatchResolved) { |
| 404 | + grid.lastSearchInfo = { |
| 405 | + searchText: text, |
| 406 | + activeMatchIndex: 0, |
| 407 | + caseSensitive: caseSensitiveResolved, |
| 408 | + exactMatch: exactMatchResolved, |
| 409 | + matchInfoCache: [] |
| 410 | + }; |
| 411 | + |
| 412 | + rebuildCache = true; |
| 413 | + } else { |
| 414 | + grid.lastSearchInfo.activeMatchIndex += increment; |
| 415 | + } |
| 416 | + |
| 417 | + if (rebuildCache) { |
| 418 | + grid.dataRowList.forEach((row) => { |
| 419 | + row.cells.forEach((c) => { |
| 420 | + c.highlightText(text, caseSensitiveResolved, exactMatchResolved); |
| 421 | + }); |
| 422 | + }); |
| 423 | + |
| 424 | + this.rebuildMatchCache(id); |
| 425 | + } |
| 426 | + |
| 427 | + if (grid.lastSearchInfo.activeMatchIndex >= grid.lastSearchInfo.matchInfoCache.length) { |
| 428 | + grid.lastSearchInfo.activeMatchIndex = 0; |
| 429 | + } else if (grid.lastSearchInfo.activeMatchIndex < 0) { |
| 430 | + grid.lastSearchInfo.activeMatchIndex = grid.lastSearchInfo.matchInfoCache.length - 1; |
| 431 | + } |
| 432 | + |
| 433 | + if (grid.lastSearchInfo.matchInfoCache.length) { |
| 434 | + const matchInfo = grid.lastSearchInfo.matchInfoCache[grid.lastSearchInfo.activeMatchIndex]; |
| 435 | + |
| 436 | + IgxTextHighlightDirective.setActiveHighlight(id, { |
| 437 | + columnID: matchInfo.column, |
| 438 | + rowID: matchInfo.row, |
| 439 | + index: matchInfo.index, |
| 440 | + }); |
| 441 | + |
| 442 | + if (scroll !== false) { |
| 443 | + this.scrollTo(id, matchInfo.row, matchInfo.column); |
| 444 | + } |
| 445 | + } else { |
| 446 | + IgxTextHighlightDirective.clearActiveHighlight(id); |
| 447 | + } |
| 448 | + |
| 449 | + return grid.lastSearchInfo.matchInfoCache.length; |
| 450 | + } |
| 451 | + |
| 452 | + public clearSearch(id: string) { |
| 453 | + this.get(id).lastSearchInfo = { |
| 454 | + searchText: '', |
| 455 | + caseSensitive: false, |
| 456 | + exactMatch: false, |
| 457 | + activeMatchIndex: 0, |
| 458 | + matchInfoCache: [] |
| 459 | + }; |
| 460 | + |
| 461 | + this.get(id).dataRowList.forEach((row) => { |
| 462 | + row.cells.forEach((c) => { |
| 463 | + c.clearHighlight(); |
| 464 | + }); |
| 465 | + }); |
| 466 | + } |
| 467 | + |
| 468 | + protected scrollTo(id: string, row: any, column: any): void { |
| 469 | + const grid = this.get(id); |
| 470 | + const rowIndex = grid.filteredSortedData.indexOf(row); |
| 471 | + let columnIndex = this.get_column_by_name(id, column).visibleIndex; |
| 472 | + |
| 473 | + if (grid.paging) { |
| 474 | + grid.page = Math.floor(rowIndex / grid.perPage); |
| 475 | + } |
351 | 476 |
|
| 477 | + this.scrollDirective(id, grid.verticalScrollContainer, rowIndex); |
| 478 | + |
| 479 | + const scrollRow = grid.rowList.find(r => r.virtDirRow); |
| 480 | + const virtDir = scrollRow ? scrollRow.virtDirRow : null; |
| 481 | + |
| 482 | + if (grid.pinnedColumns.length) { |
| 483 | + if (columnIndex >= grid.pinnedColumns.length) { |
| 484 | + columnIndex -= grid.pinnedColumns.length; |
| 485 | + this.scrollDirective(id, virtDir, columnIndex); |
| 486 | + } |
| 487 | + } else { |
| 488 | + this.scrollDirective(id, virtDir, columnIndex); |
| 489 | + } |
352 | 490 | }
|
353 | 491 |
|
354 |
| - public refreshSearch(id: string, updateHighlight?: boolean) { |
| 492 | + private scrollDirective(id: string, directive: IgxForOfDirective<any>, goal: number): void { |
| 493 | + if (!directive) { |
| 494 | + return; |
| 495 | + } |
| 496 | + const grid = this.get(id); |
| 497 | + const state = directive.state; |
| 498 | + const start = state.startIndex; |
| 499 | + const isColumn = directive.igxForScrollOrientation === 'horizontal'; |
| 500 | + |
| 501 | + const size = directive.getItemCountInView(); |
| 502 | + |
| 503 | + if (start >= goal) { |
| 504 | + // scroll so that goal is at beggining of visible chunk |
| 505 | + directive.scrollTo(goal); |
| 506 | + } else if (start + size <= goal) { |
| 507 | + // scroll so that goal is at end of visible chunk |
| 508 | + if (isColumn) { |
| 509 | + directive.getHorizontalScroll().scrollLeft = |
| 510 | + directive.getColumnScrollLeft(goal) - |
| 511 | + parseInt(directive.igxForContainerSize, 10) + |
| 512 | + parseInt(grid.columns[goal].width, 10); |
| 513 | + } else { |
| 514 | + directive.scrollTo(goal - size + 1); |
| 515 | + } |
| 516 | + } |
| 517 | + } |
355 | 518 |
|
| 519 | + private rebuildMatchCache(id: string) { |
| 520 | + const grid = this.get(id); |
| 521 | + grid.lastSearchInfo.matchInfoCache = []; |
| 522 | + |
| 523 | + const caseSensitive = grid.lastSearchInfo.caseSensitive; |
| 524 | + const exactMatch = grid.lastSearchInfo.exactMatch; |
| 525 | + const searchText = caseSensitive ? grid.lastSearchInfo.searchText : grid.lastSearchInfo.searchText.toLowerCase(); |
| 526 | + const data = grid.filteredSortedData; |
| 527 | + const columnItems = grid.visibleColumns.filter((c) => !c.columnGroup).sort((c1, c2) => c1.visibleIndex - c2.visibleIndex); |
| 528 | + |
| 529 | + data.forEach((dataRow) => { |
| 530 | + columnItems.forEach((c) => { |
| 531 | + const value = c.formatter ? c.formatter(dataRow[c.field]) : dataRow[c.field]; |
| 532 | + if (value !== undefined && value !== null && c.searchable) { |
| 533 | + let searchValue = caseSensitive ? String(value) : String(value).toLowerCase(); |
| 534 | + |
| 535 | + if (exactMatch) { |
| 536 | + if (searchValue === searchText) { |
| 537 | + grid.lastSearchInfo.matchInfoCache.push({ |
| 538 | + row: dataRow, |
| 539 | + column: c.field, |
| 540 | + index: 0, |
| 541 | + }); |
| 542 | + } |
| 543 | + } else { |
| 544 | + let occurenceIndex = 0; |
| 545 | + let searchIndex = searchValue.indexOf(searchText); |
| 546 | + |
| 547 | + while (searchIndex !== -1) { |
| 548 | + grid.lastSearchInfo.matchInfoCache.push({ |
| 549 | + row: dataRow, |
| 550 | + column: c.field, |
| 551 | + index: occurenceIndex++, |
| 552 | + }); |
| 553 | + |
| 554 | + searchValue = searchValue.substring(searchIndex + searchText.length); |
| 555 | + searchIndex = searchValue.indexOf(searchText); |
| 556 | + } |
| 557 | + } |
| 558 | + } |
| 559 | + }); |
| 560 | + }); |
356 | 561 | }
|
357 | 562 |
|
| 563 | + protected remove_grouping_expression(id: string, fieldName: string) { |
| 564 | + |
| 565 | + } |
358 | 566 | }
|
0 commit comments