igniteui-angular-sovn
Version:
Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps
335 lines (294 loc) • 9.52 kB
text/typescript
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
HostBinding,
HostListener,
Input,
ViewChild,
TemplateRef,
OnDestroy,
Inject
} from '@angular/core';
import { NgIf, NgTemplateOutlet, DecimalPipe, DatePipe, getLocaleCurrencyCode, PercentPipe, CurrencyPipe } from '@angular/common';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { IGroupByRecord } from '../../data-operations/groupby-record.interface';
import { GridColumnDataType } from '../../data-operations/data-util';
import { IgxGridSelectionService } from '../selection/selection.service';
import { GridType, IGX_GRID_BASE } from '../common/grid.interface';
import { IgxFilteringService } from '../filtering/grid-filtering.service';
import { IgxGridRowComponent } from './grid-row.component';
import { GridSelectionMode } from '../common/enums';
import { ISelectionNode } from '../common/types';
import { IgxCheckboxComponent } from '../../checkbox/checkbox.component';
import { IgxBadgeComponent } from '../../badge/badge.component';
import { IgxIconComponent } from '../../icon/icon.component';
import { IgxColumnFormatterPipe } from '../common/pipes';
export class IgxGridGroupByRowComponent implements OnDestroy {
/**
* @hidden
*/
public hideGroupRowSelectors: boolean;
/**
* @hidden
*/
public rowDraggable: boolean;
/**
* An @Input property that sets the index of the row.
* ```html
* <igx-grid-groupby-row [gridID]="id" [index]="rowIndex" [groupRow]="rowData" #row></igx-grid-groupby-row>
* ```
*/
public index: number;
/**
* An @Input property that sets the id of the grid the row belongs to.
* ```html
* <igx-grid-groupby-row [gridID]="id" [index]="rowIndex" [groupRow]="rowData" #row></igx-grid-groupby-row>
* ```
*/
public gridID: string;
/**
* An @Input property that specifies the group record the component renders for.
* ```typescript
* <igx-grid-groupby-row [gridID]="id" [index]="rowIndex" [groupRow]="rowData" #row></igx-grid-groupby-row>
* ```
*/
public groupRow: IGroupByRecord;
/**
* Returns a reference of the content of the group.
* ```typescript
* const groupRowContent = this.grid1.rowList.first.groupContent;
* ```
*/
public groupContent: ElementRef;
/**
* @hidden
*/
protected isFocused = false;
/**
* @hidden
*/
protected defaultGroupByExpandedTemplate: TemplateRef<any>;
/**
* @hidden
*/
protected defaultGroupByCollapsedTemplate: TemplateRef<any>;
/**
* @hidden
*/
protected destroy$ = new Subject<any>();
/**
* @hidden
*/
protected defaultCssClass = 'igx-grid__group-row';
/**
* @hidden
*/
protected paddingIndentationCssClass = 'igx-grid__group-row--padding-level';
/**
* Returns whether the row is focused.
* ```
* let gridRowFocused = this.grid1.rowList.first.focused;
* ```
*/
public get focused(): boolean {
return this.isActive();
}
/** @hidden @internal */
public get currencyCode(): string {
return this.groupRow.column.pipeArgs.currencyCode ?
this.groupRow.column.pipeArgs.currencyCode : getLocaleCurrencyCode(this.grid.locale);
}
constructor(
public grid: GridType,
public gridSelection: IgxGridSelectionService,
public element: ElementRef,
public cdr: ChangeDetectorRef,
public filteringService: IgxFilteringService) {
this.gridSelection.selectedRowsChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.cdr.markForCheck();
});
}
public activate() {
this.grid.navigation.setActiveNode({ row: this.index });
}
/**
* @hidden
* @internal
*/
public ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
/**
* Returns whether the group row is expanded.
* ```typescript
* const groupRowExpanded = this.grid1.rowList.first.expanded;
* ```
*/
public get expanded(): boolean {
return this.grid.isExpandedGroup(this.groupRow);
}
/**
* @hidden
*/
public get describedBy(): string {
const grRowExpr = this.groupRow.expression !== undefined ? this.groupRow.expression.fieldName : '';
return this.gridID + '_' + grRowExpr;
}
public get dataRowIndex() {
return this.index;
}
/**
* Returns a reference to the underlying HTML element.
* ```typescript
* const groupRowElement = this.nativeElement;
* ```
*/
public get nativeElement(): any {
return this.element.nativeElement;
}
public get attrCellID() {
return `${this.gridID}_${this.index}`;
}
/**
* Returns the style classes applied to the group rows.
* ```typescript
* const groupCssStyles = this.grid1.rowList.first.styleClasses;
* ```
*/
public get styleClasses(): string {
return `${this.defaultCssClass} ` + `${this.paddingIndentationCssClass}-` + this.groupRow.level +
(this.isActive() ? ` ${this.defaultCssClass}--active` : '');
}
public isActive() {
return this.grid.navigation.activeNode ? this.grid.navigation.activeNode.row === this.index : false;
}
/**
* @hidden @internal
*/
public getRowID(rowData): IgxGridRowComponent {
return this.grid.primaryKey ? rowData[this.grid.primaryKey] : rowData;
}
/**
* @hidden @internal
*/
public onGroupSelectorClick(event) {
if (!this.grid.isMultiRowSelectionEnabled) {
return;
}
event.stopPropagation();
if (this.areAllRowsInTheGroupSelected) {
this.gridSelection.deselectRows(this.groupRow.records.map(x => this.getRowID(x)));
} else {
this.gridSelection.selectRows(this.groupRow.records.map(x => this.getRowID(x)));
}
}
/**
* Toggles the group row.
* ```typescript
* this.grid1.rowList.first.toggle()
* ```
*/
public toggle() {
this.grid.toggleGroup(this.groupRow);
}
public get iconTemplate() {
if (this.expanded) {
return this.grid.rowExpandedIndicatorTemplate || this.defaultGroupByExpandedTemplate;
} else {
return this.grid.rowCollapsedIndicatorTemplate || this.defaultGroupByCollapsedTemplate;
}
}
protected get selectionNode(): ISelectionNode {
return {
row: this.index,
column: this.gridSelection.activeElement ? this.gridSelection.activeElement.column : 0
};
}
/**
* @hidden @internal
*/
public get dataType(): any {
const column = this.groupRow.column;
return (column && column.dataType) || GridColumnDataType.String;
}
/**
* @hidden @internal
*/
public get formatter(): any {
const column = this.groupRow.column;
return (column && column.formatter) || null;
}
/**
* @hidden @internal
*/
public get areAllRowsInTheGroupSelected(): boolean {
return this.groupRow.records.every(x => this.gridSelection.isRowSelected(this.getRowID(x)));
}
/**
* @hidden @internal
*/
public get selectedRowsInTheGroup(): any[] {
const selectedIds = new Set(this.gridSelection.filteredSelectedRowIds);
return this.groupRow.records.filter(rowID => selectedIds.has(this.getRowID(rowID)));
}
/**
* @hidden @internal
*/
public get groupByRowCheckboxIndeterminateState(): boolean {
if (this.selectedRowsInTheGroup.length > 0) {
return !this.areAllRowsInTheGroupSelected;
}
return false;
}
/**
* @hidden @internal
*/
public get groupByRowSelectorBaseAriaLabel(): string {
const ariaLabel: string = this.areAllRowsInTheGroupSelected ?
this.grid.resourceStrings.igx_grid_groupByArea_deselect_message : this.grid.resourceStrings.igx_grid_groupByArea_select_message;
return ariaLabel.replace('{0}', this.groupRow.expression.fieldName).replace('{1}', this.groupRow.value);
}
/**
* @hidden @internal
*/
public get showRowSelectors(): boolean {
return this.grid.rowSelection !== GridSelectionMode.none && !this.hideGroupRowSelectors;
}
}