-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support week view and add test(#INFR-1996) (#113)
* fix: remove console.log * fix: fix review code(#INFR-1996) * fix: fix test erro * feat: fix test * fix(view): fix week view cellWidth Co-authored-by: zhangwen <1062680993@qq.com>
- Loading branch information
1 parent
febfaaa
commit ac1c252
Showing
7 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { GanttDate } from '../../utils/date'; | ||
import { GanttViewWeek } from '../week'; | ||
import { date, today } from './mock'; | ||
|
||
describe('GanttViewWeek', () => { | ||
let ganttViewWeek: GanttViewWeek; | ||
|
||
beforeEach(() => { | ||
ganttViewWeek = new GanttViewWeek(date.start, date.end, { | ||
cellWidth: 140, | ||
start: today.startOfYear().startOfWeek({ weekStartsOn: 1 }), | ||
end: today.endOfYear().endOfWeek({ weekStartsOn: 1 }) | ||
}); | ||
}); | ||
|
||
it(`should has correct view start`, () => { | ||
const startOfWeek = ganttViewWeek.startOf(date.start.date).getUnixTime(); | ||
expect(startOfWeek).toEqual(new GanttDate('2019-12-30 00:00:00').getUnixTime()); | ||
}); | ||
|
||
it(`should has correct view end`, () => { | ||
const endOfWeek = ganttViewWeek.endOf(date.end.date).getUnixTime(); | ||
expect(endOfWeek).toEqual(new GanttDate('2021-01-03 23:59:59').getUnixTime()); | ||
}); | ||
|
||
it(`should has correct cell width`, () => { | ||
const weekCellWidth = ganttViewWeek.getDayOccupancyWidth(); | ||
expect(weekCellWidth).toEqual(20); | ||
}); | ||
|
||
it(`should has correct primary width`, () => { | ||
const weekPrimaryWidth = ganttViewWeek.getPrimaryWidth(); | ||
expect(weekPrimaryWidth).toEqual(140); | ||
}); | ||
|
||
it(`should has correct primary date points`, () => { | ||
const weekPoints = ganttViewWeek.getPrimaryDatePoints(); | ||
expect(weekPoints.length).toBe(54); | ||
}); | ||
|
||
it(`should has correct secondary date points`, () => { | ||
const weekPoints = ganttViewWeek.getSecondaryDatePoints(); | ||
expect(weekPoints.length).toBe(54); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { GanttDatePoint } from '../class/date-point'; | ||
import { eachWeekOfInterval, GanttDate } from '../utils/date'; | ||
import { GanttView, GanttViewDate, GanttViewOptions, primaryDatePointTop, secondaryDatePointTop } from './view'; | ||
|
||
const viewOptions: GanttViewOptions = { | ||
cellWidth: 280, | ||
start: new GanttDate().startOfYear().startOfWeek({ weekStartsOn: 1 }), | ||
end: new GanttDate().endOfYear().endOfWeek({ weekStartsOn: 1 }), | ||
addAmount: 1, | ||
addUnit: 'month' | ||
}; | ||
|
||
export class GanttViewWeek extends GanttView { | ||
constructor(start: GanttViewDate, end: GanttViewDate, options?: GanttViewOptions) { | ||
super(start, end, Object.assign({}, viewOptions, options)); | ||
} | ||
|
||
startOf(date: GanttDate) { | ||
return date.startOfWeek({ weekStartsOn: 1 }); | ||
} | ||
|
||
endOf(date: GanttDate) { | ||
return date.endOfWeek({ weekStartsOn: 1 }); | ||
} | ||
|
||
getPrimaryWidth() { | ||
return this.getCellWidth(); | ||
} | ||
|
||
getDayOccupancyWidth(): number { | ||
return this.cellWidth / 7; | ||
} | ||
|
||
getPrimaryDatePoints(): GanttDatePoint[] { | ||
const weeks = eachWeekOfInterval({ start: this.start.value, end: this.end.addSeconds(1).value }, { weekStartsOn: 1 }); | ||
const points: GanttDatePoint[] = []; | ||
for (let i = 0; i < weeks.length; i++) { | ||
const weekStart = new GanttDate(weeks[i]); | ||
const increaseWeek = weekStart.getDaysInMonth() - weekStart.getDate() >= 3 ? 0 : 1; | ||
const point = new GanttDatePoint( | ||
weekStart, | ||
weekStart.addWeeks(increaseWeek).format('yyyy年'), | ||
this.getCellWidth() / 2 + i * this.getCellWidth(), | ||
primaryDatePointTop | ||
); | ||
points.push(point); | ||
} | ||
return points; | ||
} | ||
|
||
getSecondaryDatePoints(): GanttDatePoint[] { | ||
const weeks = eachWeekOfInterval({ start: this.start.value, end: this.end.value }); | ||
const points: GanttDatePoint[] = []; | ||
for (let i = 0; i < weeks.length; i++) { | ||
const start = new GanttDate(weeks[i]); | ||
const point = new GanttDatePoint( | ||
start, | ||
`第${start.format('w')}周`, | ||
i * this.getCellWidth() + this.getCellWidth() / 2, | ||
secondaryDatePointTop | ||
); | ||
points.push(point); | ||
} | ||
return points; | ||
} | ||
} |