Skip to content

Commit 5185be8

Browse files
committed
clean up bell constants
1 parent c34bc3f commit 5185be8

6 files changed

Lines changed: 68 additions & 76 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
{
8686
"path": "./build/releases/OneSignalSDK.page.es6.js",
87-
"limit": "49.631 kB",
87+
"limit": "49.5 kB",
8888
"gzip": true
8989
},
9090
{

src/page/bell/Bell.ts

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,24 @@ import Button from './Button';
2525
import Dialog from './Dialog';
2626
import Launcher from './Launcher';
2727
import Message from './Message';
28+
import {
29+
BellState,
30+
type BellStateType,
31+
MESSAGE_TIMEOUT,
32+
MesageType,
33+
} from './constants';
2834

2935
const 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

3137
type 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+
3343
export 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
}

src/page/bell/Button.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import LimitStore from 'src/shared/services/LimitStore';
44
import OneSignalEvent from 'src/shared/services/OneSignalEvent';
55
import AnimatedElement from './AnimatedElement';
66
import Bell from './Bell';
7-
import Message from './Message';
7+
import { MESSAGE_TIMEOUT, MesageType } from './constants';
88

99
export default class Button extends AnimatedElement {
1010
public _isHandlingClick: boolean = false;
@@ -101,7 +101,7 @@ export default class Button extends AnimatedElement {
101101
try {
102102
if (
103103
this.bell.message.shown &&
104-
this.bell.message.contentType == Message.TYPES.MESSAGE
104+
this.bell.message.contentType == MesageType._Message
105105
) {
106106
// A message is being shown, it'll disappear soon
107107
return;
@@ -115,9 +115,9 @@ export default class Button extends AnimatedElement {
115115
OneSignal._emitter.once(OneSignal.EVENTS.SUBSCRIPTION_CHANGED, () => {
116116
this.bell.message
117117
.display(
118-
Message.TYPES.MESSAGE,
118+
MesageType._Message,
119119
this.bell.options.text['message.action.subscribed'],
120-
Message.TIMEOUT,
120+
MESSAGE_TIMEOUT,
121121
)
122122
.then(() => {
123123
this.bell._ignoreSubscriptionState = false;

src/page/bell/Dialog.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getPlatformNotificationIcon } from 'src/shared/utils/utils';
1010
import OneSignalEvent from '../../shared/services/OneSignalEvent';
1111
import AnimatedElement from './AnimatedElement';
1212
import Bell from './Bell';
13+
import { BellState } from './constants';
1314

1415
const STATIC_RESOURCES_URL = new URL('https://media.onesignal.com/web-sdk');
1516

@@ -79,9 +80,9 @@ export default class Dialog extends AnimatedElement {
7980
}
8081

8182
if (
82-
(this.bell.state === Bell.STATES.SUBSCRIBED &&
83+
(this.bell.state === BellState._Subscribed &&
8384
currentSetSubscription === true) ||
84-
(this.bell.state === Bell.STATES.UNSUBSCRIBED &&
85+
(this.bell.state === BellState._Unsubscribed &&
8586
currentSetSubscription === false)
8687
) {
8788
let notificationIconHtml = '';
@@ -93,13 +94,13 @@ export default class Dialog extends AnimatedElement {
9394
}
9495

9596
let buttonHtml = '';
96-
if (this.bell.state !== Bell.STATES.SUBSCRIBED)
97+
if (this.bell.state !== BellState._Subscribed)
9798
buttonHtml = `<button type="button" class="action" id="${this.subscribeButtonSelectorId}">${this.bell.options.text['dialog.main.button.subscribe']}</button>`;
9899
else
99100
buttonHtml = `<button type="button" class="action" id="${this.unsubscribeButtonSelectorId}">${this.bell.options.text['dialog.main.button.unsubscribe']}</button>`;
100101

101102
contents = `<h1>${this.bell.options.text['dialog.main.title']}</h1><div class="divider"></div><div class="push-notification">${notificationIconHtml}<div class="push-notification-text-container"><div class="push-notification-text push-notification-text-short"></div><div class="push-notification-text"></div><div class="push-notification-text push-notification-text-medium"></div><div class="push-notification-text"></div><div class="push-notification-text push-notification-text-medium"></div></div></div><div class="action-container">${buttonHtml}</div>${footer}`;
102-
} else if (this.bell.state === Bell.STATES.BLOCKED) {
103+
} else if (this.bell.state === BellState._Blocked) {
103104
let imageUrl = null;
104105

105106
const browserName = getBrowserName();

src/page/bell/Message.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { decodeHtmlEntities } from 'src/shared/helpers/dom';
22
import { delay, nothing } from 'src/shared/helpers/general';
33
import Log from 'src/shared/libraries/Log';
44
import AnimatedElement from './AnimatedElement';
5-
import Bell from './Bell';
5+
import type Bell from './Bell';
6+
import { BellState } from './constants';
67

78
export default class Message extends AnimatedElement {
89
public bell: Bell;
@@ -23,18 +24,6 @@ export default class Message extends AnimatedElement {
2324
this.queued = [];
2425
}
2526

26-
static get TIMEOUT() {
27-
return 2500;
28-
}
29-
30-
static get TYPES() {
31-
return {
32-
TIP: 'tip', // Appears on button hover, disappears on button endhover
33-
MESSAGE: 'message', // Appears manually for a specified duration, site visitor cannot control its display. Messages override tips
34-
QUEUED: 'queued', // This message was a user-queued message
35-
};
36-
}
37-
3827
display(type: string, content: string, duration = 0) {
3928
Log._debug(`Calling display(${type}, ${content}, ${duration}).`);
4029
return (this.shown ? this.hide() : nothing())
@@ -57,11 +46,11 @@ export default class Message extends AnimatedElement {
5746
}
5847

5948
getTipForState(): string {
60-
if (this.bell.state === Bell.STATES.UNSUBSCRIBED)
49+
if (this.bell.state === BellState._Unsubscribed)
6150
return this.bell.options.text['tip.state.unsubscribed'];
62-
else if (this.bell.state === Bell.STATES.SUBSCRIBED)
51+
else if (this.bell.state === BellState._Subscribed)
6352
return this.bell.options.text['tip.state.subscribed'];
64-
else if (this.bell.state === Bell.STATES.BLOCKED)
53+
else if (this.bell.state === BellState._Blocked)
6554
return this.bell.options.text['tip.state.blocked'];
6655
return '';
6756
}

src/page/bell/constants.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const BellState = {
2+
_Uninitialized: 0,
3+
_Subscribed: 1,
4+
_Unsubscribed: 2,
5+
_Blocked: 3,
6+
};
7+
8+
export type BellStateType = (typeof BellState)[keyof typeof BellState];
9+
10+
export const MESSAGE_TIMEOUT = 2500;
11+
12+
export const MesageType = {
13+
_Tip: 'tip', // Appears on button hover, disappears on button endhover
14+
_Message: 'message', // Appears manually for a specified duration, site visitor cannot control its display. Messages override tips
15+
_Queued: 'queued', // This message was a user-queued message
16+
};

0 commit comments

Comments
 (0)