@@ -25,14 +25,25 @@ import Button from './Button';
2525import Dialog from './Dialog' ;
2626import Launcher from './Launcher' ;
2727import Message from './Message' ;
28+ import {
29+ BellEvent ,
30+ BellState ,
31+ type BellStateType ,
32+ MESSAGE_TIMEOUT ,
33+ MesageType ,
34+ } from './constants' ;
2835
2936const logoSvg = `<svg class="onesignal-bell-svg" xmlns="http://www.w3.org/2000/svg" width="99.7" height="99.7" viewBox="0 0 99.7 99.7"><circle class="background" cx="49.9" cy="49.9" r="49.9"/><path class="foreground" d="M50.1 66.2H27.7s-2-.2-2-2.1c0-1.9 1.7-2 1.7-2s6.7-3.2 6.7-5.5S33 52.7 33 43.3s6-16.6 13.2-16.6c0 0 1-2.4 3.9-2.4 2.8 0 3.8 2.4 3.8 2.4 7.2 0 13.2 7.2 13.2 16.6s-1 11-1 13.3c0 2.3 6.7 5.5 6.7 5.5s1.7.1 1.7 2c0 1.8-2.1 2.1-2.1 2.1H50.1zm-7.2 2.3h14.5s-1 6.3-7.2 6.3-7.3-6.3-7.3-6.3z"/><ellipse class="stroke" cx="49.9" cy="49.9" rx="37.4" ry="36.9"/></svg>` ;
3037
3138type BellState = 'uninitialized' | 'subscribed' | 'unsubscribed' | 'blocked' ;
3239
40+ const DEFAULT_SIZE : BellSize = 'medium' ;
41+ const DEFAULT_POSITION : BellPosition = 'bottom-right' ;
42+ const DEFAULT_THEME = 'default' ;
43+
3344export default class Bell {
3445 public options : AppUserConfigNotifyButton ;
35- public state : BellState = Bell . STATES . UNINITIALIZED ;
46+ public state : BellStateType = BellState . _Uninitialized ;
3647 public _ignoreSubscriptionState = false ;
3748 public hovering = false ;
3849 public initialized = false ;
@@ -42,48 +53,12 @@ export default class Bell {
4253 public _message : Message | undefined ;
4354 public _dialog : Dialog | undefined ;
4455
45- private DEFAULT_SIZE : BellSize = 'medium' ;
46- private DEFAULT_POSITION : BellPosition = 'bottom-right' ;
47- private DEFAULT_THEME = 'default' ;
48-
49- static get EVENTS ( ) {
50- return {
51- STATE_CHANGED : 'notifyButtonStateChange' ,
52- LAUNCHER_CLICK : 'notifyButtonLauncherClick' ,
53- BELL_CLICK : 'notifyButtonButtonClick' ,
54- SUBSCRIBE_CLICK : 'notifyButtonSubscribeClick' ,
55- UNSUBSCRIBE_CLICK : 'notifyButtonUnsubscribeClick' ,
56- HOVERING : 'notifyButtonHovering' ,
57- HOVERED : 'notifyButtonHover' ,
58- } ;
59- }
60-
61- static get STATES ( ) {
62- return {
63- UNINITIALIZED : 'uninitialized' as BellState ,
64- SUBSCRIBED : 'subscribed' as BellState ,
65- UNSUBSCRIBED : 'unsubscribed' as BellState ,
66- BLOCKED : 'blocked' as BellState ,
67- } ;
68- }
69-
70- static get TEXT_SUBS ( ) {
71- return {
72- 'prompt.native.grant' : {
73- default : 'Allow' ,
74- chrome : 'Allow' ,
75- firefox : 'Always Receive Notifications' ,
76- safari : 'Allow' ,
77- } ,
78- } ;
79- }
80-
8156 constructor ( config : Partial < AppUserConfigNotifyButton > , launcher ?: Launcher ) {
8257 this . options = {
8358 enable : config . enable || false ,
84- size : config . size || this . DEFAULT_SIZE ,
85- position : config . position || this . DEFAULT_POSITION ,
86- theme : config . theme || this . DEFAULT_THEME ,
59+ size : config . size || DEFAULT_SIZE ,
60+ position : config . position || DEFAULT_POSITION ,
61+ theme : config . theme || DEFAULT_THEME ,
8762 showLauncherAfter : config . showLauncherAfter || 10 ,
8863 showBadgeAfter : config . showBadgeAfter || 300 ,
8964 text : this . setDefaultTextOptions ( config . text || { } ) ,
@@ -100,7 +75,7 @@ export default class Bell {
10075 if ( ! this . options . enable ) return ;
10176
10277 this . validateOptions ( this . options ) ;
103- this . state = Bell . STATES . UNINITIALIZED ;
78+ this . state = BellState . _Uninitialized ;
10479 this . _ignoreSubscriptionState = false ;
10580
10681 this . installEventHooks ( ) ;
@@ -200,7 +175,7 @@ export default class Bell {
200175
201176 private installEventHooks ( ) {
202177 // Install event hooks
203- OneSignal . _emitter . on ( Bell . EVENTS . SUBSCRIBE_CLICK , ( ) => {
178+ OneSignal . _emitter . on ( BellEvent . _SubscribeClick , ( ) => {
204179 const subscribeButton = this . dialog . subscribeButton ;
205180 if ( subscribeButton ) {
206181 subscribeButton . disabled = true ;
@@ -215,9 +190,9 @@ export default class Bell {
215190 } )
216191 . then ( ( ) => {
217192 return this . message . display (
218- Message . TYPES . MESSAGE ,
193+ MesageType . _Message ,
219194 this . options . text [ 'message.action.resubscribed' ] ,
220- Message . TIMEOUT ,
195+ MESSAGE_TIMEOUT ,
221196 ) ;
222197 } )
223198 . then ( ( ) => {
@@ -233,7 +208,7 @@ export default class Bell {
233208 } ) ;
234209 } ) ;
235210
236- OneSignal . _emitter . on ( Bell . EVENTS . UNSUBSCRIBE_CLICK , ( ) => {
211+ OneSignal . _emitter . on ( BellEvent . _UnsubscribeClick , ( ) => {
237212 const unsubscribeButton = this . dialog . unsubscribeButton ;
238213 if ( unsubscribeButton ) {
239214 unsubscribeButton . disabled = true ;
@@ -251,17 +226,17 @@ export default class Bell {
251226 } )
252227 . then ( ( ) => {
253228 return this . message . display (
254- Message . TYPES . MESSAGE ,
229+ MesageType . _Message ,
255230 this . options . text [ 'message.action.unsubscribed' ] ,
256- Message . TIMEOUT ,
231+ MESSAGE_TIMEOUT ,
257232 ) ;
258233 } )
259234 . then ( ( ) => {
260235 return this . updateState ( ) ;
261236 } ) ;
262237 } ) ;
263238
264- OneSignal . _emitter . on ( Bell . EVENTS . HOVERING , ( ) => {
239+ OneSignal . _emitter . on ( BellEvent . _Hovering , ( ) => {
265240 this . hovering = true ;
266241 this . launcher . activateIfInactive ( ) ;
267242
@@ -273,7 +248,7 @@ export default class Bell {
273248
274249 // If the message is a message and not a tip, don't show it (only show tips)
275250 // Messages will go away on their own
276- if ( this . message . contentType === Message . TYPES . MESSAGE ) {
251+ if ( this . message . contentType === MesageType . _Message ) {
277252 this . hovering = false ;
278253 return ;
279254 }
@@ -283,14 +258,14 @@ export default class Bell {
283258 if ( this . message . queued . length > 0 ) {
284259 return this . message . dequeue ( ) . then ( ( msg : any ) => {
285260 this . message . content = msg ;
286- this . message . contentType = Message . TYPES . QUEUED ;
261+ this . message . contentType = MesageType . _Queued ;
287262 resolve ( ) ;
288263 } ) ;
289264 } else {
290265 this . message . content = decodeHtmlEntities (
291266 this . message . getTipForState ( ) ,
292267 ) ;
293- this . message . contentType = Message . TYPES . TIP ;
268+ this . message . contentType = MesageType . _Tip ;
294269 resolve ( ) ;
295270 }
296271 } )
@@ -305,9 +280,9 @@ export default class Bell {
305280 } ) ;
306281 } ) ;
307282
308- OneSignal . _emitter . on ( Bell . EVENTS . HOVERED , ( ) => {
283+ OneSignal . _emitter . on ( BellEvent . _Hovered , ( ) => {
309284 // If a message is displayed (and not a tip), don't control it. Visitors have no control over messages
310- if ( this . message . contentType === Message . TYPES . MESSAGE ) {
285+ if ( this . message . contentType === MesageType . _Message ) {
311286 return ;
312287 }
313288
@@ -324,7 +299,7 @@ export default class Bell {
324299 // fire within a few milliseconds of each other
325300 this . message
326301 . show ( )
327- . then ( ( ) => delay ( Message . TIMEOUT ) )
302+ . then ( ( ) => delay ( MESSAGE_TIMEOUT ) )
328303 . then ( ( ) => this . message . hide ( ) )
329304 . then ( ( ) => {
330305 if ( this . launcher . wasInactive && ! this . dialog . shown ) {
@@ -359,26 +334,26 @@ export default class Bell {
359334
360335 const permission =
361336 await OneSignal . _context . _permissionManager . getPermissionStatus ( ) ;
362- let bellState : BellState ;
337+ let bellState : BellStateType ;
363338 if ( isSubscribed . current . optedIn ) {
364- bellState = Bell . STATES . SUBSCRIBED ;
339+ bellState = BellState . _Subscribed ;
365340 } else if ( permission === 'denied' ) {
366- bellState = Bell . STATES . BLOCKED ;
341+ bellState = BellState . _Blocked ;
367342 } else {
368- bellState = Bell . STATES . UNSUBSCRIBED ;
343+ bellState = BellState . _Unsubscribed ;
369344 }
370345 this . setState ( bellState , this . _ignoreSubscriptionState ) ;
371346 } ,
372347 ) ;
373348
374- OneSignal . _emitter . on ( Bell . EVENTS . STATE_CHANGED , ( state ) => {
349+ OneSignal . _emitter . on ( BellEvent . _StateChanged , ( state ) => {
375350 if ( ! this . launcher . element ) {
376351 // Notify button doesn't exist
377352 return ;
378353 }
379- if ( state . to === Bell . STATES . SUBSCRIBED ) {
354+ if ( state . to === BellState . _Subscribed ) {
380355 this . launcher . inactivate ( ) ;
381- } else if ( state . to === Bell . STATES . UNSUBSCRIBED || Bell . STATES . BLOCKED ) {
356+ } else if ( state . to === BellState . _Unsubscribed || BellState . _Blocked ) {
382357 this . launcher . activate ( ) ;
383358 }
384359 } ) ;
@@ -511,7 +486,7 @@ export default class Bell {
511486 // where the bell, at a different size than small, jerks sideways to go from large -> small or medium -> small
512487 const resizeTo = isPushEnabled
513488 ? 'small'
514- : this . options . size || this . DEFAULT_SIZE ;
489+ : this . options . size || DEFAULT_SIZE ;
515490 await this . launcher . resize ( resizeTo ) ;
516491
517492 this . addDefaultClasses ( ) ;
@@ -728,10 +703,10 @@ export default class Bell {
728703 ] )
729704 . then ( ( [ isEnabled , permission ] ) => {
730705 this . setState (
731- isEnabled ? Bell . STATES . SUBSCRIBED : Bell . STATES . UNSUBSCRIBED ,
706+ isEnabled ? BellState . _Subscribed : BellState . _Unsubscribed ,
732707 ) ;
733708 if ( permission === 'denied' ) {
734- this . setState ( Bell . STATES . BLOCKED ) ;
709+ this . setState ( BellState . _Blocked ) ;
735710 }
736711 } )
737712 . catch ( ( e ) => {
@@ -743,11 +718,11 @@ export default class Bell {
743718 * Updates the current state to the specified new state.
744719 * @param newState One of ['subscribed', 'unsubscribed'].
745720 */
746- setState ( newState : BellState , silent = false ) {
721+ setState ( newState : BellStateType , silent = false ) {
747722 const lastState = this . state ;
748723 this . state = newState ;
749724 if ( lastState !== newState && ! silent ) {
750- OneSignalEvent . trigger ( Bell . EVENTS . STATE_CHANGED , {
725+ OneSignalEvent . trigger ( BellEvent . _StateChanged , {
751726 from : lastState ,
752727 to : newState ,
753728 } ) ;
@@ -791,14 +766,14 @@ export default class Bell {
791766 }
792767
793768 get subscribed ( ) {
794- return this . state === Bell . STATES . SUBSCRIBED ;
769+ return this . state === BellState . _Subscribed ;
795770 }
796771
797772 get unsubscribed ( ) {
798- return this . state === Bell . STATES . UNSUBSCRIBED ;
773+ return this . state === BellState . _Unsubscribed ;
799774 }
800775
801776 get blocked ( ) {
802- return this . state === Bell . STATES . BLOCKED ;
777+ return this . state === BellState . _Blocked ;
803778 }
804779}
0 commit comments