Skip to content

Commit

Permalink
fix: 当开始时间或者截止时间未设时,甘特图中显示的默认bar的长度应该因视图不同而不同(#INFR-2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengshuicmq authored and HandsomeButterball committed Jun 30, 2021
1 parent 4501406 commit 1c31fc6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 21 additions & 5 deletions packages/gantt/src/class/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GanttDate } from '../utils/date';
import { BehaviorSubject } from 'rxjs';
import { GanttViewType } from './view-type';

interface GanttItemRefs {
width: number;
Expand Down Expand Up @@ -49,14 +50,15 @@ export class GanttItemInternal {
children: GanttItemInternal[];
type?: GanttItemType;
progress?: number;
viewType?: GanttViewType;

get refs() {
return this.refs$.getValue();
}

refs$ = new BehaviorSubject<{ width: number; x?: number; y?: number }>(null);

constructor(item: GanttItem) {
constructor(item: GanttItem, options?: { viewType: GanttViewType }) {
this.origin = item;
this.id = this.origin.id;
this.links = this.origin.links || [];
Expand All @@ -68,20 +70,34 @@ export class GanttItemInternal {
this.expanded = this.origin.expanded === undefined ? false : this.origin.expanded;
this.start = item.start ? new GanttDate(item.start) : null;
this.end = item.end ? new GanttDate(item.end) : null;
this.viewType = options && options.viewType ? options.viewType : GanttViewType.month;
this.children = (item.children || []).map((subItem) => {
return new GanttItemInternal(subItem);
return new GanttItemInternal(subItem, { viewType: this.viewType });
});
this.type = this.origin.type || GanttItemType.bar;
this.progress = this.origin.progress;
// fill one month when start or end is null
if (item.start && !item.end) {
this.end = new GanttDate(item.start).addMonths(1).endOfDay();
this.end = this.fillItemByViewType(item.start, true);
}
if (!item.start && item.end) {
this.start = new GanttDate(item.end).addMonths(-1).startOfDay();
this.start = this.fillItemByViewType(item.end, false);
}
}

fillItemByViewType(date: Date | string | number, hasStart?: boolean) {
let _date: GanttDate;
switch (this.viewType) {
case GanttViewType.day:
_date = hasStart ? new GanttDate(date).addDays(7).endOfDay() : new GanttDate(date).addMonths(-7).startOfDay();
break;
default:
_date = hasStart ? new GanttDate(date).addMonths(1).endOfDay() : new GanttDate(date).addMonths(-1).startOfDay();
break;
}
return _date;
}

updateRefs(refs: GanttItemRefs) {
this.refs$.next(refs);
}
Expand All @@ -96,7 +112,7 @@ export class GanttItemInternal {
addChildren(items: GanttItem[]) {
this.origin.children = items;
this.children = (items || []).map((subItem) => {
return new GanttItemInternal(subItem);
return new GanttItemInternal(subItem, { viewType: this.viewType });
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/gantt/src/gantt-upper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ export abstract class GanttUpper {
this.originItems.forEach((origin) => {
const group = this.groupsMap[origin.group_id];
if (group) {
const item = new GanttItemInternal(origin);
const item = new GanttItemInternal(origin, { viewType: this.viewType });
group.items.push(item);
}
});
} else {
this.originItems.forEach((origin) => {
const item = new GanttItemInternal(origin);
const item = new GanttItemInternal(origin, { viewType: this.viewType });
this.items.push(item);
});
}
Expand Down

0 comments on commit 1c31fc6

Please sign in to comment.