Skip to content

Commit 1977249

Browse files
committed
clean up bell constants
1 parent c34bc3f commit 1977249

File tree

7 files changed

+94
-103
lines changed

7 files changed

+94
-103
lines changed

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.43 kB",
8888
"gzip": true
8989
},
9090
{

src/page/bell/Bell.ts

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

2936
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>`;
3037

3138
type 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+
3344
export 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
}

src/page/bell/Button.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { registerForPushNotifications } from 'src/shared/helpers/init';
33
import LimitStore from 'src/shared/services/LimitStore';
44
import OneSignalEvent from 'src/shared/services/OneSignalEvent';
55
import AnimatedElement from './AnimatedElement';
6-
import Bell from './Bell';
7-
import Message from './Message';
6+
import type Bell from './Bell';
7+
import { BellEvent, MESSAGE_TIMEOUT, MesageType } from './constants';
88

99
export default class Button extends AnimatedElement {
1010
public _isHandlingClick: boolean = false;
@@ -69,14 +69,14 @@ export default class Button extends AnimatedElement {
6969
LimitStore.isEmpty(this.events.mouse) ||
7070
LimitStore.getLast(this.events.mouse) === 'out'
7171
) {
72-
OneSignalEvent.trigger(Bell.EVENTS.HOVERING);
72+
OneSignalEvent.trigger(BellEvent._Hovering);
7373
}
7474
LimitStore.put(this.events.mouse, 'over');
7575
}
7676

7777
onHovered() {
7878
LimitStore.put(this.events.mouse, 'out');
79-
OneSignalEvent.trigger(Bell.EVENTS.HOVERED);
79+
OneSignalEvent.trigger(BellEvent._Hovered);
8080
}
8181

8282
onTap() {
@@ -95,13 +95,13 @@ export default class Button extends AnimatedElement {
9595
if (this._isHandlingClick) return;
9696
this._isHandlingClick = true;
9797

98-
OneSignalEvent.trigger(Bell.EVENTS.BELL_CLICK);
99-
OneSignalEvent.trigger(Bell.EVENTS.LAUNCHER_CLICK);
98+
OneSignalEvent.trigger(BellEvent._BellClick);
99+
OneSignalEvent.trigger(BellEvent._LauncherClick);
100100

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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
import { getPlatformNotificationIcon } from 'src/shared/utils/utils';
1010
import OneSignalEvent from '../../shared/services/OneSignalEvent';
1111
import AnimatedElement from './AnimatedElement';
12-
import Bell from './Bell';
12+
import type Bell from './Bell';
13+
import { BellEvent, 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();
@@ -142,12 +143,12 @@ export default class Dialog extends AnimatedElement {
142143
a notification shown in this resubscription case.
143144
*/
144145
OneSignal._doNotShowWelcomeNotification = false;
145-
OneSignalEvent.trigger(Bell.EVENTS.SUBSCRIBE_CLICK);
146+
OneSignalEvent.trigger(BellEvent._SubscribeClick);
146147
});
147148
}
148149
if (this.unsubscribeButton) {
149150
this.unsubscribeButton.addEventListener('click', () =>
150-
OneSignalEvent.trigger(Bell.EVENTS.UNSUBSCRIBE_CLICK),
151+
OneSignalEvent.trigger(BellEvent._UnsubscribeClick),
151152
);
152153
}
153154
this.bell.setCustomColorsIfSpecified();

0 commit comments

Comments
 (0)