This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
233 lines (190 loc) · 7.87 KB
/
index.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import './main.css'
const MS_PER_DAY = 86400000
const GOOGLE_ANALYTICS_DELAY_MS = 30
const GLOBAL_CLIMATE_STRIKE_URLS = {
en: 'https://digital.globalclimatestrike.net/join/?source=digitalstrikebanner',
es: 'https://es.globalclimatestrike.net/?source=digitalstrikebanner',
de: 'https://de.globalclimatestrike.net/?source=digitalstrikebanner',
cs: 'https://www.tydenproklima.cz',
fr: 'https://fr.globalclimatestrike.net/?source=digitalstrikebanner',
nl: 'https://globalclimatestrike.net/?source=digitalstrikebanner',
tr: 'https://sifirgelecek.org/',
pt: 'https://pt.globalclimatestrike.net/?source=digitalstrikebanner',
it: 'https://digital.globalclimatestrike.net/join/?source=digitalstrikebanner',
}
const GLOBAL_CLIMATE_STRIKE_FULL_PAGE_URLS = {
en: 'https://globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
es: 'https://es.globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
de: 'https://de.globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
cs: 'https://www.tydenproklima.cz',
fr: 'https://fr.globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
nl: 'https://globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
tr: 'https://sifirgelecek.org',
pt: 'https://pt.globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
it: 'https://globalclimatestrike.net/digital-strike-day/?source=digitalstrikebanner',
}
const LOCALE_CODE_MAPPING = {
en: 'en-EN',
de: 'de-DE',
es: 'es-ES',
cs: 'cs-CZ',
fr: 'fr-FR',
nl: 'nl-NL',
tr: 'tr-TR',
pt: 'pt-BR',
it: 'it-IT',
}
let joinUrls = null
let isMaximizing = false
let language = 'en'
function maximize() {
if (isMaximizing) return
isMaximizing = true
postMessage('maximize')
const stickyFooter = document.querySelector('.dcs-footer')
stickyFooter.style.display = 'none'
const fullPage = document.querySelector('.dcs-full-page')
fullPage.style.display = 'flex'
}
function showCloseButtonOnFullPageWidget() {
const fullPageWidget = document.querySelector('.dcs-full-page')
fullPageWidget.style.background = 'rgba(78,229,139, 0.8)'
const fullPageCloseButton = document.querySelector('.dcs-full-page__close')
fullPageCloseButton.style.display = 'flex'
const fullPageCloseButtonContent = document.querySelector('.dcs-close')
fullPageCloseButtonContent.classList.add('dcs-full-page-close')
const fullPageFooter = document.querySelector('.dcs-full-page__footer')
fullPageFooter.style.display = 'none'
}
function handleCustomWebsiteName(websiteName) {
const websiteNameDefault = document.querySelector('.dcs-website-name__default')
websiteNameDefault.style.display = 'none'
const websiteNamePrefix = document.querySelector('.dcs-website-name__prefix')
websiteNamePrefix.style.display = 'inline-block'
const websiteNameText = document.querySelector('.dcs-website-name')
websiteNameText.innerHTML = decodeURI(websiteName)
}
function isTruthy(str) {
return typeof(str) === 'undefined' || `${str}` === 'true' || `${str}` === '1'
}
function parseQuery(queryString) {
var query = {}
var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&')
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=')
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '')
}
return query
}
function postMessage(action, data) {
data || (data = {})
data.action = action
data.DIGITAL_CLIMATE_STRIKE = true
window.parent.postMessage(data, '*')
}
function handleCloseButtonClick(event) {
event.preventDefault()
event.stopPropagation()
//adding delay to allow google analytics call to complete
setTimeout(() => {
postMessage('closeButtonClicked')
}, GOOGLE_ANALYTICS_DELAY_MS)
}
function handleJoinStrikeButtonClick(event) {
event.preventDefault()
event.stopPropagation()
//adding delay to allow google analytics call to complete
setTimeout(() => {
postMessage('buttonClicked', { linkUrl: joinUrls[language] })
}, GOOGLE_ANALYTICS_DELAY_MS)
}
function setGlobalClimateStrikeLinkUrl(selector) {
const element = document.querySelector(selector)
element.setAttribute('href', joinUrls[language])
}
function attachEvent(selector, event, callback) {
var elements = document.querySelectorAll(selector)
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener(event, callback)
}
}
function initGoogleAnalytics() {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga')
if (typeof window.ga !== 'undefined') {
window.ga('create', 'UA-145982710-1', 'auto')
window.ga('send', 'pageview')
}
}
function addTrackingEvents(hostname, forceFullPageWidget) {
attachEvent('.dcs-footer .dcs-button', 'click', () => trackEvent('footer-join-button', 'click', hostname))
attachEvent('.dcs-footer .dcs-close', 'click', () => trackEvent('footer-close-button', 'click', hostname))
attachEvent('.dcs-full-page .dcs-button', 'click', () => trackEvent('full-page-join-button', 'click', hostname))
attachEvent('.dcs-full-page .dcs-close', 'click', () => trackEvent('full-page-close-button', 'click', hostname))
if (forceFullPageWidget) {
trackEvent('full-page-widget', 'load', hostname)
} else {
trackEvent('footer-widget', 'load', hostname)
}
}
function trackEvent(category, action, label, value) {
if (!window.ga) return
const params = {
hitType: 'event',
eventCategory: category,
eventAction: action
}
if (label) {
params.eventLabel = label
}
if (value) {
params.eventValue = value
}
window.ga('send', params)
}
function todayIs(date) {
var today = new Date()
return date.getFullYear() === today.getFullYear()
&& date.getMonth() === today.getMonth()
&& date.getDate() === today.getDate()
}
function getFormattedDate(date, language) {
return date.toLocaleDateString(LOCALE_CODE_MAPPING[language], { day: 'numeric', month: 'long' })
}
function initializeInterface() {
const query = parseQuery(location.search)
const fullPageDisplayStartDate = new Date(Date.parse(query.fullPageDisplayStartDate))
const fullPageDisplayStopDate = new Date(fullPageDisplayStartDate.getTime() + MS_PER_DAY)
const isFullPage = query.forceFullPageWidget || todayIs(fullPageDisplayStartDate)
joinUrls = isFullPage ? GLOBAL_CLIMATE_STRIKE_FULL_PAGE_URLS : GLOBAL_CLIMATE_STRIKE_URLS
setGlobalClimateStrikeLinkUrl('.dcs-footer .dcs-button')
setGlobalClimateStrikeLinkUrl('.dcs-footer__logo')
setGlobalClimateStrikeLinkUrl('.dcs-full-page .dcs-button')
setGlobalClimateStrikeLinkUrl('.dcs-full-page__logo')
attachEvent('.dcs-close', 'click', handleCloseButtonClick)
attachEvent('.dcs-button', 'click', handleJoinStrikeButtonClick)
attachEvent('.dcs-footer__logo', 'click', handleJoinStrikeButtonClick)
attachEvent('.dcs-full-page__logo', 'click', handleJoinStrikeButtonClick)
language = query.language ? query.language : language
if (query.showCloseButtonOnFullPageWidget) {
showCloseButtonOnFullPageWidget()
}
if (query.websiteName) {
handleCustomWebsiteName(query.websiteName)
}
if (isTruthy(query.googleAnalytics) && !navigator.doNotTrack) {
initGoogleAnalytics()
addTrackingEvents(query.hostname, query.forceFullPageWidget)
}
if (isFullPage) {
maximize()
}
// Set display dates on full-size widget
var fullscreenDateString = getFormattedDate(fullPageDisplayStartDate, language)
var nextDayDateString = getFormattedDate(fullPageDisplayStopDate, language)
document.getElementById('dcs-strike-date').innerText = fullscreenDateString
document.getElementById('dcs-tomorrow-date').innerText = nextDayDateString
}
document.addEventListener('DOMContentLoaded', initializeInterface)