@@ -25,14 +25,24 @@ import Button from './Button';
2525import Dialog from './Dialog' ;
2626import Launcher from './Launcher' ;
2727import Message from './Message' ;
28+ import {
29+ BellState ,
30+ type BellStateType ,
31+ MESSAGE_TIMEOUT ,
32+ MesageType ,
33+ } from './constants' ;
2834
2935const 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>` ;
3036
3137type BellState = 'uninitialized' | 'subscribed' | 'unsubscribed' | 'blocked' ;
3238
39+ const DEFAULT_SIZE : BellSize = 'medium' ;
40+ const DEFAULT_POSITION : BellPosition = 'bottom-right' ;
41+ const DEFAULT_THEME = 'default' ;
42+
3343export default class Bell {
3444 public options : AppUserConfigNotifyButton ;
35- public state : BellState = Bell . STATES . UNINITIALIZED ;
45+ public state : BellStateType = BellState . _Uninitialized ;
3646 public _ignoreSubscriptionState = false ;
3747 public hovering = false ;
3848 public initialized = false ;
@@ -42,10 +52,6 @@ export default class Bell {
4252 public _message : Message | undefined ;
4353 public _dialog : Dialog | undefined ;
4454
45- private DEFAULT_SIZE : BellSize = 'medium' ;
46- private DEFAULT_POSITION : BellPosition = 'bottom-right' ;
47- private DEFAULT_THEME = 'default' ;
48-
4955 static get EVENTS ( ) {
5056 return {
5157 STATE_CHANGED : 'notifyButtonStateChange' ,
@@ -58,32 +64,12 @@ export default class Bell {
5864 } ;
5965 }
6066
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-
8167 constructor ( config : Partial < AppUserConfigNotifyButton > , launcher ?: Launcher ) {
8268 this . options = {
8369 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 ,
70+ size : config . size || DEFAULT_SIZE ,
71+ position : config . position || DEFAULT_POSITION ,
72+ theme : config . theme || DEFAULT_THEME ,
8773 showLauncherAfter : config . showLauncherAfter || 10 ,
8874 showBadgeAfter : config . showBadgeAfter || 300 ,
8975 text : this . setDefaultTextOptions ( config . text || { } ) ,
@@ -100,7 +86,7 @@ export default class Bell {
10086 if ( ! this . options . enable ) return ;
10187
10288 this . validateOptions ( this . options ) ;
103- this . state = Bell . STATES . UNINITIALIZED ;
89+ this . state = BellState . _Uninitialized ;
10490 this . _ignoreSubscriptionState = false ;
10591
10692 this . installEventHooks ( ) ;
@@ -215,9 +201,9 @@ export default class Bell {
215201 } )
216202 . then ( ( ) => {
217203 return this . message . display (
218- Message . TYPES . MESSAGE ,
204+ MesageType . _Message ,
219205 this . options . text [ 'message.action.resubscribed' ] ,
220- Message . TIMEOUT ,
206+ MESSAGE_TIMEOUT ,
221207 ) ;
222208 } )
223209 . then ( ( ) => {
@@ -251,9 +237,9 @@ export default class Bell {
251237 } )
252238 . then ( ( ) => {
253239 return this . message . display (
254- Message . TYPES . MESSAGE ,
240+ MesageType . _Message ,
255241 this . options . text [ 'message.action.unsubscribed' ] ,
256- Message . TIMEOUT ,
242+ MESSAGE_TIMEOUT ,
257243 ) ;
258244 } )
259245 . then ( ( ) => {
@@ -273,7 +259,7 @@ export default class Bell {
273259
274260 // If the message is a message and not a tip, don't show it (only show tips)
275261 // Messages will go away on their own
276- if ( this . message . contentType === Message . TYPES . MESSAGE ) {
262+ if ( this . message . contentType === MesageType . _Message ) {
277263 this . hovering = false ;
278264 return ;
279265 }
@@ -283,14 +269,14 @@ export default class Bell {
283269 if ( this . message . queued . length > 0 ) {
284270 return this . message . dequeue ( ) . then ( ( msg : any ) => {
285271 this . message . content = msg ;
286- this . message . contentType = Message . TYPES . QUEUED ;
272+ this . message . contentType = MesageType . _Queued ;
287273 resolve ( ) ;
288274 } ) ;
289275 } else {
290276 this . message . content = decodeHtmlEntities (
291277 this . message . getTipForState ( ) ,
292278 ) ;
293- this . message . contentType = Message . TYPES . TIP ;
279+ this . message . contentType = MesageType . _Tip ;
294280 resolve ( ) ;
295281 }
296282 } )
@@ -307,7 +293,7 @@ export default class Bell {
307293
308294 OneSignal . _emitter . on ( Bell . EVENTS . HOVERED , ( ) => {
309295 // 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 ) {
296+ if ( this . message . contentType === MesageType . _Message ) {
311297 return ;
312298 }
313299
@@ -324,7 +310,7 @@ export default class Bell {
324310 // fire within a few milliseconds of each other
325311 this . message
326312 . show ( )
327- . then ( ( ) => delay ( Message . TIMEOUT ) )
313+ . then ( ( ) => delay ( MESSAGE_TIMEOUT ) )
328314 . then ( ( ) => this . message . hide ( ) )
329315 . then ( ( ) => {
330316 if ( this . launcher . wasInactive && ! this . dialog . shown ) {
@@ -359,13 +345,13 @@ export default class Bell {
359345
360346 const permission =
361347 await OneSignal . _context . _permissionManager . getPermissionStatus ( ) ;
362- let bellState : BellState ;
348+ let bellState : BellStateType ;
363349 if ( isSubscribed . current . optedIn ) {
364- bellState = Bell . STATES . SUBSCRIBED ;
350+ bellState = BellState . _Subscribed ;
365351 } else if ( permission === 'denied' ) {
366- bellState = Bell . STATES . BLOCKED ;
352+ bellState = BellState . _Blocked ;
367353 } else {
368- bellState = Bell . STATES . UNSUBSCRIBED ;
354+ bellState = BellState . _Unsubscribed ;
369355 }
370356 this . setState ( bellState , this . _ignoreSubscriptionState ) ;
371357 } ,
@@ -376,9 +362,9 @@ export default class Bell {
376362 // Notify button doesn't exist
377363 return ;
378364 }
379- if ( state . to === Bell . STATES . SUBSCRIBED ) {
365+ if ( state . to === BellState . _Subscribed ) {
380366 this . launcher . inactivate ( ) ;
381- } else if ( state . to === Bell . STATES . UNSUBSCRIBED || Bell . STATES . BLOCKED ) {
367+ } else if ( state . to === BellState . _Unsubscribed || BellState . _Blocked ) {
382368 this . launcher . activate ( ) ;
383369 }
384370 } ) ;
@@ -511,7 +497,7 @@ export default class Bell {
511497 // where the bell, at a different size than small, jerks sideways to go from large -> small or medium -> small
512498 const resizeTo = isPushEnabled
513499 ? 'small'
514- : this . options . size || this . DEFAULT_SIZE ;
500+ : this . options . size || DEFAULT_SIZE ;
515501 await this . launcher . resize ( resizeTo ) ;
516502
517503 this . addDefaultClasses ( ) ;
@@ -728,10 +714,10 @@ export default class Bell {
728714 ] )
729715 . then ( ( [ isEnabled , permission ] ) => {
730716 this . setState (
731- isEnabled ? Bell . STATES . SUBSCRIBED : Bell . STATES . UNSUBSCRIBED ,
717+ isEnabled ? BellState . _Subscribed : BellState . _Unsubscribed ,
732718 ) ;
733719 if ( permission === 'denied' ) {
734- this . setState ( Bell . STATES . BLOCKED ) ;
720+ this . setState ( BellState . _Blocked ) ;
735721 }
736722 } )
737723 . catch ( ( e ) => {
@@ -743,7 +729,7 @@ export default class Bell {
743729 * Updates the current state to the specified new state.
744730 * @param newState One of ['subscribed', 'unsubscribed'].
745731 */
746- setState ( newState : BellState , silent = false ) {
732+ setState ( newState : BellStateType , silent = false ) {
747733 const lastState = this . state ;
748734 this . state = newState ;
749735 if ( lastState !== newState && ! silent ) {
@@ -791,14 +777,14 @@ export default class Bell {
791777 }
792778
793779 get subscribed ( ) {
794- return this . state === Bell . STATES . SUBSCRIBED ;
780+ return this . state === BellState . _Subscribed ;
795781 }
796782
797783 get unsubscribed ( ) {
798- return this . state === Bell . STATES . UNSUBSCRIBED ;
784+ return this . state === BellState . _Unsubscribed ;
799785 }
800786
801787 get blocked ( ) {
802- return this . state === Bell . STATES . BLOCKED ;
788+ return this . state === BellState . _Blocked ;
803789 }
804790}
0 commit comments