2011-11-24 14:33:22 +01:00
|
|
|
(function() {
|
|
|
|
var defaults;
|
|
|
|
var apps = {};
|
|
|
|
var themesLoaded = {};
|
2011-11-28 11:59:34 +01:00
|
|
|
var widgetsets = {};
|
2011-11-24 14:33:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
var log = function() {
|
|
|
|
if (console && console.log) {
|
|
|
|
console.log(arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var loadTheme = function(url) {
|
|
|
|
if(!themesLoaded[url]) {
|
2011-11-28 12:42:02 +01:00
|
|
|
log("loadTheme", url);
|
2011-11-24 14:33:22 +01:00
|
|
|
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) {
|
2011-11-28 11:59:34 +01:00
|
|
|
if (widgetsets[widgetset]) {
|
2011-11-24 14:33:22 +01:00
|
|
|
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);
|
|
|
|
|
2011-11-28 11:59:34 +01:00
|
|
|
widgetsets[widgetset] = {
|
|
|
|
pendingApps: []
|
|
|
|
};
|
2011-11-24 14:33:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
window.vaadin = window.vaadin || {
|
|
|
|
setDefaults: function(d) {
|
|
|
|
if (defaults) {
|
|
|
|
throw "Defaults already defined";
|
|
|
|
}
|
2011-11-25 15:04:20 +01:00
|
|
|
log("Got defaults", d)
|
2011-11-24 14:33:22 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-25 15:04:20 +01:00
|
|
|
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) {
|
2011-11-28 12:42:02 +01:00
|
|
|
if (r.readyState == 4) {
|
2011-11-25 15:04:20 +01:00
|
|
|
if (r.status == 200){
|
2011-11-28 12:42:02 +01:00
|
|
|
log("Got root config response", r.responseText);
|
2011-11-25 15:04:20 +01:00
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 12:42:02 +01:00
|
|
|
config.initPending = false;
|
2011-11-25 15:04:20 +01:00
|
|
|
|
|
|
|
// Try bootstrapping again, this time without fetching missing info
|
|
|
|
bootstrapApp(false);
|
|
|
|
} else {
|
|
|
|
log('Error', r.statusText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
r.send(null);
|
|
|
|
|
|
|
|
log('sending request to ', url);
|
|
|
|
};
|
2011-11-24 15:57:53 +01:00
|
|
|
|
2011-11-24 14:33:22 +01:00
|
|
|
//Export public data
|
|
|
|
var app = {
|
|
|
|
'getConfig': getConfig
|
|
|
|
};
|
|
|
|
apps[appId] = app;
|
|
|
|
|
2011-11-25 15:04:20 +01:00
|
|
|
var bootstrapApp = function(mayDefer) {
|
|
|
|
var themeUri = getConfig('themeUri');
|
|
|
|
if (themeUri) {
|
|
|
|
loadTheme(themeUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
var widgetsetBase = getConfig('widgetsetBase');
|
|
|
|
var widgetset = getConfig('widgetset');
|
2011-11-28 12:42:02 +01:00
|
|
|
var initPending = getConfig('initPending');
|
2011-11-25 15:04:20 +01:00
|
|
|
if (widgetset && widgetsetBase) {
|
|
|
|
loadWidgetset(widgetsetBase, widgetset);
|
2011-11-28 12:42:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (initPending) {
|
|
|
|
if (mayDefer) {
|
|
|
|
fetchRootConfig();
|
|
|
|
} else {
|
|
|
|
throw "May not defer bootstrap any more";
|
|
|
|
}
|
|
|
|
} else {
|
2011-11-28 11:59:34 +01:00
|
|
|
if (widgetsets[widgetset].callback) {
|
|
|
|
log("Starting from bootstrap", appId);
|
|
|
|
widgetsets[widgetset].callback(appId);
|
2011-11-25 15:04:20 +01:00
|
|
|
} else {
|
2011-11-28 12:42:02 +01:00
|
|
|
log("Setting pending startup", appId);
|
2011-11-28 11:59:34 +01:00
|
|
|
widgetsets[widgetset].pendingApps.push(appId);
|
2011-11-25 15:04:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-24 14:33:22 +01:00
|
|
|
return app;
|
|
|
|
},
|
|
|
|
getApp: function(appId) {
|
|
|
|
var app = apps[appId];
|
|
|
|
return app;
|
|
|
|
},
|
2011-11-28 11:59:34 +01:00
|
|
|
registerWidgetset: function(widgetset, callback) {
|
|
|
|
log("Widgetset registered", widgetset)
|
|
|
|
widgetsets[widgetset].callback = callback;
|
|
|
|
for(var i = 0; i < widgetsets[widgetset].pendingApps.length; i++) {
|
|
|
|
var appId = widgetsets[widgetset].pendingApps[i];
|
|
|
|
log("Starting from register widgetset", appId);
|
|
|
|
callback(appId);
|
2011-11-24 14:33:22 +01:00
|
|
|
}
|
2011-11-28 11:59:34 +01:00
|
|
|
widgetsets[widgetset].pendingApps = null;
|
2011-11-24 14:33:22 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
log('Vaadin bootstrap loaded');
|
|
|
|
})();
|