-
Notifications
You must be signed in to change notification settings - Fork 138
/
index.vue
233 lines (221 loc) · 5.75 KB
/
index.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<template>
<div class="gantt-timeline" :style="{ 'margin-left': -cellWidth / 2 + 'px' }">
<div
v-if="lazy"
class="gantt-timeline-padding_block"
:style="{ width: paddingWidth + 'px' }"
></div>
<template v-for="(day, index) in allDayBlocks">
<div
class="gantt-timeline-block"
v-if="!lazy || isInRenderingDayRange(day)"
:style="{ width: getTimeScales(day).length * cellWidth + 'px' }"
:key="index"
>
<slot :day="day" :getTimeScales="getTimeScales">
<div class="gantt-timeline-day " :style="heightStyle">
{{ day.format("MM/DD") }}
</div>
<div
v-if="!isDayScale"
class="gantt-timeline-scale "
:style="heightStyle"
>
<div
:style="cellWidthStyle"
v-for="(time, index) in getTimeScales(day)"
:key="index"
>
{{ scale >= 60 ? time.format("HH") : time.format("HH:mm") }}
</div>
</div>
</slot>
</div>
</template>
</div>
</template>
<script>
import dayjs from "dayjs";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
import isBetween from "dayjs/plugin/isBetween";
dayjs.extend(isSameOrBefore);
dayjs.extend(isSameOrAfter);
dayjs.extend(isBetween);
import {
isDayScale,
MINUTE_OF_ONE_DAY,
getBeginTimeOfTimeLine
} from "../../utils/timeLineUtils.js";
const START_DAY = Symbol();
const MIDDLE_DAY = Symbol();
const END_DAY = Symbol();
function isSameDay(one, two) {
return one.isSame(two, "day");
}
function isSameOrBetween(start, end, mid) {
return mid.isSameOrAfter(start) && mid.isSameOrBefore(end);
}
export default {
name: "Timeline",
props: {
start: {
type: dayjs
},
end: {
type: dayjs
},
cellWidth: {
type: Number
},
titleHeight: {
type: Number
},
scale: {
type: Number
},
endTimeOfRenderArea: [dayjs, null],
startTimeOfRenderArea: [dayjs, null],
getPositonOffset: {
type: Function
},
lazy: {
type: Boolean,
default: true
}
},
computed: {
startDayOfRenderArea() {
return this.startTimeOfRenderArea.startOf("day");
},
endDayOfRenderArea() {
return this.endTimeOfRenderArea.endOf("day");
},
paddingWidth() {
const { allDayBlocks, scale, startDayOfRenderArea } = this;
const temp = allDayBlocks.find((day) => {
if (
scale >= MINUTE_OF_ONE_DAY &&
startDayOfRenderArea.isBetween(
day,
day.add(scale / MINUTE_OF_ONE_DAY, "day")
)
) {
return true;
} else {
return isSameDay(day, startDayOfRenderArea);
}
});
if (!temp || temp == allDayBlocks[0]) {
return 0;
} else {
return this.getPositonOffset(temp.toString());
}
},
isDayScale() {
const { scale } = this;
return isDayScale(scale);
},
/**
* 天列表
* @returns {[dayjs]} 该data中所有需要渲染的数据
*/
allDayBlocks() {
const temp = [];
let { start, end, scale, isDayScale } = this;
let tempStart = start.clone().startOf("day");
let addNum =
isDayScale && scale > MINUTE_OF_ONE_DAY ? scale / MINUTE_OF_ONE_DAY : 1;
while (tempStart.isSameOrBefore(end)) {
temp.push(tempStart);
tempStart = tempStart.add(addNum, "day");
}
return temp;
},
cellWidthStyle() {
return {
width: `${this.cellWidth}px`
};
},
heightStyle() {
return {
height: this.titleHeight / (this.isDayScale ? 1 : 2) + "px",
"line-height": this.titleHeight / (this.isDayScale ? 1 : 2) + "px"
};
}
},
methods: {
isInRenderingDayRange(day) {
const { startDayOfRenderArea, endDayOfRenderArea, scale } = this;
if (
scale >= MINUTE_OF_ONE_DAY &&
startDayOfRenderArea.isBetween(
day,
day.add(scale / MINUTE_OF_ONE_DAY, "day")
)
) {
return true;
} else if (
isSameOrBetween(startDayOfRenderArea, endDayOfRenderArea, day)
) {
return true;
} else {
return false;
}
},
/**
* 获取时间刻度数组
*
* @param {dayjs} date
* @returns {[string]} 该data中所有需要渲染的数据
*/
getTimeScales(date) {
const { start, end } = this;
if (isSameDay(date, start)) {
return this.generateTimeScale(START_DAY);
} else if (isSameDay(date, end)) {
return this.generateTimeScale(END_DAY);
} else {
return this.generateTimeScale(MIDDLE_DAY);
}
},
/**
* 生成时间刻度数组
*
* @param {Symbol} type
* @returns {[string]} 该data中所有需要渲染的数据
*/
generateTimeScale(type) {
const totalblock = [];
const { start, end, scale } = this;
let a, b;
switch (type) {
case START_DAY: //和start同一天
a = getBeginTimeOfTimeLine(start, scale);
//start和end同一天特殊处理
if (isSameDay(start, end)) {
b = end;
} else {
b = start.endOf("day");
}
break;
case END_DAY: //和end 同一天
a = end.startOf("day");
b = end;
break;
case MIDDLE_DAY: //start和end中间的天
a = start.startOf("day");
b = start.endOf("day");
break;
default:
throw new TypeError("错误的计算类型");
}
while (!a.isAfter(b)) {
totalblock.push(a);
a = a.add(scale, "minute");
}
return totalblock;
}
}
};
</script>