-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.js
More file actions
223 lines (188 loc) · 7.22 KB
/
temp.js
File metadata and controls
223 lines (188 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import Constants from "./Constants";
import Cookie from "./Cookie";
import PushNotification from "./PushNotification";
import Api from "./Api";
/**
* Browser public object
*/
export let routee = {};
/**
* Create cookies and get configuration
*
* @param {object} data
* @return {Promise<void>}
* @throws {string} no routee token provided
*/
routee.initialize = async function (data) {
Constants.VISIT_TIMESTAMP = new Date().getTime();
// Check if routeeToken (UUID) was provided
if (Constants.KEYS.routeeToken in data) {
localStorage.set(Constants.KEYS.routeeToken, data[Constants.KEYS.routeeToken]);
} else {
throw "RouteeToken not provided";
}
// Check if custom URL for service worker was provided
if (Constants.KEYS.serviceWorker in data) {
localStorage.set(Constants.KEYS.routeeToken, JSON.stringify(data[Constants.KEYS.serviceWorker]));
}
routee.errors = [];
const configurationRequest = Api.getConfig(data.routeeToken);
await setCookies();
const trackingId = Cookie.get(Constants.COOKIES.tracking);
/* Check if the main COOKIE was created and run the main code */
if (trackingId) {
const configuration = await configurationRequest.catch((error) => {
setError(Constants.ERRORS.config, error);
});
if (configuration) {
runConfig(configuration, trackingId);
}
} else {
setError(Constants.ERRORS.nullId, trackingId);
}
};
/**
* Create cookies
*
* @returns {Promise<void>}
*/
async function setCookies() {
let analyticsId = Cookie.get(Constants.COOKIES.analytics);
let trackingId = Cookie.get(Constants.COOKIES.tracking);
let session = Cookie.getSession();
if (!trackingId) {
let frameRequest = Api.createFrame();
trackingId = await Api.createSession().catch((error) => {
setError(Constants.ERRORS.session, error);
});
if (trackingId) {
Cookie.set(Constants.COOKIES.tracking, trackingId);
Cookie.setSession();
if (!analyticsId) {
frameRequest.then((aId) => {
Cookie.set(Constants.COOKIES.analytics, aId);
Api.updateSession(trackingId);
}).catch((error) => {
setError(Constants.ERRORS.analytics, error);
});
} else {
Cookie.set(Constants.COOKIES.analytics, analyticsId);
Api.updateSession(trackingId).catch((error) => {
setError(Constants.ERRORS.analytics, error);
});
}
}
} else {
// If there is no session cookie, create new
if (!session) {
Api.updateSession(trackingId).then(() => {
Cookie.setSession();
}).catch((error) => {
setError(Constants.ERRORS.sessionUpdate, error);
});
}
// If analyticsId cookie doesn't exist, create it
if (!analyticsId) {
Api.createFrame().then((analyticsId) => {
Cookie.set(Constants.COOKIES.analytics, analyticsId);
Api.updateSession(trackingId);
}).catch((error) => {
setError(Constants.ERRORS.analytics, error);
});
}
}
}
/**
* Take actions based on configurations
*
* @param {object} configuration
* @param {object} configuration.pushConfig
* @param {object} configuration.pushConfig.dialogConfig
* @param trackingId
* @private
*/
function runConfig(configuration, trackingId) {
/* Check if there is push config */
if ('pushConfig' in configuration) {
const pushConfig = {...configuration.pushConfig};
const dialogConfig = {...configuration.pushConfig.dialogConfig};
const title = ('title' in dialogConfig) ? dialogConfig.title : null;
const icon = ('iconUrl' in configuration) ? configuration.iconUrl : null;
const popupHtml = getDialogHTML(title, dialogConfig.message, dialogConfig.allow, dialogConfig.disallow, icon);
const pushOptInCallBack = (optInData) => {
const analyticsId = Cookie.get(Constants.COOKIES.analytics);
optInData.trackingId = trackingId;
optInData.routeeToken = localStorage.getItem(Constants.KEYS.routeeToken);
if (analyticsId) {
optInData.analyticsId = analyticsId;
}
Api.savePushOptIn(optInData).catch((error) => {
setError(Constants.ERRORS.pushOptIn, error)
});
};
/* Create a "push notification service" object */
const pushNotification = new PushNotification(pushConfig, popupHtml, pushOptInCallBack);
/* Catch push notification errors */
pushNotification.onError((error) => {
routee.errors.push(error);
});
/* Initialize and run push notification service */
pushNotification.init().catch((error) => {
setError(Constants.ERRORS.pushInit, error);
});
} else {
setError(Constants.ERRORS.nullConfig, configuration);
}
}
/**
* Store an error message
*
* @param {*} errorMessages
* @private
*/
function setError(...errorMessages) {
let error = {};
errorMessages.forEach((errorMessage, index) => {
error[index] = errorMessage;
});
routee.errors.push(error);
}
/**
* Render the content for the push notification permission popup
*
* @param {string|null} title
* @param {string} message
* @param {string} allow
* @param {string} disallow
* @param {string|null} [icon=null]
* @return {HTMLDivElement}
* @private
*/
function getDialogHTML(title, message, allow, disallow, icon = null) {
const pushContainerElement = document.createElement('div');
pushContainerElement.classList.add('routee-push-container');
let imageContainerElement;
const websiteIcon = (icon === null) ? Constants.URL.icon : icon;
imageContainerElement = '<div class="routee-push-image-container">' +
'<div style="background-image: url(' + websiteIcon + ')"></div>' +
'</div>';
let textContainerElement = "<div class=\"routee-push-text-container\">";
if (title !== null)
textContainerElement += "<div class=\"routee-push-title\">" + title + "</div>";
textContainerElement += "<div class=\"routee-push-description\">" + message + "</div>";
textContainerElement += "</div>";
const btnContainerElement = document.createElement('div');
btnContainerElement.classList.add('routee-push-button-container');
const laterBtnElement = document.createElement('div');
laterBtnElement.classList.add('routee-push-button', 'routee-push-disallow-button', 'push-notification-later');
laterBtnElement.innerText = disallow;
const allowBtnElement = document.createElement('div');
allowBtnElement.classList.add('routee-push-button', 'routee-push-allow-button', 'push-notification-allow');
allowBtnElement.innerText = allow;
btnContainerElement.appendChild(laterBtnElement);
btnContainerElement.appendChild(allowBtnElement);
pushContainerElement.innerHTML += imageContainerElement;
pushContainerElement.innerHTML += textContainerElement;
pushContainerElement.appendChild(btnContainerElement);
return pushContainerElement;
}