Refactor toggle chart helpers

This commit is contained in:
Paul Chavard 2018-10-09 11:44:02 +02:00
parent 9d5ffba068
commit 08d5e7d328

View file

@ -1,28 +1,24 @@
import $ from 'jquery';
import Chartkick from 'chartkick';
import { toggle } from '@utils';
export function toggleChart(event, chartClass) {
const nextSelectorItem = $(event.target),
nextChart = $(chartClass),
nextChartId = nextChart
.children()
.first()
.attr('id'),
currentSelectorItem = nextSelectorItem
.parent()
.find('.segmented-control-item-active'),
currentChart = nextSelectorItem
.parent()
.parent()
.find('.chart:not(.hidden)');
const nextSelectorItem = event.target,
nextChart = document.querySelector(chartClass),
nextChartId = nextChart.children[0].id,
currentSelectorItem = nextSelectorItem.parentElement.querySelector(
'.segmented-control-item-active'
),
currentChart = nextSelectorItem.parentElement.parentElement.querySelector(
'.chart:not(.hidden)'
);
// Change the current selector and the next selector states
currentSelectorItem.toggleClass('segmented-control-item-active');
nextSelectorItem.toggleClass('segmented-control-item-active');
currentSelectorItem.classList.toggle('segmented-control-item-active');
nextSelectorItem.classList.toggle('segmented-control-item-active');
// Hide the currently shown chart and show the new one
currentChart.toggleClass('hidden');
nextChart.toggleClass('hidden');
toggle(currentChart);
toggle(nextChart);
// Reflow needed, see https://github.com/highcharts/highcharts/issues/1979
Chartkick.charts[nextChartId].getChartObject().reflow();