Skip to content

Commit db800b6

Browse files
author
m.ivanov4
committed
feat: add ngx-column component
1 parent af3543d commit db800b6

32 files changed

+383
-208
lines changed

package-lock.json

+73-152
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"typedoc": "typedoc projects/table-builder/src",
77
"test": "jest --config ./jest.config.js --coverage",
88
"start": "ng serve --open --aot=false",
9-
"build": "ng build",
9+
"build": "ng build && ng build table-builder",
1010
"build.meta": "echo 'prebuild'",
1111
"lint": "ng lint"
1212
},
@@ -29,6 +29,7 @@
2929
"prettier": "^1.17.1",
3030
"rxjs": "~6.3.3",
3131
"tslib": "^1.9.0",
32+
"typedoc-plugin-markdown": "^1.2.1",
3233
"zone.js": "~0.8.26"
3334
},
3435
"devDependencies": {

projects/table-builder/src/lib/table-builder.interfaces.ts

-24
This file was deleted.

projects/table-builder/src/lib/table-builder.module.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import {
99
ENABLE_INTERACTION_OBSERVER,
1010
ROW_HEIGHT,
1111
WHEEL_MAX_DELTA
12-
} from './table-builder.tokens';
12+
} from './table/config/table-builder.tokens';
1313
import { TableBuilderComponent } from './table/table-builder.component';
14-
import { TableBuilderOptions } from './table-builder.interfaces';
15-
import { TableBuilderConfig } from './table-builder.config';
14+
import { TableBuilderConfig } from './table/config/table-builder.config';
1615
import { WheelThrottlingDirective } from './table/directives/wheel.directive';
1716
import { TableTheadComponent } from './table/components/table-thead/table-thead.component';
1817
import { TableTbodyComponent } from './table/components/table-tbody/table-tbody.component';
1918
import { DynamicHeightDirective } from './table/directives/dynamic-height.directive';
19+
import { NgxColumnComponent } from './table/components/ngx-column/ngx-column.component';
20+
import { TableBuilderOptions } from './table/interfaces/table-builder.external';
21+
import { TemplateHeadThDirective } from './table/directives/rows/template-head-th.directive';
22+
import { TemplateBodyTdDirective } from './table/directives/rows/template-body-td.directive';
2023

2124
@NgModule({
2225
imports: [CommonModule, VirtualScrollerModule, InViewportModule],
@@ -25,9 +28,12 @@ import { DynamicHeightDirective } from './table/directives/dynamic-height.direct
2528
WheelThrottlingDirective,
2629
DynamicHeightDirective,
2730
TableTheadComponent,
28-
TableTbodyComponent
31+
TableTbodyComponent,
32+
NgxColumnComponent,
33+
TemplateHeadThDirective,
34+
TemplateBodyTdDirective
2935
],
30-
exports: [TableBuilderComponent]
36+
exports: [TableBuilderComponent, NgxColumnComponent, TemplateHeadThDirective, TemplateBodyTdDirective]
3137
})
3238
export class TableBuilderModule {
3339
public static forRoot(options: Partial<TableBuilderOptions> = {}): ModuleWithProviders {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
ngx-column works!
3+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component, Input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'ngx-column',
5+
templateUrl: './ngx-column.component.html'
6+
})
7+
export class NgxColumnComponent {
8+
@Input() public key: string;
9+
}

projects/table-builder/src/lib/table/components/table-tbody/table-tbody.component.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { ChangeDetectionStrategy, Component, Inject, Input, ViewChild, ViewEncapsulation } from '@angular/core';
2-
import { VirtualScrollerComponent } from 'ngx-virtual-scroller';
1+
import { ChangeDetectionStrategy, Component, Inject, Input, ViewEncapsulation } from '@angular/core';
32

