CalDOM 1.0.7 Documentation

_( query_or_elems, children )

Initiates a CalDom instance.

Parameters:

query_or_elems : String | CalDom | Node | Array<Node> | NodeList | HTMLCollection

(Optional) "+tag" creates a new Element.

'<tag></tag>' creates specified HTML structure. "~svg_tag" creates a SVG element.

Otherwise, it can be a CSS Selector, an XPath query starting with "$", a CalDom instance, a single Node/Element, an array of Nodes or a NodeList/HTMLCollection.

children : CalDom | Node | String | Array<CalDom | Node | String>

(Optional) Child elements to be passed into append(). Can be CalDOM/Node/String or an array of them. See append() for all possibilities.

This might be useful for code-clarity at render() function for reactive components. See examples at react().

parentWindow=window : Window

(Optional) parent_window default to current window. Use this to work with iframes or external windows.

Returns:

{CalDom} A new CalDom instance with created/found elements.

Examples:

elems: Array<Node>

Nodes/Elements of this CalDom instance.

(Note that directly changing items here is not recommended.)

Examples:

elem: Node

First Node of this CalDom instance. .elem == .elems[0]

Examples:

find(selector_or_xpath)

Get a new CalDom instance with matching descendent elements for all elements in this CalDom instance.

Note: This could return duplicates when there are inter-connected elements in this CalDom instance.

Parameters:

selector_or_xpath : String

CSS Selector to find or XPath query starting with "$"

Returns:

{CalDom} A new CalDom instance with found elements.

Examples:

eq( index )

Get a new CalDom instance with i-th element in this CalDom instance.

Parameters:

index : Number

Can be a zero-based index or a minus(-i) index from end (Eg: -1 returns last element)

Returns:

{CalDom} A new CalDom instance with the i-th element

Examples:

parent( n_or_selector )

Get a new CalDom instance with immediate parent(s) or n-th parent(s) or parent(s) matching the CSS selector. (If this CalDom instance has more than 1 element, parents of all of them are included.)

Parameters:

n_or_selector : Number | String

(Optional) Default is immediate parent. If a number is given, n-th parent is returned (zero-based). If a string is given, first parent(s) to match the CSS selector is returned.

Returns:

{CalDom} A new CalDom instance with matched parents. Multiple parents will be returned if there are 2 or more elements in this CalDom instance.

Examples:

children( match_selector )

Get a new CalDom instance with children of all elements in this CalDom instance. (If this CalDom instance has more than 1 element, children of all of them are included.)

Parameters:

match_selector : String

(Optional) CSS selector to match

Returns:

{CalDom} A new CalDom instance with children.

Examples:

each(callback)

Iterate each element in this CalDom instance with a callback.

Parameters:

callback : Function

Function to callback. Callback is called with 2 arguments: callback(elem: Node, index: Number). The enumeration stops if the callback returns false.

Returns:

{CalDom} Returns current CalDom instance

Examples:

map(callback)

Iterate each element in this CalDom instance with a callback and get an array of callback's returned values.

Parameters:

callback : Function

Function to callback. Callback is called with 2 arguments: callback(elem: Node, index: Number).

Returns:

{Array} An array of values returned by the callback.

Examples:

html( html_or_html_array )

Get or set innerHTML of all elements in this CalDom instance.

