-
Notifications
You must be signed in to change notification settings - Fork 160
/
FlowBootstrap.js
262 lines (229 loc) · 7.87 KB
/
FlowBootstrap.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* This is a copy of the regular `BootstrapHandler.js` in the flow-server
module, but with the following modifications:
- The main function is exported as an ES module for lazy initialization.
- Application configuration is passed as a parameter instead of using
replacement placeholders as in the regular bootstrapping.
- It reuses `Vaadin.Flow.clients` if exists.
- Fixed lint errors.
*/
const init = function(appInitResponse) {
window.Vaadin = window.Vaadin || {};
window.Vaadin.Flow = window.Vaadin.Flow || {};
var apps = {};
var widgetsets = {};
var log;
if (typeof window.console === undefined || !window.location.search.match(/[&?]debug(&|$)/)) {
/* If no console.log present, just use a no-op */
log = function() {};
} else if (typeof window.console.log === 'function') {
/* If it's a function, use it with apply */
log = function() {
window.console.log.apply(window.console, arguments);
};
} else {
/* In IE, its a native function for which apply is not defined, but it works
without a proper 'this' reference */
log = window.console.log;
}
var isInitializedInDom = function(appId) {
var appDiv = document.getElementById(appId);
if (!appDiv) {
return false;
}
for (var i = 0; i < appDiv.childElementCount; i++) {
var className = appDiv.childNodes[i].className;
/* If the app div contains a child with the class
'v-app-loading' we have only received the HTML
but not yet started the widget set
(UIConnector removes the v-app-loading div). */
if (className && className.indexOf('v-app-loading') != -1) {
return false;
}
}
return true;
};
/*
* Needed for Testbench compatibility, but prevents any Vaadin 7 app from
* bootstrapping unless the legacy vaadinBootstrap.js file is loaded before
* this script.
*/
window.Vaadin = window.Vaadin || {};
window.Vaadin.Flow = window.Vaadin.Flow || {};
/*
* Needed for wrapping custom javascript functionality in the components (i.e. connectors)
*/
window.Vaadin.Flow.tryCatchWrapper = function(originalFunction, component, repo) {
return function() {
try {
// eslint-disable-next-line
const result = originalFunction.apply(this, arguments);
return result;
} catch (error) {
console.error(
'There seems to be an error in the ' + component + ':\n' + error.message + '\n'
+ 'Please submit an issue to https://github.com/vaadin/' + repo
+ '/issues/new!');
}
};
};
if (!window.Vaadin.Flow.initApplication) {
window.Vaadin.Flow.clients = window.Vaadin.Flow.clients || {};
window.Vaadin.Flow.initApplication = function(appId, config) {
var testbenchId = appId.replace(/-\d+$/, '');
if (apps[appId]) {
if (window.Vaadin
&& window.Vaadin.Flow
&& window.Vaadin.Flow.clients
&& window.Vaadin.Flow.clients[testbenchId]
&& window.Vaadin.Flow.clients[testbenchId].initializing) {
throw new Error('Application ' + appId + ' is already being initialized');
}
if (isInitializedInDom(appId)) {
throw new Error('Application ' + appId + ' already initialized');
}
}
log('init application', appId, config);
window.Vaadin.Flow.clients[testbenchId] = {
isActive: function() {
return true;
},
initializing: true,
productionMode: mode
};
var getConfig = function(name) {
var value = config[name];
return value;
};
/* Export public data */
var app = {
getConfig: getConfig
};
apps[appId] = app;
if (!window.name) {
window.name = appId + '-' + Math.random();
}
var widgetset = 'client';
widgetsets[widgetset] = {
pendingApps: []
};
if (widgetsets[widgetset].callback) {
log('Starting from bootstrap', appId);
widgetsets[widgetset].callback(appId);
} else {
log('Setting pending startup', appId);
widgetsets[widgetset].pendingApps.push(appId);
}
return app;
};
window.Vaadin.Flow.getAppIds = function() {
var ids = [];
for (var id in apps) {
if (apps.hasOwnProperty(id)) {
ids.push(id);
}
}
return ids;
};
window.Vaadin.Flow.getApp = function(appId) {
return apps[appId];
};
window.Vaadin.Flow.registerWidgetset = function(widgetset, callback) {
log('Widgetset registered', widgetset);
var ws = widgetsets[widgetset];
if (ws && ws.pendingApps) {
ws.callback = callback;
for (var i = 0; i < ws.pendingApps.length; i++) {
var appId = ws.pendingApps[i];
log('Starting from register widgetset', appId);
callback(appId);
}
ws.pendingApps = null;
}
};
window.Vaadin.Flow.getBrowserDetailsParameters = function() {
var params = {};
/* Screen height and width */
params['v-sh'] = window.screen.height;
params['v-sw'] = window.screen.width;
/* Browser window dimensions */
params['v-wh'] = window.innerHeight;
params['v-ww'] = window.innerWidth;
/* Body element dimensions */
params['v-bh'] = document.body.clientHeight;
params['v-bw'] = document.body.clientWidth;
/* Current time */
var date = new Date();
params['v-curdate'] = date.getTime();
/* Current timezone offset (including DST shift) */
var tzo1 = date.getTimezoneOffset();
/* Compare the current tz offset with the first offset from the end
of the year that differs --- if less that, we are in DST, otherwise
we are in normal time */
var dstDiff = 0;
var rawTzo = tzo1;
for (var m = 12; m > 0; m--) {
date.setUTCMonth(m);
var tzo2 = date.getTimezoneOffset();
if (tzo1 != tzo2) {
dstDiff = (tzo1 > tzo2 ? tzo1 - tzo2 : tzo2 - tzo1);
rawTzo = (tzo1 > tzo2 ? tzo1 : tzo2);
break;
}
}
/* Time zone offset */
params['v-tzo'] = tzo1;
/* DST difference */
params['v-dstd'] = dstDiff;
/* Time zone offset without DST */
params['v-rtzo'] = rawTzo;
/* DST in effect? */
params['v-dston'] = (tzo1 != rawTzo);
/* Time zone id (if available) */
try {
params['v-tzid'] = Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch (err) {
params['v-tzid'] = '';
}
/* Window name */
if (window.name) {
params['v-wn'] = window.name;
}
/* Detect touch device support */
var supportsTouch = false;
try {
document.createEvent('TouchEvent');
supportsTouch = true;
} catch (e) {
/* Chrome and IE10 touch detection */
supportsTouch = 'ontouchstart' in window
|| (typeof navigator.msMaxTouchPoints !== 'undefined');
}
params['v-td'] = supportsTouch;
/* Device Pixel Ratio */
params['v-pr'] = window.devicePixelRatio;
if (navigator.platform) {
params['v-np'] = navigator.platform;
}
/* Stringify each value (they are parsed on the server side) */
Object.keys(params).forEach(function(key) {
var value = params[key];
if (typeof value !== 'undefined') {
params[key] = value.toString();
}
});
return params;
};
}
log('Flow bootstrap loaded');
if (appInitResponse.appConfig.productionMode && typeof window.__gwtStatsEvent != 'function') {
window.Vaadin.Flow.gwtStatsEvents = [];
window.__gwtStatsEvent = function(event) {
window.Vaadin.Flow.gwtStatsEvents.push(event);
return true;
};
}
var config = appInitResponse.appConfig;
var mode = appInitResponse.appConfig.productionMode;
window.Vaadin.Flow.initApplication(config.appId, config);
};
export {init};