Class ResourceLoader

java.lang.Object
com.vaadin.client.ResourceLoader

public class ResourceLoader extends Object
ResourceLoader lets you dynamically include external scripts and styles on the page and lets you know when the resource has been loaded. You can also preload resources, allowing them to get cached by the browser without being evaluated. This enables downloading multiple resources at once while still controlling in which order e.g. scripts are executed.
Since:
1.0
Author:
Vaadin Ltd
  • Constructor Details

    • ResourceLoader

      public ResourceLoader(Registry registry, boolean initFromDom)
      Creates a new resource loader. You should not create you own resource loader, but instead use Registry.getResourceLoader() to get an instance.
      Parameters:
      registry - the global registry
      initFromDom - true if currently loaded resources should be marked as loaded, false to ignore currently loaded resources
  • Method Details

    • clearLoadedResourceById

      public void clearLoadedResourceById(String dependencyId)
      Clears a resource from the loaded resources set by its dependency ID.

      This is used when a resource is removed from the DOM using its dependency ID.

      Parameters:
      dependencyId - the dependency ID of the resource to clear
    • loadScript

      public void loadScript(String scriptUrl, ResourceLoader.ResourceLoadListener resourceLoadListener)
      Load a script and notify a listener when the script is loaded. Calling this method when the script is currently loading or already loaded doesn't cause the script to be loaded again, but the listener will still be notified when appropriate.

      Loads all dependencies with async = false and defer = false attribute values, see loadScript(String, ResourceLoadListener, boolean, boolean).

      Parameters:
      scriptUrl - the url of the script to load
      resourceLoadListener - the listener that will get notified when the script is loaded
    • loadScript

      public void loadScript(String scriptUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, boolean async, boolean defer)
      Load a script and notify a listener when the script is loaded. Calling this method when the script is currently loading or already loaded doesn't cause the script to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      scriptUrl - url of script to load
      resourceLoadListener - listener to notify when script is loaded
      async - What mode the script.async attribute should be set to
      defer - What mode the script.defer attribute should be set to
    • loadJsModule

      public void loadJsModule(String scriptUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, boolean async, boolean defer)
      Load a script with type module and notify a listener when the script is loaded. Calling this method when the script is currently loading or already loaded doesn't cause the script to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      scriptUrl - url of script to load. It should be an external URL.
      resourceLoadListener - listener to notify when script is loaded
      async - What mode the script.async attribute should be set to
      defer - What mode the script.defer attribute should be set to
    • inlineScript

      public void inlineScript(String scriptContents, ResourceLoader.ResourceLoadListener resourceLoadListener)
      Inlines a script and notify a listener when the script is loaded. Calling this method when the script is currently loading or already loaded doesn't cause the script to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      scriptContents - the script contents to inline
      resourceLoadListener - listener to notify when script is loaded
    • loadHtml

      public void loadHtml(String htmlUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, boolean async)
      Loads an HTML import and notify a listener when the HTML import is loaded. Calling this method when the HTML import is currently loading or already loaded doesn't cause the HTML import to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      htmlUrl - url of HTML import to load
      resourceLoadListener - listener to notify when the HTML import is loaded
      async - loads the import asynchronously, if true, synchronously otherwise
    • inlineHtml

      public void inlineHtml(String htmlContents, ResourceLoader.ResourceLoadListener resourceLoadListener)
      Inlines an HTML import and notify a listener when the HTML import is loaded. Calling this method when the HTML import is currently loading or already loaded doesn't cause the HTML import to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      htmlContents - the html contents to inline
      resourceLoadListener - listener to notify when the HTML import is loaded
    • runWhenHtmlImportsReady

      public void runWhenHtmlImportsReady(Runnable task)
      Sets the provided task to be run by HTMLImports.whenReady. The task is run immediately if HTMLImports.whenReady is not supported.
      Parameters:
      task - the task to run, not null
    • addOnloadHandler

      public static void addOnloadHandler(elemental.dom.Element element, ResourceLoader.ResourceLoadListener listener, ResourceLoader.ResourceLoadEvent event)
      Adds an onload listener to the given element, which should be a link or a script tag. The listener is called whenever loading is complete or an error occurred.
      Parameters:
      element - the element to attach a listener to
      listener - the listener to call
      event - the event passed to the listener
    • loadStylesheet

      public void loadStylesheet(String stylesheetUrl, ResourceLoader.ResourceLoadListener resourceLoadListener)
      Load a stylesheet and notify a listener when the stylesheet is loaded. Calling this method when the stylesheet is currently loading or already loaded doesn't cause the stylesheet to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      stylesheetUrl - the url of the stylesheet to load
      resourceLoadListener - the listener that will get notified when the stylesheet is loaded
    • loadStylesheet

      public void loadStylesheet(String stylesheetUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, String dependencyId)
      Load a stylesheet with a specific dependency ID for tracking. Calling this method when the stylesheet is currently loading or already loaded doesn't cause the stylesheet to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      stylesheetUrl - the url of the stylesheet to load
      resourceLoadListener - the listener that will get notified when the stylesheet is loaded
      dependencyId - the ID to track this dependency with (for later removal)
    • inlineStyleSheet

      public void inlineStyleSheet(String styleSheetContents, ResourceLoader.ResourceLoadListener resourceLoadListener)
      Inlines a stylesheet and notify a listener when the stylesheet is loaded. Calling this method when the stylesheet is currently loading or already loaded doesn't cause the stylesheet to be loaded again, but the listener will still be notified when appropriate.
      Parameters:
      styleSheetContents - the contents to inline
      resourceLoadListener - the listener that will get notified when the stylesheet is loaded
    • inlineStyleSheet

      public void inlineStyleSheet(String styleSheetContents, ResourceLoader.ResourceLoadListener resourceLoadListener, String dependencyId)
      Inline a stylesheet with a specific dependency ID for tracking.
      Parameters:
      styleSheetContents - the contents to inline
      resourceLoadListener - the listener that will get notified when the stylesheet is loaded
      dependencyId - the ID to track this dependency with (for later removal)
    • loadDynamicImport

      public void loadDynamicImport(String expression, ResourceLoader.ResourceLoadListener resourceLoadListener)
      Loads a dynamic import via the provided JS expression and reports the result via the resourceLoadListener.
      Parameters:
      expression - the JS expression which returns a Promise
      resourceLoadListener - a listener to report the Promise result exection