summaryrefslogtreecommitdiffstats
path: root/WebContent/VAADIN/vaadinBootstrap.js
blob: 0aec37efe79d58659f717a04bb541e7eeabc2a14 (plain)
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
(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();
		
		//document.write("<script type='text/javascript' src='"+url+"'></script>");
		
		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", defaults)
			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 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];
				}
			}
			
			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;
			}
			
			//Export public data
			var app = {
				'getConfig': getConfig
			};
			apps[appId] = app;
			
			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');
})();