43
import { TableLineRow } from '../common/table-line-row.class';
5-
import { TableRow } from '../../../table-builder.interfaces';
6-
import { BUFFER_AMOUNT } from '../../../table-builder.tokens';
7-
import { fadeAnimation } from '../../core/fade.animation';
4+
import { BUFFER_AMOUNT } from '../../config/table-builder.tokens';
5+
import { fadeAnimation } from '../../animations/fade.animation';
6+
import { TableRow } from '../../interfaces/table-builder.external';
87

98
@Component({
109
selector: 'table-tbody',
@@ -19,7 +18,6 @@ export class TableTbodyComponent extends TableLineRow {
1918
@Input('table-viewport') public tableViewport: HTMLElement;
2019
@Input('column-virtual-height') public columnVirtualHeight: number;
2120
@Input('buffer-amount') public bufferAmount: number;
22-
@ViewChild('scroll') public scroll: VirtualScrollerComponent;
2321

2422
constructor(@Inject(BUFFER_AMOUNT) public defaultBufferAmount: number) {
2523
super();

projects/table-builder/src/lib/table/components/table-thead/table-thead.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChangeDetectionStrategy, Component, ViewEncapsulation } from '@angular/core';
22

33
import { TableLineRow } from '../common/table-line-row.class';
4-
import { fadeAnimation } from '../../core/fade.animation';
4+
import { fadeAnimation } from '../../animations/fade.animation';
55

66
@Component({
77
selector: 'table-thead',

projects/table-builder/src/lib/table-builder.config.ts renamed to projects/table-builder/src/lib/table/config/table-builder.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TableBuilderOptions } from './table-builder.interfaces';
1+
import { TableBuilderOptions } from '../interfaces/table-builder.external';
22

33
export class TableBuilderConfig implements TableBuilderOptions {
44
public readonly bufferAmount: number = 10;

projects/table-builder/src/lib/table/directives/dynamic-height.directive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApplicationRef, Directive, ElementRef, Input, NgZone, OnChanges, OnDestroy, OnInit } from '@angular/core';
2-
import { DynamicHeightOptions } from '../../table-builder.interfaces';
2+
import { DynamicHeightOptions } from '../interfaces/table-builder.internal';
33

44
@Directive({ selector: '[ngDynamicHeight]' })
55
export class DynamicHeightDirective implements OnInit, OnChanges, OnDestroy {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Directive, Input, TemplateRef } from '@angular/core';
2+
3+
@Directive({ selector: 'ng-template[ngx-td]' })
4+
export class TemplateBodyTdDirective {
5+
@Input('ngx-td') public type: string = null;
6+
constructor(public template: TemplateRef<unknown>) {}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Directive, Input, TemplateRef } from '@angular/core';
2+
3+
@Directive({ selector: 'ng-template[ngx-th]' })
4+
export class TemplateHeadThDirective {
5+
@Input('ngx-th') public type: string = null;
6+
constructor(public template: TemplateRef<unknown>) {}
7+
}

projects/table-builder/src/lib/table/directives/wheel.directive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Directive, ElementRef, EventEmitter, Inject, NgZone, OnDestroy, OnInit, Output } from '@angular/core';
2-
import { WHEEL_MAX_DELTA } from '../../table-builder.tokens';
2+
import { WHEEL_MAX_DELTA } from '../config/table-builder.tokens';
33

44
@Directive({ selector: '[wheelThrottling]' })
55
export class WheelThrottlingDirective implements OnInit, OnDestroy {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { TemplateRef } from '@angular/core';
2+
3+
export interface TableRow<T = any> {
4+
[key: string]: T;
5+
}
6+
7+
export interface TableBuilderOptions {
8+
bufferAmount: number;
9+
rowHeight: number;
10+
columnWidth: number;
11+
wheelMaxDelta: number;
12+
enableInteractionObserver: boolean;
13+
}
14+
15+
export interface TableColumnOptions<T> {
16+
template: TemplateRef<T>;
17+
}
18+
19+
export interface ColumnsSchema<T = any> {
20+
[key: string]: {
21+
td: TableColumnOptions<T>;
22+
th: TableColumnOptions<T>;
23+
width?: string;
24+
};
25+
}
26+
27+
export interface TableSchema<T = any> {
28+
columns: ColumnsSchema;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export enum PrimaryKey {
2+
ID = 'id'
3+
}
4+
5+
export interface DynamicHeightOptions {
6+
autoHeight: boolean;
7+
height: number;
8+
}
9+
10+
export interface ScrollOffsetStatus {
11+
offset: boolean;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ColumnsSchema, TableSchema } from '../../interfaces/table-builder.external';
2+
3+
export class SchemaBuilder<T = any> implements TableSchema<T> {
4+
public columns: ColumnsSchema = {};
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Injectable, QueryList } from '@angular/core';
2+
3+
import { NgxColumnComponent } from '../../components/ngx-column/ngx-column.component';
4+
import { TableSchema } from '../../interfaces/table-builder.external';
5+
import { SchemaBuilder } from './schema-builder.class';
6+
7+
@Injectable()
8+
export class TemplateParserService {
9+
public schema: TableSchema = new SchemaBuilder();
10+
11+
public parse(columns: QueryList<NgxColumnComponent>): void {
12+
columns.forEach((column: NgxColumnComponent) => this.extractColumnTemplate(column));
13+
}
14+
15+
private extractColumnTemplate(column: NgxColumnComponent): void {
16+
const { key }: NgxColumnComponent = column;
17+
this.schema.columns[key] = {
18+
th: {
19+
template: null
20+
},
21+
td: {
22+
template: null
23+
}
24+
};
25+
26+
console.log(this.schema);
27+
}
28+
29+
public get templateColumnKeys(): string[] {
30+
return Object.keys(this.schema.columns);
31+
}
32+
}

projects/table-builder/src/lib/table/table-builder.api.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Input } from '@angular/core';
2-
import { PrimaryKey, TableRow } from '../table-builder.interfaces';
2+
import { PrimaryKey } from './interfaces/table-builder.internal';
3+
import { TableRow } from './interfaces/table-builder.external';
34

45
export class TableBuilderApiImpl {
56
@Input() public height: number;

projects/table-builder/src/lib/table/table-builder.component.ts

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
import {
2+
AfterContentInit,
23
ChangeDetectionStrategy,
34
ChangeDetectorRef,
45
Component,
6+
ContentChildren,
57
Inject,
68
OnChanges,
79
OnInit,
10+
QueryList,
811
ViewEncapsulation
912
} from '@angular/core';
1013

11-
import { COL_WIDTH, ENABLE_INTERACTION_OBSERVER, ROW_HEIGHT } from '../table-builder.tokens';
12-
import { ScrollOffsetStatus, TableRow } from '../table-builder.interfaces';
14+
import { COL_WIDTH, ENABLE_INTERACTION_OBSERVER, ROW_HEIGHT } from './config/table-builder.tokens';
15+
import { ScrollOffsetStatus } from './interfaces/table-builder.internal';
1316
import { TableBuilderApiImpl } from './table-builder.api';
14-
import { fadeAnimation } from './core/fade.animation';
17+
import { fadeAnimation } from './animations/fade.animation';
18+
import { NgxColumnComponent } from './components/ngx-column/ngx-column.component';
19+
import { TemplateParserService } from './services/template-parser/template-parser.service';
20+
import { TableRow } from './interfaces/table-builder.external';
1521

1622
@Component({
1723
selector: 'ngx-table-builder',
1824
templateUrl: './table-builder.component.html',
1925
styleUrls: ['./table-builder.component.scss'],
2026
changeDetection: ChangeDetectionStrategy.OnPush,
2127
encapsulation: ViewEncapsulation.None,
28+
providers: [TemplateParserService],
2229
animations: [fadeAnimation]
2330
})
24-
export class TableBuilderComponent extends TableBuilderApiImpl implements OnInit, OnChanges {
31+
export class TableBuilderComponent extends TableBuilderApiImpl implements OnInit, OnChanges, AfterContentInit {
2532
public scrollOffset: ScrollOffsetStatus = { offset: false };
2633
public columnKeys: string[] = [];
2734

35+
@ContentChildren(NgxColumnComponent)
36+
private readonly columnsList: QueryList<NgxColumnComponent>;
37+
2838
constructor(
2939
@Inject(ROW_HEIGHT) public defaultRowHeight: number,
3040
@Inject(COL_WIDTH) public defaultColumnWidth: number,
3141
@Inject(ENABLE_INTERACTION_OBSERVER) public enabledObserver: boolean,
42+
private templateParser: TemplateParserService,
3243
private readonly cd: ChangeDetectorRef
3344
) {
3445
super();
@@ -50,7 +61,7 @@ export class TableBuilderComponent extends TableBuilderApiImpl implements OnInit
5061
return this.source.length * this.clientRowHeight + this.clientRowHeight;
5162
}
5263

53-
private get modelKeys(): string[] {
64+
private get modelColumnKeys(): string[] {
5465
return Object.keys(this.rowKeyValue);
5566
}
5667

@@ -59,12 +70,10 @@ export class TableBuilderComponent extends TableBuilderApiImpl implements OnInit
5970
}
6071

6172
public ngOnChanges(): void {
62-
this.columnKeys = this.modelKeys.slice();
73+
this.setupTableColumnKeys();
6374
}
6475

65-
public ngOnInit(): void {
66-
this.columnKeys = this.modelKeys;
67-
}
76+
public ngOnInit(): void {}
6877

6978
public updateScrollOffset(offset: boolean): void {
7079
this.scrollOffset = { offset };
@@ -74,4 +83,15 @@ export class TableBuilderComponent extends TableBuilderApiImpl implements OnInit
7483
public inViewportAction(column: HTMLDivElement, $event: { visible: boolean }): void {
7584
column['visible'] = $event.visible;
7685
}
86+
87+
public ngAfterContentInit(): void {
88+
this.templateParser.parse(this.columnsList);
89+
this.setupTableColumnKeys();
90+
}
91+
92+
private setupTableColumnKeys(): void {
93+
const templateColumnKeys: string[] = this.templateParser.templateColumnKeys;
94+
const modelColumnKeys: string[] = this.modelColumnKeys;
95+
this.columnKeys = templateColumnKeys.length ? templateColumnKeys.slice() : modelColumnKeys.slice();
96+
}
7797
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Public API Surface of table-builder
33
*/
4-
54
export { TableBuilderModule } from './lib/table-builder.module';
65
export { TableBuilderComponent } from './lib/table/table-builder.component';
7-
export * from './lib/table-builder.interfaces';
6+
export { NgxColumnComponent } from './lib/table/components/ngx-column/ngx-column.component';
7+
export * from './lib/table/interfaces/table-builder.external';

src/app/app-routing.module.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import { IntroductionComponent } from './samples/introduction/introduction.compo
2222
{
2323
path: 'first',
2424
loadChildren: './samples/sample-first/sample-first.module#SampleFirstModule'
25+
},
26+
{
27+
path: 'second',
28+
loadChildren: './samples/sample-second/sample-second.module#SampleSecondModule'
2529
}
2630
]
2731
}

src/app/app.component.html

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<a routerLink="samples/first">- Example virtual scroll table</a>
1515
</mat-list-item>
1616
<mat-divider></mat-divider>
17+
<mat-list-item class="menu__item">
18+
<a routerLink="samples/second">- Define the column templates</a>
19+
</mat-list-item>
20+
<mat-divider></mat-divider>
1721
</mat-list>
1822
</mat-drawer>
1923

src/app/samples/introduction/introduction.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ <h1>Angular Table Builder</h1>
1515
add the Angular table builder module to our app module (src/app.module.ts):
1616
</p>
1717

18-
<pre><code class="typescript">
18+
<pre><code class="javascript">
1919
import &#123; TableBuilder &#125; from '@angular-ru/table-builder';
2020

2121
@NgModule(&#123;

0 commit comments

Comments
 (0)