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

feat(gantt): add mainViewport for dom service && add gantt-quick-time-focus-item #497

Closed
wants to merge 4 commits into from
Closed
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
20 changes: 2 additions & 18 deletions packages/gantt/src/components/main/gantt-main.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,9 @@

@if (quickTimeFocus) {
<div class="gantt-quick-time-focus-container" [style.width.px]="ganttUpper.view.width">
<div class="gantt-quick-time-focus" [style.width.px]="dom.visibleRangeX().max - dom.visibleRangeX().min">
<div class="gantt-quick-time-focus" [style.width.px]="dom.mainViewport().clientWidth">
<ng-container *ngFor="let data of viewportItems; let i = index; trackBy: trackBy">
<div class="gantt-quick-time-focus-item" [style.height.px]="ganttUpper.styles.lineHeight">
<span class="ml-2">
@if ((data.refs.x < dom.visibleRangeX().min ) && data.refs.width ) {
<a class="gantt-quick-time-focus-item-arrow link-secondary" href="javascript:;" (click)="quickTime(data.origin, 'left')">
<gantt-icon iconName="arrow-left"></gantt-icon>
</a>
}
</span>

<span class="mr-2">
@if((data.refs.x + data.refs.width > dom.visibleRangeX().max) && data.refs.width) {
<a class="gantt-quick-time-focus-item-arrow link-secondary" href="javascript:;" (click)="quickTime(data.origin, 'right')">
<gantt-icon iconName="arrow-right"></gantt-icon>
</a>
}
</span>
</div>
<gantt-quick-time-focus [mainViewport]="dom.mainViewport()" [item]="data"></gantt-quick-time-focus>
</ng-container>
</div>
</div>
Expand Down
36 changes: 4 additions & 32 deletions packages/gantt/src/components/main/gantt-main.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import { NgxGanttBarComponent } from '../bar/bar.component';
import { NgxGanttRangeComponent } from '../range/range.component';
import { NgFor, NgIf, NgClass, NgTemplateOutlet } from '@angular/common';
import { GanttLinksComponent } from '../links/links.component';
import { NgxGanttRootComponent } from 'ngx-gantt';
import { GanttIconComponent } from '../icon/icon.component';
import { GanttDomService } from '../../gantt-dom.service';
import { combineLatest, from, Subject, take, takeUntil } from 'rxjs';
import { GanttQuickTimeFocusItemComponent } from '../quick-time-focus/quick-time-focus.component';

@Component({
selector: 'gantt-main',
Expand All @@ -28,10 +26,10 @@ import { combineLatest, from, Subject, take, takeUntil } from 'rxjs';
IsGanttRangeItemPipe,
IsGanttBarItemPipe,
IsGanttCustomItemPipe,
GanttIconComponent
GanttQuickTimeFocusItemComponent
]
})
export class GanttMainComponent implements OnInit {
export class GanttMainComponent {
@Input() viewportItems: (GanttGroupInternal | GanttItemInternal)[];

@Input() flatItems: (GanttGroupInternal | GanttItemInternal)[];
Expand All @@ -46,8 +44,6 @@ export class GanttMainComponent implements OnInit {

@Input() baselineTemplate: TemplateRef<any>;

@Input() ganttRoot: NgxGanttRootComponent;

@Input() quickTimeFocus: boolean;

@Output() barClick = new EventEmitter<GanttBarClickEvent>();
Expand All @@ -56,33 +52,9 @@ export class GanttMainComponent implements OnInit {

@HostBinding('class.gantt-main-container') ganttMainClass = true;

private unsubscribe$ = new Subject<void>();

constructor(@Inject(GANTT_UPPER_TOKEN) public ganttUpper: GanttUpper, public dom: GanttDomService, protected ngZone: NgZone) {}

ngOnInit(): void {
const onStable$ = this.ngZone.isStable ? from(Promise.resolve()) : this.ngZone.onStable.pipe(take(1));
this.ngZone.runOutsideAngular(() => {
onStable$.pipe(takeUntil(this.unsubscribe$)).subscribe(() => {
this.setupResize();
});
});
}
constructor(@Inject(GANTT_UPPER_TOKEN) public ganttUpper: GanttUpper, public dom: GanttDomService) {}

trackBy(index: number, item: GanttGroupInternal | GanttItemInternal) {
return item.id || index;
}

private setupResize() {
combineLatest([this.dom.getResize(), this.dom.getResizeByElement(this.dom.mainContainer)])
.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => {
this.dom.setVisibleRangeX();
});
}

quickTime(item: GanttItemInternal, type: 'left' | 'right') {
const date = type === 'left' ? item.start || item.end : item.end || item.start;
this.ganttRoot.scrollToDate(date);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<span class="ml-2">
@if ((item.refs?.x < mainViewport().leftBoundary ) && item.refs?.width ) {
<a class="gantt-quick-time-focus-item-arrow link-secondary" href="javascript:;" (click)="quickTime(item, 'left')">
<gantt-icon iconName="arrow-left"></gantt-icon>
</a>
}
</span>

<span class="mr-2">
@if((item.refs?.x + item.refs?.width > mainViewport().rightBoundary) && item.refs.width) {
<a class="gantt-quick-time-focus-item-arrow link-secondary" href="javascript:;" (click)="quickTime(item, 'right')">
<gantt-icon iconName="arrow-right"></gantt-icon>
</a>
}
</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Component, HostBinding, Inject, Input, input, InputSignal } from '@angular/core';
import { GanttItemInternal } from '../../class/item';
import { GANTT_UPPER_TOKEN, GanttUpper } from '../../gantt-upper';
import { GanttIconComponent } from '../icon/icon.component';

@Component({
selector: 'gantt-quick-time-focus',
templateUrl: './quick-time-focus.component.html',
standalone: true,
imports: [GanttIconComponent]
})
export class GanttQuickTimeFocusItemComponent {
@HostBinding('class.gantt-quick-time-focus-item') class = true;

@HostBinding('style.height')
get height() {
return this.ganttUpper.styles.lineHeight + 'px';
}

mainViewport: InputSignal<{ clientWidth?: number; leftBoundary?: number; rightBoundary?: number }> = input();

@Input() item: GanttItemInternal;

constructor(@Inject(GANTT_UPPER_TOKEN) public ganttUpper: GanttUpper) {}

quickTime(item: GanttItemInternal, type: 'left' | 'right') {
const date = type === 'left' ? item.start || item.end : item.end || item.start;
this.ganttUpper.scrollToDate(date);
}
}
35 changes: 26 additions & 9 deletions packages/gantt/src/gantt-dom.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isPlatformServer } from '@angular/common';
import { Injectable, ElementRef, OnDestroy, Inject, PLATFORM_ID, NgZone, WritableSignal, signal } from '@angular/core';
import { fromEvent, Subject, merge, EMPTY, Observable } from 'rxjs';
import { pairwise, map, auditTime, takeUntil } from 'rxjs/operators';
import { Injectable, ElementRef, OnDestroy, Inject, PLATFORM_ID, NgZone, WritableSignal, signal, InjectionToken } from '@angular/core';
import { fromEvent, Subject, merge, EMPTY, Observable, combineLatest } from 'rxjs';
import { pairwise, map, auditTime, takeUntil, startWith } from 'rxjs/operators';
import { isNumber } from './utils/helpers';
import { passiveListenerOptions } from './utils/passive-listeners';

Expand Down Expand Up @@ -40,7 +40,7 @@ export class GanttDomService implements OnDestroy {

public linksOverlay: Element;

public visibleRangeX: WritableSignal<{ min: number; max: number }> = signal({ min: 0, max: 0 });
public mainViewport: WritableSignal<{ clientWidth?: number; leftBoundary?: number; rightBoundary?: number }> = signal({});

private mainFooter: Element;

Expand Down Expand Up @@ -125,6 +125,7 @@ export class GanttDomService implements OnDestroy {

this.monitorScrollChange();
this.disableBrowserWheelEvent();
this.monitorMainViewPortChange();
}

/**
Expand All @@ -143,7 +144,6 @@ export class GanttDomService implements OnDestroy {
map(() => this.mainContainer.scrollLeft),
pairwise(),
map(([previous, current]) => {
this.setVisibleRangeX();
const event: ScrollEvent = {
target: this.mainContainer,
direction: ScrollDirection.NONE
Expand Down Expand Up @@ -193,10 +193,27 @@ export class GanttDomService implements OnDestroy {
}
}

setVisibleRangeX() {
this.visibleRangeX.set({
min: this.mainContainer.scrollLeft,
max: this.mainContainer.scrollLeft + this.mainContainer.clientWidth
monitorMainViewPortChange() {
combineLatest([
this.getResize().pipe(startWith(null)),
this.getResizeByElement(this.mainContainer),
this.setUpMainContainer(passiveListenerOptions)
])
.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => {
this.setMainViewPort();
});
}

setUpMainContainer(options: AddEventListenerOptions) {
return fromEvent(this.mainContainer, 'scroll', options);
}

setMainViewPort() {
this.mainViewport.set({
clientWidth: this.mainContainer.clientWidth,
leftBoundary: this.mainContainer.scrollLeft,
rightBoundary: this.mainContainer.scrollLeft + this.mainContainer.clientWidth
});
}

Expand Down
18 changes: 16 additions & 2 deletions packages/gantt/src/gantt-upper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
Inject,
OnInit,
OnDestroy,
OnChanges
OnChanges,
Optional
} from '@angular/core';
import { from, Subject } from 'rxjs';
import { takeUntil, take, skip } from 'rxjs/operators';
Expand All @@ -41,6 +42,7 @@ import { SelectionModel } from '@angular/cdk/collections';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { GanttBaselineItem, GanttBaselineItemInternal } from './class/baseline';
import { NgxGanttTableComponent } from './table/gantt-table.component';
import { GanttDomService } from './gantt-dom.service';

@Directive()
export abstract class GanttUpper implements OnChanges, OnInit, OnDestroy {
Expand Down Expand Up @@ -185,7 +187,8 @@ export abstract class GanttUpper implements OnChanges, OnInit, OnDestroy {
protected elementRef: ElementRef<HTMLElement>,
protected cdr: ChangeDetectorRef,
protected ngZone: NgZone,
@Inject(GANTT_GLOBAL_CONFIG) public config: GanttGlobalConfig
@Inject(GANTT_GLOBAL_CONFIG) public config: GanttGlobalConfig,
@Optional() public dom?: GanttDomService
) {}

private createView() {
Expand Down Expand Up @@ -453,6 +456,17 @@ export abstract class GanttUpper implements OnChanges, OnInit, OnDestroy {
rerenderView() {
this.changeView(this.viewType);
}

scrollToDate(date: number | GanttDate) {
let x: number;
if (typeof date === 'number') {
x = this.view.getXPointByDate(new GanttDate(date));
} else {
x = this.view.getXPointByDate(date);
}

this.dom.scrollMainContainer(x);
}
}

export const GANTT_UPPER_TOKEN = new InjectionToken<GanttUpper>('GANTT_UPPER_TOKEN');
1 change: 0 additions & 1 deletion packages/gantt/src/gantt.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
></gantt-calendar-grid>
<div class="gantt-main">
<gantt-main
[ganttRoot]="ganttRoot"
[flatItems]="flatItems"
[viewportItems]="viewportItems"
[groupHeaderTemplate]="groupHeaderTemplate"
Expand Down
10 changes: 4 additions & 6 deletions packages/gantt/src/gantt.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { GANTT_GLOBAL_CONFIG, GanttGlobalConfig } from './gantt.config';
import { NgxGanttRootComponent } from './root.component';
import { NgxGanttTableColumnComponent } from './table/gantt-column.component';
import { NgxGanttTableComponent } from './table/gantt-table.component';
import { GanttDate } from './utils/date';
import { Dictionary, keyBy, recursiveItems, uniqBy } from './utils/helpers';
import { GanttScrollbarComponent } from './components/scrollbar/scrollbar.component';

Expand Down Expand Up @@ -165,7 +164,7 @@ export class NgxGanttComponent extends GanttUpper implements OnInit, OnChanges,
private viewportRuler: ViewportRuler,
@Inject(GANTT_GLOBAL_CONFIG) config: GanttGlobalConfig
) {
super(elementRef, cdr, ngZone, config);
super(elementRef, cdr, ngZone, config, null);
this.computeAllRefs = false;
}

Expand Down Expand Up @@ -226,6 +225,9 @@ export class NgxGanttComponent extends GanttUpper implements OnInit, OnChanges,
this.computeTempDataRefs();
});
}
if (this.ganttRoot && this.ganttRoot.dom) {
this.dom = this.ganttRoot.dom;
}
}

ngAfterViewChecked() {
Expand Down Expand Up @@ -363,10 +365,6 @@ export class NgxGanttComponent extends GanttUpper implements OnInit, OnChanges,
this.ganttRoot.scrollToToday();
}

scrollToDate(date: number | GanttDate) {
this.ganttRoot.scrollToDate(date);
}

scrolledIndexChange(index: number) {
this.virtualScrolledIndexChange.emit({
index,
Expand Down
5 changes: 4 additions & 1 deletion packages/gantt/src/gantt.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { NgxGanttRootComponent } from './root.component';
import { NgxGanttTableColumnComponent } from './table/gantt-column.component';
import { NgxGanttTableComponent } from './table/gantt-table.component';
import { GanttScrollbarComponent } from './components/scrollbar/scrollbar.component';
import { GanttQuickTimeFocusItemComponent } from './components/quick-time-focus/quick-time-focus.component';

@NgModule({
imports: [
Expand All @@ -47,6 +48,7 @@ import { GanttScrollbarComponent } from './components/scrollbar/scrollbar.compon
NgxGanttBaselineComponent,
NgxGanttToolbarComponent,
GanttScrollbarComponent,
GanttQuickTimeFocusItemComponent,
IsGanttRangeItemPipe,
IsGanttBarItemPipe,
IsGanttCustomItemPipe
Expand All @@ -63,7 +65,8 @@ import { GanttScrollbarComponent } from './components/scrollbar/scrollbar.compon
GanttCalendarHeaderComponent,
GanttCalendarGridComponent,
GanttDragBackdropComponent,
GanttScrollbarComponent
GanttScrollbarComponent,
GanttQuickTimeFocusItemComponent
],
providers: [
CdkVirtualScrollViewport,
Expand Down
1 change: 1 addition & 0 deletions packages/gantt/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export * from './components/calendar/header/calendar-header.component';
export * from './components/calendar/grid/calendar-grid.component';
export * from './components/drag-backdrop/drag-backdrop.component';
export * from './components/scrollbar/scrollbar.component';
export * from './components/quick-time-focus/quick-time-focus.component';
11 changes: 2 additions & 9 deletions packages/gantt/src/root.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class NgxGanttRootComponent implements OnInit, OnDestroy {
constructor(
private elementRef: ElementRef<HTMLElement>,
private ngZone: NgZone,
private dom: GanttDomService,
public dom: GanttDomService,
public dragContainer: GanttDragContainer,
@Inject(GANTT_UPPER_TOKEN) public ganttUpper: GanttUpper,
@Optional() private printService: GanttPrintService
Expand Down Expand Up @@ -182,13 +182,6 @@ export class NgxGanttRootComponent implements OnInit, OnDestroy {
}

public scrollToDate(date: number | GanttDate) {
let x: number;
if (typeof date === 'number') {
x = this.view.getXPointByDate(new GanttDate(date));
} else {
x = this.view.getXPointByDate(date);
}

this.dom.scrollMainContainer(x);
this.ganttUpper.scrollToDate(date);
}
}