/
widget.js
216 lines (181 loc) · 6.44 KB
/
widget.js
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
/* eslint-disable */
(function() {
'use strict';
var DOM_ID = 'DIGITAL_STRIKE';
var CLOSED_COOKIE = '_DIGITAL_STRIKE_WIDGET_CLOSED_';
var NOW = new Date().getTime();
var MS_PER_DAY = 86400000;
// user-configurable options
var options = window.DIGITAL_STRIKE_OPTIONS || {};
// FIXME: default iframeDir
var iframeDir = options.iframeDir !== undefined ? options.iframeDir : 'https://onestla.tech/widget-engreve/';
var websiteName = options.websiteName || null;
var footerDisplayStartDate = options.footerDisplayStartDate || new Date(1990, 10, 10);
const nextTuesday = new Date();
nextTuesday.setDate(nextTuesday.getDate() + ((7-nextTuesday.getDay())%7+2) % 7);
var fullPageDisplayStartDate = options.fullPageDisplayStartDate || nextTuesday;
var minMode = !!options.minMode;
var cookieExpirationDays = parseFloat(options.cookieExpirationDays || 1);
var alwaysShowWidget = !!(options.alwaysShowWidget || window.location.hash.indexOf('ALWAYS_SHOW_DIGITAL_STRIKE') !== -1);
var disableGoogleAnalytics = !!options.disableGoogleAnalytics;
var showCloseButtonOnFullPageWidget = !!options.showCloseButtonOnFullPageWidget;
var popup = options.popup;
var customStrikeEnURL = options.customStrikeEnURL;
var customStrikeFrURL = options.customStrikeFrURL;
var language = getLanguage();
function getIframeSrc() {
var src = iframeDir;
src += language === 'fr' ? '/index.html?' : '/index-' + language + '.html?';
var urlParams = [
['hostname', window.location.host],
['fullPageDisplayStartDate', fullPageDisplayStartDate.toISOString()],
['language', language]
];
minMode && urlParams.push(['minMode', 'true']);
showCloseButtonOnFullPageWidget && urlParams.push(['showCloseButtonOnFullPageWidget', 'true']);
disableGoogleAnalytics && urlParams.push(['googleAnalytics', 'false']);
websiteName && urlParams.push(['websiteName', encodeURI(websiteName)]);
popup && urlParams.push(['popup', 'true']);
customStrikeEnURL && urlParams.push(['customStrikeEnURL', customStrikeEnURL]);
customStrikeFrURL && urlParams.push(['customStrikeFrURL', customStrikeFrURL]);
var params = urlParams.map(function(el) {
return el.join('=');
});
return src + params.join('&');
}
function createIframe() {
var wrapper = document.createElement('div');
wrapper.id = DOM_ID;
var iframe = document.createElement('iframe');
iframe.src = getIframeSrc();
iframe.frameBorder = 0;
iframe.allowTransparency = true;
wrapper.appendChild(iframe);
document.body.appendChild(wrapper);
iframe.contentWindow.focus();
return wrapper;
}
function getLanguage() {
// Overriden value
if (options.language) {
return options.language;
}
var languages = [
'fr',
'en',
];
for (var i=0,l=languages.length;i<l;i++) {
var language = languages[i];
var languageRegexp = new RegExp('^'+language, 'i');
if (navigator && languageRegexp.test(navigator.language)) {
return language;
}
}
// Default
return 'fr';
}
function maximize() {
document.getElementById(DOM_ID).style.width = '100%';
document.getElementById(DOM_ID).style.height = '100%';
}
function closeWindow() {
document.getElementById(DOM_ID).remove();
window.removeEventListener('message', receiveMessage);
setCookie(CLOSED_COOKIE, 'true', cookieExpirationDays);
}
function navigateToLink(linkUrl) {
if (!popup) {
document.location = linkUrl;
} else {
var win = window.open(linkUrl, '_blank');
win.focus();
}
}
function injectCSS(id, css) {
var style = document.createElement('style');
style.type = 'text/css';
style.id = id;
if (style.styleSheet) {
style.styleSheet.cssText = css;
}
else {
style.appendChild(document.createTextNode(css));
}
document.head.appendChild(style);
}
function setCookie(name, value, expirationDays) {
var d = new Date();
d.setTime(d.getTime()+(expirationDays * MS_PER_DAY));
var expires = 'expires='+d.toGMTString();
document.cookie = name + '=' + value + '; ' + expires + '; path=/';
}
function getCookie(cookieName) {
var name = cookieName + '=';
var ca = document.cookie.split(';');
var c;
for(var i = 0; i < ca.length; i++) {
c = ca[i].trim();
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
function receiveMessage(event) {
if (!event.data.DIGITAL_STRIKE) return;
switch (event.data.action) {
case 'maximize':
return maximize();
case 'closeButtonClicked':
return closeWindow();
case 'buttonClicked':
if (event.data.linkUrl.lastIndexOf('http', 0) !== 0) return;
return navigateToLink(event.data.linkUrl);
}
}
/**
* There are a few circumstances when the iFrame should not be shown:
* 1. When the CLOSED_COOKIE has been set on that device
* 2. We haven't reached either display start date
* 3. We're past the date to display the full screen widget.
* 4. We haven't set alwaysShowWidget to be true in the config.
*/
function iFrameShouldNotBeShown() {
if (alwaysShowWidget) return false;
return (footerDisplayStartDate.getTime() > NOW && fullPageDisplayStartDate.getTime() > NOW)
|| new Date(fullPageDisplayStartDate.getTime() + MS_PER_DAY) < NOW
|| !!getCookie(CLOSED_COOKIE);
}
function initializeInterface() {
if (iFrameShouldNotBeShown()) {
return;
}
createIframe();
var iFrameHeight = getIframeHeight();
injectCSS('DIGITAL_STRIKE_CSS',
'#' + DOM_ID + ' { position: fixed; right: 0; left: 0; bottom: 0px; width: 100%; height: ' + iFrameHeight + '; z-index: 20000; -webkit-overflow-scrolling: touch; overflow: hidden; } ' +
'#' + DOM_ID + ' iframe { width: 100%; height: 100%; }'
);
// listen for messages from iframe
window.addEventListener('message', receiveMessage);
document.removeEventListener('DOMContentLoaded', initializeInterface);
}
function getIframeHeight() {
var isProbablyMobile = window.innerWidth < 600;
if (isProbablyMobile) {
return '200px';
} else {
return '145px';
}
}
// Wait for DOM content to load.
switch(document.readyState) {
case 'complete':
case 'loaded':
case 'interactive':
initializeInterface();
break;
default:
document.addEventListener('DOMContentLoaded', initializeInterface);
}
})();