Spanish Total.js
English
Documentation For AI agents
Contents

Settings and their form

Default values, the settings form and the script that applies the settings to an instance.

.md GitHub
On this page

Settings let the user configure each instance of a widget. You define three things: the default values in exports.config, the fields in <settings>, and a script that applies the values to the HTML of the instance.

Default values

exports.config = { name: 'World', align: 'left' };

The editor copies this object into each new instance. Make the <body> match these values, because the HTML is what the user sees before the first change.

The form

Each field is a UI component with a path that starts with ?. followed by the key of the setting:

<settings>
    <div class="padding">
        <div class="m">
            <ui-component name="input" path="?.name" config="required:1">Name</ui-component>
        </div>
        <ui-component name="input" path="?.align" config="dirsource:left|Left,center|Center">Align</ui-component>
    </div>
    <script>
        exports.save = function(config, el) {
            el.find('.CLASS-box b').text(config.name);
        };
    </script>
</settings>

The official widgets use these components:

ComponentExampleWhat it shows
input<ui-component name="input" path="?.url" config="required:1">URL address</ui-component>A text field. required:1 makes it mandatory.
input with type:checkbox<ui-component name="input" path="?.rounded" config="type:checkbox">Rounded</ui-component>A checkbox for a true or false value.
input with type:number<ui-component name="input" path="?.count" config="type:number">Count</ui-component>A number field.
input with dirsource<ui-component name="input" path="?.size" config="dirsource:small|Small,large|Large">Size</ui-component>A list to choose from, as value|Label pairs.
choose<ui-component name="choose" path="?.color" config="selector:div"> with one <div data-id="color1"> for each optionA row of swatches. The data-id of the selected one is the value.
configurationSee belowA form built from a list of fields.

The configuration component takes the list of fields in a <script type="text/plain">:

<ui-component name="configuration" path="?" class="invisible">
    <script type="text/plain">
        [
            { id: 'open', name: 'Open the first answer', type: 'boolean' },
            { id: 'size', name: 'Size', type: 'dropdown', items: [{ id: 'small', name: 'Small' }, { id: 'large', name: 'Large' }] },
            { id: 'maxwidth', name: 'Max. width', summary: 'For example 50% or 300px' }
        ]
    </script>
</ui-component>

The official widgets use the types boolean, dropdown, number (with min and max) and string, the default. summary adds a help text.

<style> inside <settings> styles the form. CLASS works there too.

CMS adds the four options Hide for large display, Hide for medium display, Hide for small display and Hide for extra small display below your form. They add the classes hidden-lg, hidden-md, hidden-sm and hidden-xs to the wrapper of the instance, and they are not part of the configuration. Copy and Paste at the top of the window copy the values between instances of the same widget.

Apply the settings

The first <script> inside <settings> can export three functions. Each one receives the configuration and the wrapper of the instance as a jQuery element:

  • exports.load(config, el) runs when the user opens the settings. Change config to change the values in the form.
  • exports.save(config, el) runs when the user clicks APPLY. Change the HTML of the instance to match the new values.
  • exports.check(config, el) runs for each instance on the page when the editor opens it, and again when the list of widgets changes.

After save, CMS stores the configuration in the data-cms attribute of the instance. The details are in the Settings script reference.

Good practice

  • Change only what the setting controls. Use tclass, rclass2 and aclass on existing elements instead of replacing the HTML, so the text that the user wrote stays.
  • Make save safe to run many times with the same values.
  • Validate values that become class names or styles.
  • Settings that only the server needs, such as an address in Importer, need no save at all: render reads them from opt.config.
Made in Europe