View Reference

Table of contents


Introduction

The View is accessed using the view object. The view restricts the viewport to be within the "safe area": A rectangular area that does not include any cutouts for cameras or other devices.

The view attaches to a DOM node with an ID of root. This must be defined in the app's index.html file before the view is initialized.

Standard colors

The View defines the following custom properties:

These properties are adjusted when switching between dark mode and light mode. The application should use these properties when appropriate.

Device size classes

The application can choose a set of device size breakpoints to use in its CSS. A size class is associated with each range of sizes between the breakpoints. For example, if the breakpoints are viewport widths of 512 and 1024 then the class large can be associated with widths ≥ 1024, the class medium can be associated with widths ≥ 512 and < 512, and the class small can be associated with widths < 512.

Once the size classes are defined, the view can set up useful class strings. For example, if the device size is medium, then the view can set up the following CSS classes in the root node:

See the view.setSizeClass() method.


Exported identifiers

view

Syntax

import {view} from 'common/mvcs/export.js';

The view object contains the properties and methods used to communicate with the View.


view methods

appendNodes()

Syntax

view.appendNodes(parent, nodes)

Appends child DOM nodes to a parent DOM node. The nodes array will be flattened and compacted. Elements may be strings or DOM nodes. If an element is a string, it is converted to a DOM test node after having any custom character entities processed (See Character Entities).

Parameters


createNode()

Syntax

view.createNode(tag, props, children)

Returns a DOM node with the specified properties and children. Properties starting with 'data-' are set as node data properties. The following attributes are converted to their equivalent DOM properties:

Parameters


defineComponentTag()

Syntax

view.defineComponentTag(tag, component)

Associate a component tag string with its Component subclass. Once defined, the tag may be used in TML strings. Otherwise, the TML template would have to import and refer to the component itself.

Parameters


doSync()

Syntax

view.doSync()

Synchronize the entire view from the root. Calls the sync() method on the root Component. This will traverse the Component tree calling the sync() method as required to cause the Components to update the displayed elements to the current state variable state.

Unlike view.sync(), this synchronizes the View immediately.


getComponentById()

Syntax

view.getComponentById(id)

Return the component given the ID.

Most Components generate DOM elements containing the Component ID in the data-cid attribute. Use this ID with view.getComponentById() in the debugger console to explore the Component controlling the DOM element.

Return value

A Component.


getComponentClass()

Syntax

view.getComponentClass(tag)

Return the component class for a given component tag string. See defineComponentTag()

Parameters

Return value

A Component.


resize()

Syntax

view.resize()

Notify the entire View that the viewport has been resized.


setSizeClass()

Syntax

view.setSizeClass(curSizeClass, sizeClasses)

Sets the size class CSS strings in the root node. The sizeClasses parameter is an array of the possible size class strings ordered from largest to smallest. The curSizeClass is the viewport's current size class. The setSizeClass() method will set a series of CSS class strings of the form prefix-sizeClass. The sizeCLass is one of the size class strings and the prefix is one of:

Parameters


sync()

Syntax

view.sync()

Schedule a synchronization of the entire View. This can be called multiple times and the synchronization, using view.doSync()).


toText()

Syntax

view.toText(template)

Extracts the text from anything that the view accepts as a template (strings, arrays or templates).

Parameters


view properties

bottom

The viewport offset from the bottom of the screen in px.

Value

A number.


hasSafeArea

True if the device's viewport has a "safe area": A rectangular area that excludes any intrusions on the viewport.

Value

A Boolean.


rootNode

The current root DOM node of the View.

Value

A DOM node


height

The viewport height in px.

Value

A number


left

The viewport offset from the left of the screen in px.

Value

A number.


The viewport offset from the right of the screen in px.

Value

A number.


top

The viewport offset from the top of the screen in px.

Value

A number.


width

The viewport width in px.

Value

A number.