1
+ 'use strict' ;
2
+
3
+ var _createClass = ( function ( ) { 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 ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
4
+
5
+ Object . defineProperty ( exports , "__esModule" , {
6
+ value : true
7
+ } ) ;
8
+
9
+ var _react = require ( 'react' ) ;
10
+
11
+ var _react2 = _interopRequireDefault ( _react ) ;
12
+
13
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
14
+
15
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
16
+
17
+ function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
18
+
19
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
20
+
21
+ var PERMISSION_GRANTED = 'granted' ;
22
+
23
+ var seqGen = function seqGen ( ) {
24
+ var i = 0 ;
25
+ return function ( ) {
26
+ return i ++ ;
27
+ } ;
28
+ } ;
29
+ var seq = seqGen ( ) ;
30
+
31
+ var Notification = ( function ( _React$Component ) {
32
+ _inherits ( Notification , _React$Component ) ;
33
+
34
+ function Notification ( props ) {
35
+ _classCallCheck ( this , Notification ) ;
36
+
37
+ var _this = _possibleConstructorReturn ( this , Object . getPrototypeOf ( Notification ) . call ( this , props ) ) ;
38
+
39
+ var supported = false ;
40
+ var granted = false ;
41
+ if ( 'Notification' in window && window . Notification ) {
42
+ supported = true ;
43
+ if ( window . Notification . permission === PERMISSION_GRANTED ) {
44
+ granted = true ;
45
+ }
46
+ } else {
47
+ supported = false ;
48
+ }
49
+
50
+ _this . state = {
51
+ supported : supported ,
52
+ granted : granted
53
+ } ;
54
+ // Do not save Notification instance in state
55
+ _this . notifications = { } ;
56
+ _this . windowFocus = true ;
57
+ _this . onWindowFocus = _this . _onWindowFocus . bind ( _this ) ;
58
+ _this . onWindowBlur = _this . _onWindowBlur . bind ( _this ) ;
59
+ return _this ;
60
+ }
61
+
62
+ _createClass ( Notification , [ {
63
+ key : '_onWindowFocus' ,
64
+ value : function _onWindowFocus ( ) {
65
+ this . windowFocus = true ;
66
+ }
67
+ } , {
68
+ key : '_onWindowBlur' ,
69
+ value : function _onWindowBlur ( ) {
70
+ this . windowFocus = false ;
71
+ }
72
+ } , {
73
+ key : 'componentDidMount' ,
74
+ value : function componentDidMount ( ) {
75
+ var _this2 = this ;
76
+
77
+ if ( this . props . disableActiveWindow ) {
78
+ if ( window . addEventListener ) {
79
+ window . addEventListener ( 'focus' , this . onWindowFocus ) ;
80
+ window . addEventListener ( 'blur' , this . onWindowBlur ) ;
81
+ } else if ( window . attachEvent ) {
82
+ window . attachEvent ( 'focus' , this . onWindowFocus ) ;
83
+ window . attachEvent ( 'blur' , this . onWindowBlur ) ;
84
+ }
85
+ }
86
+
87
+ if ( ! this . state . supported ) {
88
+ this . props . notSupported ( ) ;
89
+ } else {
90
+ if ( this . state . granted ) {
91
+ this . props . onPermissionGranted ( ) ;
92
+ } else {
93
+ window . Notification . requestPermission ( function ( permission ) {
94
+ var result = permission === PERMISSION_GRANTED ;
95
+ _this2 . setState ( {
96
+ granted : result
97
+ } , function ( ) {
98
+ if ( result ) {
99
+ _this2 . props . onPermissionGranted ( ) ;
100
+ } else {
101
+ _this2 . props . onPermissionDenied ( ) ;
102
+ }
103
+ } ) ;
104
+ } ) ;
105
+ }
106
+ }
107
+ }
108
+ } , {
109
+ key : 'componentWillUnmount' ,
110
+ value : function componentWillUnmount ( ) {
111
+ if ( this . props . disableActiveWindow ) {
112
+ if ( window . removeEventListner ) {
113
+ window . removeEventListener ( 'focus' , this . onWindowFocus ) ;
114
+ window . removeEventListener ( 'blur' , this . onWindowBlur ) ;
115
+ } else if ( window . detachEvent ) {
116
+ window . detachEvent ( 'focus' , this . onWindowFocus ) ;
117
+ window . detachEvent ( 'blur' , this . onWindowBlur ) ;
118
+ }
119
+ }
120
+ }
121
+ } , {
122
+ key : 'render' ,
123
+ value : function render ( ) {
124
+ var _this3 = this ;
125
+
126
+ var doNotShowOnActiveWindow = this . props . disableActiveWindow && this . windowFocus ;
127
+ if ( ! this . props . ignore && this . props . title && this . state . supported && this . state . granted && ! doNotShowOnActiveWindow ) {
128
+ ( function ( ) {
129
+
130
+ var opt = _this3 . props . options ;
131
+ if ( typeof opt . tag !== 'string' ) {
132
+ opt . tag = 'web-notification-' + seq ( ) ;
133
+ }
134
+
135
+ if ( ! _this3 . notifications [ opt . tag ] ) {
136
+ var n = new window . Notification ( _this3 . props . title , opt ) ;
137
+ n . onshow = function ( e ) {
138
+ _this3 . props . onShow ( e , opt . tag ) ;
139
+ setTimeout ( function ( ) {
140
+ _this3 . close ( opt . tag ) ;
141
+ } , _this3 . props . timeout ) ;
142
+ } ;
143
+ n . onclick = function ( e ) {
144
+ _this3 . props . onClick ( e , opt . tag ) ;
145
+ } ;
146
+ n . onclose = function ( e ) {
147
+ _this3 . props . onClose ( e , opt . tag ) ;
148
+ } ;
149
+ n . onerror = function ( e ) {
150
+ _this3 . props . onError ( e , opt . tag ) ;
151
+ } ;
152
+
153
+ _this3 . notifications [ opt . tag ] = n ;
154
+ }
155
+ } ) ( ) ;
156
+ }
157
+
158
+ // return null cause
159
+ // Error: Invariant Violation: Notification.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
160
+ return _react2 . default . createElement ( 'input' , { type : 'hidden' , name : 'dummy-for-react-web-notification' , style : { display : 'none' } } ) ;
161
+ }
162
+ } , {
163
+ key : 'close' ,
164
+ value : function close ( tag ) {
165
+ if ( this . notifications [ tag ] && typeof this . notifications [ tag ] . close === 'function' ) {
166
+ this . notifications [ tag ] . close ( ) ;
167
+ }
168
+ }
169
+
170
+ // for debug
171
+
172
+ } , {
173
+ key : '_getNotificationInstance' ,
174
+ value : function _getNotificationInstance ( tag ) {
175
+ return this . notifications [ tag ] ;
176
+ }
177
+ } ] ) ;
178
+
179
+ return Notification ;
180
+ } ) ( _react2 . default . Component ) ;
181
+
182
+ Notification . propTypes = {
183
+ ignore : _react2 . default . PropTypes . bool ,
184
+ disableActiveWindow : _react2 . default . PropTypes . bool ,
185
+ notSupported : _react2 . default . PropTypes . func ,
186
+ onPermissionGranted : _react2 . default . PropTypes . func ,
187
+ onPermissionDenied : _react2 . default . PropTypes . func ,
188
+ onShow : _react2 . default . PropTypes . func ,
189
+ onClick : _react2 . default . PropTypes . func ,
190
+ onClose : _react2 . default . PropTypes . func ,
191
+ onError : _react2 . default . PropTypes . func ,
192
+ timeout : _react2 . default . PropTypes . number ,
193
+ title : _react2 . default . PropTypes . string . isRequired ,
194
+ options : _react2 . default . PropTypes . object
195
+ } ;
196
+
197
+ Notification . defaultProps = {
198
+ ignore : false ,
199
+ disableActiveWindow : false ,
200
+ notSupported : function notSupported ( ) { } ,
201
+ onPermissionGranted : function onPermissionGranted ( ) { } ,
202
+ onPermissionDenied : function onPermissionDenied ( ) { } ,
203
+ onShow : function onShow ( ) { } ,
204
+ onClick : function onClick ( ) { } ,
205
+ onClose : function onClose ( ) { } ,
206
+ onError : function onError ( ) { } ,
207
+ timeout : 5000 ,
208
+ options : { }
209
+ } ;
210
+
211
+ exports . default = Notification ;
0 commit comments