Example usage for com.vaadin.client ResourceLoader loadScript

List of usage examples for com.vaadin.client ResourceLoader loadScript

Introduction

In this page you can find the example usage for com.vaadin.client ResourceLoader loadScript.

Prototype

public void loadScript(final String scriptUrl, final ResourceLoadListener resourceLoadListener) 

Source Link

Document

Load a script and notify a listener when the script is loaded.

Usage

From source file:org.opencms.ui.client.CmsWidgetSetEntryPoint.java

License:Open Source License

/**
 * Loads JavaScript resources into the window context.<p>
 *
 * @param dependencies the dependencies to load
 * @param callback the callback to execute once the resources are loaded
 *//*from  w  w  w . j ava2s. c  o m*/
public static void loadScriptDependencies(final JsArrayString dependencies, final JavaScriptObject callback) {

    if (dependencies.length() == 0) {
        return;
    }

    // Listener that loads the next when one is completed
    ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {

        @Override
        public void onError(ResourceLoadEvent event) {

            CmsDebugLog.consoleLog(event.getResourceUrl() + " could not be loaded.");
            // The show must go on
            onLoad(event);
        }

        @Override
        public void onLoad(ResourceLoadEvent event) {

            if (dependencies.length() != 0) {
                String url = dependencies.shift();
                // Load next in chain (hopefully already preloaded)
                event.getResourceLoader().loadScript(url, this);
            } else {
                // finished loading dependencies
                callNativeFunction(callback);
            }
        }
    };

    ResourceLoader loader = ResourceLoader.get();

    // Start chain by loading first
    String url = dependencies.shift();
    loader.loadScript(url, resourceLoadListener);
    if (ResourceLoader.supportsInOrderScriptExecution()) {
        for (int i = 0; i < dependencies.length(); i++) {
            String preloadUrl = dependencies.get(i);
            loader.loadScript(preloadUrl, null);
        }
    } else {
        // Preload all remaining
        for (int i = 0; i < dependencies.length(); i++) {
            String preloadUrl = dependencies.get(i);
            loader.preloadResource(preloadUrl, null);
        }
    }
}