The Application Class

The Application() class provides the foundation for UI elements in Foundry VTT. As a developer, you should extend this class to define your own custom UI concepts which can be rendered in-game.

class Application(options)

The standard application window that is rendered for a large variety of UI elements in Foundry VTT

Arguments:
  • options (Object) – Configuration options which control how the application is rendered
  • options.height (Number) – The height in pixels (or “auto”) for the rendered element
  • options.width (Number) – The width in pixels (or “auto”) for the rendered element
  • options.top (Number) – The vertical position (from the top) of the rendered element
  • options.left (Number) – The horizontal position (from the left) of the rendered element
  • options.popOut (Boolean) – Display the element wrapped in a containing window frame (true, the default) or only the inner HTML (false).
Application._element

type: jQuery

An internal reference to the HTML element this application renders

Application.activateListeners(html)

Once the HTML for an Application has been rendered, activate event listeners which provide interactivity for the application

Arguments:
  • html (jQuery|HTMLElement) –
Application.appId

type: Number

The application ID is a unique incrementing integer which is used to identify every application window drawn by the VTT

Application.close()

Close the application and un-register references to it within UI mappings

Application.defaultOptions

Assign the default options which are supported by this Application

Application.element

type: jQuery|HTMLElement

Return the active application element, if it currently exists in the DOM

Application.getData()

An application should define the data object used to render its template. This function may either return an Object directly, or a Promise which resolves to an Object If undefined, the default implementation will return an empty object allowing only for rendering of static HTML

Returns:Object|Promise
Application.options

type: Object

The options provided to this application upon initialization

Application.popOut

type: boolean

Control the rendering style of the application. If popOut is true, the application is rendered in its own wrapper window, otherwise only the inner app content is rendered

Application.position

A convenient reference to the positioning attributes provided as Application options

Application.render(force=false)

Render the Application by evaluating it’s HTML template against the object of data provided by the getData method If the Application is rendered as a pop-out window, wrap the contained HTML in an outer frame with window controls

Arguments:
  • force (Boolean) – Add the rendered application to the DOM if it is not already present. If false, the Application will only be re-rendered if it is already present.
Application.setPosition(left, top)

Set the application position and store it’s new location

Arguments:
  • left (Number) –
  • top (Number) –
Application.template

type: String

The path to the HTML template file which should be used to render the inner content of the app

Application.title

type: String

An Application window should define its own title definition logic which may be dynamic depending on its data

Application Example

This section provides a working example Application that could be defined in your game. When creating a module, or a UI component of a game system, you would pair this JavaScript code which defines the Application with an HTML template (options.template) as well as accompanying CSS rules to define how the HTML should be styled.

/**
 * A simple demo application which displays each active player's impersonated Actor
 * Displays the portrait and health bar for that Actor
 */

class PartySummary extends Application {

  /**
   * Define default options for the PartySummary application
   */
  static get defaultOptions() {
    const options = super.defaultOptions;
    options.template = "public/modules/party-summary/templates/summary.html";
    options.width = 600;
    options.height = "auto";
    return options;
  }

  /* -------------------------------------------- */

  /**
   * Return the data used to render the summary template
   * Get all the active users
   * The impersonated character for each player is available as user.character
   */
   getData() {
    return game.users.entities.filter(u => u.active);
   }

  /* -------------------------------------------- */

  /**
   * Add some event listeners to the UI to provide interactivity
   * Let's have the game highlight each player's token when their name is clicked on
   */
  activateListeners(html) {

    html.find(".actor-name").click(ev => {
      ev.preventDefault();

      // Get the actor which was clicked and the active token(s) for that actor
      let actorId = ev.currentTarget.parentElement.getAttribute("data-actor-id"),
          actor = game.actors.get(actorId),
          tokens = actor.getActiveTokens(true);

      // Highlight active token(s) by triggering the token's mouse-over handler
      for ( let t of tokens ) {
        t._onMouseOver();
      }
    })
  }
}

To render your party summary UI to the player, all that needs to be done is to create an instance of the application class and call it’s render function.

let ps = new PartySummary();
ps.render(true);