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" ; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol . iterator ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && "function" == typeof Symbol && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } , _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
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
21
+
22
+ 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 ) ; } }
23
+
24
+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; Object . defineProperty ( Constructor , "prototype" , { writable : false } ) ; return Constructor ; }
25
+
26
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function" ) ; } Object . defineProperty ( subClass , "prototype" , { value : Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , writable : true , configurable : true } } ) , writable : false } ) ; if ( superClass ) _setPrototypeOf ( subClass , superClass ) ; }
27
+
28
+ function _setPrototypeOf ( o , p ) { _setPrototypeOf = Object . setPrototypeOf || function _setPrototypeOf ( o , p ) { o . __proto__ = p ; return o ; } ; return _setPrototypeOf ( o , p ) ; }
29
+
30
+ 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 ) ; } ; }
31
+
32
+ function _possibleConstructorReturn ( self , call ) { if ( call && ( _typeof ( call ) === "object" || typeof call === "function" ) ) { return call ; } else if ( call !== void 0 ) { throw new TypeError ( "Derived constructors may only return object or undefined" ) ; } return _assertThisInitialized ( self ) ; }
33
+
34
+ function _assertThisInitialized ( self ) { if ( self === void 0 ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return self ; }
35
+
36
+ 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 ; } }
37
+
38
+ function _getPrototypeOf ( o ) { _getPrototypeOf = Object . setPrototypeOf ? Object . getPrototypeOf : function _getPrototypeOf ( o ) { return o . __proto__ || Object . getPrototypeOf ( o ) ; } ; return _getPrototypeOf ( o ) ; }
39
+
40
+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
41
+
42
+ /**
43
+ * @author Mathias Arlaud <[email protected] >
44
+ */
45
+ var _default = /*#__PURE__*/ function ( _Controller ) {
46
+ _inherits ( _default , _Controller ) ;
47
+
48
+ var _super = _createSuper ( _default ) ;
49
+
50
+ function _default ( ) {
51
+ var _this ;
52
+
53
+ _classCallCheck ( this , _default ) ;
54
+
55
+ for ( var _len = arguments . length , args = new Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
56
+ args [ _key ] = arguments [ _key ] ;
57
+ }
58
+
59
+ _this = _super . call . apply ( _super , [ this ] . concat ( args ) ) ;
60
+
61
+ _defineProperty ( _assertThisInitialized ( _this ) , "eventSources" , [ ] ) ;
62
+
63
+ return _this ;
64
+ }
65
+
66
+ _createClass ( _default , [ {
67
+ key : "initialize" ,
68
+ value : function initialize ( ) {
69
+ var _this2 = this ;
70
+
71
+ var errorMessages = [ ] ;
72
+ if ( ! this . hasHubValue ) errorMessages . push ( 'A "hub" value pointing to the Mercure hub must be provided.' ) ;
73
+ if ( ! this . hasTopicsValue ) errorMessages . push ( 'A "topics" value must be provided.' ) ;
74
+ if ( errorMessages . length ) throw new Error ( errorMessages . join ( ' ' ) ) ;
75
+ this . eventSources = this . topicsValue . map ( function ( topic ) {
76
+ var u = new URL ( _this2 . hubValue ) ;
77
+ u . searchParams . append ( 'topic' , topic ) ;
78
+ return new EventSource ( u ) ;
79
+ } ) ;
80
+ }
81
+ } , {
82
+ key : "connect" ,
83
+ value : function connect ( ) {
84
+ var _this3 = this ;
85
+
86
+ if ( ! ( 'Notification' in window ) ) {
87
+ console . warn ( 'This browser does not support desktop notifications.' ) ;
88
+ return ;
89
+ }
90
+
91
+ ;
92
+ this . eventSources . forEach ( function ( eventSource ) {
93
+ eventSource . addEventListener ( 'message' , function ( event ) {
94
+ return _this3 . _notify ( JSON . parse ( event . data ) . summary ) ;
95
+ } ) ;
96
+ } ) ;
97
+
98
+ this . _dispatchEvent ( 'notify:connect' , {
99
+ eventSources : this . eventSources
100
+ } ) ;
101
+ }
102
+ } , {
103
+ key : "disconnect" ,
104
+ value : function disconnect ( ) {
105
+ var _this4 = this ;
106
+
107
+ this . eventSources . forEach ( function ( eventSource ) {
108
+ eventSource . removeEventListener ( 'message' , _this4 . _notify ) ;
109
+ eventSource . close ( ) ;
110
+ } ) ;
111
+ this . eventSources = [ ] ;
112
+ }
113
+ } , {
114
+ key : "_notify" ,
115
+ value : function _notify ( content ) {
116
+ if ( ! content ) return ;
117
+
118
+ if ( 'granted' === Notification . permission ) {
119
+ new Notification ( content ) ;
120
+ return ;
121
+ }
122
+
123
+ if ( 'denied' !== Notification . permission ) {
124
+ Notification . requestPermission ( ) . then ( function ( permission ) {
125
+ if ( 'granted' === permission ) {
126
+ new Notification ( content ) ;
127
+ }
128
+ } ) ;
129
+ }
130
+ }
131
+ } , {
132
+ key : "_dispatchEvent" ,
133
+ value : function _dispatchEvent ( name ) {
134
+ var payload = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : null ;
135
+ var canBubble = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : false ;
136
+ var cancelable = arguments . length > 3 && arguments [ 3 ] !== undefined ? arguments [ 3 ] : false ;
137
+ var userEvent = document . createEvent ( 'CustomEvent' ) ;
138
+ userEvent . initCustomEvent ( name , canBubble , cancelable , payload ) ;
139
+ this . element . dispatchEvent ( userEvent ) ;
140
+ }
141
+ } ] ) ;
142
+
143
+ return _default ;
144
+ } ( _stimulus . Controller ) ;
145
+
146
+ exports [ "default" ] = _default ;
147
+
148
+ _defineProperty ( _default , "values" , {
149
+ hub : String ,
150
+ topics : Array
151
+ } ) ;
0 commit comments