### Install Total.js Framework 5 Source: https://github.com/totaljs/framework5/blob/main/README.md This snippet shows how to install the Total.js framework version 5 using npm. It's a prerequisite for using the framework in your Node.js projects. ```bash $ npm install total5 ``` -------------------------------- ### RESTBuilder Example: xhr().callback() Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md An example demonstrating how to make a GET request using RESTBuilder, chain the xhr() method, and handle the response using a callback function. ```javascript RESTBuilder.GET('https://www.totaljs.com').xhr().callback(function(err, response) { console.log(err, response); }); ``` -------------------------------- ### Basic NEWACTION Example with Query and Params Source: https://github.com/totaljs/framework5/blob/main/docs/actions.md An example demonstrating the usage of NEWACTION with query and parameter schemas. It shows how to define expected query and parameter fields and includes a basic action function that utilizes the provided context. ```javascript NEWACTION('Find', { query: 'page:Number, sort:String', params: 'projectid:String', route: '+API ?', action: function($) { // $ {Options} Documentation: https://docs.totaljs.com/total5/IbGpBV25x60f/ // $.query {Object key:value} // $.user {Object key:value} // $.params {Object key:value} // $.model {Object key:value} or model "is prepared according to the input data schema" // $.headers {Object key:value} // $.ip {String} // $.files {Array} // $.invalid(error_or_http_status) // $.callback({ success: true }); // $.success(data); // $.redirect(url); $.success(); } }); ``` -------------------------------- ### HTML and CSS for Application Start Display Source: https://github.com/totaljs/framework5/blob/main/pause.html Provides the basic HTML structure and CSS styling for displaying a 'Please wait' message and a countdown timer during application startup. It includes styles for the body, table layout, animation, and the timer element itself. ```html html,body { font: normal normal 14px Arial; background-color: white; height: 100%; margin: 0; padding: 0; font-smoothing: antialiased; } .table { display: table; width: 100%; height: 100%} .cell { display:table-cell;vertical-align:middle;text-align:center} table { max-width: 300px; margin: 20px auto; width: 100%; border:1px solid #E0E0E0; border-collapse: collapse; font-size: 12px; } table td { border: 1px solid #E0E0E0; padding: 6px 8px; text-align: center; } table td:first-child { text-align: left; width: 85%; } .anim { animation: anim 2s infinite; } .text { color: #A0A0A0; font-size: 10px; margin-bottom: 3px; } #time { font-size: 60px; font-weight: bold; margin-bottom: 10px; background-color: #000; color: #FFF; padding: 8px 15px; min-width: 70px; text-align: center; border-radius: 10px; position: relative; display: inline-block; border-bottom: 5px solid #D0D0D0; } @keyframes anim { 0% { transform:scale(1); } 50%{ transform:scale(1.5) rotate(180deg); color:gray } 100%{ transform:scale(1); } } ``` -------------------------------- ### Total.js Plugin Action Example (Todo List) Source: https://github.com/totaljs/framework5/blob/main/docs/plugin.md An example of defining a server-side action within a Total.js plugin to list tasks. It utilizes the `NEWACTION` helper to define the action's name, route, and the asynchronous function to fetch data using `DATA.list().autoquery()`. ```javascript exports.name = '@(Todo)'; exports.icon = 'ti ti-check'; exports.position = 1; exports.visible = user => user.permissions.includes('todo'); exports.permissions = [{ id: 'todo', name: 'ToDo' }]; // Action example NEWACTION('Todo|list', { name: 'List of tasks', route: '+API ?', action: async function($, model) { // $ {Options} Documentation: https://docs.totaljs.com/total5/IbGpBV25x60f/ // $.query {Object key:value} // $.user {Object key:value} // $.params {Object key:value} // $.model {Object key:value} or model "is prepared according to the input data schema" // $.headers {Object key:value} // $.ip {String} // $.files {Array} // $.invalid(error_or_http_status) // $.callback({ success: true }); // $.success(data); // $.redirect(url); // It creates a list with pagination and filtering and sorting of the fields defined in the .autoquery() method. let response = await DATA.list('tbl_todo').autoquery($.query, 'id:String, name:String, body:String, iscompleted:Boolean, createdby:String, updatedby:String, completedby:String, dtcompleted:Date, dtcreated:Date, dtupdated:Date', 'dtcreated_desc', 100).promise($); $.callback(response); } }); ``` -------------------------------- ### SQL Table Creation with Total.js Conventions Source: https://github.com/totaljs/framework5/blob/main/docs/databases.md Example of a PostgreSQL table creation script adhering to Total.js naming conventions and field ordering. It includes identifiers, main fields, numbers, booleans, and dates, with specific data types and default values. ```SQL CREATE TABLE "public"."tbl_channel_message" ( -- IDENTIFIERS FIRST "id" text NOT NULL, "userid" text, "channelid" text, "openplatformid" text, -- MAIN FIELDS "body" text, -- NUMBERS "countupdate" INT2 DEFAULT 0, "countviews" INT2 DEFAULT 0, -- BOOLEANS "isrobot" bool DEFAULT FALSE, "ismobile" bool DEFAULT FALSE, "ispinned" bool DEFAULT FALSE, "isremoved" bool DEFAULT FALSE, -- AND LAST DATES "dtupdated" timestamp, "dtremoved" timestamp, "dtcreated" timestamp DEFAULT timezone('utc'::text, now()), -- DO NOT FORGET FOR FOREIGN KEYS CONSTRAINT ... CONSTRAINT ... PRIMARY KEY ("id") ); ``` -------------------------------- ### Python Script Loading and Execution Source: https://github.com/totaljs/framework5/blob/main/changelog.txt This snippet demonstrates the support for Python scripts within the Total.js framework. It includes the addition of the PYPELINE method, which allows for starting a Python process with specified options. ```javascript added support for Python scripts in the `pypelines` folder added `PYPELINE(name, [options])` method starts a python process ``` -------------------------------- ### Configure Routing with ROUTE Directive (JavaScript) Source: https://github.com/totaljs/framework5/blob/main/docs/actions.md This example demonstrates how to configure routes for different HTTP methods and paths using the `ROUTE` directive in Total.js. It shows how to map paths to specific actions and handle different HTTP verbs like `+API`, `+GET`, and `+POST`. ```javascript ROUTE('+API /api/ --> action1'); ROUTE('+GET /api/products/ --> action1 action2 action3'); ROUTE('+POST /api/products/add/ --> action1 action2 (response) action3'); ``` -------------------------------- ### Example: Find users by age range Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Searches for users within a specific age range and who are not removed, using callbacks for results. ```javascript // Search for users between 20 and 30 years old who have not been removed. "DATA.find()" returns always Array of objects (rows). DATA.find('tbl_user').where('isremoved', false).between('age', 20, 30).callback(function(err, response) { // response {Array of objects} console.log(err, response); }); ``` -------------------------------- ### Unify debug and release mode Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Unifies debug and release modes by using monitoring files. Monitoring can be disabled via `options.watcher = false` in the start script. ```javascript options.watcher = false ``` -------------------------------- ### Total.js Plugin Server-Side Definition (index.js) Source: https://github.com/totaljs/framework5/blob/main/docs/plugin.md Defines the core server-side logic for a Total.js plugin. Includes properties for name, icon, position, visibility, permissions, and installation hooks. The `visible` function controls client-side access based on user roles or permissions. ```javascript exports.name = '@(Todo)'; // A plugin name. The markup "@(your_text)" is automatically localized. It's the Total.js localization markup. exports.icon = 'ti ti-check'; // A plugin icon. exports.position = 1; // A position index in the navigation. exports.hidden = false; // Although this property "exports.hidden" can hide the plugin in the menu, but it will still be evaluated on the client side. exports.import = 'extensions.html'; // Optional. This property can contain a file name from the "plugin/todo/public/" folder. The client-side library imports that file while the web app loads. It's intended for very specific cases, and most plugins don't use it. exports.visible = function(user) { // In this handler, we can check the permissions for accessing this plugin on the client side. It's superior to the "exports.hidden" property. // IMPORTANT: The "user.sa {Boolean}" or "user.su {Boolean}" sees everything. return user.sa || user.permissions.includes('myitems_view'); }; exports.permissions = [{ id: 'myitems_view', name: 'View My Items' }, { id: 'myitems_edit', name: 'Edit My Items' }]; // A custom permissions, It's only targeted for the Total.js OpenPlatform (SSO portal). exports.install = function() { // Some specific routes can be defined here, such as file handlers and uploading. }; ``` -------------------------------- ### Total.js Template Loop Source: https://github.com/totaljs/framework5/blob/main/pause.html A server-side template snippet using Total.js syntax to iterate over a 'model'. If the model contains items, each item 'm' is rendered. This is a common pattern for dynamic content generation in Total.js applications. ```totaljs @{foreach m in model} @{end} @{m} ``` -------------------------------- ### Example: Find user by name Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Searches for a single user by name, assuming 'DATA.read()' returns a single object. ```javascript // Search for a user named Peter who has not been removed. "DATA.read()" returns always single object (row). ``` -------------------------------- ### JavaScript Countdown Timer Source: https://github.com/totaljs/framework5/blob/main/pause.html Implements a client-side countdown timer that decrements from 10 to 0. When the counter reaches zero, it reloads the current page. This script is intended to be used during application startup. ```javascript var counter = 10; setInterval(function() { if (counter < 0) return; if (counter === 0) window.location.reload(); else time.innerHTML = counter + ''; counter--; }, 1000); ``` -------------------------------- ### Total.js: RESTBuilder GET Request Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Creates an HTTP GET request to an external endpoint using RESTBuilder. Requires an absolute URL. ```javascript RESTBuilder.GET(url); ``` -------------------------------- ### JavaScript GUID Generation Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Creates a unique identifier (GUID) which is at least 12 characters long. It includes a timestamp, minute counter, and a hash with a checksum. An optional length parameter can specify the maximum length. ```javascript let guid1 = GUID(); let guid2 = GUID(10); ``` -------------------------------- ### NEWMIDDLEWARE Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Notes the addition of the missing NEWMIDDLEWARE() method. ```javascript added missing method `NEWMIDDLEWARE()` ``` -------------------------------- ### Extended Ctrl File System Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Extends the `ctrl.filefs()` method to return the `opt` object. This enhancement likely provides more detailed information or access to options used during file system operations within the controller. ```javascript const options = ctrl.filefs(); // Use options object ``` -------------------------------- ### Ctrl Image Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces the `ctrl.image(opt)` method for handling image-related operations within a controller. The `opt` parameter likely allows for configuration of image processing, such as resizing, cropping, or format conversion. ```javascript ctrl.image({ width: 100, height: 100, format: 'png' }); ``` -------------------------------- ### Add $.ctrl(ctrl_instance) method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces the `$.ctrl(ctrl_instance)` method, allowing for the association of a controller instance with the current context. ```javascript $.ctrl(ctrl_instance) ``` -------------------------------- ### Extend ViewEngine with view.renderlayout() Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Extends the `ViewEngine` by adding the `view.renderlayout(name, content)` method for rendering layouts with provided content. ```javascript view.renderlayout(name, content) ``` -------------------------------- ### View Engine @{meta} Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Notes the addition of the missing ViewEngine method `@{meta(...)}`. ```javascript added missing ViewEngine `@{meta(...)}` method ``` -------------------------------- ### Total.js Framework File Loading Fixes Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Addresses issues with loading plugin files, ensuring only `.js` files are loaded and excluding `.DS_STORE` files. ```javascript - fixed loading plugin files (only `.js` files will load) - removed loading of `.DS_STORE` files in plugins ``` -------------------------------- ### FILESTORAGE Clone Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces the FILESTORAGE().clone(id, newid, [callback]) method for creating a copy of a file storage entry. ```javascript added `FILESTORAGE().clone(id, newid, [callback])` method ``` -------------------------------- ### Total.js Framework COMPONENTANTOR Download Procedure Update Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Updates the download procedure in the COMPONENTANTOR() method for improved reliability. ```javascript - updated download procedure in the `COMPONENTANTOR()` method ``` -------------------------------- ### Add CMS.run() method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces a new method `CMS.run(widget_html)` for executing CMS widgets, likely for content management functionalities. ```javascript CMS.run(widget_html) ``` -------------------------------- ### RESTBuilderInstance: auth(user_or_token, [password]) Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Creates an 'Authorization' header for HTTP authentication or token-based authentication. Accepts a username/token and an optional password. Returns the RESTBuilderInstance object for chaining. ```javascript builder.auth('petersirka', '123456'); builder.auth('Bearer YOUR_AUTH_TOKEN'); ``` -------------------------------- ### NEWACTION Syntax and Options Source: https://github.com/totaljs/framework5/blob/main/docs/actions.md Defines the syntax for the NEWACTION method, including the required ID and the optional options object. The options object allows for detailed configuration of the action's behavior, including data schemas, routing, and permissions. ```javascript NEWACTION(id, options) ``` -------------------------------- ### Total.js Framework HEAD Method Support Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds support for the HTTP `HEAD` method, allowing clients to retrieve headers without the response body. ```javascript - added support for `HEAD` method ``` -------------------------------- ### F.backup Ignore List Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Improves the F.backup() method by adding an internal ignore list for common backup exclusion files like .DS_Store and .gitignore. ```javascript improved `F.backup()` by adding internal ignore list for `.DS_Store` and `.gitignore` files ``` -------------------------------- ### View Engine @{import} Method Enhancement Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Details the enhancement of the `@{import()}` method to support importing CSS and JS files with automatic cache-busting timestamps appended to the URL. ```javascript extended `@{import()}` method now supports `@style_name.css` and `@script_name.js` (due to HTTP cache reasons, it automatically appends the argument `?ts=start_app_timestamp` to the URL) ``` -------------------------------- ### Schema/Auth Options Hostname Replacement Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Replaces the `$.hostname` property with a method in the Schema/Auth Options for consistency and potential future enhancements. ```javascript replaced `$.hostname` property with a method in the Schema/Auth Options ``` -------------------------------- ### List Todo Tasks Source: https://github.com/totaljs/framework5/blob/main/docs/actions.md Retrieves a list of todo tasks from the 'tbl_todo' database table. It supports pagination, filtering, and sorting based on the provided query parameters. The response includes task details like ID, name, body, completion status, and timestamps. ```javascript NEWACTION('Todo|list', { name: 'List of tasks', route: '+API ?', action: async function($, model) { let response = await DATA.list('tbl_todo').autoquery($.query, 'id:String, name:String, body:String, iscompleted:Boolean, createdby:String, updatedby:String, completedby:String, dtcompleted:Date, dtcreated:Date, dtupdated:Date', 'dtcreated_desc', 100).promise($); $.callback(response); } }); ``` -------------------------------- ### Add PROMISIFY() and $.promisify() methods Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces `PROMISIFY(fn, [a], [b])` and `$.promisify(fn, [a], [b])` methods for converting callback-based functions into Promise-based functions. ```javascript PROMISIFY(fn, [a], [b]) ``` ```javascript $.promisify(fn, [a], [b]) ``` -------------------------------- ### Add U.paginate() method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces a new utility method `U.paginate(page, pages, [max])` for handling pagination logic. ```javascript U.paginate(page, pages, [max]) ``` -------------------------------- ### Add FILESTORAGE().copy() method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds the `FILESTORAGE().copy(id, path, [callback(err, meta)])` method for copying files within the file storage system. ```javascript FILESTORAGE().copy(id, path, [callback(err, meta)]) ``` -------------------------------- ### QueryBuilder Pipe Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces the QueryBuilder.pipe() method as an alias for QueryBuilder.callback(), providing an alternative way to handle callback operations. ```javascript added `QueryBuilder.pipe()` method as alias to `QueryBuilder.callback()` ``` -------------------------------- ### Add support for plugins in TEMPLATE() Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds support for plugins within the `TEMPLATE()` method, enabling extensibility and customization of template rendering processes. ```javascript TEMPLATE() ``` -------------------------------- ### Action Pipe Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces the Action.pipe() method as an alias for Action.callback(), simplifying callback management within actions. ```javascript added `Action.pipe()` method as alias to `Action.callback()` ``` -------------------------------- ### Total.js Framework CONF.$root Functionality Fix Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Fixes the functionality of CONF.$root, ensuring correct configuration loading. ```javascript - fixed `CONF.$root` functionality ``` -------------------------------- ### Total.js Framework Auto Script Loader Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds an auto script loader for the `extensions` and `transforms` directories, simplifying the management of scripts. ```javascript - added auto script loader for `extensions` directory - added auto script loader for `transforms` directory ``` -------------------------------- ### Total.js Framework Specific File Extension Routing Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces support for routing specific file extensions using the `ROUTE('FILE *.php', ...)` syntax. ```javascript - added support for handling specific file extensions in the form `ROUTE('FILE *.php', ...)` ``` -------------------------------- ### QueryBuilder Search with ILIKE Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Adds a standard SQL comparison using ILIKE for case-insensitive searching. Supports pattern matching. ```javascript builder.search(column, value, [operator]); // The method adds a standard SQL comparison, such as "COLUMN ILIKE VALUE". // column {String} a column name. // value {String} a value for comparison. // operator {String} optional, possible values: "*" throughout the text, "beg" at the beginning, and "end" at the end. // returns {QueryBuilder} object; ``` -------------------------------- ### Custom View Engine Helpers Source: https://github.com/totaljs/framework5/blob/main/changelog.txt This section highlights the ability to define custom helper functions for the Total.js View engine. These helpers can be registered using DEF.helpers. ```javascript added support for custom View engine helpers `DEF.helpers.myhelper` ``` -------------------------------- ### Add Utils.filestreamer() Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds the `Utils.filestreamer(filename, onbuffer(buffer, next), onend, [buffer_size])` utility for efficient file streaming. ```javascript Utils.filestreamer(filename, onbuffer(buffer, next), onend, [buffer_size]) ``` -------------------------------- ### Improve user-agent parser Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Enhances the user-agent parser to include support for new headers like `Sec-CH-UA`, improving the accuracy of device and browser identification. ```javascript Sec-CH-UA ``` -------------------------------- ### Extend edit mode with restart command Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Enhances the 'edit' mode by adding a 'restart' command, allowing for easier server restarts during development. ```javascript restart ``` -------------------------------- ### Add support for multiple events Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Enables support for listening to multiple events simultaneously using the `ON('event1 + event2 + event3', function() {})` syntax. ```javascript ON('event1 + event2 + event3', function() {}) ``` -------------------------------- ### Add REQUEST opt.writer option Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Enhances the REQUEST function by introducing the `opt.writer` option, which accepts a function to handle the response. This provides a way to customize response processing. ```javascript REQUEST() opt.writer {function(res)} ``` -------------------------------- ### Define Action with Input/Output Schema (JavaScript) Source: https://github.com/totaljs/framework5/blob/main/docs/actions.md This snippet shows how to define a new action using `NEWACTION` in Total.js. It specifies the input schema (`name:String, age:Number`), output schema (`success:Boolean`), parameters, and the route. The action function receives the model, processes it, and sends a success response. ```javascript NEWACTION('Save', { input: '*name:String, age:Number', output: 'success:Boolean', params: 'projectid:String, id:String', route: '+API ?', action: function($, model) { // $.query {Object key:value} // $.user {Object key:value} // $.params {Object key:value} // $.model {Object key:value} or model "is prepared according to the input data schema" // $.headers {Object key:value} // $.ip {String} // $.files {Array} // $.invalid(error_or_http_status) // $.callback({ success: true }); // $.success(data); // $.redirect(url); model.id = UID(); $.success(model.id); } }); ``` -------------------------------- ### Add HTTP auto-redirect for WebSocketClient Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Implements HTTP auto-redirect support for `WebSocketClient`, improving connection stability and handling. ```javascript WebSocketClient ``` -------------------------------- ### Total.js Framework Watcher Addition Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds a watcher for `extensions/*.html` files to monitor changes in HTML extension files. ```javascript - added watcher for `extensions/*.html` files ``` -------------------------------- ### Extend HTMLParser with selector for prefixes Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Extends the `HTMLParser` to support selectors for prefixes, such as `node.find('xsd:')`, enabling more specific element selection. ```javascript node.find('xsd:') ``` -------------------------------- ### Add action.status() and $.status() Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Introduces `action.status(fn)` for handling status within actions and `$.status(value)` for calling status updates. ```javascript action.status(fn) ``` ```javascript $.status(value) ``` -------------------------------- ### RESTBuilderInstance: stream(callback) Source: https://github.com/totaljs/framework5/blob/main/docs/globals.md Creates a request and returns the wrapped response stream to a callback function. The callback receives an error and a response object containing the stream, host, headers, and status. Returns the RESTBuilderInstance object for chaining. ```javascript builder.stream(function(err, response) { // response.stream {Response stream} // response.host {String} Resolved host // response.headers {Object} Obtained headers // response.status {Number} HTTP status code }); ``` -------------------------------- ### WebSocketController Hostname Method Source: https://github.com/totaljs/framework5/blob/main/changelog.txt Adds the missing `$.hostname()` method to the WebSocketController for retrieving the host's network name. ```javascript added missing `$.hostname()` method in the `WebSocketController` ``` -------------------------------- ### HTML and CSS for Total.js Framework 5 Layout Source: https://github.com/totaljs/framework5/blob/main/error.html This snippet defines the fundamental HTML structure and CSS styling for a Total.js Framework 5 application. It includes styles for the overall page layout, table-based elements, responsive design adjustments, and a keyframe animation for visual effects. The CSS is designed to ensure a consistent and appealing user interface across different devices. ```HTML