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

Commit 945c8a7

Browse files
m.ivanov4m.ivanov4
authored andcommitted
feat: auto calculate height for context menu
1 parent 47f167f commit 945c8a7

File tree

12 files changed

+123
-57
lines changed

12 files changed

+123
-57
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.24.0",
3+
"version": "0.29.0",
44
"license": "MIT",
55
"bugs": {
66
"url": "https://github.com/Angular-RU/ng-table-builder/issues"

projects/table-builder/src/lib/table/components/common/modal-view-layer.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApplicationRef, ChangeDetectorRef, ElementRef, Injector, NgZone, OnDestroy, ViewChild } from '@angular/core';
22
import { Subscription } from 'rxjs';
33

4+
import { TABLE_GLOBAL_OPTIONS } from '../../config/table-global-options';
45
import { MousePosition } from '../../interfaces/table-builder.internal';
56
import { detectChanges } from '../../operators/detect-changes';
67
import { ContextMenuService } from '../../services/context-menu/context-menu.service';
@@ -18,6 +19,8 @@ export abstract class ModalViewLayer<T extends PositionState> implements OnDestr
1819
public width: number = null;
1920
public height: number = null;
2021
public isViewed: boolean = false;
22+
public isRendered: boolean = false;
23+
public minHeight: number;
2124
protected subscription: Subscription = null;
2225
protected readonly app: ApplicationRef;
2326
protected readonly utils: UtilsService;
@@ -50,17 +53,43 @@ export abstract class ModalViewLayer<T extends PositionState> implements OnDestr
5053
}
5154

5255
public get overflowY(): number {
53-
const overflowY: number = this.getHeight() + this.top - this.utils.bodyRect.height;
56+
const overflowY: number = this.calculatedHeight + this.top - this.utils.bodyRect.height;
5457
return overflowY > 0 ? overflowY + SCROLLBAR_WIDTH : 0;
5558
}
5659

5760
public abstract get state(): Partial<T>;
5861

62+
public get calculatedHeight(): number {
63+
let height: number;
64+
65+
try {
66+
if (this.height) {
67+
height =
68+
this.menu.nativeElement.scrollHeight > this.height
69+
? this.menu.nativeElement.offsetHeight
70+
: this.height;
71+
} else {
72+
height = this.menu.nativeElement.scrollHeight;
73+
}
74+
} catch (e) {
75+
height = this.height;
76+
}
77+
78+
return height;
79+
}
80+
5981
public updateView(): void {
6082
detectChanges(this.cd);
83+
6184
this.ngZone.runOutsideAngular(
6285
(): void => {
63-
window.requestAnimationFrame((): void => this.app.tick());
86+
window.requestAnimationFrame(
87+
(): void => {
88+
detectChanges(this.cd);
89+
this.app.tick();
90+
this.refresh();
91+
}
92+
);
6493
}
6594
);
6695
}
@@ -87,16 +116,15 @@ export abstract class ModalViewLayer<T extends PositionState> implements OnDestr
87116
);
88117
}
89118

90-
private getHeight(): number {
91-
let height: number;
92-
93-
try {
94-
height =
95-
this.menu.nativeElement.scrollHeight > this.height ? this.menu.nativeElement.offsetHeight : this.height;
96-
} catch (e) {
97-
height = this.height;
98-
}
99-
100-
return height;
119+
private refresh(): void {
120+
this.ngZone.runOutsideAngular(
121+
(): void => {
122+
window.setTimeout((): void => {
123+
this.isRendered = true;
124+
this.minHeight = this.calculatedHeight;
125+
detectChanges(this.cd);
126+
}, TABLE_GLOBAL_OPTIONS.TIME_IDLE);
127+
}
128+
);
101129
}
102130
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
(contextmenu)="close($event)"
1313
[ngStyle]="{
1414
'width.px': width,
15-
'min-height.px': height,
16-
'max-height.px': maxHeight,
15+
opacity: isRendered ? 1 : 0,
16+
'max-height.px': isRendered ? maxHeight : 0,
17+
'min-height.px': isRendered ? minHeight : 0,
1718
'top.px': state?.position?.top - overflowY,
1819
'left.px': state?.position?.left - overflowX,
19-
visibility: state?.position ? 'visible' : 'hidden'
20+
visibility: isRendered ? 'visible' : 'hidden'
2021
}"
2122
>
2223
<ng-content></ng-content>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
z-index: 10;
1212
border: 1px solid #dcdcdc;
1313
flex-direction: column;
14-
transition: max-height, height, min-height 0.3s;
14+
visibility: hidden;
15+
transition: opacity 0.3s;
1516
min-height: 0;
1617
max-height: 0;
17-
height: 0;
1818
overflow: hidden;
1919
background: rgb(255, 255, 255);
2020
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.35);
2121
border-radius: 4px;
22+
opacity: 0;
2223
font-family: Lucida Grande, sans-serif;
2324

