Class JavascriptWebResourceBuilder
java.lang.Object
com.foreach.across.modules.web.ui.ViewElementBuilderSupport<T,SELF>
com.foreach.across.modules.web.ui.elements.builder.ContainerViewElementBuilderSupport<T,SELF>
com.foreach.across.modules.web.ui.elements.builder.AbstractNodeViewElementBuilder<NodeViewElement,JavascriptWebResourceBuilder>
com.foreach.across.modules.web.resource.elements.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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.foreach.across.modules.web.ui.ViewElementBuilderSupport
ViewElementBuilderSupport.ElementOrBuilder
Nested classes/interfaces inherited from interface com.foreach.across.modules.web.ui.ViewElementBuilder
ViewElementBuilder.Wither<T extends ViewElementBuilder>
-
Field Summary
Fields inherited from class com.foreach.across.modules.web.ui.ViewElementBuilderSupport
customTemplate, name
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionasync()
Shorthand forasync(true)
async
(boolean async) protected NodeViewElement
createElement
(@NonNull ViewElementBuilderContext builderContext) crossOrigin
(String crossOrigin) defer()
Shorthand fordefer(true)
defer
(boolean defer) static ViewElementBuilder<TextViewElement>
globalJsonData
(@NonNull String globalKey, Object data) Renders a client-side javascript snippet that attempts to register the specifieddata
object as JSON values on the globalwindow
, under theglobalKey
specified.inline
(ViewElementBuilder inline) Set theinline
script for this script element.Set theinline
script for this script element.type
(@NonNull org.springframework.http.MediaType mediaType) Set thesrc
for this script element.Methods inherited from class com.foreach.across.modules.web.ui.elements.builder.AbstractNodeViewElementBuilder
apply, attribute, attributes, clearAttributes, css, htmlId, removeAttribute, removeCss
Methods inherited from class com.foreach.across.modules.web.ui.elements.builder.ContainerViewElementBuilderSupport
add, add, addAll, addFirst, addFirst, sort
Methods inherited from class com.foreach.across.modules.web.ui.ViewElementBuilderSupport
build, configure, customTemplate, elementSupplier, elementSupplier, name, postProcess, postProcessor, registerWebResources, with
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.foreach.across.modules.web.ui.elements.builder.HtmlViewElementBuilder
data, removeData
Methods inherited from interface com.foreach.across.modules.web.ui.ViewElementBuilder
andThen, build, doWith, map, map, postProcess, postProcess
-
Constructor Details
-
JavascriptWebResourceBuilder
public JavascriptWebResourceBuilder()
-
-
Method Details
-
type
public JavascriptWebResourceBuilder type(@NonNull @NonNull org.springframework.http.MediaType mediaType) -
type
-
defer
Shorthand fordefer(true)
-
async
Shorthand forasync(true)
-
url
Set thesrc
for this script element. Will remove any previously setinline(String)
value.- Parameters:
url
- for src- Returns:
- current builder
-
inline
Set theinline
script for this script element. Will remove any previously seturl(String)
value.- Parameters:
inline
- script value- Returns:
- current builder
-
inline
Set theinline
script for this script element. Will remove any previously seturl(String)
value.- Parameters:
inline
- element builder- Returns:
- current builder
-
crossOrigin
-
getWebResourceKey
- Specified by:
getWebResourceKey
in interfaceWebResourceKeyProvider
- Returns:
- the key that should be used for identifying the web resource
-
createElement
- Specified by:
createElement
in classViewElementBuilderSupport<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 specifieddata
object as JSON values on the globalwindow
, under theglobalKey
specified. Only meant for use in a browser environment as it expects thewindow
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 thewindow
variable, anything past the first segment will be the sub key. Keys with exactly two segments and no whitespace will be directly accessible. For exampleMyApp.Settings
can be accessed from client-side code in the same fashion; but registeringMyApp.Settings.Enabled
would result inMyApp[Settings.Enabled]
being registered. The data object can be any type, a simple fixedObjectMapper
will be used to convert it to JSON. It is not possible to customize theObjectMapper
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
- Returns:
this
.
-
defer
- Returns:
this
.
-