2
0
mirror of https://github.com/tenrok/vue-ganttastic.git synced 2026-06-24 18:00:34 +03:00

fix: remove inject in grid and timeaxis

This commit is contained in:
2021-10-12 16:50:02 +03:00
parent e90a2ba13f
commit feccc27a5d
4 changed files with 88 additions and 63 deletions
+15 -6
View File
@@ -11,6 +11,9 @@
:timemarker-offset="timemarkerOffset" :timemarker-offset="timemarkerOffset"
:theme-colors="themeColors" :theme-colors="themeColors"
:locale="locale" :locale="locale"
:precision="precision"
:time-format="timeFormat"
:time-count="timeCount"
/> />
<g-gantt-grid <g-gantt-grid
@@ -19,6 +22,8 @@
:chart-end="chartEnd" :chart-end="chartEnd"
:row-label-width="rowLabelWidth" :row-label-width="rowLabelWidth"
:highlighted-hours="highlightedHours" :highlighted-hours="highlightedHours"
:precision="precision"
:time-count="timeCount"
/> />
<div <div
@@ -54,7 +59,7 @@ export default {
props: { props: {
chartStart: { type: String }, chartStart: { type: String },
chartEnd: { type: String }, chartEnd: { type: String },
hideTimeaxis: Boolean, hideTimeaxis: { type: Boolean },
rowLabelWidth: { type: String, default: '200px' }, rowLabelWidth: { type: String, default: '200px' },
rowHeight: { type: Number, default: 40 }, rowHeight: { type: Number, default: 40 },
locale: { type: String, default: 'en' }, locale: { type: String, default: 'en' },
@@ -67,8 +72,7 @@ export default {
snapBackOnOverlap: { type: Boolean }, snapBackOnOverlap: { type: Boolean },
minGapBetweenBars: { type: Number, default: 0 }, minGapBetweenBars: { type: Number, default: 0 },
defaultBarLength: { type: Number, default: 1 }, defaultBarLength: { type: Number, default: 1 },
// ["month", "day"] precision: { type: String, default: 'month' }, // 'month', 'day'
precision: { type: String, default: 'month' },
barStartKey: { type: String, default: 'start' }, // property name of the bar objects that represents the start datetime barStartKey: { type: String, default: 'start' }, // property name of the bar objects that represents the start datetime
barEndKey: { type: String, default: 'end' }, // property name of the bar objects that represents the end datetime, barEndKey: { type: String, default: 'end' }, // property name of the bar objects that represents the end datetime,
}, },
@@ -77,13 +81,18 @@ export default {
return { return {
timemarkerOffset: 0, timemarkerOffset: 0,
movedBarsInDrag: new Set(), movedBarsInDrag: new Set(),
timeUnit: this.precision === 'month' ? 'days' : 'hours',
timeFormat:
this.precision === 'month' ? 'YYYY-MM-DD HH' : 'YYYY-MM-DD HH:mm',
} }
}, },
computed: { computed: {
timeUnit() {
return this.precision === 'month' ? 'days' : 'hours'
},
timeFormat() {
return this.precision === 'month' ? 'YYYY-MM-DD HH' : 'YYYY-MM-DD HH:mm'
},
timeCount() { timeCount() {
let momentChartStart = moment(this.chartStart) let momentChartStart = moment(this.chartStart)
let momentChartEnd = moment(this.chartEnd) let momentChartEnd = moment(this.chartEnd)
+20 -15
View File
@@ -3,16 +3,19 @@
class="g-grid-container" class="g-grid-container"
:style="{ :style="{
left: rowLabelWidth, left: rowLabelWidth,
width: `${getTimeCount() * 30}px`, width: `${timeCount * 30}px`,
}" }"
> >
<div <div
v-for="(childPoint, index) in allChildPoints" v-for="(childPoint, index) in allChildPoints"
:key="index" :key="index"
:class="{ :class="[
'g-grid-line': true, 'g-grid-line',
'g-grid-line-highlighted': highlightedHours.includes(childPoint), {
}" 'g-grid-line-highlighted':
precision === 'day' && highlightedHours.includes(childPoint),
},
]"
/> />
</div> </div>
</template> </template>
@@ -23,13 +26,13 @@ import moment from 'moment'
export default { export default {
name: 'GGanttGrid', name: 'GGanttGrid',
inject: ['ganttChartProps', 'getTimeCount'],
props: { props: {
chartStart: { type: String }, chartStart: { type: String },
chartEnd: { type: String }, chartEnd: { type: String },
rowLabelWidth: String, rowLabelWidth: { type: String },
highlightedHours: { type: Array, default: () => [] }, highlightedHours: { type: Array, default: () => [] },
precision: { type: String },
timeCount: { type: Number },
}, },
computed: { computed: {
@@ -37,14 +40,16 @@ export default {
let momentChartStart = moment(this.chartStart) let momentChartStart = moment(this.chartStart)
let momentChartEnd = moment(this.chartEnd) let momentChartEnd = moment(this.chartEnd)
let res = [] let res = []
const precision = this.ganttChartProps.precision
while (momentChartStart.isSameOrBefore(momentChartEnd)) { while (momentChartStart.isSameOrBefore(momentChartEnd)) {
if (precision === 'month') { switch (this.precision) {
res.push(momentChartStart.date()) case 'day':
momentChartStart.add(1, 'day') res.push(momentChartStart.date())
} else if (precision === 'day') { momentChartStart.add(1, 'hour')
res.push(momentChartStart.date()) break
momentChartStart.add(1, 'hour') case 'month':
res.push(momentChartStart.date())
momentChartStart.add(1, 'day')
break
} }
} }
return res return res
+50 -40
View File
@@ -3,9 +3,7 @@
ref="g-timeaxis" ref="g-timeaxis"
class="g-timeaxis" class="g-timeaxis"
:style="{ :style="{
width: `${ width: `${timeCount * 30 + parseInt(rowLabelWidth.replace('px', ''))}px`,
getTimeCount() * 30 + parseInt(rowLabelWidth.replace('px', ''))
}px`,
}" }"
> >
<div <div
@@ -59,15 +57,16 @@ import moment from 'moment'
export default { export default {
name: 'GGanttTimeaxis', name: 'GGanttTimeaxis',
inject: ['ganttChartProps', 'getTimeUnit', 'getTimeFormat', 'getTimeCount'],
props: { props: {
chartStart: String, chartStart: { type: String },
chartEnd: String, chartEnd: { type: String },
rowLabelWidth: String, rowLabelWidth: { type: String },
timemarkerOffset: { type: Number, default: 0 }, timemarkerOffset: { type: Number, default: 0 },
locale: String, locale: { type: String },
themeColors: Object, themeColors: { type: Object },
precision: { type: String },
timeFormat: { type: String },
timeCount: { type: Number },
}, },
data() { data() {
@@ -77,12 +76,19 @@ export default {
timemarker: null, timemarker: null,
hourFontSize: '11px', hourFontSize: '11px',
dayFormat: 'DD MMMM', dayFormat: 'DD MMMM',
precision: this.ganttChartProps.precision, monthFormat: 'MMMM YYYY',
timeUnit: this.getTimeUnit(),
timeFormat: this.getTimeFormat(),
} }
}, },
watch: {
chartStart() {
this.initAxis()
},
chartEnd() {
this.initAxis()
},
},
mounted() { mounted() {
this.timemarker = this.$refs['g-timeaxis-marker'] this.timemarker = this.$refs['g-timeaxis-marker']
this.initAxis() this.initAxis()
@@ -92,19 +98,27 @@ export default {
window.addEventListener('dragover', (event) => this.moveTimemarker(event)) window.addEventListener('dragover', (event) => this.moveTimemarker(event))
}, },
destroyed() {
window.removeEventListener('resize', this.onWindowResize)
},
methods: { methods: {
initAxis() { initAxis() {
if (this.precision === 'month') { switch (this.precision) {
this.initAxisMonthsAndDays() case 'day':
} else if (this.precision === 'day') { this.initAxisDaysAndHours()
this.initAxisDaysAndHours() break
case 'month':
this.initAxisMonthsAndDays()
break
} }
}, },
initAxisMonthsAndDays() { initAxisMonthsAndDays() {
let start = moment(this.chartStart) let start = moment(this.chartStart)
let end = moment(this.chartEnd) let end = moment(this.chartEnd)
this.childPointCount = Math.floor(end.diff(start, 'day', true)) this.childPointCount = Math.floor(end.diff(start, 'days', true))
this.axisPoints = []
while (start.isBefore(end)) { while (start.isBefore(end)) {
let dayCountOfMonth = start.isSame(end, 'month') let dayCountOfMonth = start.isSame(end, 'month')
? end.date() - 1 ? end.date() - 1
@@ -122,14 +136,14 @@ export default {
initAxisDaysAndHours() { initAxisDaysAndHours() {
let start = moment(this.chartStart) let start = moment(this.chartStart)
let end = moment(this.chartEnd) let end = moment(this.chartEnd)
this.childPointCount = Math.floor(end.diff(start, 'hour', true)) this.childPointCount = Math.floor(end.diff(start, 'hours', true))
this.axisPoints = []
while (start.isBefore(end)) { while (start.isBefore(end)) {
let hourCountOfDay = start.isSame(end, 'day') let hourCountOfDay = start.isSame(end, 'day')
? end.hour() ? end.hour()
: 24 - start.hour() : 24 - start.hour()
let widthPercentage = (hourCountOfDay / this.childPointCount) * 100 let widthPercentage = (hourCountOfDay / this.childPointCount) * 100
let endHour = start.day() === end.day() ? end.hour() - 1 : 23 // -1 because the last hour is not included e.g if chartEnd=04:00 the last interval we display is between 03 and 04 let endHour = start.date() === end.date() ? end.hour() - 1 : 23 // -1 because the last hour is not included e.g if chartEnd=04:00 the last interval we display is between 03 and 04
this.axisPoints.push( this.axisPoints.push(
this.getAxisDayObject(start, widthPercentage, endHour) this.getAxisDayObject(start, widthPercentage, endHour)
) )
@@ -177,6 +191,7 @@ export default {
moveTimemarker(event) { moveTimemarker(event) {
const chart = this.timemarker.closest('.g-gantt-chart') const chart = this.timemarker.closest('.g-gantt-chart')
if (!chart) return
this.timemarker.style.left = this.timemarker.style.left =
chart.scrollLeft + chart.scrollLeft +
event.clientX - event.clientX -
@@ -186,6 +201,7 @@ export default {
}, },
onWindowResize() { onWindowResize() {
if (!this.$refs['g-timeaxis']) return
this.horizontalAxisContainer = this.horizontalAxisContainer =
this.$refs['g-timeaxis'].getBoundingClientRect() this.$refs['g-timeaxis'].getBoundingClientRect()
this.hourFontSize = this.hourFontSize =
@@ -196,34 +212,28 @@ export default {
}, },
pointFormatted(point) { pointFormatted(point) {
if (this.precision === 'month') { switch (this.precision) {
return this.monthFormatted(point) case 'day':
} else if (this.precision === 'day') { return this.dayFormatted(point)
return this.dayFormatted(point) case 'month':
return this.monthFormatted(point)
} }
}, },
monthFormatted(point) { monthFormatted(point) {
// do not display month text if the month is smaller than x% // do not display month text if the month is smaller than x%
return point.widthPercentage >= (1 / 32) * 100 // return point.widthPercentage >= (1 / 32) * 100
? moment().locale(this.locale).localeData().months(point.value) // ? moment().locale(this.locale).localeData().months(point.value)
: '' // : ''
return moment(point.value).locale(this.locale).format(this.monthFormat)
}, },
dayFormatted(point) { dayFormatted(point) {
// do not display day text if the day is smaller than 12% // do not display day text if the day is smaller than 12%
return point.widthPercentage >= 12 // return point.widthPercentage >= 12
? moment(point.value).locale(this.locale).format(this.dayFormat) // ? moment(point.value).locale(this.locale).format(this.dayFormat)
: '' // : ''
}, return moment(point.value).locale(this.locale).format(this.dayFormat)
},
watch: {
chartStart() {
this.initAxis()
},
chartEnd() {
this.initAxis()
}, },
}, },
} }
+3 -2
View File
@@ -8,7 +8,7 @@
:hide-timeaxis="hideTimeaxis" :hide-timeaxis="hideTimeaxis"
:push-on-overlap="pushOnOverlap" :push-on-overlap="pushOnOverlap"
snap-back-on-overlap snap-back-on-overlap
precision="day" :precision="precision"
:is-magnetic="isMagnetic" :is-magnetic="isMagnetic"
:highlighted-hours="highlightedHours" :highlighted-hours="highlightedHours"
:row-label-width="rowLabelWidth" :row-label-width="rowLabelWidth"
@@ -46,7 +46,8 @@ export default {
data() { data() {
return { return {
chartStart: '2020-03-02 00:00', chartStart: '2020-03-02 00:00',
chartEnd: '2020-03-05 10:00', chartEnd: '2020-03-31 10:00',
precision: 'day',
pushOnOverlap: true, pushOnOverlap: true,
isMagnetic: true, isMagnetic: true,
grid: true, grid: true,