1- import { addDomElement , clearDomElementChildren } from 'src/shared/helpers/dom' ;
1+ import { addDomElement , clearDomElementChildren , decodeHtmlEntities } from 'src/shared/helpers/dom' ;
22import type { NotificationIcons } from 'src/shared/notifications/types' ;
33import { Browser } from 'src/shared/useragent/constants' ;
44import { getBrowserName , isMobileBrowser , isTabletBrowser } from 'src/shared/useragent/detect' ;
@@ -19,6 +19,8 @@ const UNBLOCK_IMAGES: Partial<Record<string, string>> = {
1919 [ Browser . _Edge ] : '/bell/edge-unblock.png' ,
2020} ;
2121
22+ const BODY_SELECTOR = '.onesignal-bell-launcher-dialog-body' ;
23+
2224export default class Dialog {
2325 public _bell : Bell ;
2426 public _notificationIcons : NotificationIcons | null = null ;
@@ -53,27 +55,27 @@ export default class Dialog {
5355 async _updateContent ( ) {
5456 const isEnabled = await OneSignal . _context . _subscriptionManager . _isPushNotificationsEnabled ( ) ;
5557
56- const bodySelector = '.onesignal-bell-launcher-dialog-body' ;
57- clearDomElementChildren ( bodySelector ) ;
58+ const body = clearDomElementChildren ( BODY_SELECTOR ) ;
5859
5960 const text = this . _bell . _options . text ;
60- const footer = this . _bell . _options . showCredit
61- ? `<div class="divider"></div><div class="kickback">Powered by <a href="https://onesignal.com" class="kickback" target="_blank">OneSignal</a></div>`
62- : '' ;
63-
64- let contents = 'Nothing to show.' ;
6561 const state = this . _bell . _state ;
6662 const stateMatchesSubscription =
6763 ( state === BellState . _Subscribed && isEnabled ) ||
6864 ( state === BellState . _Unsubscribed && ! isEnabled ) ;
6965
7066 if ( stateMatchesSubscription ) {
71- contents = this . _buildSubscriptionContent ( text , footer ) ;
67+ this . _renderSubscription ( body , text ) ;
7268 } else if ( state === BellState . _Blocked ) {
73- contents = this . _buildBlockedContent ( text , footer ) ;
69+ this . _renderBlocked ( body , text ) ;
7470 }
7571
76- addDomElement ( bodySelector , 'beforeend' , contents ) ;
72+ if ( this . _bell . _options . showCredit ) {
73+ addDomElement (
74+ body ,
75+ 'beforeend' ,
76+ `<div class="divider"></div><div class="kickback">Powered by <a href="https://onesignal.com" class="kickback" target="_blank">OneSignal</a></div>` ,
77+ ) ;
78+ }
7779
7880 this . _subscribeButton ?. addEventListener ( 'click' , ( ) => {
7981 OneSignal . _doNotShowWelcomeNotification = false ;
@@ -82,11 +84,12 @@ export default class Dialog {
8284 this . _unsubscribeButton ?. addEventListener ( 'click' , ( ) => this . _bell . _onUnsubscribeClick ( ) ) ;
8385 }
8486
85- private _buildSubscriptionContent ( text : Bell [ '_options' ] [ 'text' ] , footer : string ) : string {
87+ private _renderSubscription ( body : Element , text : Bell [ '_options' ] [ 'text' ] ) : void {
8688 const imageUrl = getPlatformNotificationIcon ( this . _notificationIcons ) ;
89+
8790 const iconHtml =
8891 imageUrl !== 'default-icon'
89- ? `<div class="push-notification-icon"><img src=" ${ imageUrl } " ></div>`
92+ ? `<div class="push-notification-icon"><img></div>`
9093 : `<div class="push-notification-icon push-notification-icon-default"></div>` ;
9194
9295 const isSubscribed = this . _bell . _state === BellState . _Subscribed ;
@@ -95,27 +98,43 @@ export default class Dialog {
9598 ? text [ 'dialog.main.button.unsubscribe' ]
9699 : text [ 'dialog.main.button.subscribe' ] ;
97100
98- return `<h1>${ text [ 'dialog.main.title' ] } </h1><div class="divider"></div><div class="push-notification">${ iconHtml } <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"><button type="button" class="action" id="${ buttonId } ">${ buttonText } </button></div>${ footer } ` ;
101+ addDomElement (
102+ body ,
103+ 'beforeend' ,
104+ `<h1></h1><div class="divider"></div><div class="push-notification">${ iconHtml } <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"><button type="button" class="action" id="${ buttonId } "></button></div>` ,
105+ ) ;
106+
107+ body . querySelector ( 'h1' ) ! . textContent = decodeHtmlEntities ( text [ 'dialog.main.title' ] ) ;
108+ body . querySelector ( `#${ buttonId } ` ) ! . textContent = decodeHtmlEntities ( buttonText ) ;
109+ if ( imageUrl !== 'default-icon' ) {
110+ body . querySelector ( '.push-notification-icon img' ) ! . setAttribute ( 'src' , imageUrl ) ;
111+ }
99112 }
100113
101- private _buildBlockedContent ( text : Bell [ '_options' ] [ 'text' ] , footer : string ) : string {
114+ private _renderBlocked ( body : Element , text : Bell [ '_options' ] [ 'text' ] ) : void {
102115 const browserName = getBrowserName ( ) ;
103116 const isMobileOrTablet = isMobileBrowser ( ) || isTabletBrowser ( ) ;
104117
105118 let instructionsHtml = '' ;
106119 if ( isMobileOrTablet && browserName === Browser . _Chrome ) {
107120 instructionsHtml = `<ol><li>Access <strong>Settings</strong> by tapping the three menu dots <strong>⋮</strong></li><li>Click <strong>Site settings</strong> under Advanced.</li><li>Click <strong>Notifications</strong>.</li><li>Find and click this entry for this website.</li><li>Click <strong>Notifications</strong> and set it to <strong>Allow</strong>.</li></ol>` ;
108121 } else {
109- const imagePath =
110- browserName === Browser . _Chrome && isMobileOrTablet
111- ? undefined
112- : UNBLOCK_IMAGES [ browserName ] ;
122+ const imagePath = UNBLOCK_IMAGES [ browserName ] ;
113123 if ( imagePath ) {
114124 const imageUrl = STATIC_RESOURCES_URL + imagePath ;
115125 instructionsHtml = `<a href="${ imageUrl } " target="_blank"><img src="${ imageUrl } "></a>` ;
116126 }
117127 }
118128
119- return `<h1>${ text [ 'dialog.blocked.title' ] } </h1><div class="divider"></div><div class="instructions"><p>${ text [ 'dialog.blocked.message' ] } </p>${ instructionsHtml } </div>${ footer } ` ;
129+ addDomElement (
130+ body ,
131+ 'beforeend' ,
132+ `<h1></h1><div class="divider"></div><div class="instructions"><p></p>${ instructionsHtml } </div>` ,
133+ ) ;
134+
135+ body . querySelector ( 'h1' ) ! . textContent = decodeHtmlEntities ( text [ 'dialog.blocked.title' ] ) ;
136+ body . querySelector ( '.instructions p' ) ! . textContent = decodeHtmlEntities (
137+ text [ 'dialog.blocked.message' ] ,
138+ ) ;
120139 }
121140}
0 commit comments