(WARNING: This might lead to XSS vulnerabilities. Use text() or append("text_content") if you're only updating text).

Parameters:

html_or_html_array : String | Array<String>

(Optional) A HTML string or an array of HTML strings to set at corresponding n-th element.

If not given, an array of innerHTML for all elements in this CalDom instance is returned.

Returns:

{ CalDom | Array<String> } If html_or_html_array param is given, current CalDom instance is returned.

Otherwise, an array of innerHTML from all elements in this CalDom instance is returned.

Examples:

text( text_or_text_array )

Get or set textContent of all elements in this CalDom instance.

Parameters:

text_or_text_array : String | Array<String>

(Optional) Text or an array of text to set at corresponding n-th element.

If not given, an array of textContent is returned.

Returns:

{ CalDom | Array<String> } If text_or_text_array is given, current CalDom instance is returned.

Otherwise, an array of textContent from all elements in this CalDom instance is returned.

Examples:

val( val_or_val_array )

Get or set value property of elements in this CalDom instance.

Parameters:

val_or_val_array : String | Number | Array<String | Number>

(Optional) Value or array of values to to be set at corresponding n-th element.

If not given, array of values will be returned.

Returns:

{ CalDom | Array<String> } If val_or_val_array is given, current CalDom instance is returned.

Otherwise, array of value from all elements in this CalDom instance is returned.

Examples:

attr(...)

Get or set attribute(s) of elements in this CalDom instance.

Parameters:

key_or_key_values : String | Object

Attribute name as a String or { key: value, ... } object to set multiple attributes

val_or_val_array : any | Array<String | Number>

(Optional) Value or array of values to be assigned at corresponding n-th element.

If not given, an array of attribute values for the given key is returned.

Returns:

{ CalDom | Array<String | Number> } If it's a set request, current CalDom instance is returned.

Otherwise, an attribute value array from all elements in this CalDom instance is returned.

Examples:

prop(...)

Get or set variable(s) at elements' root. Other than assigning custom data of any type at elements' root, this can be used to access all properties/attributes of elements as well.

Warning: Look out for untracked circular references that might lead to memory leaks.

Parameters:

key_or_key_values : String | Object

Variable name as a String or { key: value, ... } object to set multiple variables.

val_or_val_array : any | Array

(Optional) Value or an array of values to be to be assigned at the corresponding n-th element. If empty, an array of given key's values is returned.

Returns:

{CalDom | Array} If it's a set request, this chainable CalDom instance is returned.

Otherwise, a value array from all elements in this CalDom instance is returned.

Examples:

data( key, value )

Store & retrieve { key: value, ... } data from elements in this CalDom instance.

(Data is stored at element._data. However, _data location might change over time, avoid accessing it directly as possible.)

Warning: Look out for untracked circular references that might lead to memory leaks.

Parameters:

key : Object

Can be any javascript Object, usually a string.

value : any | Array

(Optional) Can be any javascript Object or an array of Objects to be to be assigned at the corresponding n-th element.

Returns:

If it's a set request, this CalDom instance is returned.

Otherwise, an array of values for the provided key from all elements is returned.

css(....)

Get & set CSS style rule(s) of elements in this CalDom instance.

Parameters:

key_or_key_values : String | Object

CSS property name or { property: value, ... } object to set multiple rules

val_or_val_array : String | Number | Array<String | Number>

CSS value or an array of values to be set at n-th element for the given CSS property name.

Returns:

{CalDom | Array<String | Number>} If it's a set request, this CalDom instance is returned.

Otherwise, a CSS value array for the given property from all elements in this CalDom instance is returned.

Examples:

addClass(class_names)

Add class name(s) to elements in this CalDom instance.

Parameters:

class_names : String | Array

A single class name or multiple class names separated by spaces or as an array.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

removeClass(class_names)

Remove class name(s) from elements in this CalDom instance.

Parameters:

class_names : String | Array

A single class name or multiple class names separated by spaces or as an array.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

show([display_value])

Set display CSS property of all elements in this CalDom instance.

Parameters:

display_value : String

(optional) Display value. Default to "block"

Returns:

{CalDom} Returns this CalDom instance.

Examples:

hide()

Set display CSS property of all elements in this CalDom instance to "None".

Returns:

{CalDom} Returns this CalDom instance.

Examples:

on( event_names, handler, options )

Add event listeners to elements in this CalDom instance.

Parameters:

event_names : String | Array

A single event name or multiple event names separated by spaces or as an array.

handler : Function

Callback function to handle the event.

options : any

(Optional) options to pass into addEventListener's 3rd param.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

off( event_names, handler, options )

Remove event listeners from elements in this CalDom instance.

Parameters:

event_names : String | Array

A single event name or multiple event names separated by spaces or as an array.

handler : Function

Callback. The same callback provided at on() or Node.addEventListener() should be provided.

options : any

(Optional) options to pass into removeEventListener's 3rd param.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

append( elems_caldom_generator, ...elems )

Append/Move elements to first element of this CalDom instance.

Null and undefined inputs are silently ignored. Note that if you append an existing element, it is moved to the new destination (not cloning).

Parameters:

elems_or_caldom : Node | String | CalDom | Array | NodeList | HTMLCollection

First argument can be a CalDom instance, a Node/String or an array of Node/String.

Provided elements are added to the first element of this CalDom instance. This is XSS safe as it's considering String inputs as a text node. Use .html() or init _("<h1></h1>") to inject HTML.

See the examples for wide range of possibilities.

...elems : Node | String | CalDom

(Optional) If the first argument is not an array-type, all ...arguments are added to the first element of this CalDom instance.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

prepend( elems_or_caldom, before_elem_or_caldom )

Prepend/Move elements to the first element of this CalDom instance.

(Same as append(), except 2nd argument is reserved to identify before element.)

Null and undefined inputs are silently ignored. Note that if you prepend an existing element, it is moved to the new destination (not cloning).

Parameters:

elems_or_caldom : Node | CalDom | Array | NodeList | HTMLCollection

First argument can be a CalDom instance, a Node/String or an array of Node/String.

Items are prepended to the first element of this CalDom instance. This is XSS safe as it's considering String inputs as a text node. Use .html() or init _("<h1></h1>") to inject HTML.

before_elem_or_caldom : Node | String | CalDom

(Optional) If provided, items are inserted before this element instead before the firstChild.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

remove( )

Remove all elements of this CalDom instance from the DOM.

Returns:

{CalDom} Returns this empty CalDom instance.

Examples:

replace( render_callback )

Replaces current elements in this CalDom instance with new elements returned by the render_callback function.

The algorithm compares the elements(including their descendents) and only replace the nodes if there is a difference against the actual DOM. This is useful to minimize expensive browser layout repaints.

(This is the same algorithm used by react() to compare & apply DOM changes.)

Parameters:

render_callback : Function

Render function that returns the new replacement element.

The callback receieves 3 parameters: render_callback( current_elem: Node, index: Number, caldom_instance: CalDom ): {Node || CalDom}

Returns:

{CalDom} Returns this CalDom instance.

Examples:

react( state, config )

Make this CalDom instance a reactive component by providing a state, render() or an update() function. CalDom compares differences and efficiently updates relevant DOM Node changes.

Alternatively, this can be also achieved by extending _.Component class. See below examples.

Parameters:

state : any

(Optional, but requires to initiate) Any value or an { key: value, ... } object.

config : Object

(Optional, but requires to initiate) Reactive configuration containing render(), update(), etc.

config.render : Function

(Optional, if update() is given) Should return a CalDom or a Node/Element. Return false to terminate render process.

render(state: any, component: CalDom): receieves state and component(this CalDom instance) as arguments.

All CalDom methods & properties (find(), text(), css(), etc.) can be accessed via component. Eg: component.text("Hello World!").

CalDom sync child nodes (including text), attributes, CSS, value, checked, indeterminate, selected & _data property set by .data() between connected DOM & virtual DOM. It doesn't sync changed events & other custom properties directly attached to DOM nodes.

To force a complete Node replacement with all events and properties, set a different/incremental "caldom-v" attribute to the element at render() ("v" as in version of the element).

The first render() gets executed synchronously. After that, render() gets executed asynchronously through requestAnimationFrame when the state changes. This is asynchronous in both watched = true mode or when react() is called manually.

Calling react() with a brand new state execute render() synchronously. Eg: app.react( new_state );

config.update : Function

(Optional, if render() is given) If an update() function is present, CalDom will execute it instead of render(). (Intial render() get executed regardless).

Manually updating DOM changes directly by accessing the native DOM API is obviously more performant than a render() based virtual-DOM approach. Set this.$ references at render() to access the native Nodes directly. Refer this.$ below for details.

Also, update() is useful to update existing non-javascript originated(.html file based) HTML structures reactively.

If the update() returns true, CalDom will also execute render() and proceed with virtual-real DOM comparison and apply subsequent changes.

update(

 state: any,

 component: CalDom,

 state_changed_keys: Object,

 state_change_count: Number

);

Arguments are state, component(this CalDom instance) and batched and changed state key names & change count since the last render()/update().

state_changed_keys & state_change_count is useful to do select direct DOM changes or fall back to render() by returning true if the changes are complex.

state_changed_keys is an Object. Eg: { key_name: String, value_is_available: Boolean }. If the state change is a deletion, value_is_available is set to false.

state_changed_keys is one-dimentional. If the same key_name is used more than once at deeper levels of state object, only the last key-value change is represented here. Use this.state for deeper analysis.

If the same key change multiple times, state_change_count is increased accordingly. state_changed_keys & state_change_count are only populated in watched = true mode.

All CalDom methods & properties (find(), text(), css(), etc,) can be accessed via component. Eg: component.css( "color", "green" )

config.watched=true : Boolean

(Optional) True by default. If false, CalDom will not react to state changes automatically. Call react() manually after changing state.

CalDom is using Javascript Proxies to detect state changes. Browser versions released before 2016 may not support it natively. Implement a pollyfil for older browsers or you can call react() manually after state changes.

(Note that Proxy pollyfils require variable to be defined in advance.)

If Proxy is not supported caldom.watched will set to false despite initially setting config.watched = true.

If you chose not to use the Proxy for some reason, you can call .react() after state changes. There is no performance hit by calling it repetedly because CalDom is using requestAnimationFrame to batch DOM updates efficiently.

Also, calling react() manually immediately after state changes does not cause a re-render even when Proxy is supported.

config.willMount : Function

(Optional) Called before the Component is mounted(appended) into a parent Node. Receives current CalDom instance as the only argument.

config.didMount : Function

(Optional) Called after the Component is mounted(appended) into a parent Node. Receives current CalDom instance as the only argument.

config.willUnmount : Function

(Optional) Called before the Component is removed.

Callback receives two argument as callback( component: CalDom, [directly_removed: Boolean] ). directly_removed is set to true if removed directly by calling .remove().

Otherwise, set to undefined if removed while applying virtual-DOM changes to the real DOM. The removed component might get re-connected at a different position in the DOM tree.

config.didUnmount : Function

(Optional) Called after the Component is removed.

Callback receives two argument as callback( component: CalDom, [directly_removed: Boolean] ). directly_removed is set to true if removed directly by calling .remove().

Otherwise, set to undefined if removed while applying virtual-DOM changes to the real DOM. The removed component might get re-connected at a different position in the DOM tree.

config.didUpdate : Function

(Optional) Called after the Component is updated and/or rendered and all virtual-DOM changes applied to the real DOM. Receives current CalDom instance as the only argument.

Returns:

{CalDom} Returns this CalDom instance.

Examples:

watched: Boolean

Whether state changes are monitored in the reactive mode. See react() for details.

this.$: Object

Reference holder for CalDOM or Node appended to the DOM. This can be used to keep CalDOM or direct DOM Node references in sync even when the render() drastically modify the DOM structure.

If referenced, an attribute named "_ref" will be added to the Element. This is to track & update references when elements get moved/replaced by virtual-DOM diffing algorithm. (Sync requires native Proxy support or a polyfill).

Examples:

_.q( query, parent_node )

Call CalDom's query/create function directly using _.q(query)

Parameters:

query : String

"+tag" creates a new Element. '<tag></tag>' creates specified HTML structure. ~svg_tag" creates a SVG element. Otherwise it can be a CSS Selector or an XPath query starting with "$"

parent_node : Node

Treated as the parent node for CSS selector or XPath query. Otherwise, default to window.document

Returns:

{Array<Node>} Array with the new element or NodeList returned by the CSS selector or the XPath query

(Shorten for query())

Examples:

_.Component.prototype

Extend and modify core functionality of CalDom by accessing its prototype.

Examples:

_.version: String

Get version of CalDom.

Examples:

_cal_dom_alias

CalDOM is using '_' variable as a global short-hand by default. To use a different alias, set window['_cal_dom_alias'] before loading CalDOM.

Examples: