Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ResizeObserver to get chart size #56

Merged
merged 7 commits into from Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/.vuepress/client.ts
@@ -1,5 +1,5 @@
import { defineClientConfig } from '@vuepress/client'
import {ganttastic} from "../../src/index"
import {ganttastic} from "../../src/vue-ganttastic"

// see: https://v2.vuepress.vuejs.org/advanced/cookbook/usage-of-client-config.html
export default defineClientConfig({
Expand Down
75 changes: 33 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -68,5 +68,8 @@
"repository": {
"type": "git",
"url": "https://github.com/InfectoOne/vue-ganttastic"
},
"dependencies": {
"@vueuse/core": "^9.1.1"
}
}
47 changes: 16 additions & 31 deletions src/components/GGanttBar.vue
Expand Up @@ -25,23 +25,15 @@
</template>

<script setup lang="ts">
import {
computed,
ref,
toRefs,
watch,
nextTick,
type CSSProperties,
onMounted,
onUnmounted
} from "vue"
import { computed, ref, toRefs, watch, type CSSProperties, onMounted, inject } from "vue"

import useBarDragManagement from "../composables/useBarDragManagement.js"
import useTimePositionMapping from "../composables/useTimePositionMapping.js"
import useBarDragLimit from "../composables/useBarDragLimit.js"
import type { GanttBarObject } from "../types"
import provideEmitBarEvent from "../provider/provideEmitBarEvent.js"
import provideConfig from "../provider/provideConfig.js"
import { BAR_CONTAINER_KEY } from "../provider/symbols"

const props = defineProps<{
bar: GanttBarObject
Expand Down Expand Up @@ -84,43 +76,36 @@ const prepareForDrag = () => {
)
}

const barElement = ref<HTMLElement | null>(null)
const barContainerEl = inject(BAR_CONTAINER_KEY)

const onMouseEvent = (e: MouseEvent) => {
e.preventDefault()
if (e.type === "mousedown") {
prepareForDrag()
}
const barContainer = barElement.value
?.closest(".g-gantt-row-bars-container")
?.getBoundingClientRect()
const barContainer = barContainerEl?.value?.getBoundingClientRect()
let datetime
if (barContainer) {
datetime = mapPositionToTime(e.clientX - barContainer.left)
}
emitBarEvent(e, bar.value, datetime)
}

const { barStart, barEnd, width, chartStart, chartEnd } = config
const { barStart, barEnd, width, chartStart, chartEnd, chartSize } = config

const xStart = ref(0)
const xEnd = ref(0)

function calculateBarSize() {
xStart.value = mapTimeToPosition(bar.value[barStart.value])
xEnd.value = mapTimeToPosition(bar.value[barEnd.value])
}

watch(
[bar, width, chartStart, chartEnd],
async () => {
await nextTick()
calculateBarSize()
},
{ deep: true, immediate: true }
)

onMounted(() => window.addEventListener("resize", calculateBarSize))
onUnmounted(() => window.removeEventListener("resize", calculateBarSize))
onMounted(() => {
watch(
[bar, width, chartStart, chartEnd, chartSize.width],
() => {
xStart.value = mapTimeToPosition(bar.value[barStart.value])
xEnd.value = mapTimeToPosition(bar.value[barEnd.value])
},
{ deep: true, immediate: true }
)
})

const barStyle = computed(() => {
return {
Expand Down
16 changes: 8 additions & 8 deletions src/components/GGanttBarTooltip.vue
Expand Up @@ -73,15 +73,15 @@ const { toDayjs } = useDayjsHelper()
const { precision, font } = config
const tooltipContent = computed(() => {
const format = TOOLTIP_FORMATS[precision.value]
if (bar && bar.value) {
const barStartFormatted = toDayjs(bar.value, "start").format(format)
const barEndFormatted = toDayjs(bar.value, "end").format(format)
// NOTE: this is not the HYPHEN-MINUS (-) character by intend.
// Instead we use the correct typographic sign the en-dash
// see: https://en.wikipedia.org/wiki/Dash#Ranges_of_values
return `${barStartFormatted} – ${barEndFormatted}`
if (!bar?.value) {
return ""
}
return ""
const barStartFormatted = toDayjs(bar.value, "start").format(format)
const barEndFormatted = toDayjs(bar.value, "end").format(format)
// NOTE: this is not the HYPHEN-MINUS (-) character by intend.
// Instead we use the correct typographic sign the en-dash
// see: https://en.wikipedia.org/wiki/Dash#Ranges_of_values
return `${barStartFormatted} – ${barEndFormatted}`
})
</script>

Expand Down
14 changes: 10 additions & 4 deletions src/components/GGanttChart.vue
@@ -1,6 +1,6 @@
<template>
<div
ref="gGanttChart"
ref="ganttChart"
class="g-gantt-chart"
:style="{ width, background: colors.background, fontFamily: font }"
>
Expand Down Expand Up @@ -49,6 +49,7 @@ import { colorSchemes, type ColorScheme } from "../color-schemes.js"
import type { ColorSchemeKey } from "../color-schemes.js"
import { CHART_ROWS_KEY, CONFIG_KEY, EMIT_BAR_EVENT_KEY } from "../provider/symbols.js"
import type { GanttBarObject } from "../types"
import { useElementSize } from "@vueuse/core"

export interface GGanttChartProps {
chartStart: string
Expand All @@ -70,7 +71,10 @@ export interface GGanttChartProps {

export type GGanttChartConfig = ToRefs<Required<GGanttChartProps>> & {
colors: ComputedRef<ColorScheme>
gGanttChart: Ref<HTMLElement | null>
chartSize: {
width: Ref<number>
height: Ref<number>
}
}

const props = withDefaults(defineProps<GGanttChartProps>(), {
Expand Down Expand Up @@ -205,12 +209,14 @@ const emitBarEvent = (
}
}

const gGanttChart = ref<HTMLElement | null>(null)
const ganttChart = ref<HTMLElement | null>(null)
const chartSize = useElementSize(ganttChart)

provide(CHART_ROWS_KEY, getChartRows)
provide(CONFIG_KEY, {
...toRefs(props),
colors,
gGanttChart
chartSize
})
provide(EMIT_BAR_EVENT_KEY, emitBarEvent)
</script>
Expand Down
5 changes: 4 additions & 1 deletion src/components/GGanttRow.vue
Expand Up @@ -24,12 +24,13 @@
</template>

<script setup lang="ts">
import { ref, type Ref, toRefs, computed, type StyleValue } from "vue"
import { ref, type Ref, toRefs, computed, type StyleValue, provide } from "vue"

import useTimePositionMapping from "../composables/useTimePositionMapping.js"
import provideConfig from "../provider/provideConfig.js"
import type { GanttBarObject } from "../types"
import GGanttBar from "./GGanttBar.vue"
import { BAR_CONTAINER_KEY } from "../provider/symbols"

const props = defineProps<{
label: string
Expand All @@ -55,6 +56,8 @@ const rowStyle = computed(() => {
const { mapPositionToTime } = useTimePositionMapping()
const barContainer: Ref<HTMLElement | null> = ref(null)

provide(BAR_CONTAINER_KEY, barContainer)

const onDrop = (e: MouseEvent) => {
const container = barContainer.value?.getBoundingClientRect()
if (!container) {
Expand Down