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
|
(function() {
var defaults;
var apps = {};
var themesLoaded = {};
var widgetsetsRequested = {}
var widgetsetApps = {};
var log = function() {
if (console && console.log) {
console.log(arguments);
}
}
var loadTheme = function(url) {
log("loadTheme", url);
if(!themesLoaded[url]) {
var stylesheet = document.createElement('link');
stylesheet.setAttribute('rel', 'stylesheet');
stylesheet.setAttribute('type', 'text/css');
stylesheet.setAttribute('href', url + "/styles.css");
document.getElementsByTagName('head')[0].appendChild(stylesheet);
themesLoaded[url] = true;
}
}
var isWidgetsetLoaded = function(widgetset) {
var className = widgetset.replace(/\./g, "_");
return (typeof window[className]) != "undefined";
}
var loadWidgetset = function(basePath, widgetset) {
if (widgetsetsRequested[widgetset]) {
//TODO Tell the widgetset to load another application
return;
}
log("load widgetset", basePath, widgetset)
setTimeout(function() {
if (!isWidgetsetLoaded(widgetset)) {
alert("Failed to load the widgetset: " + url);
}
}, 15000);
var url = basePath + widgetset + "/" + widgetset + ".nocache.js?" + new Date().getTime();
var scriptTag = document.createElement('script');
scriptTag.setAttribute('type', 'text/javascript');
scriptTag.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(scriptTag);
widgetsetsRequested[widgetset] = true;
}
window.vaadin = window.vaadin || {
setDefaults: function(d) {
if (defaults) {
throw "Defaults already defined";
}
log("Got defaults", d)
defaults = d;
},
initApplication: function(appId, config) {
if (apps[appId]) {
throw "Application " + appId + " already initialized";
}
log("init application", appId, config);
var getConfig = function(name) {
var value = config[name];
if (value === undefined) {
value = defaults[name];
}
return value;
}
var fetchRootConfig = function() {
log('Fetching root config');
var url = getConfig('appUri');
// Root id
url += ((/\?/).test(url) ? "&" : "?") + "browserDetails";
url += '&rootId=' + getConfig('rootId');
// Uri fragment
url += '&f=' + encodeURIComponent(location.hash);
// Timestamp to avoid caching
url += '&' + (new Date()).getTime();
var r = new XMLHttpRequest();
r.open('POST', url, true);
r.onreadystatechange = function (aEvt) {
if (r.readyState == 4) {
if (r.status == 200){
log(r.responseText);
// TODO Does this work in all supported browsers?
var updatedConfig = JSON.parse(r.responseText);
// Copy new properties to the config object
for (var property in updatedConfig) {
if (updatedConfig.hasOwnProperty(property)) {
config[property] = updatedConfig[property];
}
}
// Try bootstrapping again, this time without fetching missing info
bootstrapApp(false);
} else {
log('Error', r.statusText);
}
}
};
r.send(null);
log('sending request to ', url);
};
//Export public data
var app = {
'getConfig': getConfig
};
apps[appId] = app;
var bootstrapApp = function(mayDefer) {
var themeUri = getConfig('themeUri');
if (themeUri) {
loadTheme(themeUri);
}
var widgetsetBase = getConfig('widgetsetBase');
var widgetset = getConfig('widgetset');
if (widgetset && widgetsetBase) {
loadWidgetset(widgetsetBase, widgetset);
if (widgetsetApps[widgetset]) {
widgetsetApps[widgetset].push(appId);
} else {
widgetsetApps[widgetset] = [appId];
}
} else if (mayDefer) {
fetchRootConfig();
} else {
throw "Widgetset not defined";
}
}
bootstrapApp(true);
if (getConfig("debug")) {
// TODO debug state is now global for the entire page, but should somehow only be set for the current application
window.vaadin.debug = true;
}
return app;
},
getApp: function(appId) {
var app = apps[appId];
return app;
},
popWidgetsetApp: function(widgetset) {
if (widgetsetApps[widgetset]) {
return widgetsetApps[widgetset].pop();
} else {
return null;
}
}
};
log('Vaadin bootstrap loaded');
})();
|