Class JSLoader

java.lang.Object
org.parttio.vaadinjsloader.JSLoader

public class JSLoader extends Object
Dynamic JavaScript library loader for Vaadin.
  • Field Details

  • Constructor Details

    • JSLoader

      public JSLoader()
  • Method Details

    • load

      public static void load(com.vaadin.flow.component.Component component, String libraryName, String version, String libraryFile, String urlPattern)
      Loads a JavaScript and CSS files dynamically from given URL.

      Pattern is used to construct the URL for the library. The pattern can contain named placeholders {library}for the library name and {version} for the version. The placeholders are replaced with the actual values before the URL is loaded.

      E.g. for unpkg.com: https://unpkg.com/{library}@{version}/dist/{library}

      Parameters:
      component - the UI instance to load the library for
      urlPattern - the base URL pattern to use for loading the library
      libraryName - the name of the library to load
      version - the version of the library to load
      libraryFile - the JavaScript file to load or null
    • loadFiles

      public static void loadFiles(com.vaadin.flow.component.Component component, String urlPattern, String libraryName, String version, String... file)
      Loads a JavaScript and CSS files dynamically from given URL.

      Pattern is used to construct the URL for the library. The pattern can contain named placeholders {library}for the library name and {version} for the version. The placeholders are replaced with the actual values before the URL is loaded.

      E.g. for unpkg.com: https://unpkg.com/{library}@{version}/dist/{library}

      Parameters:
      component - the UI instance to load the library for
      urlPattern - the base URL pattern to use for loading the library
      libraryName - the name of the library to load
      version - the version of the library to load
      file - the files of the library to load
    • getLoadedVersion

      public static String getLoadedVersion(com.vaadin.flow.component.Component component, String library)
      Returns the version of the library that has been loaded for the given UI.
      Parameters:
      component - the UI instance to check
      library - the name of the library to check
      Returns:
      the version of the library that has been loaded for the given UI
    • loadCdnjs

      public static void loadCdnjs(com.vaadin.flow.component.Component component, String libraryName, String version)
      Loads a minified JavaScript library dynamically from cdnjs.com.
      Parameters:
      component - the UI instance to load the library for
      libraryName - the name of the library to load
      version - the version of the library to load
      See Also:
    • loadCdnjs

      public static void loadCdnjs(com.vaadin.flow.component.Component component, String libraryName, String version, boolean minified)
      Loads a JavaScript library dynamically from cdnjs.com.
      Parameters:
      component - the UI instance to load the library for
      libraryName - the name of the library to load
      version - the version of the library to load
    • loadUnpkg

      public static void loadUnpkg(com.vaadin.flow.component.Component component, String libraryName, String version, String... libraryFile)
      Loads a JavaScript library dynamically from unpkg.com.
      Parameters:
      component - the UI instance to load the library for
      libraryName - the name of the library to load
      version - the version of the library to load
      libraryFile - the file(s) to load or null
    • loadJavaResource

      public static void loadJavaResource(com.vaadin.flow.component.UI ui, Class<?> cls, String libraryName, String... files)
      Load library from Java resources. Useful when loading JavaScript and CSS resources e.g. from Maven's src/main/resources folder. Effectively uses Class.getResourceAsStream().
      Parameters:
      ui - The UI where we are loading the resources.
      cls - Class to load resources from using getResourceAsStream.
      libraryName - Name of the library used as part of URL. This is also used to avoid loading twice.
      files - The files to load for this library. Supports .js and .css.
      See Also:
    • isLoaded

      public static boolean isLoaded(com.vaadin.flow.component.Component component, String library)
      Check if the given library has been loaded for the UI.
      Parameters:
      component - the UI instance to check
      library - the name of the library to check
      Returns:
      true if the given library has been loaded for the given UI
    • isLoaded

      public static boolean isLoaded(com.vaadin.flow.component.Component component, String library, String version)
      Check if the given library and version has been loaded for the UI.
      Parameters:
      component - the UI instance to check
      library - the name of the library to check
      version - the version of the library to check
      Returns:
      true if the given library and version has been loaded for the given UI