Jump to content

Introduction

Initializing widgets

UI widgets can now be created on elements without needing to write any code. This is done by specifying special attributes on the element using the HTML5 data attributes. Widgets each define their own behaviors - some wait for user interactions, others might immediately change the display when executed.

Usage

Each UI widget has a base attribute that identifies it, and causes the element to be passed to the widget module to be handled. All widget attributes have the prefix ips.

<!-- If a widget called 'Widget' existed, we could initialize it on an element like this -->
<div data-ipsWidget>
	...
</div>

In this example, ipsWidget would be referred to as the widget key. The data- part of the HTML attribute is implied throughout the documentation for individual widgets.

Each element can have multiple widgets specified; simply add each necessary widget prefix to the element as above. That said, bear in mind multiple widgets on a single element might cause conflicts if more than one responds to a user interaction. Test to be sure.

Note: In many cases, the helpers available in the IPS4 PHP framework will generate all of the required HTML for you, including widget attributes, meaning you won't need to manually specify UI widgets in your HTML. For example, the autocomplete form input type adds the necessary attributes, so you don't need to handle it yourself. Consult the PHP documentation for more information.

Passing options

Most widgets have a number of options that control their behavior. To pass an option, use this format:

<div data-ipsWidget
     data-ipsWidget-option1='Option value'
     data-ipsWidget-option2='Another option value'
>
...
</div>

Each option is added as an additional attribute on the element, with the attribute key being data-ipsWidget- followed by the option name.

By default, options are passed as strings because they come from the DOM. However, if the option contains only numbers, it will be cast explicitly as a number when passed into the widget. If the option contains the strings true or false, it will be cast as a boolean. In addition, options without a provided value are treated as the boolean true, meaning it is possible to specify true values like so:

<div data-ipsWidget data-ipsWidget-showWide>

Automatic initialization

Widgets are automatically initialized when the data API is used as described. Further to that, when new content is loaded into the page (such as when an AJAX request fires), any widgets within the new content will also be automatically initialized.


  Report Guide


×
×
  • Create New...