From 8669452651eb984126c9eb2aa9e40b4b888baa90 Mon Sep 17 00:00:00 2001 From: Jakub Juszczak Date: Tue, 20 Mar 2018 21:24:11 +0100 Subject: [PATCH] wip: Refactor mixin in seperate functions --- src/mixins/index.js | 105 +++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/src/mixins/index.js b/src/mixins/index.js index 85ec49c5..658b74d0 100644 --- a/src/mixins/index.js +++ b/src/mixins/index.js @@ -1,67 +1,74 @@ +/* eslint-disable */ + function dataHandler (newData, oldData) { if (oldData) { - let chart = this.$data._chart - - // Get new and old DataSet Labels - let newDatasetLabels = newData.datasets.map((dataset) => { - return dataset.label - }) + const chart = this.$data._chart + _checkChanges(newData, oldData, chart) + } else { + _destroyAndRenderChart(chart) +} - let oldDatasetLabels = oldData.datasets.map((dataset) => { - return dataset.label - }) +function _checkChanges (newData, oldData, chart) { + // Get new and old DataSet Labels + let newDatasetLabels = newData.datasets.map((dataset) => { + return dataset.label + }) - // Stringify 'em for easier compare - const oldLabels = JSON.stringify(oldDatasetLabels) - const newLabels = JSON.stringify(newDatasetLabels) + let oldDatasetLabels = oldData.datasets.map((dataset) => { + return dataset.label + }) - // Check if Labels are equal and if dataset length is equal - if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { - newData.datasets.forEach((dataset, i) => { - // Get new and old dataset keys - const oldDatasetKeys = Object.keys(oldData.datasets[i]) - const newDatasetKeys = Object.keys(dataset) + // Stringify 'em for easier compare + const oldLabels = JSON.stringify(oldDatasetLabels) + const newLabels = JSON.stringify(newDatasetLabels) - // Get keys that aren't present in the new data - const deletionKeys = oldDatasetKeys.filter((key) => { - return key !== '_meta' && newDatasetKeys.indexOf(key) === -1 - }) + // Check if Labels are equal and if dataset length is equal + if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { + newData.datasets.forEach((dataset, i) => { + // Get new and old dataset keys + const oldDatasetKeys = Object.keys(oldData.datasets[i]) + const newDatasetKeys = Object.keys(dataset) - // Remove outdated key-value pairs - deletionKeys.forEach((deletionKey) => { - delete chart.data.datasets[i][deletionKey] - }) + // Get keys that aren't present in the new data + const deletionKeys = oldDatasetKeys.filter((key) => { + return key !== '_meta' && newDatasetKeys.indexOf(key) === -1 + }) - // Update attributes individually to avoid re-rendering the entire chart - for (const attribute in dataset) { - if (dataset.hasOwnProperty(attribute)) { - chart.data.datasets[i][attribute] = dataset[attribute] - } - } + // Remove outdated key-value pairs + deletionKeys.forEach((deletionKey) => { + delete chart.data.datasets[i][deletionKey] }) - if (newData.hasOwnProperty('labels')) { - chart.data.labels = newData.labels - } - if (newData.hasOwnProperty('xLabels')) { - chart.data.xLabels = newData.xLabels - } - if (newData.hasOwnProperty('yLabels')) { - chart.data.yLabels = newData.yLabels + // Update attributes individually to avoid re-rendering the entire chart + for (const attribute in dataset) { + if (dataset.hasOwnProperty(attribute)) { + chart.data.datasets[i][attribute] = dataset[attribute] + } } - chart.update() - } else { - chart.destroy() - this.renderChart(this.chartData, this.options) + }) + + if (newData.hasOwnProperty('labels')) { + chart.data.labels = newData.labels } - } else { - if (this.$data._chart) { - this.$data._chart.destroy() + if (newData.hasOwnProperty('xLabels')) { + chart.data.xLabels = newData.xLabels + } + if (newData.hasOwnProperty('yLabels')) { + chart.data.yLabels = newData.yLabels } - this.renderChart(this.chartData, this.options) + chart.update() + } else { + _destroyAndRenderChart(chart) } } +function _destroyAndRenderChart (chart) { + if (chart) { + chart.destroy() + } + this.renderChart(this.chartData, this.options) +} + export const reactiveData = { data () { return { @@ -77,7 +84,6 @@ export const reactiveData = { export const reactiveProp = { props: { chartData: { - type: Object, required: true, default: () => {} } @@ -91,3 +97,4 @@ export default { reactiveData, reactiveProp } +