-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (125 loc) · 3.88 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
import './main.css';
const GOOGLE_ANALYTICS_DELAY_MS = 30;
const REPAIR_ORG_URLS = {};
const REPAIR_ORG_FULL_PAGE_URLS = {};
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 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.REPAIR_ORG = 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 handleButtonClick (event) {
event.preventDefault();
event.stopPropagation();
//adding delay to allow google analytics call to complete
setTimeout(() => {
postMessage('actionClicked');
}, GOOGLE_ANALYTICS_DELAY_MS);
}
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-56309303-15', 'auto');
window.ga('send', 'pageview');
}
}
function addTrackingEvents (hostname) {
attachEvent('.dcs-footer .dcs-button', 'click', () => {
trackEvent('footer-join-button', 'click', hostname);
});
attachEvent('.dcs-footer .dcs-close', 'click', () => trackEvent('footer-close-button', 'click', hostname));
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 initializeInterface () {
const query = parseQuery(location.search);
const isFullPage = false;
joinUrls = isFullPage ? REPAIR_ORG_FULL_PAGE_URLS : REPAIR_ORG_URLS;
attachEvent('.dcs-close', 'click', handleCloseButtonClick);
attachEvent('.dcs-button', 'click', handleButtonClick);
attachEvent('.dcs-footer__logo', 'click', handleButtonClick);
language = 'en';
if (isTruthy(query.googleAnalytics) && !navigator.doNotTrack) {
initGoogleAnalytics();
addTrackingEvents(query.hostname);
}
setTimeout(() => {
postMessage('resize', {
iframeHeight: document.body.scrollHeight
});
}, 100);
var ro = new ResizeObserver( entries => {
postMessage('resize', {
iframeHeight: document.querySelector('.dcs-footer__container').scrollHeight
});
});
// Observe one or multiple elements
ro.observe(document.body);
}
document.addEventListener('DOMContentLoaded', initializeInterface);