-
Notifications
You must be signed in to change notification settings - Fork 56
/
core_oil.js
175 lines (150 loc) · 5.01 KB
/
core_oil.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
import { OilVersion, sendEventToHostSite, setGlobalOilObject } from './core_utils';
import { handleOptOut } from './core_optout';
import { logError, logInfo, logPreviewInfo } from './core_log';
import { checkOptIn } from './core_optin';
import { getSoiCookie, isBrowserCookieEnabled, isPreviewCookieSet, removePreviewCookie, removeVerboseCookie, setPreviewCookie, setVerboseCookie } from './core_cookies';
import { doSetTealiumVariables } from './core_tealium_loading_rules';
import { getLocale, isPreviewMode, resetConfiguration, setGdprApplies } from './core_config';
import { EVENT_NAME_HAS_OPTED_IN, EVENT_NAME_NO_COOKIES_ALLOWED, EVENT_NAME_OIL_SHOWN } from './core_constants';
import { executeCommandCollection } from './core_command_collection';
import { manageDomElementActivation } from './core_tag_management';
/**
* Initialize Oil on Host Site
* This functions gets called directly after Oil has loaded
*/
export function initOilLayer() {
logInfo(`Init OilLayer (version ${OilVersion.get()})`);
if (isPreviewMode() && !isPreviewCookieSet()) {
logPreviewInfo('Preview mode ON and OIL layer remains hidden. Run AS_OIL.previewModeOn() and reload to display the layer.');
}
registerDomElementActivationManager();
attachUtilityFunctionsToWindowObject();
doSetTealiumVariables();
/**
* We show OIL depending on the following conditions:
* With Dev Mode turned on, we only show Oil if a developer cookie is set
*/
if (!isPreviewMode() || isPreviewCookieSet()) {
/**
* Cookies are not enabled
*/
if (!isBrowserCookieEnabled()) {
logInfo('This browser doesn\'t allow cookies.');
import('../userview/locale/userview_oil.js')
.then(userview_modal => {
userview_modal.locale(uv_m => uv_m.renderOil({noCookie: true}));
})
.catch((e) => {
logError('Locale could not be loaded.', e);
});
sendEventToHostSite(EVENT_NAME_NO_COOKIES_ALLOWED);
return;
}
/**
* We read our cookie and get an opt-in value, true or false
*/
checkOptIn().then((optin) => {
if (optin) {
/**
* User has opted in
*/
sendEventToHostSite(EVENT_NAME_HAS_OPTED_IN);
executeCommandCollection();
attachCommandCollectionFunctionToWindowObject();
} else {
/**
* Any other case, when the user didn't decide before and oil needs to be shown:
*/
import('../userview/locale/userview_oil.js')
.then(userview_modal => {
userview_modal.locale(uv_m => uv_m.renderOil({optIn: false}));
attachCommandCollectionFunctionToWindowObject();
})
.catch((e) => {
logError('Locale could not be loaded.', e);
});
sendEventToHostSite(EVENT_NAME_OIL_SHOWN);
}
});
}
}
function registerDomElementActivationManager() {
document.addEventListener('DOMContentLoaded', onDomContentLoaded);
}
function onDomContentLoaded() {
document.removeEventListener('DOMContentLoaded', onDomContentLoaded);
manageDomElementActivation();
}
function attachCommandCollectionFunctionToWindowObject() {
setGlobalOilObject('commandCollectionExecutor', executeCommandCollection);
}
/**
* Attach Utility Functions to window Object, so users of oil can use it.
*/
function attachUtilityFunctionsToWindowObject() {
function loadLocale(callbackMethod) {
import('../userview/locale/userview_oil.js')
.then(userview_modal => {
if (!getLocale()) {
userview_modal.locale(callbackMethod);
} else {
callbackMethod(userview_modal);
}
})
.catch((e) => {
logError('Locale could not be loaded.', e);
});
}
setGlobalOilObject('previewModeOn', () => {
setPreviewCookie();
return 'preview mode on';
});
setGlobalOilObject('previewModeOff', () => {
removePreviewCookie();
return 'preview mode off';
});
setGlobalOilObject('verboseModeOn', () => {
setVerboseCookie();
return 'verbose mode on';
});
setGlobalOilObject('verboseModeOff', () => {
removeVerboseCookie();
return 'verbose mode off';
});
setGlobalOilObject('reload', () => {
resetConfiguration();
initOilLayer();
return 'OIL reloaded';
});
setGlobalOilObject('status', () => {
return getSoiCookie();
});
setGlobalOilObject('showPreferenceCenter', () => {
loadLocale(userview_modal => {
userview_modal.oilShowPreferenceCenter();
});
});
setGlobalOilObject('triggerOptIn', () => {
loadLocale(userview_modal => {
userview_modal.handleOptIn();
});
});
setGlobalOilObject('triggerSoiOptIn', () => {
loadLocale(userview_modal => {
userview_modal.handleSoiOptIn();
});
});
setGlobalOilObject('triggerPoiOptIn', () => {
loadLocale(userview_modal => {
userview_modal.handlePoiOptIn();
});
});
setGlobalOilObject('triggerOptOut', () => {
handleOptOut();
});
setGlobalOilObject('applyGDPR', () => {
setGdprApplies(true);
initOilLayer();
return 'GDPR applied';
});
}