Skip to content

Commit ebeaedb

Browse files
committed
[Chartjs] Rely on Stimulus values instead of custom attributes
1 parent ba4dd1b commit ebeaedb

File tree

8 files changed

+36
-22
lines changed

8 files changed

+36
-22
lines changed

src/Chartjs/DependencyInjection/ChartjsExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Reference;
1617
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1718
use Symfony\UX\Chartjs\Builder\ChartBuilder;
1819
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
1920
use Symfony\UX\Chartjs\Twig\ChartExtension;
21+
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
2022
use Twig\Environment;
2123

2224
/**
@@ -38,9 +40,10 @@ public function load(array $configs, ContainerBuilder $container)
3840
->setPublic(false)
3941
;
4042

41-
if (class_exists(Environment::class)) {
43+
if (class_exists(Environment::class) && class_exists(StimulusTwigExtension::class)) {
4244
$container
4345
->setDefinition('chartjs.twig_extension', new Definition(ChartExtension::class))
46+
->addArgument(new Reference('webpack_encore.twig_stimulus_extension'))
4447
->addTag('twig.extension')
4548
->setPublic(false)
4649
;

src/Chartjs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class HomeController extends AbstractController
6868
All options and data are provided as-is to Chart.js. You can read
6969
[Chart.js documentation](https://www.chartjs.org/docs/latest/) to discover them all.
7070

71-
Once created in PHP, a chart can be displayed using Twig:
71+
Once created in PHP, a chart can be displayed using Twig if installed
72+
(requires [Symfony Webpack Encore](https://symfony.com/doc/current/frontend/encore/installation.html)):
7273

7374
```twig
7475
{{ render_chart(chart) }}

src/Chartjs/Resources/assets/src/controller.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ import { Controller } from '@hotwired/stimulus';
1313
import { Chart } from 'chart.js';
1414

1515
export default class extends Controller {
16+
static values = {
17+
view: Object,
18+
};
19+
1620
connect() {
1721
if (!(this.element instanceof HTMLCanvasElement)) {
1822
throw new Error('Invalid element');
1923
}
2024

21-
const viewData = this.element.getAttribute('data-view');
22-
if (!viewData) {
23-
throw new Error('Missing data-view attribute.');
24-
}
25-
26-
const payload = JSON.parse(viewData);
25+
const payload = this.viewValue;
2726
if (Array.isArray(payload.options) && 0 === payload.options.length) {
2827
payload.options = {};
2928
}

src/Chartjs/Resources/assets/test/controller.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('ChartjsController', () => {
4646
<canvas
4747
data-testid="canvas"
4848
data-controller="check chartjs"
49-
data-view="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x5B;&#x5D;&#x7D;"
49+
data-chartjs-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x5B;&#x5D;&#x7D;"
5050
></canvas>
5151
`);
5252

@@ -68,7 +68,7 @@ describe('ChartjsController', () => {
6868
<canvas
6969
data-testid="canvas"
7070
data-controller="check chartjs"
71-
data-view="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x7B;&quot;showLines&quot;&#x3A;false&#x7D;&#x7D;"
71+
data-chartjs-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x7B;&quot;showLines&quot;&#x3A;false&#x7D;&#x7D;"
7272
></canvas>
7373
`);
7474

src/Chartjs/Tests/Kernel/TwigAppKernel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\HttpKernel\Kernel;
1919
use Symfony\UX\Chartjs\ChartjsBundle;
20+
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
2021

2122
/**
2223
* @author Titouan Galopin <[email protected]>
@@ -29,14 +30,15 @@ class TwigAppKernel extends Kernel
2930

3031
public function registerBundles(): iterable
3132
{
32-
return [new FrameworkBundle(), new TwigBundle(), new ChartjsBundle()];
33+
return [new FrameworkBundle(), new TwigBundle(), new WebpackEncoreBundle(), new ChartjsBundle()];
3334
}
3435

3536
public function registerContainerConfiguration(LoaderInterface $loader)
3637
{
3738
$loader->load(function (ContainerBuilder $container) {
3839
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
3940
$container->loadFromExtension('twig', ['default_path' => __DIR__.'/templates', 'strict_variables' => true, 'exception_controller' => null]);
41+
$container->loadFromExtension('webpack_encore', ['output_path' => '%kernel.project_dir%/public/build']);
4042

4143
$container->setAlias('test.chartjs.builder', 'chartjs.builder')->setPublic(true);
4244
$container->setAlias('test.chartjs.twig_extension', 'chartjs.twig_extension')->setPublic(true);

src/Chartjs/Tests/Twig/ChartExtensionTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ public function testRenderChart()
5858
);
5959

6060
$this->assertSame(
61-
'<canvas
62-
data-controller="mycontroller symfony--ux-chartjs--chart"
63-
data-view="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x7B;&quot;showLines&quot;&#x3A;false&#x7D;&#x7D;"
64-
class="myclass"></canvas>',
61+
'<canvas data-controller="mycontroller symfony--ux-chartjs--chart" data-symfony--ux-chartjs--chart-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x7B;&quot;showLines&quot;&#x3A;false&#x7D;&#x7D;" class="myclass"></canvas>',
6562
$rendered
6663
);
6764
}

src/Chartjs/Twig/ChartExtension.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\Chartjs\Twig;
1313

1414
use Symfony\UX\Chartjs\Model\Chart;
15+
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
1516
use Twig\Environment;
1617
use Twig\Extension\AbstractExtension;
1718
use Twig\TwigFunction;
@@ -24,6 +25,13 @@
2425
*/
2526
class ChartExtension extends AbstractExtension
2627
{
28+
private $stimulus;
29+
30+
public function __construct(StimulusTwigExtension $stimulus)
31+
{
32+
$this->stimulus = $stimulus;
33+
}
34+
2735
public function getFunctions(): array
2836
{
2937
return [
@@ -35,11 +43,13 @@ public function renderChart(Environment $env, Chart $chart, array $attributes =
3543
{
3644
$chart->setAttributes(array_merge($chart->getAttributes(), $attributes));
3745

38-
$html = '
39-
<canvas
40-
data-controller="'.trim($chart->getDataController().' symfony--ux-chartjs--chart').'"
41-
data-view="'.twig_escape_filter($env, json_encode($chart->createView()), 'html_attr').'"
42-
';
46+
$controllers = [];
47+
if ($chart->getDataController()) {
48+
$controllers[$chart->getDataController()] = [];
49+
}
50+
$controllers['@symfony/ux-chartjs/chart'] = ['view' => $chart->createView()];
51+
52+
$html = '<canvas '.$this->stimulus->renderStimulusController($env, $controllers).' ';
4353

4454
foreach ($chart->getAttributes() as $name => $value) {
4555
if ('data-controller' === $name) {

src/Chartjs/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
"symfony/framework-bundle": "^4.4.17|^5.0|^6.0",
3636
"symfony/phpunit-bridge": "^5.2|^6.0",
3737
"symfony/twig-bundle": "^4.4.17|^5.0|^6.0",
38-
"symfony/var-dumper": "^4.4.17|^5.0|^6.0"
38+
"symfony/var-dumper": "^4.4.17|^5.0|^6.0",
39+
"symfony/webpack-encore-bundle": "^1.11"
3940
},
4041
"conflict": {
41-
"symfony/flex": "<1.13"
42+
"symfony/flex": "<1.13",
43+
"symfony/webpack-encore-bundle": "<1.11"
4244
},
4345
"extra": {
4446
"branch-alias": {

0 commit comments

Comments
 (0)