Secutio Documentation

Table of Contents:

Example Usage

If using Node.js or another CommonJS environment

  const Secutio = require('./secutio');
  const app = new Secutio();
  app.init();

If in a browser environment

  <script
    type="text/javascript"
    src="https://cdn.jsdelivr.net/gh/mrhdias/secutio@master/dist/js/secutio.min.js">
  </script>
  <script>
    const app = new Secutio();
    app.init();
  </script>

Project Directory Structure

The following directory structure is just a suggestion and can be adjusted to the needs of each project.

├── app/
│   ├── public/
│   │   ├── css/
│   │   │   ├── styles.css
│   │   ├── js/
│   │   │   ├── script.js
│   │   ├── templates/
│   │   │   ├── tpl-example.html
│   │   ├── tasks.json
└── server.bin

Tasks

Tasks are a set of properties that can be applied to each HTML element by adding the “data-tasks” attribute. Properties are specified in a JSON file with a “.json” extension. The JSON file is loaded from the path specified in “src” of “script” HTML element with the “data-tasktable” attribute and the “type” attribute must be set to “application/json”. More than one task can be defined per element as long as they correspond to different events (click, mouseover, submit, etc).

Example of a tasks file.

{
  "get-contacts-list": {
    "action": "/listcontacts",
    "method": "get",
    "trigger": "click"
  },
  "switch-contact-status": {
    "action": "/statuscontact",
    "method": "put",
    "callback": "prepare-data",
    "trigger": "click"
  }
}

Properties

The following properties can be used in the “data-tasks” attribute:

The following properties can be used both client-side and server-side via custom HTTP header:

Custom Attributes

Custom attributes can be added to the elements where “tasks” are specified, allowing one to override the default value of one property with another. The property in the “tasks” file that defines the attribute must always begin with the substring ‘attribute-‘ followed by the attribute with property to replace. This transformation is available for the following properties: action, src-data, method, remove, target, swap, before, after, and trigger.

Example:

tasks.json

{
  "load-paradises": {
    "action": "/listparadises",
    "attribute-action": "data-action",
    "method": "get",
    "trigger": "click",
    "target": "#paradises",
    "swap": "inner"
  }
}

The attribute-action replaces the “action” property with the specified attribute, which should be present on the element with the “data-tasks” attribute. This property overrides the default and allows actions to be unique.

Example

<div id="paradises"></div>
<button data-tasks="load-paradises">List All</button>
<button data-tasks="load-paradises" data-action="/listparadises/earth">Go To Earth</button>

Special HTTP Header Secutio-Transformation

Custom HTTP header (Secutio-Transformation) containing information about the transformation to be applied and overrides the same default properties if indicated in the tasks file.

An example is provided below.

HTTP/1.1 200 OK
Secutio-Transformation: target:#contacts-list;template:#contacts-list-tpl;swap:innerHTML
Content-Type: application/json

Subtasks

These special tasks are executed before or after the task with the event that triggered the action and must have the “selector” property.

Example of a subtask:

tasks.json

{
  "active-paradise": {
    "selector": ".paradises > .earth",
    "add": {
      "class": "active",
      "style": "color: #0d6efd;"
    }
  }
}

Properties

Description of each of the properties allowed in the subtasks:

In the “class” and “style” properties, which are already attributes, the value is a string and in the attributes it is a list in “remove” and an “object” in “add”.

Templates

These templates can be used to generate HTML content on the client side and can be embedded into the HTML page or loaded. This has nothing to do with the templating engines used on the server side to render pages before sending them to the client.

There is a special variable that can be used in templates:

Embedded

Embedded templates should have a unique id property, and their content must be enclosed within the HTML tag “script” with type=”text/template”.

<script id="contacts-list-tpl" type="text/template">
  <!-- HTML Content -->
</script>

Loaded

These templates function just like normal HTML files and reside within the public directory. To load a template, tasks utilize the “template” property. The value assigned to this property should be the path to the HTML file containing the template.

templates/example.html

<!-- HTML Content -->

Template Literals

Template literals, formerly known as template strings. Template literals are enclosed with backtick ( `) characters, enabling multi-line strings, string interpolation with embedded expressions, and special constructs known as tagged templates.

let name = 'John Doe';
console.log(`Hello ${name}!`);
// returns Hello John Doe!

Template literals provide a concise and expressive way to create reusable components with HTML templates, simplifying variable insertion, supporting multiline strings, enabling expression evaluation, and enhancing overall code readability.

Todo List