Package com.vaadin.client
Class ResourceLoader
java.lang.Object
com.vaadin.client.ResourceLoader
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEvent fired when a resource has been loaded.static interfaceEvent listener that gets notified when a resource has been loaded. -
Constructor Summary
ConstructorsConstructorDescriptionResourceLoader(Registry registry, boolean initFromDom) Creates a new resource loader. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddOnloadHandler(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.voidclearLoadedResourceById(String dependencyId) Clears a resource from the loaded resources set by its dependency ID.voidinlineHtml(String htmlContents, ResourceLoader.ResourceLoadListener resourceLoadListener) Inlines an HTML import and notify a listener when the HTML import is loaded.voidinlineScript(String scriptContents, ResourceLoader.ResourceLoadListener resourceLoadListener) Inlines a script and notify a listener when the script is loaded.voidinlineStyleSheet(String styleSheetContents, ResourceLoader.ResourceLoadListener resourceLoadListener) Inlines a stylesheet and notify a listener when the stylesheet is loaded.voidinlineStyleSheet(String styleSheetContents, ResourceLoader.ResourceLoadListener resourceLoadListener, String dependencyId) Inline a stylesheet with a specific dependency ID for tracking.voidloadDynamicImport(String expression, ResourceLoader.ResourceLoadListener resourceLoadListener) Loads a dynamic import via the provided JSexpressionand reports the result via theresourceLoadListener.voidloadHtml(String htmlUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, boolean async) Loads an HTML import and notify a listener when the HTML import is loaded.voidloadJsModule(String scriptUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, boolean async, boolean defer) Load a script with type module and notify a listener when the script is loaded.voidloadScript(String scriptUrl, ResourceLoader.ResourceLoadListener resourceLoadListener) Load a script and notify a listener when the script is loaded.voidloadScript(String scriptUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, boolean async, boolean defer) Load a script and notify a listener when the script is loaded.voidloadStylesheet(String stylesheetUrl, ResourceLoader.ResourceLoadListener resourceLoadListener) Load a stylesheet and notify a listener when the stylesheet is loaded.voidloadStylesheet(String stylesheetUrl, ResourceLoader.ResourceLoadListener resourceLoadListener, String dependencyId) Load a stylesheet with a specific dependency ID for tracking.voidSets the provided task to be run byHTMLImports.whenReady.
-
Constructor Details
-
ResourceLoader
Creates a new resource loader. You should not create you own resource loader, but instead useRegistry.getResourceLoader()to get an instance.- Parameters:
registry- the global registryinitFromDom-trueif currently loaded resources should be marked as loaded,falseto ignore currently loaded resources
-
-
Method Details
-
clearLoadedResourceById
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
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 = falseanddefer = falseattribute values, seeloadScript(String, ResourceLoadListener, boolean, boolean).- Parameters:
scriptUrl- the url of the script to loadresourceLoadListener- 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 loadresourceLoadListener- listener to notify when script is loadedasync- What mode the script.async attribute should be set todefer- 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 loadedasync- What mode the script.async attribute should be set todefer- 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 inlineresourceLoadListener- 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 loadresourceLoadListener- listener to notify when the HTML import is loadedasync- loads the import asynchronously, iftrue, 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 inlineresourceLoadListener- listener to notify when the HTML import is loaded
-
runWhenHtmlImportsReady
Sets the provided task to be run byHTMLImports.whenReady. The task is run immediately ifHTMLImports.whenReadyis not supported.- Parameters:
task- the task to run, notnull
-
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 tolistener- the listener to callevent- 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 loadresourceLoadListener- 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 loadresourceLoadListener- the listener that will get notified when the stylesheet is loadeddependencyId- 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 inlineresourceLoadListener- 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 inlineresourceLoadListener- the listener that will get notified when the stylesheet is loadeddependencyId- 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 JSexpressionand reports the result via theresourceLoadListener.- Parameters:
expression- the JS expression which returns a PromiseresourceLoadListener- a listener to report the Promise result exection
-