Package run.undead.js

Class JS

java.lang.Object
run.undead.js.JS

public class JS extends Object
JS is a helper class for building JS commands that are rendered in UndeadTemplates. JS provides a number of methods that make it easier to manipulate the DOM, dispatch client-side events, and push events (and data) to the server. Here is an example of using JS to toggle the visibility of an element:

    // in your {@link View#render} method ...
    public UndeadTemplate render() {
      return Undead.HTML."""
        <div id="my-div" class="hidden">Hello World!</div>
        <button ud-click="\{ JS.toggle("#my-div") }">Toggle</button>
      """ ;
    }
 
JS commands are "chainable" so you can build up a list of commands to execute in order. For example, the following hides an element and pushes an event to the server:

   // in your {@link View#render} method ...
   return Undead.HTML."""
    <button ud-click="\{ JS.hide("#my-div").push("my-event") }">Hide</button>
   """ ;
 
List of all the JS commands:
  • Field Details

    • DEFAULT_DISPLAY

      protected static final String DEFAULT_DISPLAY
      See Also:
    • moshi

      protected static final com.squareup.moshi.Moshi moshi
    • listAdaptor

      protected static final com.squareup.moshi.JsonAdapter<List> listAdaptor
  • Constructor Details

    • JS

      public JS()
  • Method Details

    • toJSON

      public String toJSON()
      `toJSON` returns the JSON String representation of the JS commands.
      Returns:
    • addClass

      public JS addClass(AddClassOpts addClassOpts)
      addClass adds css classes to DOM elements
      Parameters:
      addClassOpts - options for the add_class command
      Returns:
      the JS instance for chaining
      See Also:
    • addClass

      public JS addClass(String classNames)
      addClass adds css classes to DOM elements
      Parameters:
      classNames - the css class names to add (space separated)
      Returns:
      the JS instance for chaining
    • dispatch

      public JS dispatch(DispatchOpts dispatchOpts)
      dispatch dispatches an event to the DOM
      Parameters:
      dispatchOpts - options for the dispatch command
      Returns:
      the JS instance for chaining
      See Also:
    • dispatch

      public JS dispatch(String event)
      dispatch dispatches an event to the DOM
      Parameters:
      event - the name of the event to dispatch
      Returns:
      the JS instance for chaining
    • exec

      public JS exec(ExecOpts execOpts)
      exec executes JS commands located in element attributes
      Parameters:
      execOpts - options for the exec command
      Returns:
      the JS instance for chaining
      See Also:
    • exec

      public JS exec(String attr)
      exec executes JS commands located in element attributes
      Parameters:
      attr - the name of the attribute that contains the JS commands
      Returns:
      the JS instance for chaining
    • focusFirst

      public JS focusFirst(FocusFirstOpts focusFirstOpts)
      focusFirst sends focus to the first focusable child in selector
      Parameters:
      focusFirstOpts - options for the focus_first command
      Returns:
      the JS instance for chaining
      See Also:
    • focusFirst

      public JS focusFirst()
      focusFirst sends focus to the first focusable child in selector
      Returns:
      the JS instance for chaining
    • focus

      public JS focus()
      focus sends focus to a selector
      Returns:
      the JS instance for chaining
    • focus

      public JS focus(String to)
      focus sends focus to a selector
      Parameters:
      to - the DOM selector to send focus to
      Returns:
      the JS instance for chaining
    • hide

      public JS hide(HideOpts hideOpts)
      hide makes DOM elements invisible
      Parameters:
      hideOpts - options for the hide command
      Returns:
      the JS instance for chaining
      See Also:
    • hide

      public JS hide()
      hide makes DOM elements invisible
      Returns:
      the JS instance for chaining
    • patch

      public JS patch(PatchOpts patchOpts)
      patch sends a patch event to the server
      Parameters:
      patchOpts - options for the patch command
      Returns:
      the JS instance for chaining
      See Also:
    • patch

      public JS patch(String href)
      patch sends a patch event to the server
      Parameters:
      href - the href to patch
      Returns:
      the JS instance for chaining
    • popFocus

      public JS popFocus()
      popFocus focuses the last pushed element
      Returns:
      the JS instance for chaining
    • pushFocus

      public JS pushFocus(PushFocusOpts pushFocusOpts)
      pushFocus pushes focus from the source element to be later popped
      Parameters:
      pushFocusOpts - options for the push_focus command
      Returns:
      the JS instance for chaining
      See Also:
    • pushFocus

      public JS pushFocus(String to)
      pushFocus pushes focus from the source element to be later popped
      Parameters:
      to - the DOM selector to push focus to
      Returns:
      the JS instance for chaining
    • push

      public JS push(PushOpts pushOpts)
      push sends an event to the server
      Parameters:
      pushOpts - options for the push command
      Returns:
      the JS instance for chaining
      See Also:
    • push

      public JS push(String event)
      push sends an event to the server
      Parameters:
      event - the name of the event to push
      Returns:
      the JS instance for chaining
    • removeAttr

      public JS removeAttr(RemoveAttrOpts removeAttrOpts)
      removeAttr removes an attribute from a DOM element
      Parameters:
      removeAttrOpts - options for the remove_attr command
      Returns:
      the JS instance for chaining
      See Also:
    • removeAttr

      public JS removeAttr(String name)
      removeAttr removes an attribute from a DOM element
      Parameters:
      name - the name of the attribute to remove
      Returns:
      the JS instance for chaining
    • removeClass

      public JS removeClass(RemoveClassOpts removeClassOpts)
      removeClass removes css classes from DOM elements
      Parameters:
      removeClassOpts - options for the remove_class command
      Returns:
      the JS instance for chaining
      See Also:
    • removeClass

      public JS removeClass(String classNames)
      removeClass removes css classes from DOM elements
      Parameters:
      classNames - the css class names to remove (space separated)
      Returns:
      the JS instance for chaining
    • setAttr

      public JS setAttr(SetAttrOpts setAttrOpts)
      setAttr sets an attribute on a DOM element
      Parameters:
      setAttrOpts - options for the set_attr command
      Returns:
      the JS instance for chaining
      See Also:
    • setAttr

      public JS setAttr(String name, String value)
      setAttr sets an attribute on a DOM element
      Parameters:
      name - the name of the attribute to set
      value - the value of the attribute to set
      Returns:
      the JS instance for chaining
    • show

      public JS show(ShowOpts showOpts)
      show makes DOM elements visible
      Parameters:
      showOpts - options for the show command
      Returns:
      the JS instance for chaining
      See Also:
    • show

      public JS show()
      show makes DOM elements visible
      Returns:
      the JS instance for chaining
    • toggle

      public JS toggle(ToggleOpts toggleOpts)
      toggle toggles the visibility of DOM elements
      Parameters:
      toggleOpts - options for the toggle command
      Returns:
      the JS instance for chaining
      See Also:
    • toggle

      public JS toggle()
      toggle toggles the visibility of DOM elements
      Returns:
      the JS instance for chaining
    • transition

      public JS transition(TransitionOpts transitionOpts)
      transition applies a css transition to a DOM element
      Parameters:
      transitionOpts - options for the transition command
      Returns:
      the JS instance for chaining
      See Also: