Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove duplicate template code #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Directive } from '@angular/core';
import { Group, TreeStatus } from '../../types/public.types';

@Directive({
selector: '[ngx-datatable-body-row]'
})
export class DatatableBodyRowDirective {
static ngTemplateContextGuard<TRow extends { treeStatus?: TreeStatus } = any>(
directive: DatatableBodyRowDirective,
context: unknown
): context is {
row: TRow;
groupValue: Group<TRow>[];
index: number;
} {
return true;
}
}
139 changes: 49 additions & 90 deletions projects/ngx-datatable/src/lib/components/body/body.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,46 @@ import {
[selected]="selected"
(rowContextmenu)="rowContextmenu.emit($event)"
>
<ng-template
ngx-datatable-body-row
#bodyRow
let-groupRow="row"
let-rowIndex="index"
let-groupValue="groupValue"
>
@let rowItem = groupRow ?? group;
<datatable-body-row
role="row"
tabindex="-1"
#rowElement
[disable$]="rowWrapper.disable$"
[isSelected]="selector.getRowSelected(rowItem)"
[innerWidth]="innerWidth"
[offsetX]="offsetX"
[columns]="columns"
[rowHeight]="getRowHeight(rowItem)"
[row]="rowItem"
[group]="groupValue"
[rowIndex]="getRowIndex(rowItem)"
[expanded]="getRowExpanded(rowItem)"
[rowClass]="rowClass"
[displayCheck]="!groupRow ? displayCheck : null"
[treeStatus]="!groupRow ? $any(group)?.treeStatus : null"
[ghostLoadingIndicator]="ghostLoadingIndicator"
[draggable]="rowDraggable"
[verticalScrollVisible]="verticalScrollVisible"
(treeAction)="isRow(rowItem) ? onTreeAction(rowItem) : false"
(activate)="selector.onActivate($event, rowIndex ?? indexes().first + i)"
(drop)="drop($event, rowItem, rowElement)"
(dragover)="dragOver($event, rowItem)"
(dragenter)="dragEnter($event, rowItem, rowElement)"
(dragleave)="dragLeave($event, rowItem, rowElement)"
(dragstart)="drag($event, rowItem, rowElement)"
(dragend)="dragEnd($event, rowItem)"
>
</datatable-body-row>
</ng-template>

@if (rowDefTemplate) {
<ng-container
*rowDefInternal="{
Expand All @@ -116,102 +156,21 @@ import {
/>
} @else {
@if (isRow(group)) {
<datatable-body-row
role="row"
tabindex="-1"
#rowElement
[disable$]="rowWrapper.disable$"
[isSelected]="selector.getRowSelected(group)"
[innerWidth]="innerWidth"
[offsetX]="offsetX"
[columns]="columns"
[rowHeight]="getRowHeight(group)"
[row]="group"
[rowIndex]="getRowIndex(group)"
[expanded]="getRowExpanded(group)"
[rowClass]="rowClass"
[displayCheck]="displayCheck"
[treeStatus]="group?.treeStatus"
[ghostLoadingIndicator]="ghostLoadingIndicator"
[draggable]="rowDraggable"
[verticalScrollVisible]="verticalScrollVisible"
(treeAction)="onTreeAction(group)"
(activate)="selector.onActivate($event, indexes().first + i)"
(drop)="drop($event, group, rowElement)"
(dragover)="dragOver($event, group)"
(dragenter)="dragEnter($event, group, rowElement)"
(dragleave)="dragLeave($event, group, rowElement)"
(dragstart)="drag($event, group, rowElement)"
(dragend)="dragEnd($event, group)"
>
</datatable-body-row>
<ng-container [ngTemplateOutlet]="bodyRow"></ng-container>
}
}

<ng-template #bodyRow>
@if (isRow(group)) {
<datatable-body-row
role="row"
tabindex="-1"
#rowElement
[disable$]="rowWrapper.disable$"
[isSelected]="selector.getRowSelected(group)"
[innerWidth]="innerWidth"
[offsetX]="offsetX"
[columns]="columns"
[rowHeight]="getRowHeight(group)"
[row]="group"
[rowIndex]="getRowIndex(group)"
[expanded]="getRowExpanded(group)"
[rowClass]="rowClass"
[displayCheck]="displayCheck"
[treeStatus]="group?.treeStatus"
[ghostLoadingIndicator]="ghostLoadingIndicator"
[draggable]="rowDraggable"
[verticalScrollVisible]="verticalScrollVisible"
(treeAction)="onTreeAction(group)"
(activate)="selector.onActivate($event, indexes().first + i)"
(drop)="drop($event, group, rowElement)"
(dragover)="dragOver($event, group)"
(dragenter)="dragEnter($event, group, rowElement)"
(dragleave)="dragLeave($event, group, rowElement)"
(dragstart)="drag($event, group, rowElement)"
(dragend)="dragEnd($event, group)"
>
</datatable-body-row>
}
</ng-template>

@if (isGroup(group)) {
<!-- The row typecast is due to angular compiler acting weird. It is obvious that it is of type TRow, but the compiler does not understand. -->
@for (row of group.value; track rowTrackingFn(i, row); let i = $index) {
<datatable-body-row
role="row"
[disable$]="rowWrapper.disable$"
tabindex="-1"
#rowElement
[isSelected]="selector.getRowSelected(row)"
[innerWidth]="innerWidth"
[offsetX]="offsetX"
[columns]="columns"
[rowHeight]="getRowHeight(row)"
[row]="row"
[group]="group.value"
[rowIndex]="getRowIndex(row)"
[expanded]="getRowExpanded(row)"
[rowClass]="rowClass"
[ghostLoadingIndicator]="ghostLoadingIndicator"
[draggable]="rowDraggable"
[verticalScrollVisible]="verticalScrollVisible"
(activate)="selector.onActivate($event, i)"
(drop)="drop($event, row, rowElement)"
(dragover)="dragOver($event, row)"
(dragenter)="dragEnter($event, row, rowElement)"
(dragleave)="dragLeave($event, row, rowElement)"
(dragstart)="drag($event, row, rowElement)"
(dragend)="dragEnd($event, row)"
>
</datatable-body-row>
<ng-container
[ngTemplateOutlet]="bodyRow"
[ngTemplateOutletContext]="{
row,
groupValue: group?.value,
index: i
}"
></ng-container>
}
}
</datatable-row-wrapper>
Expand Down
7 changes: 5 additions & 2 deletions projects/ngx-datatable/src/lib/ngx-datatable.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
DatatableRowDefDirective,
DatatableRowDefInternalDirective
} from './components/body/body-row-def.component';
import { DatatableBodyRowDirective } from './components/body/body-row.directive';

@NgModule({
imports: [CommonModule],
Expand Down Expand Up @@ -77,7 +78,8 @@ import {
DisableRowDirective,
DatatableRowDefComponent,
DatatableRowDefDirective,
DatatableRowDefInternalDirective
DatatableRowDefInternalDirective,
DatatableBodyRowDirective
],
exports: [
DatatableComponent,
Expand All @@ -95,7 +97,8 @@ import {
DatatableGroupHeaderTemplateDirective,
DisableRowDirective,
DatatableRowDefComponent,
DatatableRowDefDirective
DatatableRowDefDirective,
DatatableBodyRowDirective
]
})
export class NgxDatatableModule {
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-datatable/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from './lib/components/columns/tree.directive';
export * from './lib/components/row-detail/row-detail.directive';
export * from './lib/components/row-detail/row-detail-template.directive';
export * from './lib/components/body/body-row-def.component';
export * from './lib/components/body/body-row.directive';

// directives
export * from './lib/directives/draggable.directive';
Expand Down