Spanish Total.js
English
Documentation For AI agents
Contents

Status and the look of the node

Show live information on the node with a status, design its body and add a script that runs in the designer.

.md GitHub
On this page

A node can show live information on the canvas: a counter, the last value, the state of a connection. The server code sends a status, and the <body> of the component shows it.

This page builds Tally, which counts the messages it receives and turns the number red from a limit in its settings.

A Tally node that has counted 12 messages, with a Reset button

Send a status

instance.status(data) sends any data to the designers that have the FlowStream open:

var status = { count: 0 };

instance.message = function($) {
    status.count++;
    instance.status(status, 500);
    $.destroy();
};

instance.status(status);

With a second argument, Flow waits that many milliseconds and then sends the last status. A node that receives many messages sends one status instead of one for each message: twelve quick messages to Tally arrive in the designer as one status with count 12.

Flow keeps the last status of each node and sends it again when someone opens the designer.

Show the status in the body

$STATUS in <body> is the last status of the node. Bind it with ui-bind:

<body>
    <header>
        <i class="$ICON"></i>$NAME
    </header>
    <footer>
        <ui-bind path="$STATUS" config="text:value && value.count ? value.count : '0'" class="count block"></ui-bind>
        <button name="trigger">Reset</button>
    </footer>
</body>

The <header> with the icon and the name is the title bar of the node. A <button name="trigger"> calls instance.trigger on the server, see Triggers, timers and closing.

Style the body in <style> with .CLASS, which applies only to the nodes of this component:

<style>
    .CLASS footer { padding: 10px; text-align: center; }
    .CLASS .count { font-size: 30px; font-weight: bold; }
    .CLASS .over { color: #E73323; }
    .CLASS button { width: 100%; margin-top: 5px; }
</style>

A script in the designer

A <script> without total runs in the browser, in the designer. TOUCH gives you an object for each node of the component:

<script>
    TOUCH(function(exports, reinit) {
        exports.status = function(status, isinit) {
            exports.element.find('.count').tclass('over', !!status && status.count >= exports.config.limit);
        };
    });
</script>
NameWhat it is
exports.idThe id of the node.
exports.configThe configuration of the node.
exports.elementThe element of the node, as a jQuery object.
exports.status = function(status, isinit)Called when a new status arrives.
exports.configure = function(config, isinit)Called when the configuration changes.
exports.click = function(el)Called when the user clicks a button or an element with the class clickable in the node.
exports.trigger(data)Calls instance.trigger on the server with data.
exports.call(data, callback)Calls instance.call on the server and gets its answer.

reinit is true when the component has changed and the designer runs the script again.

Show progress

For long work, instance.activity(key, name, progress) adds a line to Activities in the designer toolbar:

instance.activity('import', 'Importing orders', 50);   // 50 %
instance.activity('import', 'Importing orders', 100);  // done, the line disappears

The whole component

<script total>

    exports.id = 'tally';
    exports.name = 'Tally';
    exports.icon = 'ti ti-calculator';
    exports.author = 'Jane Doe';
    exports.version = '1';
    exports.group = 'Demo';
    exports.config = { limit: 10 };
    exports.inputs = [{ id: 'input', name: 'Input' }];

    exports.make = function(instance, config) {

        var status = { count: 0 };

        instance.message = function($) {
            status.count++;
            instance.status(status, 500);
            $.destroy();
        };

        instance.trigger = function() {
            status.count = 0;
            instance.status(status);
        };

        instance.status(status);
    };

</script>

<style>
    .CLASS footer { padding: 10px; text-align: center; }
    .CLASS .count { font-size: 30px; font-weight: bold; }
    .CLASS .over { color: #E73323; }
    .CLASS button { width: 100%; margin-top: 5px; }
</style>

<readme>
Counts the incoming messages. Select **Reset** to start again from zero.
</readme>

<settings>
    <div class="padding">
        <ui-component name="input" path="?.limit" config="type:number;min:1" class="m">Highlight from</ui-component>
    </div>
</settings>

<body>
    <header>
        <i class="$ICON"></i>$NAME
    </header>
    <footer>
        <ui-bind path="$STATUS" config="text:value && value.count ? value.count : '0'" class="count block"></ui-bind>
        <button name="trigger">Reset</button>
    </footer>
</body>

<script>
    TOUCH(function(exports, reinit) {
        exports.status = function(status, isinit) {
            exports.element.find('.count').tclass('over', !!status && status.count >= exports.config.limit);
        };
    });
</script>
Made in Europe