Skip to content
This repository was archived by the owner on Jun 22, 2020. It is now read-only.

Commit 47f167f

Browse files
m.ivanov4m.ivanov4
authored andcommitted
feat: improved performance
1 parent cbd2493 commit 47f167f

File tree

20 files changed

+216
-75
lines changed

20 files changed

+216
-75
lines changed

projects/table-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-ru/ng-table-builder",
3-
"version": "0.23.0",
3+
"version": "0.24.0",
44
"license": "MIT",
55
"bugs": {
66
"url": "https://github.com/Angular-RU/ng-table-builder/issues"

projects/table-builder/src/lib/table/components/ngx-context-menu/ngx-context-menu.component.scss

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
.context-menu {
99
display: flex;
1010
position: fixed;
11-
background: #fff;
1211
z-index: 10;
1312
border: 1px solid #dcdcdc;
14-
box-shadow: 1px 1px 2px #9c9c9c;
1513
flex-direction: column;
14+
transition: max-height, height, min-height 0.3s;
15+
min-height: 0;
16+
max-height: 0;
17+
height: 0;
18+
overflow: hidden;
19+
background: rgb(255, 255, 255);
20+
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.35);
21+
border-radius: 4px;
22+
font-family: Lucida Grande, sans-serif;
1623

1724
&__item {
1825
margin-top: 0;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@
4040
class="table-grid__column--sortable"
4141
*ngIf="columnSchema?.isModel && columnSchema?.sortable"
4242
[class.table-grid__column--sortable-active]="sortableDefinition[columnSchema.key]"
43-
[class.table-grid__column--sortable-asc]="sortableDefinition[columnSchema.key] === orderType.ASC"
44-
[class.table-grid__column--sortable-desc]="sortableDefinition[columnSchema.key] === orderType.DESC"
4543
(click)="columnSchema?.sortable ? sortByKey.emit(columnSchema.key) : null"
4644
>
4745
<img
46+
alt="sort"
4847
class="table-grid-icon--sortable"
48+
[class.table-grid-icon--sortable-asc]="sortableDefinition[columnSchema.key] === orderType.ASC"
49+
[class.table-grid-icon--sortable-desc]="sortableDefinition[columnSchema.key] === orderType.DESC"
4950
src='data:image/svg+xml;utf8,<svg id="Layer_1" style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="512px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M128.4,189.3L233.4,89c5.8-6,13.7-9,22.4-9c8.7,0,16.5,3,22.4,9l105.4,100.3c12.5,11.9,12.5,31.3,0,43.2 c-12.5,11.9-32.7,11.9-45.2,0L288,184.4v217c0,16.9-14.3,30.6-32,30.6c-17.7,0-32-13.7-32-30.6v-217l-50.4,48.2 c-12.5,11.9-32.7,11.9-45.2,0C115.9,220.6,115.9,201.3,128.4,189.3z"/></svg>'
50-
alt="sort"
5151
/>
52+
53+
<span class="table-grid-icon--sortable-number" *ngIf="sortableCount > 1" [@fadeAnimation]>
54+
{{ positionMap[columnSchema.key] }}
55+
</span>
5256
</div>
5357

5458
<div

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, ViewEncapsulation } from '@angular/core';
22

3+
import { NGX_ANIMATION } from '../../animations/fade.animation';
34
import { ColumnsSchema } from '../../interfaces/table-builder.external';
45
import { KeyMap, ResizeEvent } from '../../interfaces/table-builder.internal';
56
import { FilterableService } from '../../services/filterable/filterable.service';
@@ -10,13 +11,16 @@ import { OVERLOAD_WIDTH_TABLE_HEAD_CELL } from '../../symbols';
1011
selector: 'table-thead',
1112
templateUrl: './table-thead.component.html',
1213
changeDetection: ChangeDetectionStrategy.OnPush,
13-
encapsulation: ViewEncapsulation.None
14+
encapsulation: ViewEncapsulation.None,
15+
animations: [NGX_ANIMATION]
1416
})
1517
export class TableTheadComponent {
1618
@Input('header-top') public headerTop: number;
1719
@Input('column-width') public columnWidth: number;
1820
@Input('head-height') public headHeight: string | number;
1921
@Input('sortable-definition') public sortableDefinition: KeyMap<SortOrderType>;
22+
@Input('sortable-position') public positionMap: KeyMap<number>;
23+
@Input('sortable-count') public sortableCount: number;
2024
@Input('filterable-definition') public filterableDefinition: KeyMap<string>;
2125
@Input('client-row-height') public clientRowHeight: number;
2226
@Input('column-schema') public columnSchema: ColumnsSchema;

projects/table-builder/src/lib/table/config/table-global-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export const TABLE_GLOBAL_OPTIONS: TableGlobalOptions = {
1616
FRAME_TIME: 66,
1717
ROW_HEIGHT: 45,
1818
TIME_IDLE: 200,
19-
MIN_BUFFER: 20,
20-
BUFFER_OFFSET: 5
19+
MIN_BUFFER: 10,
20+
BUFFER_OFFSET: 3
2121
};

projects/table-builder/src/lib/table/directives/virtual-for.directive.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ export class VirtualForDirective implements OnDestroy {
105105
}
106106

107107
private removeEmbeddedViewByIndex(index: number): void {
108-
const [, viewRefItem]: InternalVirtualRef = this.cache.get(index);
109-
if (viewRefItem) {
108+
const ref: InternalVirtualRef | undefined = this.cache.get(index);
109+
if (ref) {
110+
const [, viewRefItem]: InternalVirtualRef = ref;
110111
const stackId: number = this.view.indexOf(viewRefItem);
111112
this.cache.delete(index);
112113

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,8 @@ export interface CalculateRange {
110110
}
111111

112112
export type ProduceDisableFn = ((item: TableRow) => boolean) | null;
113+
114+
export interface OrderedField {
115+
field: string;
116+
order: string;
117+
}

projects/table-builder/src/lib/table/services/sortable/sortable.service.ts

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import { Injectable, NgZone } from '@angular/core';
1+
import { EventEmitter, Injectable, NgZone } from '@angular/core';
22

33
import { TABLE_GLOBAL_OPTIONS } from '../../config/table-global-options';
4-
import { TableRow } from '../../interfaces/table-builder.external';
4+
import { OrderedField, TableRow } from '../../interfaces/table-builder.external';
55
import { KeyMap, Resolver } from '../../interfaces/table-builder.internal';
66
import { WebWorkerThreadService } from '../../worker/worker-thread.service';
7-
import { UtilsService } from '../utils/utils.service';
87
import { sortWorker } from './sort.worker';
98
import { SortableMessage, SortOrderType } from './sortable.interfaces';
109

1110
@Injectable()
1211
export class SortableService {
1312
public definition: KeyMap<SortOrderType> = {};
13+
public positionMap: KeyMap<number> = {};
14+
public sortableCount: number = 0;
15+
private skipInternalSort: boolean = false;
16+
private sortChanges: EventEmitter<OrderedField[]> | null;
1417

15-
constructor(
16-
private readonly thread: WebWorkerThreadService,
17-
private readonly utils: UtilsService,
18-
private readonly zone: NgZone
19-
) {}
18+
constructor(private readonly thread: WebWorkerThreadService, private readonly zone: NgZone) {}
2019

2120
public get empty(): boolean {
2221
return Object.keys(this.definition).length === 0;
@@ -29,27 +28,59 @@ export class SortableService {
2928
public sort(data: TableRow[]): Promise<TableRow[]> {
3029
return new Promise(
3130
(resolve: Resolver<TableRow[]>): void => {
31+
if (this.skipInternalSort) {
32+
resolve(data);
33+
return;
34+
}
35+
3236
this.thread
3337
.run<TableRow[], SortableMessage>(sortWorker, { definition: this.definition, source: data })
34-
.then(
35-
(sorted: TableRow[]): void => {
36-
this.zone.runOutsideAngular(
37-
(): void => {
38-
window.setTimeout((): void => resolve(sorted), TABLE_GLOBAL_OPTIONS.TIME_IDLE);
39-
}
40-
);
41-
}
42-
);
38+
.then((sorted: TableRow[]): void => this.idleResolve(resolve, sorted));
4339
}
4440
);
4541
}
4642

4743
public setDefinition(definition: KeyMap<string>): void {
4844
this.definition = this.empty ? (definition as KeyMap<SortOrderType>) || {} : this.definition;
45+
this.triggerOnChanges();
46+
}
47+
48+
public setSkipSort(skipInternalSort: boolean): void {
49+
this.skipInternalSort = skipInternalSort;
50+
}
51+
52+
public setSortChanges(emitter: EventEmitter<OrderedField[]>): void {
53+
this.sortChanges = emitter;
4954
}
5055

5156
public updateSortKey(key: string): void {
5257
this.definition = this.updateImmutableDefinitions(key);
58+
this.triggerOnChanges();
59+
}
60+
61+
private triggerOnChanges(): void {
62+
const orderedFields: OrderedField[] = [];
63+
this.positionMap = {};
64+
65+
if (this.sortChanges) {
66+
Object.entries(this.definition).forEach(
67+
([key, ordered]: [string, SortOrderType], index: number): void => {
68+
this.positionMap[key] = index + 1;
69+
orderedFields.push({ field: key, order: ordered.toLocaleUpperCase() });
70+
}
71+
);
72+
73+
this.sortableCount = orderedFields.length;
74+
this.sortChanges.emit(orderedFields);
75+
}
76+
}
77+
78+
private idleResolve(resolve: Resolver<TableRow[]>, sorted: TableRow[]): void {
79+
this.zone.runOutsideAngular(
80+
(): void => {
81+
window.setTimeout((): void => resolve(sorted), TABLE_GLOBAL_OPTIONS.TIME_IDLE);
82+
}
83+
);
5384
}
5485

5586
private updateImmutableDefinitions(key: string): KeyMap<SortOrderType> {

projects/table-builder/src/lib/table/styles/cell.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,3 @@
157157
transform: translate(0);
158158
}
159159
}
160-
161-
.cdk-drag-preview .table-grid__header-cell {
162-
top: 0 !important;
163-
}

projects/table-builder/src/lib/table/styles/columns.scss

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,24 @@
9797
height: 20px;
9898
opacity: 0.2;
9999
font-size: 12px;
100-
transition: all 0.2s ease-in-out;
101-
transform: rotate(180deg);
102100
cursor: pointer;
103101

102+
.table-grid-icon--sortable {
103+
transition: transform 0.4s;
104+
105+
&-number {
106+
position: absolute;
107+
font-family: Lucida Grande, sans-serif;
108+
font-size: 10px;
109+
font-weight: bold;
110+
}
111+
}
112+
113+
.table-grid-icon--sortable,
114+
.table-grid-icon--sortable-asc {
115+
transform: rotate(180deg);
116+
}
117+
104118
&-active {
105119
opacity: 1;
106120
}
@@ -133,7 +147,7 @@
133147
}
134148

135149
&--sortable {
136-
&-desc {
150+
.table-grid-icon--sortable-desc {
137151
transform: rotate(0deg);
138152
}
139153
}

0 commit comments

Comments
 (0)