-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc9969b
commit 44f0f9c
Showing
6 changed files
with
127 additions
and
25 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,92 @@ | ||
export default () => { | ||
import { XDate } from '@/models/param/date'; | ||
import { isDate, isUndefined } from 'lodash'; | ||
import { type Ref, type DefineComponent } from 'vue'; | ||
import useEvent from './useEvent'; | ||
import useGanttHeader from './useGanttHeader'; | ||
import useGanttWidth from './useGanttWidth'; | ||
import useToday from './useToday'; | ||
|
||
export default (ganttRef: Ref<DefineComponent | null>) => { | ||
const { isInArea } = useToday(); | ||
const { EmitNoDateError } = useEvent(); | ||
const { ganttHeader } = useGanttHeader(); | ||
const { ganttColumnWidth, currentMillisecond } = useGanttWidth(); | ||
|
||
/** | ||
* 跳转到某个日期 | ||
*/ | ||
function handleJumpTo(_date: Date | undefined) { | ||
if (!ganttRef.value) return; | ||
|
||
let date: XDate; | ||
|
||
// 未定义日期,默认跳转到今天 | ||
if (isUndefined(_date) || !isDate(_date)) { | ||
date = new XDate(); | ||
} else { | ||
date = new XDate(_date); | ||
} | ||
|
||
if (!isInArea(date)) { | ||
EmitNoDateError(); | ||
return; | ||
} | ||
|
||
date.startOf(ganttHeader.unit); | ||
const width = | ||
(date.intervalTo(ganttHeader.start) / currentMillisecond.value) * | ||
ganttColumnWidth.value; | ||
|
||
let left = 0; | ||
let right = 0; | ||
|
||
if (ganttRef.value.$el.scrollLeft < width) { | ||
// 日期在右侧 | ||
right = width - ganttRef.value.$el.clientWidth / 3; | ||
} else { | ||
// 日期在左侧 | ||
left = Math.abs(ganttRef.value.$el.clientWidth / 6 - width); | ||
} | ||
|
||
// 滚动动画,ease-in模式 | ||
if (left && right) return; | ||
|
||
const duration = 1000; | ||
const distance = left || right; | ||
let oldTimestamp: number | null = null; | ||
let scrollX = 0; | ||
let oldLeft: number | undefined; // 初始不定义,保证第一次不会匹配 | ||
|
||
function step(newTimestamp: number) { | ||
if (oldTimestamp !== null && ganttRef.value) { | ||
// if duration is 0 scrollX will be -Infinity | ||
if ( | ||
ganttRef.value.$el.scrollLeft < left || | ||
(right > 0 && ganttRef.value.$el.scrollLeft >= right) || | ||
oldLeft === ganttRef.value.$el.scrollLeft | ||
) | ||
return; | ||
|
||
const x = (distance * (newTimestamp - oldTimestamp)) / duration; | ||
if (left) { | ||
scrollX -= x; | ||
} else if (right) { | ||
scrollX += x; | ||
} else { | ||
return; | ||
} | ||
oldLeft = ganttRef.value.$el.scrollLeft; | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
ganttRef.value.$el.scrollLeft += scrollX; | ||
} | ||
oldTimestamp = newTimestamp; | ||
window.requestAnimationFrame(step); | ||
} | ||
window.requestAnimationFrame(step); | ||
} | ||
|
||
return { | ||
setSelected: () => {}, | ||
jumpToDate: () => {}, | ||
setHeaderUnit: () => {} | ||
jumpToDate: handleJumpTo | ||
}; | ||
}; |
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