vue-gantt-schedule-timeline-calendar icon indicating copy to clipboard operation
vue-gantt-schedule-timeline-calendar copied to clipboard

How to use GSTC.api from vue component?

Open fancydev18 opened this issue 5 years ago • 5 comments

Hi,

The vue example in README works great. More advanced usage eg from https://neuronet.io/gantt-schedule-timeline-calendar/scheduler.html requires GSTC.api

However, this is undefined when using this vue component, which has only

name: "GSTC"
props: {config: {…}}
mounted: ƒ mounted()
destroyed: ƒ destroyed()
render: ƒ ()
staticRenderFns: []
_compiled: true
beforeCreate: [ƒ]
beforeDestroy: [ƒ]
__file: "node_modules/vue-gantt-schedule-timeline-calendar/src/components/GSTC.vue"
_Ctor: {0: ƒ}

How to access the "api" method, similar to https://github.com/neuronetio/gantt-schedule-timeline-calendar ?

Thanks

fancydev18 avatar Mar 21 '20 21:03 fancydev18

You are importing a vue component but api are in the gantt-schedule-timeline-calendar repository. may be you can try import main repository whit other name also:

import * as Gantt from "gantt-schedule-timeline-calendar"
console.log(Gantt.api)

giovannybm avatar Mar 22 '20 22:03 giovannybm

That works, thanks. Note, I had to use this syntax

import GSTC from 'gantt-schedule-timeline-calendar' // this works
// import * as Gantt from "gantt-schedule-timeline-calendar" doesn't work

I thought it has to be the same instance for API to work properly. So now I have 2 instances, for the the actual vue component, and another one just for the API. If API isn't linked to the instance rows/chart data, that's ok with me. Please close this issue if so.

Thanks for your help!

fancydev18 avatar Mar 23 '20 06:03 fancydev18

Does this method work for extracting the state such as in the line let GSTCState = (window.state = GSTC.api.stateFromConfig(config)); So that later you can apply an update to the state? Or is it completely detached from the data and configuration of the "real" component?

Semi-related (and I can open up a new issue to ask if needed) but is there documentation somewhere about adding tasks to the chart after the component has been created and mounted? As far as I can tell the component isn't watching the config (which is where I tried to add the tasks when creating the component but after creation it doesnt work). Is the API and State required for such an action?

dbalgley avatar Apr 23 '20 19:04 dbalgley

+1 we can access state but not gstc instance.

mushgev avatar May 15 '20 19:05 mushgev

As workaround copy this code:

limitScrollLeft(
    totalViewDurationPx: number,
    chartWidth: number,
    scrollLeft: number
  ) {
    const width = totalViewDurationPx - chartWidth;
    if (scrollLeft < 0) {
      scrollLeft = 0;
    } else if (scrollLeft > width) {
      scrollLeft = width;
    }
    return Math.round(scrollLeft);
  }

  scrollToTime(date: moment.Moment) {
    const time = this.state.get("_internal.chart.time");
    this.state.update("config.scroll", (scroll: any) => {
      const chartWidth = this.state.get("_internal.chart.dimensions.width");
      const halfTime = (chartWidth / 2) * time.timePerPixel;
      const leftGlobal = date.valueOf() - halfTime - time.finalFrom;
      scroll.left = this.limitScrollLeft(
        time.totalViewDurationPx,
        chartWidth,
        leftGlobal / time.timePerPixel
      );
      return scroll;
    });
  }

mushgev avatar May 15 '20 19:05 mushgev