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
- 'use strict' ;
10
-
11
- function _typeof ( obj ) { "@babel/helpers - typeof" ; if ( typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ) { _typeof = function _typeof ( obj ) { return typeof obj ; } ; } else { _typeof = function _typeof ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ; } return _typeof ( obj ) ; }
12
-
13
- Object . defineProperty ( exports , "__esModule" , {
14
- value : true
15
- } ) ;
16
- exports [ "default" ] = void 0 ;
17
-
18
- var _stimulus = require ( "@hotwired/stimulus" ) ;
19
-
20
- var _chart = require ( "chart.js" ) ;
21
-
22
- function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
23
-
24
- function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
25
-
26
- function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
27
-
28
- function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function" ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , writable : true , configurable : true } } ) ; if ( superClass ) _setPrototypeOf ( subClass , superClass ) ; }
29
-
30
- function _setPrototypeOf ( o , p ) { _setPrototypeOf = Object . setPrototypeOf || function _setPrototypeOf ( o , p ) { o . __proto__ = p ; return o ; } ; return _setPrototypeOf ( o , p ) ; }
31
-
32
- function _createSuper ( Derived ) { var hasNativeReflectConstruct = _isNativeReflectConstruct ( ) ; return function _createSuperInternal ( ) { var Super = _getPrototypeOf ( Derived ) , result ; if ( hasNativeReflectConstruct ) { var NewTarget = _getPrototypeOf ( this ) . constructor ; result = Reflect . construct ( Super , arguments , NewTarget ) ; } else { result = Super . apply ( this , arguments ) ; } return _possibleConstructorReturn ( this , result ) ; } ; }
33
-
34
- function _possibleConstructorReturn ( self , call ) { if ( call && ( _typeof ( call ) === "object" || typeof call === "function" ) ) { return call ; } return _assertThisInitialized ( self ) ; }
35
-
36
- function _assertThisInitialized ( self ) { if ( self === void 0 ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return self ; }
37
-
38
- function _isNativeReflectConstruct ( ) { if ( typeof Reflect === "undefined" || ! Reflect . construct ) return false ; if ( Reflect . construct . sham ) return false ; if ( typeof Proxy === "function" ) return true ; try { Boolean . prototype . valueOf . call ( Reflect . construct ( Boolean , [ ] , function ( ) { } ) ) ; return true ; } catch ( e ) { return false ; } }
39
-
40
- function _getPrototypeOf ( o ) { _getPrototypeOf = Object . setPrototypeOf ? Object . getPrototypeOf : function _getPrototypeOf ( o ) { return o . __proto__ || Object . getPrototypeOf ( o ) ; } ; return _getPrototypeOf ( o ) ; }
41
-
42
- var _default = /*#__PURE__*/ function ( _Controller ) {
43
- _inherits ( _default , _Controller ) ;
44
-
45
- var _super = _createSuper ( _default ) ;
46
-
47
- function _default ( ) {
48
- _classCallCheck ( this , _default ) ;
49
-
50
- return _super . apply ( this , arguments ) ;
51
- }
52
-
53
- _createClass ( _default , [ {
54
- key : "connect" ,
55
- value : function connect ( ) {
56
- var payload = JSON . parse ( this . element . getAttribute ( 'data-view' ) ) ;
57
-
58
- if ( Array . isArray ( payload . options ) && 0 === payload . options . length ) {
59
- payload . options = { } ;
60
- }
61
-
62
- this . _dispatchEvent ( 'chartjs:pre-connect' , {
63
- options : payload . options
64
- } ) ;
65
-
66
- var chart = new _chart . Chart ( this . element . getContext ( '2d' ) , payload ) ;
67
-
68
- this . _dispatchEvent ( 'chartjs:connect' , {
69
- chart : chart
70
- } ) ;
1
+ import { Controller } from '@hotwired/stimulus' ;
2
+ import { Chart } from 'chart.js' ;
3
+
4
+ class controller extends Controller {
5
+ connect ( ) {
6
+ if ( ! ( this . element instanceof HTMLCanvasElement ) ) {
7
+ throw new Error ( 'Invalid element' ) ;
8
+ }
9
+ const viewData = this . element . getAttribute ( 'data-view' ) ;
10
+ if ( ! viewData ) {
11
+ throw new Error ( 'Missing data-view attribute.' ) ;
12
+ }
13
+ const payload = JSON . parse ( viewData ) ;
14
+ if ( Array . isArray ( payload . options ) && 0 === payload . options . length ) {
15
+ payload . options = { } ;
16
+ }
17
+ this . _dispatchEvent ( 'chartjs:pre-connect' , { options : payload . options } ) ;
18
+ const canvasContext = this . element . getContext ( '2d' ) ;
19
+ if ( ! canvasContext ) {
20
+ throw new Error ( 'Could not getContext() from Element' ) ;
21
+ }
22
+ const chart = new Chart ( canvasContext , payload ) ;
23
+ this . _dispatchEvent ( 'chartjs:connect' , { chart } ) ;
71
24
}
72
- } , {
73
- key : "_dispatchEvent" ,
74
- value : function _dispatchEvent ( name ) {
75
- var payload = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : null ;
76
- var canBubble = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : false ;
77
- var cancelable = arguments . length > 3 && arguments [ 3 ] !== undefined ? arguments [ 3 ] : false ;
78
- var userEvent = document . createEvent ( 'CustomEvent' ) ;
79
- userEvent . initCustomEvent ( name , canBubble , cancelable , payload ) ;
80
- this . element . dispatchEvent ( userEvent ) ;
25
+ _dispatchEvent ( name , payload = null , canBubble = false , cancelable = false ) {
26
+ const userEvent = document . createEvent ( 'CustomEvent' ) ;
27
+ userEvent . initCustomEvent ( name , canBubble , cancelable , payload ) ;
28
+ this . element . dispatchEvent ( userEvent ) ;
81
29
}
82
- } ] ) ;
83
-
84
- return _default ;
85
- } ( _stimulus . Controller ) ;
30
+ }
86
31
87
- exports [ "default" ] = _default ;
32
+ export { controller as default } ;
0 commit comments