2425
&__item {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@
9898
opacity: 0.2;
9999
font-size: 12px;
100100
cursor: pointer;
101+
position: relative;
101102

102103
.table-grid-icon--sortable {
103104
transition: transform 0.4s;
104105

105106
&-number {
106107
position: absolute;
107-
font-family: Lucida Grande, sans-serif;
108-
font-size: 10px;
109-
font-weight: bold;
108+
font-size: 8px;
109+
font-weight: normal;
110+
right: 0;
110111
}
111112
}
112113

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

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
TableRow,
3636
ViewPortInfo
3737
} from './interfaces/table-builder.external';
38-
import { Fn, KeyMap, PrimaryKey, QueryListRef, ResizeEvent } from './interfaces/table-builder.internal';
38+
import { KeyMap, PrimaryKey, QueryListRef, ResizeEvent } from './interfaces/table-builder.internal';
3939
import { detectChanges } from './operators/detect-changes';
4040
import { ContextMenuService } from './services/context-menu/context-menu.service';
4141
import { DraggableService } from './services/draggable/draggable.service';
@@ -224,6 +224,18 @@ export abstract class TableBuilderApiImpl
224224
return this.source && this.source.length ? this.source : [];
225225
}
226226

227+
private get filterValueExist(): boolean {
228+
return this.filterable.filterValueExist && this.enableFiltering;
229+
}
230+
231+
private get notEmpty(): boolean {
232+
return !this.sortable.empty && !!this.source;
233+
}
234+
235+
private get notChanges(): boolean {
236+
return this.sortable.empty && !this.filterable.filterValueExist;
237+
}
238+
227239
public recheckViewportChecked(): void {
228240
this.tableViewportChecked = !this.tableViewportChecked;
229241
this.idleDetectChanges();
@@ -274,25 +286,19 @@ export abstract class TableBuilderApiImpl
274286
);
275287
}
276288

277-
// eslint-disable-next-line complexity
278289
public async sortAndFilter(): Promise<void> {
279-
this.toggleFreeze();
280-
281-
if (this.filterable.filterValueExist && this.enableFiltering) {
282-
const filter: FilterWorkerEvent = await this.filterable.filter(this.originalSource);
283-
this.source = await this.sortable.sort(filter.source);
284-
filter.fireSelection();
285-
} else if (!this.sortable.empty && this.source) {
286-
this.source = await this.sortable.sort(this.originalSource);
287-
}
288-
289-
if (this.sortable.empty && !this.filterable.filterValueExist) {
290-
this.source = this.originalSource;
291-
}
292-
290+
this.toggleFreeze(true);
291+
await this.recalculationSource();
293292
this.calculateViewport(true);
294-
this.toggleFreeze(TIME_IDLE);
295293
this.onChanges.emit(this.sourceRef);
294+
295+
this.ngZone.runOutsideAngular(
296+
(): void => {
297+
window.setTimeout((): void => {
298+
this.toggleFreeze(false);
299+
}, TIME_IDLE);
300+
}
301+
);
296302
}
297303

298304
public sortByKey(key: string): void {
@@ -307,16 +313,9 @@ export abstract class TableBuilderApiImpl
307313
this.changeSchema();
308314
}
309315

310-
public toggleFreeze(time: number = null, callback: Fn = null): void {
311-
this.isFrozenView = !this.isFrozenView;
312-
if (time) {
313-
window.setTimeout((): void => {
314-
detectChanges(this.cd);
315-
callback && callback();
316-
}, time);
317-
} else {
318-
detectChanges(this.cd);
319-
}
316+
public toggleFreeze(state: boolean): void {
317+
this.isFrozenView = state;
318+
detectChanges(this.cd);
320319
}
321320

322321
public changeSchema(defaultColumns: SimpleSchemaColumns = null): void {
@@ -402,6 +401,20 @@ export abstract class TableBuilderApiImpl
402401

403402
protected abstract updateViewportInfo(start: number, end: number): void;
404403

404+
private async recalculationSource(): Promise<void> {
405+
if (this.filterValueExist) {
406+
const filter: FilterWorkerEvent = await this.filterable.filter(this.originalSource);
407+
this.source = await this.sortable.sort(filter.source);
408+
filter.fireSelection();
409+
} else if (this.notEmpty) {
410+
this.source = await this.sortable.sort(this.originalSource);
411+
}
412+
413+
if (this.notChanges) {
414+
this.source = this.originalSource;
415+
}
416+
}
417+
405418
private calculateWidth(key: string, width: number): void {
406419
this.isDragging[key] = true;
407420
this.onMouseResizeColumn(key, width);

src/app/samples/sample-eleven/sample-eleven.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<br />
4848

4949
<ngx-table-builder #table1 [source]="licences" [enable-selection]="true">
50-
<ngx-context-menu>
50+
<ngx-context-menu [height]="null">
5151
<ngx-context-menu-item contextTitle divider>
5252
<ngx-menu-content icon="https://angular.io/assets/images/logos/angular/angular.svg">
5353
</ngx-menu-content>

src/polyfills.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable simple-import-sort/sort */
12
import 'hammerjs';
23
import 'intersection-observer';
4+
import './zone-flags';
35
import 'zone.js/dist/zone';

src/tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"outDir": "../out-tsc/app",
55
"types": []
66
},
7-
"exclude": ["test.ts", "**/*.spec.ts", "**/*.worker.ts"]
7+
"include": ["main.ts", "polyfills.ts", "zone-flags.ts", "src/**/*.ts"]
88
}

src/tsconfig.spec.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
"outDir": "../out-tsc/spec",
55
"types": ["jest", "node"]
66
},
7-
"files": ["test.ts", "polyfills.ts"],
8-
"include": ["**/*.spec.ts", "**/*.d.ts"]
7+
"include": ["**/*.spec.ts", "main.ts", "polyfills.ts", "zone-flags.ts"]
98
}

0 commit comments

Comments
 (0)