---
title: Global functions
project: jComponent V19
version: 19.194
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/reference/globals/
updated: 2026-11-18T13:58:24.464Z
---

# Global functions

> The functions and objects that jComponent adds to the window object.

jComponent adds these functions and objects to `window`, so they can be used anywhere without an import. To learn how paths work first, read [Paths](../../guides/paths/).

## API

### ACMD(name, [...args])

Type: `method`

Creates a function that calls [CMD()](#cmd) later.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the command. |
| `...args` | `Object` | no |  | The first arguments. |

Returns: `Function` The function to call.

The argument of the created function is added as the last argument of the command.

Example: Use it as a callback
```js
ON('cmd', function(name, value) {
	console.log(name, value); // refresh 1
});
ACMD('refresh')(1);
```

### ADAPT(path, id, text)

Type: `method`

Prepares imported text for a path and an identifier.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The value for `~PATH~` and `CLASS`. |
| `id` | `String` | yes |  | The value for `~ID~`. |
| `text` | `String` | yes |  | The text. |

Returns: `String` The changed text.

The text is translated, `--name--` variables are replaced, `~CDN~` becomes `DEF.cdn`, `~PATH~` and `CLASS` become the path, and `~ID~` becomes the identifier.

Example: Replace the markers
```js
console.log(ADAPT('users', 'form1', '~PATH~ and ~ID~')); // users and form1
```

### ADD(value, [element], [config], [content])

Type: `method`

Adds a component to the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Object|Array` | yes |  | The component. |
| `element` | `Element|jQuery|Component` | no | `document.body` | The parent element. |
| `config` | `Object|String` | no |  | The configuration. |
| `content` | `String` | no |  | The inner HTML. |

A string has the form `name__path__config`. A leading `!` skips the component when it is already declared. An object can have the fields `name`, `path`, `config`, `element` and `prepend`. An array adds several components.

Example: Add a component
```js
ADD('box__data.title__size:10');
setTimeout(function() {
	console.log(FIND('box').config.size); // 10
}, 1200);
```

### AEXEC(path, [...args])

Type: `method`

Creates a function that calls [EXEC()](#exec) later.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The target. |
| `...args` | `Object` | no |  | The first arguments. |

Returns: `Function` The function to call.

The argument of the created function is added as the last argument.

Example: Use it as a callback
```js
window.save = value => console.log(value); // 5
AEXEC('save')(5);
```

### AJAX(url, [data], [callback], [delay])

Type: `method`

Sends an HTTP request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The method and the URL. |
| `data` | `Object|String` | no |  | The data. A `GET` request sends it in the query string. |
| `callback` | `Function|String` | no |  | `function(response, err, output)`, or a path. |
| `delay` | `Number` | no |  | Waits this many milliseconds before sending. |

Returns: `Promise` Only when no callback is given.

The URL has the form `METHOD /path/`. More items can follow it, separated by a space:

| Item | Meaning |
| --- | --- |
| `!METHOD` | Sends cookies with the request |
| `{ "Name": "value" }` | Request headers |
| `<5 minutes>` | Caches the response, as in [AJAXCACHE()](#ajaxcache) |
| `(name)` | Applies the settings from [AJAXCONFIG()](#ajaxconfig) |
| `repeat` | Repeats the request when the server is not available |
| `cancel` | Cancels the previous unfinished request to the same URL |
| `urlencoded` | Sends the data as a URL-encoded form |
| `json` | Accepts only a JSON response |
| `error` | Passes the response through [ERROR()](#error) |
| `noencrypt`, `nodecrypt`, `nocsrf` | Skips encryption, decryption or the CSRF header |
| `#id` | Names the request for `cancel`, or waits for a code list |
| `@flag` | Emits a custom flag |

The callback receives the response, the status code on an error, and an object with the details of the request. A string callback is a path where the response is stored. Without a callback, the function returns a Promise, which is rejected on a status above 399.

Example: Send data
```js
AJAX('POST /api/users/', { name: 'Peter' }, function(response, err) {
	console.log(response.method, response.url); // POST /api/users/
	console.log(response.body); // {"name":"Peter"}
});
```

Example: Use a Promise
```js
(async function() {
	var response = await AJAX('GET /api/users/');
	console.log(response.url); // /api/users/
})();
```

Example: Handle an error
```js
AJAX('GET /api/missing/', function(response, err) {
	console.log(err, response); // 404 Not found
});
```

### AJAXCACHE(url, [data], callback, expire, [delay], [clear])

Type: `method`

Sends a request and caches the response.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The method and the URL. |
| `data` | `Object` | no |  | The data. |
| `callback` | `Function|String` | yes |  | `function(response, cached)`, or a path. |
| `expire` | `String` | yes |  | For example `5 minutes` or `session`. |
| `delay` | `Number` | no |  | Waits this many milliseconds before reading. |
| `clear` | `Boolean` | no |  | With `true`, ignores the stored response. |

The callback receives the response and `true` when it comes from the cache. The value `session` for `expire` keeps the response in memory until the page is reloaded.

Example: Cache a response
```js
AJAXCACHE('GET /api/users/', function(response, cached) {
	console.log('first', cached); // first false
	AJAXCACHE('GET /api/users/', function(response, cached) {
		console.log('second', cached); // second true
	}, '5 minutes');
}, '5 minutes');
```

### AJAXCACHEREVIEW(url, [data], callback, expire, [delay], [clear])

Type: `method`

Returns the cached response and then checks the server for a newer one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The method and the URL. |
| `data` | `Object` | no |  | The data. |
| `callback` | `Function|String` | yes |  | `function(response, cached, updated)`, or a path. |
| `expire` | `String` | yes |  | For example `5 minutes`. |
| `delay` | `Number` | no |  | Waits this many milliseconds before reading. |
| `clear` | `Boolean` | no |  | With `true`, ignores the stored response. |

When the server returns different data, the callback runs again with `false` and `true` as its second and third arguments.

Example: Refresh a cached response
```js
AJAXCACHE('GET /api/time/', function() {
	AJAXCACHEREVIEW('GET /api/time/', function(response, cached, updated) {
		console.log(response.value, cached, updated === true); // 2 false true
	}, '5 minutes');
}, '5 minutes');
```

### AJAXCONFIG(name, fn)

Type: `method`

Declares settings that a request can apply by name.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the settings. |
| `fn` | `Function` | yes |  | `function(options)`. |

A request applies them with `(name)` in its URL. The function receives the options of the request.

Example: Add a header
```js
AJAXCONFIG('secure', function(options) {
	options.headers['X-Token'] = 'abc';
});
ON('request', function(opt) {
	console.log(opt.headers['X-Token']); // abc
	opt.respond({});
});
AJAX('GET /api/users/ (secure)', NOOP);
```

### API(url, [data], [callback])

Type: `method`

Calls a Total.js API endpoint.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The endpoint and the schema. |
| `data` | `Object` | no |  | The data. |
| `callback` | `Function|String` | no |  | `function(response, err, output)`, or a path. |

Returns: `Object|Promise` The object described above, or a Promise.

The URL has the form `/api/ schema`. The request is a `POST` with the name of the schema and the data in its body. Without a callback, the function returns a Promise. With a callback, it returns an object with `params(value)`, which fills the parameters of the schema, and `data(value)`, which replaces the data.

Example: Call an endpoint
```js
API('/api/ users_save', { name: 'Peter' }, function(response) {
	console.log(response.url); // /api/
	console.log(response.body); // {"schema":"users_save","data":{"name":"Peter"}}
});
```

### APPEARANCE(options)

Type: `method`

Sets the look of the application.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The CSS variables and the fields `dark` and `large`. |

Every field becomes a CSS variable on `:root`. `dark` and `large` switch the classes `ui-dark` and `ui-large` on the body. `color` is a six-digit hexadecimal color, which also sets `DEF.color` and the variable `--rgb`.

Example: Use a dark theme
```js
APPEARANCE({ dark: true, color: '#336699' });
console.log($('#cssappearance').html()); // :root{--color:#336699;--rgb:51,102,153}
console.log($('body').hclass('ui-dark')); // true
```

### ASETTER(selector, method, [...args])

Type: `method`

Creates a function that calls [SETTER()](#setter) later.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | A selector as in [FIND()](#find). |
| `method` | `String` | yes |  | The name of the method. |
| `...args` | `Object` | no |  | The first arguments of the method. |

Returns: `Function` The function to call.

The argument of the created function is added as the last argument of the method, so it fits a request callback.

Example: Use it as a callback
```js
ON('ready', function() {
	var fn = ASETTER('box', 'hello');
	fn('Peter');
	console.log(typeof(fn)); // function
});
```

### ATTRD(element, [name])

Type: `method`

Reads a `data-` attribute from an element or its parents.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element|jQuery|Event|Object` | yes |  | The source. |
| `name` | `String` | no | `id` | The name of the attribute. |

Returns: `String` The value.

A plain object returns its field of that name.

Example: Read an identifier
```html
<div id="row" data-id="15" data-type="user"></div>
<script>
console.log(ATTRD($('#row'))); // 15
console.log(ATTRD($('#row'), 'type')); // user
</script>
```

### BIND(path)

Type: `method`

Refreshes the data bindings of a path without notifying components or watchers.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String|Array` | yes |  | One path or a list of paths. |

Example: Refresh a binding
```html
<span id="name" data-bind="user.name__text"></span>
<script>
SET('user', { name: 'Peter' });
setTimeout(function() {
	GET('user').name = 'Anna';
	BIND('user.name');
	setTimeout(function() {
		console.log($('#name').text()); // Anna
	}, 300);
}, 300);
</script>
```

### BLOCKED(name, timeout, [callback])

Type: `method`

Limits how often an action can run.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the action. |
| `timeout` | `Number|String` | yes |  | Milliseconds, or a value such as `5 seconds`. |
| `callback` | `Function` | no |  | Runs when the action is not blocked. |

Returns: `Boolean` `true` when the action is blocked.

The first call returns `false` and blocks the name for the given time. A block longer than 10 seconds is kept in the browser storage.

Example: Allow one call per second
```js
console.log(BLOCKED('save', 1000)); // false
console.log(BLOCKED('save', 1000)); // true
```

### CACHE(key, [value], [expire])

Type: `method`

Reads or writes a value in the browser cache.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The key. |
| `value` | `Object` | no |  | The value to store. |
| `expire` | `String|Number` | no |  | For example `1 day`, `session`, or milliseconds. |

Returns: `Object` The stored value.

A value is stored only with an expiration. The value `session` keeps it in memory until the page is reloaded.

Example: Store a value
```js
CACHE('profile', { name: 'Peter' }, '1 day');
console.log(CACHE('profile')); // {"name":"Peter"}
```

### CACHEPATH(path, expire, [rebind])

Type: `method`

Keeps the value of a path in the browser cache.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path. |
| `expire` | `String` | yes |  | For example `1 month`. |
| `rebind` | `Boolean` | no | `true` | With `false`, the stored value is not restored. |

Every change of the path is stored, and the stored value is restored on the next visit. With the form `path__value`, the expression is the value used when nothing is stored.

Example: Remember a value
```js
CACHEPATH('settings.theme', '1 month');
SET('settings.theme', 'dark');
console.log(CACHE('PATHS')); // {"settings.theme":"dark"}
```

### CAN(path, [flags])

Type: `method`

Tells whether a form can be submitted.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it. |
| `flags` | `String|Array` | no |  | `visible` or `hidden` limits the check to those components. |

Returns: `Boolean` The result.

The result is `true` when at least one component on the path is touched and none is invalid. A path with no components also returns `true`.

Example: Enable a submit button
```js
ON('ready', function() {
	console.log(CAN('form.*')); // false
	SET('form', { name: 'Peter', age: 30 });
	CHANGE('form.name');
	console.log(CAN('form.*')); // true
});
```

### CHANGE(path, [value])

Type: `method`

Marks the components on a path as touched and modified.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Boolean` | no | `true` | With `false`, the marks are removed. |

Example: Mark a field as changed
```js
ON('ready', function() {
	CHANGE('form.age');
	console.log(MODIFIED('form.*')); // ["form.age"]
});
```

### CL(name, callback)

Type: `method`

Waits until code lists are loaded.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the code list. |
| `callback` | `Function` | yes |  | Runs when the lists are ready. |

Several names can be separated by a comma. Loaded values are stored in `DEF.cl`.

Example: Wait for a code list
```js
CL_INIT('countries', next => next(['SK', 'CZ']));
CL('countries', function() {
	console.log(DEF.cl.countries); // ["SK","CZ"]
});
```

### CL_INIT(name, source, [expire], [init])

Type: `method`

Declares how a code list is loaded.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the code list. |
| `source` | `Function|String` | yes |  | The loader. |
| `expire` | `String` | no |  | The list is loaded again after this time. |
| `init` | `Boolean` | no |  | With `true`, loads the list immediately. |

The source is a function that receives `next(value)`, or a string. A string that starts with an HTTP method goes to [AJAX()](#ajax), and any other string to [TAPI()](#tapi). `CLINIT()` is the same function.

Example: Load a list from a URL
```js
CL_INIT('roles', 'GET /api/roles/', true);
setTimeout(function() {
	console.log(DEF.cl.roles.url); // /api/roles/
}, 300);
```

### clearTimeout2(name)

Type: `method`

Cancels a named timer.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the timer. |

Returns: `Boolean` `true` when a timer was running.

Example: Cancel a timer
```js
setTimeout2('save', NOOP, 1000);
console.log(clearTimeout2('save')); // true
console.log(clearTimeout2('save')); // false
```

### CLINIT(name, source, [expire], [init])

Type: `method`

The same function as [CL_INIT()](#cl-init).

Example: Same function
```js
console.log(CLINIT === CL_INIT); // true
```

### CLONE(value, [nopath])

Type: `method`

Creates a deep copy of a value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object|String` | yes |  | The value or a path. |
| `nopath` | `Boolean` | no |  | With `true`, a string is returned as it is. |

Returns: `Object` The copy.

A string is treated as a path, and the value of the path is cloned.

Example: Clone a value
```js
SET('user', { name: 'Peter' });
var copy = CLONE('user');
copy.name = 'Anna';
console.log(GET('user.name')); // Peter
console.log(copy.name); // Anna
```

### CLRELOAD(name, [callback])

Type: `method`

Loads code lists again.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the code list. |
| `callback` | `Function` | no |  | Runs when the lists are loaded. |

Several names can be separated by a comma.

Example: Reload a list
```js
var count = 0;
CL_INIT('numbers', next => next([++count]), true);
setTimeout(function() {
	CLRELOAD('numbers', function() {
		console.log(DEF.cl.numbers); // [2]
	});
}, 100);
```

### CMD(name, [a], [b], [c], [d], [e])

Type: `method`

Runs a command in all components that registered it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the command. |
| `a` | `Object` | no |  | Up to five arguments, `a` to `e`. |

Components register commands with `self.command()`.

Example: Run a command
```js
ON('ready', function() {
	CMD('box.ping', 5);
});
ON('cmd', function(name, value) {
	console.log(name, value); // box.ping 5
});
```

### COMPILE([container])

Type: `method`

Searches the page for new components and bindings.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `container` | `Element|jQuery` | no |  | Limits the search to this element. |

Call it after adding HTML by hand.

Example: Compile new HTML
```js
$(document.body).append('<ui-component name="box" path="data.new"></ui-component>');
COMPILE();
setTimeout(function() {
	console.log(FIND('box').path); // data.new
}, 500);
```

### COMPONENT(name, [config], declaration, [dependencies])

Type: `method`

Declares a component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the component. |
| `config` | `String|Object` | no |  | The default configuration. |
| `declaration` | `Function` | yes |  | `function(self, config, cls)`. |
| `dependencies` | `Array` | no |  | Files to import first, or component names that start with `@`. |

Several names can be separated by a comma. A version can follow the name: `box@2`. The whole topic is explained in [Creating components](../../guides/creating-components/), and the object the declaration receives is described in [Component](../component/).

Example: Declare a component
```html
<ui-component name="hello"></ui-component>
<script>
COMPONENT('hello', 'name:World', function(self, config) {
	self.make = () => self.element.text('Hello ' + config.name);
});
ON('ready', function() {
	console.log($('ui-component').text()); // Hello World
});
</script>
```

### COMPONENTS(path)

Type: `method`

Lists the components on a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path with optional flags. |

Returns: `Array` The components.

The path is written without `.*`. Flags after the path filter the list: `@visible`, `@hidden`, `@touched`, `@modified`, `@required`, `@invalid`, `@valid`, `@disabled` and `@enabled`.

Example: Filter the components
```js
ON('ready', function() {
	console.log(COMPONENTS('form').length); // 2
	SET('form.name', 'Peter');
	console.log(COMPONENTS('form @invalid').map(com => com.path)); // ["form.age"]
});
```

### CONFIG(selector, config)

Type: `method`

Sets the configuration of components before they start.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String|Function` | yes |  | Which components to configure. |
| `config` | `Object|String|Function` | yes |  | The configuration, or a function that returns it. |

The selector is a component name, `.path`, `#id`, or a function that receives the component and returns a boolean. Parts separated by a space must all match, and a comma separates alternatives. Call it before the components are compiled.

Example: Configure by name
```js
CONFIG('box', { size: 10 });
ON('ready', function() {
	console.log(FIND('box').config.size); // 10
});
```

### COOKIES

Type: `property`

An object for reading and writing cookies.

It has the methods `get(name)`, `set(name, value, expire, [samesite])` and `rem(name)`. `expire` is a `Date` or a value such as `1 day`.

Example: Read the value
```js
COOKIES.set('theme', 'dark', '1 day');
console.log(COOKIES.get('theme') === 'dark' || location.protocol === 'file:'); // true
```

### COPY(source, target)

Type: `method`

Copies the fields of one object to another.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `source` | `Object` | yes |  | The object to read. |
| `target` | `Object` | yes |  | The object to write. |

Returns: `Object` The target object.

Objects inside the source are cloned.

Example: Copy fields
```js
var target = COPY({ a: 1 }, { b: 2 });
console.log(target); // {"b":2,"a":1}
```

### CSS(value, [id], [selector])

Type: `method`

The same function as [STYLE()](#style).

Example: Same function
```js
console.log(CSS === STYLE); // true
```

### DAPI(name, [data], [callback])

Type: `method`

Calls the API endpoint set in `DEF.api`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the schema. |
| `data` | `Object` | no |  | The data. |
| `callback` | `Function|String` | no |  | `function(response, err, output)`, or a path. |

Returns: `Object|Promise` The same as [API()](#api).

`DEF.api` is a URL, or a function that receives the name and the data and returns the value for [API()](#api).

Example: Use the default endpoint
```js
DEF.api = '/api/';
DAPI('users_list', null, function(response) {
	console.log(response.url, response.body.schema); // /api/ users_list
});
```

### DAYS

Type: `property`

The names of the days, starting with Sunday.

Example: Read the value
```js
console.log(DAYS[0]); // Sunday
```

### DEBUG()

Type: `method`

Turns on the debug mode.

Calls of missing methods write a warning to the console, and [API()](#api) adds the schema to the URL. The mode cannot be turned on after `DEF.secret()` has set a key.

Example: Warn about a missing method
```js
DEBUG();
EXEC('missing.method');
console.log('done'); // done
```

### DECRYPT(value, [key], [type])

Type: `method`

Decodes a value from [ENCRYPT()](#encrypt).

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The encoded value. |
| `key` | `String` | no |  | The key. |
| `type` | `String` | no |  | The prefix of the value. |

Returns: `Object` The value, or `undefined` when the key or the prefix does not match.

JSON is parsed back to an object.

Example: Use a wrong key
```js
var token = ENCRYPT('Peter', 'secret');
console.log(DECRYPT(token, 'secret')); // Peter
console.log(DECRYPT(token, 'other') === undefined); // true
```

### DEF

Type: `property`

The configuration of the library. See [DEF](../def/).

Example: Read the value
```js
console.log(DEF.delay); // 555
```

### DEFAULT(path, [delay], [reset])

Type: `method`

Restores the default values of the components on a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it, or `path__value`. |
| `delay` | `Number` | no |  | A delay in milliseconds. |
| `reset` | `Boolean` | no | `true` | With `false`, the state of the components is kept. |

A component takes its default value from its `default` attribute. With the form `path__value`, the expression after the two underscores is written to the path as the default value instead.

Example: Write a default object
```js
DEFAULT('form.* __{ name: "Peter" }');
setTimeout(function() {
	console.log(GET('form')); // {"name":"Peter"}
}, 100);
```

### DIFFDOM(element, selector, html, [attribute])

Type: `method`

Updates the children of an element so they match new HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `jQuery` | yes |  | The parent. |
| `selector` | `String` | yes |  | A selector of the children. |
| `html` | `String` | yes |  | The new HTML. |
| `attribute` | `String` | no |  | The attribute to compare. |

Returns: `Object` The counts `add`, `upd` and `rem`.

Only the changed children are added or removed. Children are compared by their HTML, or by the value of the attribute.

Example: Update a list
```html
<ul id="list"><li>A</li><li>B</li></ul>
<script>
var result = DIFFDOM($('#list'), 'li', '<li>B</li><li>C</li>');
console.log(result); // {"add":1,"upd":1,"rem":1}
console.log($('#list').text()); // BC
</script>
```

### DISABLED(path, [flags])

Type: `method`

The opposite of [CAN()](#can).

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it. |
| `flags` | `String|Array` | no |  | `visible` or `hidden` limits the check to those components. |

Returns: `Boolean` The result.

Example: Disable a submit button
```js
ON('ready', function() {
	console.log(DISABLED('form.*')); // true
});
```

### EMIT(name, [...args])

Type: `method`

Calls the handlers of an event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `...args` | `Object` | no |  | The arguments passed to the handlers. |

Returns: `Boolean` `true` when the event has at least one handler.

A custom flag can follow the name, for example `EMIT('ping @saved')`.

Example: Emit an event
```js
ON('hello', name => console.log(name)); // Peter
console.log(EMIT('hello', 'Peter')); // true
console.log(EMIT('unknown')); // false
```

### EMPTYARRAY

Type: `property`

A frozen empty array.

Example: Read the value
```js
console.log(Object.isFrozen(EMPTYARRAY)); // true
```

### EMPTYOBJECT

Type: `property`

A frozen empty object.

Example: Read the value
```js
console.log(Object.isFrozen(EMPTYOBJECT)); // true
```

### ENCRYPT(value, [key], [type])

Type: `method`

Encodes a value into a hexadecimal string signed with a key.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Object` | yes |  | The value. |
| `key` | `String` | no |  | The key. |
| `type` | `String` | no |  | A prefix of the result. |

Returns: `String` The encoded value.

The value is encoded, not strongly encrypted. Objects are converted to JSON.

Example: Encode and decode
```js
var token = ENCRYPT({ id: 5 }, 'secret');
console.log(DECRYPT(token, 'secret')); // {"id":5}
```

### ENV(name, [value])

Type: `method`

Reads or writes an environment variable.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String|Object` | yes |  | The name, or an object with values. |
| `value` | `Object` | no |  | The value. |

Returns: `Object` The value of the variable.

An object as the first argument writes several variables. Paths and URLs read the variables with `[name]`. The variables are stored in `DEF.environment`.

Example: Use a variable
```js
ENV('api', '/api/v1/');
console.log(ENV('api')); // /api/v1/
console.log('[api]users/'.env()); // /api/v1/users/
```

### ENVIRONMENT(name, [version], [language], [env])

Type: `method`

Sets the name, the version and the language of the application.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the application. |
| `version` | `String` | no |  | The version. |
| `language` | `String` | no |  | The language of imported HTML. |
| `env` | `Object` | no |  | Environment variables for [ENV()](#env). |

The name separates the browser storage of the application. It sets `DEF.localstorage`, `DEF.version` and `DEF.languagehtml`.

Example: Name the application
```js
ENVIRONMENT('shop', '1.2', 'en', { api: '/api/' });
console.log(DEF.localstorage, DEF.version, ENV('api')); // shop 1.2 /api/
```

### ERROR(value, [success], [error])

Type: `method`

Tells whether a response contains an error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object|Function` | yes |  | The response, or the success handler. |
| `success` | `Function|String` | no |  | Runs when there is no error. |
| `error` | `Function|String` | no |  | Runs when there is an error. |

Returns: `Boolean|Function` `true` when the value is an error, or the created callback.

An `Error`, an object with the field `error`, or an array whose first item has it counts as an error, and the event [ERROR](../events/#error-2) is emitted. `success` and `error` are functions or paths.

With a function as the first argument, `ERROR(success, [error])` returns a callback for a request.

Example: Check a response
```js
console.log(ERROR([{ error: 'Invalid name' }])); // true
console.log(ERROR({ id: 5 })); // false
```

Example: Create a callback
```js
var callback = ERROR(function(response) {
	console.log(response.id); // 5
});
callback({ id: 5 });
```

### ERRORS(path, [flags], [highlight])

Type: `method`

Lists the components that are touched and invalid.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it. |
| `flags` | `String|Array` | no |  | `visible` or `hidden` limits the check to those components. |
| `highlight` | `Boolean` | no |  | Refreshes the state of the found components. |

Returns: `Array` The components.

`ERRORS(path, true)` also refreshes the state of the components it finds.

Example: Find the invalid fields
```js
ON('ready', function() {
	CHANGE('form.name');
	console.log(ERRORS('form.*').map(com => com.path)); // ["form.name"]
});
```

### EVALUATE(path, expression, [nopath])

Type: `method`

Evaluates a JavaScript expression with the value of a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String|Object` | yes |  | The path, or the value itself when `nopath` is `true`. |
| `expression` | `String` | yes |  | The expression. |
| `nopath` | `Boolean` | no |  | Treats the first argument as the value. |

Returns: `Object` The result of the expression.

The expression can use the variables `value` and `path`.

Example: Evaluate an expression
```js
SET('price', 10);
console.log(EVALUATE('price', 'value * 2')); // 20
console.log(EVALUATE(4, 'value + 1', true)); // 5
```

### EXE([wait], path, [...args])

Type: `method`

The same function as [EXEC()](#exec).

Example: Same function
```js
console.log(EXE === EXEC); // true
```

### EXEC([wait], path, [...args])

Type: `method`

Calls a function, a plugin method, a component method, a command or an event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `wait` | `Boolean` | no |  | With `true`, waits for the target. |
| `path` | `String` | yes |  | The target. |
| `...args` | `Object` | no |  | The arguments. |

The form of the path selects the target:

| Path | Target |
| --- | --- |
| `plugin/method` | A method of a plugin |
| `@plugin.method` | A method of a plugin |
| `path.to.fn` | A function on a path |
| `#name` | The event `name` |
| `-selector/method` | A method of a component, as in [SETTER()](#setter) |
| `&name` | A command, as in [CMD()](#cmd) |

With `true` as the first argument, the call waits until the target exists. `EXE()` is the same function.

Example: Call a plugin method
```js
PLUGIN('users', function(exports) {
	exports.hello = function(name) {
		console.log(name); // Peter
	};
});
EXEC(true, 'users/hello', 'Peter');
```

Example: Call a function on a path
```js
window.app = { sum: (a, b) => console.log(a + b) };
EXEC('app.sum', 1, 2);
console.log(typeof(app.sum)); // function
```

### EXT(path, value, [delay], [reset])

Type: `method`

Merges the fields of an object into the object on a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Object` | yes |  | The fields to merge. |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |
| `reset` | `Boolean` | no |  | With `true`, the components on the path are also marked as not touched and not modified. |

Each field is written separately, so the other fields stay. `EXTEND()` is the same function. As with [SET()](#set), the first argument can be a request and `value` the target path.

Example: Merge fields
```js
SET('user', { name: 'Peter' });
EXT('user', { age: 30 });
console.log(GET('user')); // {"name":"Peter","age":30}
```

### EXTEND(path, value, [delay], [reset])

Type: `method`

The same function as [EXT()](#ext).

Example: Same function
```js
console.log(EXTEND === EXT); // true
```

### EXTENSION(name, [config], declaration)

Type: `method`

Extends a component or a plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the component, or `@plugin`. |
| `config` | `Object|String` | no |  | Additional configuration. |
| `declaration` | `Function` | yes |  | `function(self, config, cls)`. |

The declaration runs for every instance of the component, including the existing ones. With a name that starts with `@`, it extends the plugin of that name.

Example: Add a method to a component
```js
EXTENSION('box', function(self) {
	self.bye = () => 'Bye';
});
ON('ready', function() {
	console.log(FIND('box').bye()); // Bye
});
```

### FIND(selector, [many], [nocache], [callback])

Type: `method`

Finds a component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String|Element` | yes |  | What to find. |
| `many` | `Boolean` | no |  | With `true`, returns all matches as an array. |
| `nocache` | `Boolean` | no |  | Skips the cache of found components. |
| `callback` | `Function` | no |  | Waits for the component and receives it. |

Returns: `Component|Array` The found component, or an array with `many`. Nothing is returned when a callback is used.

The selector is a component name, `.path`, `#id`, `name[path]` or `name@version`, and parts can be combined with a space. A DOM element or a jQuery object finds the components inside it.

With a callback, the search waits until the component exists. The callback can be the second or the third argument.

Example: Find by name and path
```js
ON('ready', function() {
	console.log(FIND('box').path); // data.box
	console.log(FIND('.data.box').name); // box
	console.log(FIND('box', true).length); // 1
});
```

Example: Wait for a component
```js
FIND('box', function(com) {
	console.log(com.name); // box
});
```

### FN(expression)

Type: `method`

Creates a function from a string.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `expression` | `String` | yes |  | The expression. |

Returns: `Function` The function.

The string is an arrow function. A string that only uses the word `value` becomes a function with the argument `value`.

Example: Create functions
```js
console.log(FN('(a, b) => a + b')(1, 2)); // 3
console.log(FN('value > 5')(10)); // true
```

### FORMATTER(fn, [prepend])

Type: `method`

Registers a global formatter of component values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(path, value, type, format)`. |
| `prepend` | `Boolean` | no |  | With `true`, the formatter runs first. |

Returns: `Object` The formatted value, when the function runs the formatters.

The function returns the new value, and `undefined` keeps the current one. `FORMATTER(value, path, type, [format])` runs all registered formatters.

Example: Format a value
```js
FORMATTER(function(path, value, type) {
	if (type === 'upper')
		return value.toUpperCase();
});
console.log(FORMATTER('peter', 'user.name', 'upper')); // PETER
```

### FREE([delay])

Type: `method`

Removes unused components, bindings and watchers.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `delay` | `Number` | no | `10` | The delay in milliseconds. |

The library also does this every three minutes.

Example: Clean up
```js
FREE();
console.log(typeof(FREE)); // function
```

### FUNC

Type: `property`

An empty object for your own shared functions.

Example: Read the value
```js
FUNC.sum = (a, b) => a + b;
console.log(FUNC.sum(1, 2)); // 3
```

### GET(path, [scope])

Type: `method`

Reads the value of a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `scope` | `Object|Function|Boolean` | no |  | An object reads the path relative to that object. A function waits until the value is not `null` and then receives it. `true` resets the state of the components on the path. |

Returns: `Object` The value, or `undefined` when the path has no value. With a function as the second argument, nothing is returned.

Flags can follow the path. `@clone` returns a copy, `@reset` resets the state of the components after reading, and `@modified` returns only the values of the modified components.

Example: Read a value
```js
SET('user', { name: 'Peter', age: 30 });
console.log(GET('user.age')); // 30
console.log(GET('name', { name: 'Anna' })); // Anna
```

Example: Wait for a value
```js
GET('settings.theme', function(value) {
	console.log(value); // dark
});
setTimeout(() => SET('settings.theme', 'dark'), 100);
```

### GUID([size])

Type: `method`

Creates a random identifier.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `size` | `Number` | no |  | The length. Without it, the identifier has 36 characters in the form of a UUID. |

Returns: `String` The identifier.

Example: Create identifiers
```js
console.log(GUID().length); // 36
console.log(GUID(10).length); // 10
```

### HASH(value, [unsigned])

Type: `method`

Creates a number from a value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value. |
| `unsigned` | `Boolean` | no | `true` | With `false`, the result can be negative. |

Returns: `Number` The hash.

Objects are hashed by their JSON form. A number is returned as it is, a boolean becomes `0` or `1`, and a date becomes its time.

Example: Hash a string
```js
console.log(HASH('abc')); // 96354
```

### HIDDEN(element)

Type: `method`

Tells whether an element is not displayed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element|jQuery` | yes |  | The element. |

Returns: `Boolean` The result. A missing element returns `true`.

Example: Check an element
```html
<div id="a" style="display:none"><span></span></div>
<script>
console.log(HIDDEN($('#a')[0].firstChild)); // true
console.log(HIDDEN(null)); // true
</script>
```

### IMPORT(url, [target], [callback], [insert], [preparator])

Type: `method`

Loads a script, a style or HTML into the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String|Array` | yes |  | The URL. |
| `target` | `String|Element|jQuery|Component` | no | `body` | Where to put HTML. |
| `callback` | `Function` | no |  | `function(result)`. |
| `insert` | `Boolean` | no | `true` | With `false`, replaces the content of the target. |
| `preparator` | `Function` | no |  | `function(response, output)` changes the HTML before it is inserted. |

The topic is explained in [Importing content](../../guides/importing/). An array of URLs is loaded one by one. A URL that starts with `once ` or `!` is loaded only once, and `<path>` before the URL skips the import when the path has a value. The callback receives `1` after loading and `0` when nothing was loaded.

Example: Import HTML
```html
<div id="target"></div>
<script>
IMPORT('/parts/hello.html', '#target', function(result) {
	console.log(result, $('#target').text()); // 1 Hello
});
</script>
```

### IMPORTCACHE(url, expire, [target], [callback], [insert], [preparator])

Type: `method`

Works like [IMPORT()](#import) and caches imported HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The URL. |
| `expire` | `String` | yes |  | For example `5 minutes` or `session`. |
| `target` | `String|Element|jQuery|Component` | no | `body` | Where to put HTML. |
| `callback` | `Function` | no |  | `function(result)`. |
| `insert` | `Boolean` | no | `true` | With `false`, replaces the content of the target. |
| `preparator` | `Function` | no |  | `function(response, output)` changes the HTML before it is inserted. |

Example: Import and cache
```html
<div id="target"></div>
<script>
IMPORTCACHE('/parts/hello.html', '5 minutes', '#target', function(result) {
	console.log(result, $('#target').text()); // 1 Hello
});
</script>
```

### INC(path, [value], [delay], [reset])

Type: `method`

Adds a number to the value on a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Number` | no | `1` | The number to add. |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |
| `reset` | `Boolean` | no |  | With `true`, the components on the path are also marked as not touched and not modified. |

A missing value or a value that is not a number counts as `0`.

Example: Increase a counter
```js
INC('stats.visits');
INC('stats.visits', 5);
console.log(GET('stats.visits')); // 6
```

### inDOM(element)

Type: `method`

Tells whether an element is inside the page body.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element` | yes |  | The element. |

Returns: `Boolean` `true`, or `undefined` when the element is outside the body.

Example: Check an element
```js
console.log(inDOM(document.body)); // true
console.log(inDOM(document.createElement('div')) === undefined); // true
```

### isIE

Type: `property`

`true` in Internet Explorer.

Example: Read the value
```js
console.log(typeof(isIE)); // boolean
```

### isMOBILE

Type: `property`

`true` on a mobile device.

Example: Read the value
```js
console.log(typeof(isMOBILE)); // boolean
```

### isPRIVATEMODE

Type: `property`

`true` when the browser storage is not available.

Example: Read the value
```js
console.log(typeof(isPRIVATEMODE)); // boolean
```

### isROBOT

Type: `property`

`true` when the user agent looks like a robot.

Example: Read the value
```js
console.log(typeof(isROBOT)); // boolean
```

### isSTANDALONE

Type: `property`

`true` when the page runs as an installed application.

Example: Read the value
```js
console.log(typeof(isSTANDALONE)); // boolean
```

### isTOUCH

Type: `property`

`true` on a device with a touch screen.

Example: Read the value
```js
console.log(typeof(isTOUCH)); // boolean
```

### jComponent

Type: `property`

Another name of `MAIN`. See [MAIN](../main/).

Example: Read the value
```js
console.log(jComponent === MAIN); // true
```

### LOCALIZE(text)

Type: `method`

Adds translations to the dictionary.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `String` | yes |  | The translations. |

Each line has the form `key : value`, and a line that starts with `//` is skipped. The values are stored in `DEF.dictionary`. The topic is explained in [Localization](../../guides/localization/).

Example: Add translations
```js
LOCALIZE('// Greetings\nhello : Ahoj');
console.log(DEF.dictionary.hello); // Ahoj
```

### M

Type: `property`

A short name of `MAIN`. See [M](../m/).

Example: Read the value
```js
console.log(M === MAIN); // true
```

### MACRO(name, declaration)

Type: `method`

Declares a macro.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the macro. |
| `declaration` | `Function` | yes |  | `function(self, element, cls)`. |

The topic is explained in [Macros](../../guides/macros/), and the object is described in [Macro](../macro/).

Example: Declare a macro
```js
MACRO('upper', function(self, element) {
	self.setter = value => element.text((value || '').toUpperCase());
});
console.log(typeof(M.macros.upper)); // function
```

### MAIN

Type: `property`

The main object of the library. See [MAIN](../main/).

Example: Read the value
```js
console.log(MAIN.version); // 19.194
```

### MAKE([target], fn, [update])

Type: `method`

Builds an object in a function.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `target` | `Object|String` | no |  | An object or a path. Without it, a new object is created. |
| `fn` | `Function` | yes |  | `function(obj, path, set)`. |
| `update` | `Boolean` | no | `true` | With `false`, an existing object on the path is written back without resetting its components. |

Returns: `Object` The object.

A string target is a path: the object on the path is changed, or a new one is created and written there. The function receives the object, the path and a function `set(path, value)`.

Example: Build an object
```js
var obj = MAKE(function(o) {
	o.name = 'Peter';
});
console.log(obj); // {"name":"Peter"}
```

Example: Build an object on a path
```js
MAKE('user', function(o, path, set) {
	o.name = 'Peter';
	set('address.city', 'Bratislava');
});
console.log(GET('user')); // {"name":"Peter","address":{"city":"Bratislava"}}
```

### MIDDLEWARE(name, fn)

Type: `method`

Declares a middleware of routes. See [Routing](../../guides/routing/).

### MODIFIED(path, [flags])

Type: `method`

Lists the paths of the components that are touched and modified.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it. |
| `flags` | `String|Array` | no |  | `visible` or `hidden` limits the search to those components. |

Returns: `Array` The paths of the modified components.

Example: List the changed fields
```js
ON('ready', function() {
	CHANGE('form.name');
	console.log(MODIFIED('form.*')); // ["form.name"]
});
```

### MONTHS

Type: `property`

The names of the months.

Example: Read the value
```js
console.log(MONTHS[11]); // December
```

### NAV

Type: `property`

The router object. See [Routing](../../guides/routing/).

### NODEINDEXOF(element)

Type: `method`

Returns the position of an element among its siblings.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element|jQuery` | yes |  | The element. |

Returns: `Number` The index.

Example: Read the position
```html
<ul><li>A</li><li id="b">B</li></ul>
<script>
console.log(NODEINDEXOF($('#b'))); // 1
</script>
```

### NODEINSERT(element, target, [before])

Type: `method`

Puts an element after or before another one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element|jQuery` | yes |  | The element to move. |
| `target` | `Element|jQuery` | yes |  | The reference element. |
| `before` | `Boolean` | no |  | With `true`, puts it before the target. |

Example: Move an element
```html
<ul><li id="a">A</li><li>B</li><li id="c">C</li></ul>
<script>
NODEINSERT($('#c'), $('#a'), true);
console.log($('ul').text()); // CAB
</script>
```

### NODEMOVE(element, [up])

Type: `method`

Moves an element by one position among its siblings.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element|jQuery` | yes |  | The element. |
| `up` | `Boolean` | no |  | With `true`, moves it up. Otherwise it moves down. |

Example: Move an element up
```html
<ul><li>A</li><li id="b">B</li></ul>
<script>
NODEMOVE($('#b'), true);
console.log($('ul').text()); // BA
</script>
```

### NOOP()

Type: `method`

A function that does nothing.

Example: Use it as an empty callback
```js
console.log(NOOP() === undefined); // true
```

### NOTFOCUSED()

Type: `method`

Tells whether the page is in the background.

Returns: `Boolean` The result.

Example: Check the page
```js
console.log(typeof(NOTFOCUSED())); // boolean
```

### NOTIFY(...paths)

Type: `method`

Refreshes the components and watchers of exact paths.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `...paths` | `String` | yes |  | One or more paths. |

Only components whose path is exactly the same are refreshed.

Example: Notify a watcher
```js
WATCH('user.name', function(path, value) {
	console.log(value); // Peter
});
window.user = { name: 'Peter' };
NOTIFY('user.name');
```

### NOTMODIFIED(path, [value], [fields])

Type: `method`

Tells whether a value is the same as the last time it was checked.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, or any key when `value` is given. |
| `value` | `Object` | no |  | The value to compare. Without it, the value of the path is used. |
| `fields` | `Array` | no |  | Compares only these fields. |

Returns: `Boolean` `true` when the value has not changed.

The first check of a path returns `false`. The value is compared by a hash of its JSON form.

Example: Skip work when nothing changed
```js
console.log(NOTMODIFIED('list', [1, 2])); // false
console.log(NOTMODIFIED('list', [1, 2])); // true
console.log(NOTMODIFIED('list', [1, 2, 3])); // false
```

### NOW

Type: `property`

The current date, refreshed every minute.

Example: Read the value
```js
console.log(NOW instanceof Date); // true
```

### NUL(path, [delay])

Type: `method`

Writes `null` to a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |

`NULL()` is the same function.

Example: Clear a value
```js
SET('user.name', 'Peter');
NUL('user.name');
console.log(GET('user.name')); // null
```

### NULL(path, [delay])

Type: `method`

The same function as [NUL()](#nul).

Example: Same function
```js
console.log(NULL === NUL); // true
```

### OFF(name, [path], [fn])

Type: `method`

Removes handlers of an event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `path` | `String|Function` | no |  | The path, or the handler when no path is needed. |
| `fn` | `Function` | no |  | The handler. |

Several events can be separated by `+`. The form `owner#name` removes the handlers of that owner, and `owner#` removes all of them.

Example: Remove a handler
```js
var fn = () => console.log('never');
ON('ping', fn);
OFF('ping', fn);
console.log(EMIT('ping')); // false
```

Example: Remove the handlers of an owner
```js
ON('shop#ping', () => console.log('never'));
OFF('shop#');
console.log(EMIT('ping')); // false
```

### ON(name, [path], fn, [init])

Type: `method`

Registers a handler of an event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `path` | `String` | no |  | The path, for the event `watch`. |
| `fn` | `Function` | yes |  | The handler. |
| `init` | `Boolean` | no |  | With the event `watch`, the handler is also called immediately. |

The name can list several events separated by a space or ` + `. A name that starts with `^` puts the handler first. The form `owner#name` assigns the handler to an owner, so [OFF()](#off) can remove it later. The name `@flag myflag` reacts to a custom flag on a path.

The event `watch` works like [WATCH()](#watch) and uses `path` and `init`. The events of the library are listed in [Events](../events/).

Example: Listen to a custom event
```js
ON('cart.add', function(id, count) {
	console.log(id, count); // 15 2
});
EMIT('cart.add', 15, 2);
```

Example: React to a custom flag
```js
ON('@flag saved', function(path, value) {
	console.log(path, value); // user.name Peter
});
SET('user.name @saved', 'Peter');
```

### OPT([obj], fn)

Type: `method`

Builds an options object with nested paths.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | no |  | The object to fill. Without it, a new object is created. |
| `fn` | `Function` | yes |  | `function(set, scope)`. |

Returns: `Object` The object.

The function receives `set(path, value)` and `scope(prefix)`, which adds a prefix to the next paths.

Example: Build options
```js
var options = OPT(function(set, scope) {
	set('title', 'Users');
	scope('grid');
	set('limit', 20);
});
console.log(options); // {"title":"Users","grid":{"limit":20}}
```

### PARSE(value, [date])

Type: `method`

Parses JSON.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The JSON or a selector. |
| `date` | `Boolean` | no | `DEF.jsondate` | Converts ISO dates to `Date` objects. |

Returns: `Object` The value.

A string that starts with `#` or `.` is a selector of an element with JSON. Invalid JSON returns `null`, and an object is returned as it is.

Example: Parse JSON
```js
console.log(PARSE('{"id":5}').id); // 5
console.log(PARSE('{"d":"2024-01-01T10:00:00.000Z"}').d instanceof Date); // true
console.log(PARSE('wrong')); // null
```

### PARSER(fn, [prepend])

Type: `method`

Registers a global parser of component values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(path, value, type, format)`. |
| `prepend` | `Boolean` | no |  | With `true`, the parser runs first. |

Returns: `Object` The parsed value, when the function runs the parsers.

The function must always return the value. The built-in parser converts the types `number`, `currency`, `float`, `boolean`, `bool`, `date` and `datetime`. `PARSER(value, path, type, [format])` runs all registered parsers.

Example: Parse a value
```js
console.log(PARSER('1 200.5', 'price', 'number')); // 1200.5
console.log(PARSER('on', 'active', 'boolean')); // true
```

### PLUGIN(name, [fn], [init], [done])

Type: `method`

Declares a plugin, or returns an existing one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the plugin. |
| `fn` | `Function` | no |  | `function(exports)`. |
| `init` | `Function|String` | no |  | Runs before the declaration. A string lists middleware names. |
| `done` | `Function` | no |  | Called when the plugin is ready. |

Returns: `Plugin` The plugin.

The topic is explained in [Plugins](../../guides/plugins/), and the object is described in [Plugin](../plugin/).

Example: Declare a plugin
```js
PLUGIN('users', function(exports) {
	exports.count = 5;
});
setTimeout(function() {
	console.log(PLUGINS.users.count); // 5
}, 500);
```

### PLUGINABLE(name, fn, [init])

Type: `method`

Registers a plugin that is created by its first call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the plugin. |
| `fn` | `Function|String` | yes |  | The declaration or a URL. |
| `init` | `Function|String` | no |  | Runs before the declaration. |

The declaration can also be a URL of a file that declares the plugin.

Example: Create a plugin on demand
```js
PLUGINABLE('reports', function(exports) {
	exports.open = function() {
		console.log('opened'); // opened
	};
});
setTimeout(function() {
	console.log(PLUGINS.reports == null); // true
	EXEC('reports/open');
}, 500);
```

### PLUGINS

Type: `property`

An object with all created plugins, by name.

Example: Read a plugin
```js
PLUGIN('users', function(exports) {
	exports.limit = 10;
});
setTimeout(function() {
	console.log(Object.keys(PLUGINS)); // ["users"]
}, 500);
```

### PREF

Type: `property`

Stored user preferences. See [PREF](../pref/).

Example: Read the value
```js
PREF.set('theme', 'dark');
console.log(PREF.theme); // dark
```

### PUSH(path, value, [delay], [reset])

Type: `method`

Adds a value to the array on a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Object|Array` | yes |  | The item or the items to add. |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |
| `reset` | `Boolean` | no |  | With `true`, the components on the path are also marked as not touched and not modified. |

An array as the value adds each of its items. If the path holds no array, a new one is created. A path that starts with `^` adds to the beginning. As with [SET()](#set), the first argument can be a request and `value` the target path.

Example: Add items
```js
PUSH('tags', 'a');
PUSH('tags', ['b', 'c']);
PUSH('^tags', 'first');
console.log(GET('tags')); // ["first","a","b","c"]
```

### QUERIFY([url], obj)

Type: `method`

Adds an object to a URL as a query string.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | no |  | The URL, optionally with a method. |
| `obj` | `Object` | yes |  | The values. |

Returns: `String` The URL.

Empty values are skipped, arrays are joined with a comma, and dates use the ISO format.

Example: Build a URL
```js
console.log(QUERIFY('GET /users/', { page: 2, tags: ['a', 'b'] })); // GET /users/?page=2&tags=a%2Cb
console.log(QUERIFY({ q: 'x y' })); // ?q=x%20y
```

### QUEUE([name], fn)

Type: `method`

Runs functions one after another.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no |  | The name of the queue. |
| `fn` | `Function` | yes |  | `function(next, pending)`. |

Returns: `Number` The number of waiting items.

The function receives `next` and the number of waiting items. The next item starts after `next()` is called.

Example: Run in order
```js
QUEUE('tasks', function(next) {
	setTimeout(next, 50);
});
QUEUE('tasks', function(next, pending) {
	console.log('second', pending); // second 0
	next();
});
```

### RECONFIGURE(selector, value)

Type: `method`

Changes the configuration of all matching components.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | A selector as in [FIND()](#find). |
| `value` | `Object|String` | yes |  | The new configuration. |

Example: Change the configuration
```js
ON('ready', function() {
	RECONFIGURE('box', { size: 10 });
	console.log(FIND('box').config.size); // 10
});
```

### REDIRECT(url)

Type: `method`

Navigates to a URL. See [Routing](../../guides/routing/).

### REMOVECACHE(key)

Type: `method`

Removes a value from the browser cache.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The key. |

Example: Remove a value
```js
CACHE('profile', 'Peter', '1 day');
REMOVECACHE('profile');
console.log(CACHE('profile') === undefined); // true
```

### REPEAT([condition], process, delay, [init])

Type: `method`

Runs a function repeatedly.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `condition` | `Function|String` | no |  | The condition. |
| `process` | `Function` | yes |  | `function(counter, scope)`. |
| `delay` | `Number` | yes |  | The delay in milliseconds. |
| `init` | `Boolean` | no |  | With `true`, the first run starts immediately. |

The condition is a function or a path, and the run is skipped while it is not met. With the form `path__expression`, the expression receives the variable `value`. While the page is in the background, nothing runs unless `DEF.repeatfocus` is `false`.

Example: Run every 100 ms
```js
REPEAT(function(counter) {
	if (counter === 3)
		console.log('third run'); // third run
}, 100);
```

### REPO

Type: `property`

An empty object for your own shared data.

Example: Read the value
```js
REPO.items = [1, 2];
console.log(REPO.items.length); // 2
```

### RESET(path, [delay])

Type: `method`

Marks the components on a path as not touched and not modified, and validates them again.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it. |
| `delay` | `Number` | no |  | A delay in milliseconds. |

Example: Reset a form
```js
ON('ready', function() {
	SET('form.name', 'Peter');
	CHANGE('form.name');
	console.log(MODIFIED('form.*')); // ["form.name"]
	RESET('form.*');
	console.log(MODIFIED('form.*').length); // 0
});
```

### RETURN(command, [multiple])

Type: `method`

Reads a property of a component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `command` | `String` | yes |  | The selector and the property. |
| `multiple` | `Boolean` | no |  | With `true`, reads from all matching components. |

Returns: `Object|Array` The value, or an array with `multiple`.

The command has the form `selector/property`.

Example: Read a property
```js
ON('ready', function() {
	console.log(RETURN('box/title')); // Box
	console.log(RETURN('box/title', true)); // ["Box"]
});
```

### REWRITE(path, value, [type])

Type: `method`

Writes a value without refreshing components.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Object` | yes |  | The new value. |
| `type` | `Number` | no |  | The change type passed to watchers. |

Data bindings and watchers still run. Use the `@nowatch` flag to skip the watchers.

Example: Write without a component refresh
```js
WATCH('user.name', function(path, value) {
	console.log(value); // Peter
});
REWRITE('user.name', 'Peter');
```

### REWRITE2(path, value, [type])

Type: `method`

Writes a value without refreshing components or data bindings.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Object` | yes |  | The new value. |
| `type` | `Number` | no |  | The change type passed to watchers. |

Watchers still run.

Example: Write without bindings
```js
REWRITE2('user.name', 'Peter');
console.log(GET('user.name')); // Peter
```

### ROUTE(url, fn, [middleware], [init])

Type: `method`

Declares a route. See [Routing](../../guides/routing/).

### SCROLLBAR(element, [options])

Type: `method`

Adds a custom scrollbar to an element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `jQuery` | yes |  | The element. |
| `options` | `Object` | no |  | The options. |

Returns: `Object` The scrollbar.

The options:

| Option | Type | Description |
| --- | --- | --- |
| `orientation` | String | `x` or `y`. Without it, both directions |
| `visibleX`, `visibleY` | Boolean | Keeps the scrollbar visible |
| `native` | Boolean | Uses the scrollbar of the browser |
| `wrap` | Boolean | With `false`, the content is not wrapped |
| `controls` | jQuery | The element for the scrollbar controls |
| `shadow` | Boolean | Adds shadows at the edges |
| `padding` | Number | The padding of the bars. Default `5` |
| `minsize` | Number | The smallest size of a bar. Default `50` |
| `thickness` | Number | The thickness of a bar. Default `10` |
| `margin`, `marginX`, `marginY` | Number | The margins |
| `offsetX`, `offsetY` | Number | Added to the size of the view |
| `parent` | String or Element | Takes the size from this parent |
| `autoresize` | Boolean | With `false`, the size is not checked on resize |
| `interval` | Number | The interval of the size check |
| `onscroll`, `onidle`, `onresize` | Function | Handlers that receive the scrollbar |

The returned object has the methods `scrollTop([value])`, `scrollLeft([value])`, `scrollBottom([value])`, `scrollRight([value])`, `scroll(x, y)`, `resize()`, `check()`, `sync(element, [offset])`, `unsync()` and `destroy()`.

Example: Create a scrollbar
```html
<div id="box" style="height:100px"><div style="height:500px"></div></div>
<script>
var scrollbar = SCROLLBAR($('#box'), { orientation: 'y' });
console.log(typeof(scrollbar.scrollTop)); // function
console.log(M.scrollbars.length); // 1
</script>
```

### SCROLLBARWIDTH()

Type: `method`

Returns the width of the browser scrollbar in pixels.

Returns: `Number` The width.

Example: Read the width
```js
console.log(typeof(SCROLLBARWIDTH())); // number
```

### SEEX(target, [a], [b], [c], [d])

Type: `method`

Calls a function or writes a value, depending on the target.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `target` | `Function|String` | yes |  | A function, a method or a path. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

A function is called. A string with `/`, or a path that holds a function, goes to [EXEC()](#exec). Any other string is a path, and the first argument is written to it.

Example: Function or path
```js
SEEX(value => console.log(value), 1); // 1
SEEX('result.value', 2);
console.log(GET('result.value')); // 2
```

### SET(path, value, [delay], [reset])

Type: `method`

Writes a value to a path and refreshes everything bound to it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `value` | `Object` | yes |  | The new value. |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |
| `reset` | `Boolean` | no |  | With `true`, the components on the path are also marked as not touched and not modified. |

Components, bindings and watchers on the path are refreshed. The third argument can also be `true`, which resets the state of the components right away.

A path that starts with `+` adds the value to an array like [PUSH()](#push). A path that starts with `~` merges it like [EXT()](#ext). Flags can follow the path: see [Flags and path modifiers](../../guides/flags/).

When the first argument is a request such as `GET /api/users/`, the response is written to the path given as `value`.

Example: Write a value
```js
SET('user.name', 'Peter');
console.log(GET('user.name')); // Peter
console.log(GET('user')); // {"name":"Peter"}
```

Example: Delay the write
```js
SET('search', 'first', 100);
SET('search', 'second', 100);
setTimeout(function() {
	console.log(GET('search')); // second
}, 300);
```

Example: Store a response
```js
SET('GET /api/users/', 'users.response');
setTimeout(function() {
	console.log(GET('users.response.url')); // /api/users/
}, 200);
```

### SETTER(selector, method, [...args])

Type: `method`

Calls a method of all matching components.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | A selector as in [FIND()](#find). |
| `method` | `String` | yes |  | The name of the method. |
| `...args` | `Object` | no |  | The arguments of the method. |

The selector and the method can be joined as `selector/method`. With `true` as the first argument, the call waits for the component. A selector that starts with `!` does not start a lazy component.

Example: Call a method
```js
// The component logs 'Hello ' + name
ON('ready', function() {
	SETTER('box', 'hello', 'Peter');
	SETTER('box/hello', 'Anna');
});
```

### setTimeout2(name, fn, delay, [limit], [arg])

Type: `method`

Starts a named timer and cancels the previous timer of the same name.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the timer. |
| `fn` | `Function` | yes |  | The function. |
| `delay` | `Number` | yes |  | The delay in milliseconds. |
| `limit` | `Number` | no |  | How many times the timer can be postponed. |
| `arg` | `Object` | no |  | The argument of the function. |

Returns: `String` The name of the timer.

With a limit, the timer is postponed at most that many times, so the function still runs during constant calls.

Example: Keep only the last call
```js
function search(value) {
	console.log(value); // last
}
setTimeout2('search', search, 50, null, 'first');
setTimeout2('search', search, 50, null, 'last');
```

### STOPDEBUG()

Type: `method`

Keeps pausing the browser debugger while the developer tools are open.

The function repeats itself every 5 seconds and cannot be stopped.

### STRINGIFY(value, [compress], [fields], [encrypt])

Type: `method`

Serializes a value to JSON.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value. |
| `compress` | `Boolean` | no | `DEF.jsoncompress` | Compresses the output. |
| `fields` | `Array|Object|Function` | no |  | An array of allowed fields, an object with `field: false`, or `function(key, value)`. |
| `encrypt` | `Boolean` | no |  | Encrypts the output with the key from `DEF.secret()`. |

Returns: `String` The JSON.

Compression trims strings and removes empty strings, `false` and `null`.

Example: Serialize a value
```js
console.log(STRINGIFY({ a: 1, b: '', c: false }, true)); // {"a":1}
console.log(STRINGIFY({ a: 1, b: 2 }, false, ['b'])); // {"b":2}
```

### STYLE(value, [id], [selector])

Type: `method`

Adds CSS to the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Array` | yes |  | The CSS. |
| `id` | `String` | no |  | The identifier of the style. The element gets the id `css` plus this value. |
| `selector` | `String` | no |  | A selector that wraps the rules. |

An existing style with the same identifier is replaced. A selector is added before every rule. `CSS()` is the same function.

Example: Add scoped CSS
```js
STYLE('b { color: red }', 'mystyle', '#panel');
console.log($('#cssmystyle').html()); // #panel b{ color: red }
```

### Ta

Type: `property`

A short name of `Tangular`.

Example: Read the value
```js
console.log(Ta === Tangular); // true
```

### Tangular

Type: `property`

The template engine. See [Templates with Tangular](../../guides/tangular/).

Example: Read the value
```js
console.log(Tangular.render('Hello {{ name }}', { name: 'Peter' })); // Hello Peter
```

### TAPI(name, [data], [callback])

Type: `method`

Calls an API with [DAPI()](#dapi) when `DEF.api` is set, and with [WAPI()](#wapi) otherwise.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the schema. |
| `data` | `Object` | no |  | The data. |
| `callback` | `Function|String` | no |  | `function(response, err, output)`, or a path. |

Returns: `Object|Promise` The result of the selected function.

Example: Select the transport
```js
DEF.api = '/api/';
TAPI('users_list', null, function(response) {
	console.log(response.url); // /api/
});
```

### TEMP

Type: `property`

An object for temporary data.

The library replaces it with an empty object every five minutes.

Example: Read the value
```js
TEMP.value = 5;
console.log(TEMP.value); // 5
```

### Thelpers

Type: `property`

The helpers of the template engine. See [Tangular helpers](../tangular-helpers/).

Example: Read the value
```js
console.log(typeof(Thelpers)); // object
```

### TOGGLE(path, [delay], [reset])

Type: `method`

Writes the opposite boolean value to a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |
| `reset` | `Boolean` | no |  | With `true`, the components on the path are also marked as not touched and not modified. |

Example: Switch a value
```js
SET('menu.open', false);
TOGGLE('menu.open');
console.log(GET('menu.open')); // true
```

### TRANSLATE(text)

Type: `method`

Replaces every `@(text)` with its translation.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `String` | yes |  | The text. |

Returns: `String` The translated text.

The key in `DEF.dictionary` is `HASH(text).toString(36)`, optionally with the prefix `T`. Text without a translation stays in the original language.

Example: Translate text
```js
DEF.dictionary[HASH('Hello').toString(36)] = 'Ahoj';
console.log(TRANSLATE('@(Hello), @(Peter)')); // Ahoj, Peter
```

### UNAUTHORIZED(...roles)

Type: `method`

Tells whether the current user lacks all of the given roles.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `...roles` | `String` | yes |  | The roles. |

Returns: `Boolean` `true` when the user has none of the roles.

The user is read from `window.user`. The fields `sa` and `su` allow everything, and the roles come from `permissions` or `roles`, as an array or an object.

Example: Check a role
```js
window.user = { roles: ['editor'] };
console.log(UNAUTHORIZED('editor')); // false
console.log(UNAUTHORIZED('admin')); // true
```

### UNWATCH(path, [fn])

Type: `method`

Removes a watcher.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The watched path. |
| `fn` | `Function` | no |  | The function given to [WATCH()](#watch). |

Without a function, all watchers of the path are removed.

Example: Stop watching
```js
var count = 0;
var fn = () => count++;
WATCH('value', fn);
SET('value', 1);
UNWATCH('value', fn);
SET('value', 2);
console.log(count); // 1
```

### UPD(path, [delay], [reset])

Type: `method`

Tells the library that the value on a path has changed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path of the value. See [Paths](../../guides/paths/). |
| `delay` | `Number` | no |  | A number of 10 or more delays the change by that many milliseconds. A smaller number is the change type: `1` manual, `2` input, `3` default. |
| `reset` | `Boolean` | no |  | With `true`, the components on the path are also marked as not touched and not modified. |

Use it after changing an object directly. Components are refreshed and watchers are called. `UPDATE()` is the same function. The second argument can also be `true`, which resets the state of the components.

Example: Refresh after a direct change
```js
SET('user', { name: 'Peter' });
WATCH('user.name', function(path, value) {
	console.log(value); // Anna
});
GET('user').name = 'Anna';
UPD('user.name');
```

### UPDATE(path, [delay], [reset])

Type: `method`

The same function as [UPD()](#upd).

Example: Same function
```js
console.log(UPDATE === UPD); // true
```

### UPLOAD(url, data, [callback], [delay], [progress])

Type: `method`

Uploads data to a server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The URL, optionally with a method. |
| `data` | `FormData|Object` | yes |  | The data. |
| `callback` | `Function|String` | no |  | `function(response, err, output)`, or a path. |
| `delay` | `Number` | no |  | Waits this many milliseconds before sending. |
| `progress` | `Function|String` | no |  | `function(percentage)`, or a path. |

Returns: `Promise` Only when no callback is given.

The default method is `POST`. `FormData` is sent as it is, and anything else as JSON. Without a callback, the function returns a Promise. `progress` can be the fourth argument when no delay is needed.

Example: Upload a file
```js
var data = new FormData();
data.append('name', 'photo.jpg');
UPLOAD('/api/upload/', data, function(response) {
	console.log(response.url); // /api/upload/
});
```

### VALID(path, [flags])

Type: `method`

Tells whether no component on a path is invalid.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path, optionally with `.*` for everything below it. |
| `flags` | `String|Array` | no |  | `visible` or `hidden` limits the check to those components. |

Returns: `Boolean` The result.

Unlike [CAN()](#can), it does not look at whether the components are touched.

Example: Check a form
```js
ON('ready', function() {
	console.log(VALID('form.*')); // false
	SET('form', { name: 'Peter', age: 30 });
	console.log(VALID('form.*')); // true
});
```

### VBIND(html)

Type: `method`

Creates a virtual binder from HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | The HTML with bindings. |

Returns: `VBinder` The virtual binder.

The object is described in [Virtual binder](../vbinder/), and the topic in [Virtual bindings](../../guides/virtual-bindings/).

Example: Bind a detached element
```js
var binder = VBIND('<div><span data-bind="name__text"></span></div>');
binder.set({ name: 'Peter' });
console.log(binder.element.find('span').text()); // Peter
```

### VBINDARRAY(html, element)

Type: `method`

Renders an array with a virtual binder for each item.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | The HTML of one item. |
| `element` | `Element|jQuery|Component` | yes |  | The parent element. |

Returns: `Object` An object with the methods `set()`, `get()`, `upd()` and `remove()`.

The topic is explained in [Virtual bindings](../../guides/virtual-bindings/).

Example: Render a list
```html
<ul id="list"></ul>
<script>
var list = VBINDARRAY('<li data-bind="name__text"></li>', $('#list'));
list.set([{ name: 'Peter' }, { name: 'Anna' }]);
console.log($('#list').text()); // PeterAnna
</script>
```

### VERSION(...items)

Type: `method`

Selects the version of components that are used without a version.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `...items` | `String` | yes |  | The components and their versions. |

Each item has the form `name@version`, and one string can list several items separated by a comma.

Example: Select a version
```html
<ui-component name="tag"></ui-component>
<script>
COMPONENT('tag@2', function(self) {
	self.make = () => self.element.text('two');
});
VERSION('tag@2');
ON('ready', function() {
	console.log($('ui-component').text()); // two
});
</script>
```

### VISIBLE(element, [threshold], [mode])

Type: `method`

Tells whether an element is in the visible height of the window.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `Element|jQuery` | yes |  | The element. |
| `threshold` | `Number` | no | `0` | A margin in pixels. |
| `mode` | `String` | no | `visible` | `visible`, `above` or `below`. |

Returns: `Boolean` The result.

Example: Check an element
```html
<div id="box">Box</div>
<script>
console.log(VISIBLE($('#box'))); // true
</script>
```

### W

Type: `property`

A short name of `window`.

Example: Read the value
```js
console.log(W === window); // true
```

### WAIT(condition, callback, [interval], [timeout])

Type: `method`

Waits until a condition is met.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `condition` | `Function|String` | yes |  | The function or the path to check. |
| `callback` | `Function` | yes |  | `function(err, again)`. |
| `interval` | `Number` | no | `500` | The interval of the checks in milliseconds. |
| `timeout` | `Number` | no |  | The longest wait in milliseconds. |

The condition is a function or a path. The callback receives an error after a timeout, and a function that starts the wait again. Without a timeout, the wait never ends by itself.

Example: Wait for a value
```js
WAIT('app.ready', function() {
	console.log(GET('app.ready')); // true
}, 50);
setTimeout(() => SET('app.ready', true), 100);
```

Example: Stop after a timeout
```js
WAIT(() => false, function(err) {
	console.log(err.message); // Timeout.
}, 50, 200);
```

### WAPI([name], [data], [callback], [timeout])

Type: `method`

Calls an API over the WebSocket connection.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String|Object` | no |  | The name of the schema, or the connection options. |
| `data` | `Object` | no |  | The data. |
| `callback` | `Function|String` | no |  | `function(response, err, output)`, or a path. |
| `timeout` | `Number` | no | `10000` | The timeout in milliseconds. |

Returns: `Object|Promise` The connection, or a Promise.

An object as the first argument opens the connection like [WAPI_INIT()](#wapi-init). Without arguments, the function returns the open connection. Without a callback, it returns a Promise. The default timeout of a call is 10 seconds.

Example: Call over a socket
```js
WAPI({ url: '/api/' });
WAPI('users_list', null, function(response) {
	console.log(response.schema); // users_list
});
```

### WAPI_INIT(options)

Type: `method`

Opens the WebSocket connection used by [WAPI()](#wapi).

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The connection options. |

Returns: `Object` The options object with the added methods.

The options are `url`, `reconnect` (a delay in milliseconds, or `false`), `expire` (reconnects after this time), `callback` (called once after the first connection attempt), and the handlers `open`, `close(e)` and `message(data)`.

The returned object has the methods `send(name, data, callback, [timeout])`, `on(name, fn)` for the events `open`, `close`, `message` and `reconnect`, `emit(name, ...)` and `pause(is)`.

Example: Open a connection
```js
WAPI_INIT({
	url: '/api/',
	open: function() {
		console.log('connected'); // connected
	}
});
console.log(typeof(WAPI().send)); // function
```

### WARN(...args)

Type: `method`

Writes a warning to the console.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `...args` | `Object` | yes |  | The values to write. |

Example: Write a warning
```js
WARN('Disk is full');
console.log(typeof(WARN)); // function
```

### WATCH(path, fn, [init])

Type: `method`

Calls a function when the value on a path changes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path to watch. |
| `fn` | `Function` | yes |  | `function(path, value, type)`. |
| `init` | `Boolean` | no |  | With `true`, the function is also called immediately. |

The function receives the changed path, the value and the change type: `0` initial call, `1` manual, `2` input, `3` default. A path of `users.*` also reacts to everything below `users`. Several paths can be separated by a space, and a path that starts with `^` runs before the other watchers.

Example: React to a change
```js
WATCH('user.name', function(path, value, type) {
	console.log(path, value, type); // user.name Peter 1
});
SET('user.name', 'Peter');
```

Example: Call it immediately
```js
SET('count', 5);
WATCH('count', function(path, value, type) {
	console.log(value, type); // 5 0
}, true);
```

### WATCHONCE(path, fn)

Type: `method`

Calls a function on the next change of a path only.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path to watch. |
| `fn` | `Function` | yes |  | `function(path, value, type)`. |

Example: React once
```js
WATCHONCE('count', function(path, value) {
	console.log(value); // 1
});
SET('count', 1);
SET('count', 2);
```

### WH

Type: `property`

The height of the window in pixels.

Example: Read the value
```js
console.log(typeof(WH)); // number
```

### WIDTH([element])

Type: `method`

Returns the display size of the window or an element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `element` | `jQuery` | no | `window` | The element. |

Returns: `String` `xs`, `sm`, `md` or `lg`.

The limits are in `DEF.devices`.

Example: Read the size of an element
```html
<div id="box" style="width:1000px"></div>
<script>
console.log(WIDTH($('#box'))); // md
</script>
```

### WORKFLOW(declaration, [tasks], [callback])

Type: `method`

Runs named steps that hand over a value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `declaration` | `Function` | yes |  | `function($)`. |
| `tasks` | `Object` | no |  | The steps by name. |
| `callback` | `Function` | no |  | `function(value, err, $)`. |

Returns: `Object` The workflow.

The object passed to the declaration and to every step has these members:

| Member | Description |
| --- | --- |
| `push(name, fn)` | Adds a step: `function($, value)` |
| `next(name, [value])` | Runs a step. An unknown name ends the workflow |
| `next2(name)` | Returns a callback that runs the step |
| `done([value])` | Calls the final callback with the value |
| `invalid(err)` | Reports an error to `error` or to the final callback |
| `callback(fn)` | Replaces the final callback |
| `clone()` | Creates a workflow with the same steps |
| `destroy()` | Ends the workflow |
| `value`, `current`, `prev` | The value, the current step and the previous step |

Example: Run two steps
```js
WORKFLOW(function($) {
	$.push('load', ($, value) => $.next('save', value + 1));
	$.push('save', ($, value) => $.done(value * 2));
	$.next('load', 1);
}, function(value) {
	console.log(value); // 4
});
```

### WW

Type: `property`

The width of the window in pixels.

Example: Read the value
```js
console.log(typeof(WW)); // number
```
