Class JavascriptWebResourceBuilder

All Implemented Interfaces:
WebResourceKeyProvider, HtmlViewElementBuilder<NodeViewElement,JavascriptWebResourceBuilder>, ViewElementBuilder<NodeViewElement>

public class JavascriptWebResourceBuilder extends AbstractNodeViewElementBuilder<NodeViewElement,JavascriptWebResourceBuilder> implements WebResourceKeyProvider
Builder class for creating a ViewElement of tag <script> which supports either external (src attribute) or inlined scripts. An inline script is specified using one of the overloaded inline() methods. Either a static HTML output string is specified, or a custom ViewElementBuilder which will return the elements that should be inlined.

In effect the latter is identical as doing ContainerViewElementBuilderSupport.add(ViewElementBuilder...), the short-hand inline(String) resets any previously set url(String) value however and has increased contextual readability.

A custom inline data function is available as globalJsonData(String, Object), which registers a custom object as global JSON variables under a specific key. See the function javadoc for more details.

Since:
3.2.0
Author:
Marc Vanbrabant
See Also:
  • Constructor Details

    • JavascriptWebResourceBuilder

      public JavascriptWebResourceBuilder()
  • Method Details

    • type

      public JavascriptWebResourceBuilder type(@NonNull @NonNull org.springframework.http.MediaType mediaType)
    • type

    • defer

      Shorthand for defer(true)
    • async

      Shorthand for async(true)
    • url

      Set the src for this script element. Will remove any previously set inline(String) value.
      Parameters:
      url - for src
      Returns:
      current builder
    • inline

      public JavascriptWebResourceBuilder inline(String inline)
      Set the inline script for this script element. Will remove any previously set url(String) value.
      Parameters:
      inline - script value
      Returns:
      current builder
    • inline

      Set the inline script for this script element. Will remove any previously set url(String) value.
      Parameters:
      inline - element builder
      Returns:
      current builder
    • crossOrigin

      public JavascriptWebResourceBuilder crossOrigin(String crossOrigin)
    • getWebResourceKey

      public Optional<String> getWebResourceKey()
      Specified by:
      getWebResourceKey in interface WebResourceKeyProvider
      Returns:
      the key that should be used for identifying the web resource
    • createElement

      protected NodeViewElement createElement(@NonNull @NonNull ViewElementBuilderContext builderContext)
      Specified by:
      createElement in class ViewElementBuilderSupport<NodeViewElement,JavascriptWebResourceBuilder>
    • globalJsonData

      public static ViewElementBuilder<TextViewElement> globalJsonData(@NonNull @NonNull String globalKey, Object data)
      Renders a client-side javascript snippet that attempts to register the specified data object as JSON values on the global window, under the globalKey specified. Only meant for use in a browser environment as it expects the window variable to be available.

      It is required that the key contains at least 2 segments separated by a . (dot). The first segment will be root key for the window variable, anything past the first segment will be the sub key.

      Keys with exactly two segments and no whitespace will be directly accessible. For example MyApp.Settings can be accessed from client-side code in the same fashion; but registering MyApp.Settings.Enabled would result in MyApp[Settings.Enabled] being registered.

      The data object can be any type, a simple fixed ObjectMapper will be used to convert it to JSON. It is not possible to customize the ObjectMapper used directly.

      NOTE: this function only verifies that the global key contains at least two segments, it does not validate the key value in any other fashion, nor if the resulting javascript will in fact be executable. It is advised to use both simple keys and straightforward data objects to pass settings to the client.

      Parameters:
      globalKey - root key for the data object
      Returns:
      data writer function
    • async

      public JavascriptWebResourceBuilder async(boolean async)
      Returns:
      this.
    • defer

      public JavascriptWebResourceBuilder defer(boolean defer)
      Returns:
      this.