Skip to content

Commit 42fb419

Browse files
Create controller per version
1 parent 5cb0ee9 commit 42fb419

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/Chartjs/assets/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77
"types": "dist/controller.d.ts",
88
"symfony": {
99
"controllers": {
10-
"chart": {
11-
"main": "dist/controller.js",
10+
"chart_v3": {
11+
"main": "dist/chart_v3_controller.js",
12+
"name": "symfony--ux-chartjs--chart",
1213
"webpackMode": "eager",
1314
"fetch": "eager",
1415
"enabled": true
16+
},
17+
"chart_v4": {
18+
"main": "dist/chart_v4_controller.js",
19+
"name": "symfony--ux-chartjs--chart",
20+
"webpackMode": "eager",
21+
"fetch": "eager",
22+
"enabled": false
1523
}
1624
}
1725
},

src/Chartjs/assets/src/controller.ts renamed to src/Chartjs/assets/src/abstract_controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
'use strict';
1111

1212
import { Controller } from '@hotwired/stimulus';
13-
import Chart from 'chart.js/auto';
1413

15-
export default class extends Controller {
14+
export default abstract class AbstractChartController extends Controller {
1615
declare readonly viewValue: any;
1716

1817
static values = {
@@ -35,12 +34,17 @@ export default class extends Controller {
3534
if (!canvasContext) {
3635
throw new Error('Could not getContext() from Element');
3736
}
38-
const chart = new Chart(canvasContext, payload);
37+
const chart = this.createChart(canvasContext, payload);
3938

4039
this._dispatchEvent('chartjs:connect', { chart });
4140
}
4241

4342
_dispatchEvent(name: string, payload: any) {
4443
this.element.dispatchEvent(new CustomEvent(name, { detail: payload }));
4544
}
45+
46+
/**
47+
* To support v3 and v4 of chart.js this help function is added, could be refactored when support for v3 is dropped
48+
*/
49+
abstract createChart(canvasContext: any, payload: any): any;
4650
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
import AbstractChartController from './abstract_controller';
13+
import Chart from 'chart.js/auto';
14+
import { ChartConfiguration, ChartItem } from 'chart.js';
15+
16+
export default class extends AbstractChartController {
17+
createChart(canvasContext: ChartItem, payload: ChartConfiguration): any {
18+
return new Chart(canvasContext, payload);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
import AbstractChartController from './abstract_controller';
13+
import Chart from 'chart.js/auto';
14+
import { ChartConfiguration, ChartItem } from 'chart.js';
15+
16+
export default class extends AbstractChartController {
17+
createChart(canvasContext: ChartItem, payload: ChartConfiguration): any {
18+
return new Chart(canvasContext, payload);
19+
}
20+
}

0 commit comments

Comments
 (0)