var defaults; | var defaults; | ||||
var apps = {}; | var apps = {}; | ||||
var themesLoaded = {}; | var themesLoaded = {}; | ||||
var widgetsetsRequested = {} | |||||
var widgetsetApps = {}; | |||||
var widgetsets = {}; | |||||
var log = function() { | var log = function() { | ||||
} | } | ||||
var loadWidgetset = function(basePath, widgetset) { | var loadWidgetset = function(basePath, widgetset) { | ||||
if (widgetsetsRequested[widgetset]) { | |||||
//TODO Tell the widgetset to load another application | |||||
if (widgetsets[widgetset]) { | |||||
return; | return; | ||||
} | } | ||||
log("load widgetset", basePath, widgetset) | log("load widgetset", basePath, widgetset) | ||||
scriptTag.setAttribute('src', url); | scriptTag.setAttribute('src', url); | ||||
document.getElementsByTagName('head')[0].appendChild(scriptTag); | document.getElementsByTagName('head')[0].appendChild(scriptTag); | ||||
widgetsetsRequested[widgetset] = true; | |||||
widgetsets[widgetset] = { | |||||
pendingApps: [] | |||||
}; | |||||
} | } | ||||
window.vaadin = window.vaadin || { | window.vaadin = window.vaadin || { | ||||
var widgetset = getConfig('widgetset'); | var widgetset = getConfig('widgetset'); | ||||
if (widgetset && widgetsetBase) { | if (widgetset && widgetsetBase) { | ||||
loadWidgetset(widgetsetBase, widgetset); | loadWidgetset(widgetsetBase, widgetset); | ||||
if (widgetsetApps[widgetset]) { | |||||
widgetsetApps[widgetset].push(appId); | |||||
if (widgetsets[widgetset].callback) { | |||||
log("Starting from bootstrap", appId); | |||||
widgetsets[widgetset].callback(appId); | |||||
} else { | } else { | ||||
widgetsetApps[widgetset] = [appId]; | |||||
log("Setting pending startup of ", appId); | |||||
widgetsets[widgetset].pendingApps.push(appId); | |||||
} | } | ||||
} else if (mayDefer) { | } else if (mayDefer) { | ||||
fetchRootConfig(); | fetchRootConfig(); | ||||
} else { | } else { | ||||
throw "Widgetset not defined"; | |||||
throw "Widgetset not defined: " + widgetsetBase + " -> " + widgetset; | |||||
} | } | ||||
} | } | ||||
bootstrapApp(true); | bootstrapApp(true); | ||||
var app = apps[appId]; | var app = apps[appId]; | ||||
return app; | return app; | ||||
}, | }, | ||||
popWidgetsetApp: function(widgetset) { | |||||
if (widgetsetApps[widgetset]) { | |||||
return widgetsetApps[widgetset].pop(); | |||||
} else { | |||||
return null; | |||||
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); | |||||
} | } | ||||
widgetsets[widgetset].pendingApps = null; | |||||
} | } | ||||
}; | }; | ||||
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler; | import com.google.gwt.core.client.GWT.UncaughtExceptionHandler; | ||||
import com.google.gwt.core.client.JavaScriptObject; | import com.google.gwt.core.client.JavaScriptObject; | ||||
import com.google.gwt.core.client.JsArrayString; | import com.google.gwt.core.client.JsArrayString; | ||||
import com.google.gwt.core.client.Scheduler; | |||||
import com.google.gwt.core.client.Scheduler.ScheduledCommand; | |||||
import com.google.gwt.user.client.Command; | import com.google.gwt.user.client.Command; | ||||
import com.google.gwt.user.client.Timer; | import com.google.gwt.user.client.Timer; | ||||
import com.vaadin.terminal.gwt.client.ui.VUnknownComponent; | import com.vaadin.terminal.gwt.client.ui.VUnknownComponent; | ||||
} | } | ||||
/** | |||||
* Starts the next unstarted application. The WidgetSet should call this | |||||
* once to start the first application; after that, each application should | |||||
* call this once it has started. This ensures that the applications are | |||||
* started synchronously, which is neccessary to avoid session-id problems. | |||||
* | |||||
* @return true if an unstarted application was found | |||||
*/ | |||||
public static boolean startNextApplication() { | |||||
String applicationId = getNextUnstartedApplicationId(GWT | |||||
.getModuleName()); | |||||
if (applicationId != null) { | |||||
ApplicationConfiguration appConf = getConfigFromDOM(applicationId); | |||||
ApplicationConnection a = GWT.create(ApplicationConnection.class); | |||||
a.init(widgetSet, appConf); | |||||
a.start(); | |||||
runningApplications.add(a); | |||||
return true; | |||||
} else { | |||||
deferredWidgetLoader = new DeferredWidgetLoader(); | |||||
return false; | |||||
} | |||||
public static void startApplication(final String applicationId) { | |||||
Scheduler.get().scheduleDeferred(new ScheduledCommand() { | |||||
public void execute() { | |||||
ApplicationConfiguration appConf = getConfigFromDOM(applicationId); | |||||
ApplicationConnection a = GWT | |||||
.create(ApplicationConnection.class); | |||||
a.init(widgetSet, appConf); | |||||
a.start(); | |||||
runningApplications.add(a); | |||||
} | |||||
}); | |||||
} | } | ||||
public static List<ApplicationConnection> getRunningApplications() { | public static List<ApplicationConnection> getRunningApplications() { | ||||
return runningApplications; | return runningApplications; | ||||
} | } | ||||
private native static String getNextUnstartedApplicationId( | |||||
String widgetsetName) | |||||
/*-{ | |||||
return $wnd.vaadin.popWidgetsetApp(widgetsetName); | |||||
}-*/; | |||||
private native static JsoConfiguration getJsoConfiguration(String appId) | private native static JsoConfiguration getJsoConfiguration(String appId) | ||||
/*-{ | /*-{ | ||||
return $wnd.vaadin.getApp(appId); | return $wnd.vaadin.getApp(appId); | ||||
} | } | ||||
}); | }); | ||||
startNextApplication(); | |||||
registerCallback(GWT.getModuleName()); | |||||
deferredWidgetLoader = new DeferredWidgetLoader(); | |||||
} | } | ||||
public native static void registerCallback(String widgetsetName) | |||||
/*-{ | |||||
var callbackHandler = @com.vaadin.terminal.gwt.client.ApplicationConfiguration::startApplication(Ljava/lang/String;); | |||||
$wnd.vaadin.registerWidgetset(widgetsetName, callbackHandler); | |||||
}-*/; | |||||
/** | /** | ||||
* Checks if client side is in debug mode. Practically this is invoked by | * Checks if client side is in debug mode. Practically this is invoked by | ||||
* adding ?debug parameter to URI. | * adding ?debug parameter to URI. |
public void onError(Request request, Throwable exception) { | public void onError(Request request, Throwable exception) { | ||||
showCommunicationError(exception.getMessage()); | showCommunicationError(exception.getMessage()); | ||||
endRequest(); | endRequest(); | ||||
if (!applicationRunning) { | |||||
// start failed, let's try to start the next app | |||||
ApplicationConfiguration.startNextApplication(); | |||||
} | |||||
} | } | ||||
public void onResponseReceived(Request request, | public void onResponseReceived(Request request, | ||||
} else { | } else { | ||||
applicationRunning = true; | applicationRunning = true; | ||||
handleWhenCSSLoaded(jsonText, json); | handleWhenCSSLoaded(jsonText, json); | ||||
ApplicationConfiguration.startNextApplication(); | |||||
} | } | ||||
} | } | ||||