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 .darkMode CSS class overrides the text, background, fill, input, output, disabled-input, and haze colors. The application should use these properties when appropriate.

Utility CSS classes

view.css also defines the following classes and rules:

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 < 1024, 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:

Note that the current-size class has no hyphen: it is viewSize followed directly by the size class name. 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. It is also available as the global window.view for debugging.


view methods

addRootComponent()

Syntax

view.addRootComponent(componentOrTemplate)

Adds a Component class or a Template object to the root. A Component class is wrapped in a Template. Anything else must be a Template object, not a string or an array. Fails an assertion if called after view.init().

Parameters


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. Each property is assigned as a DOM property on the node (not set as an HTML attribute). 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.


init()

Syntax

view.init()

Initializes the entire View. Called at device ready. Builds the Component tree from the root components added with view.addRootComponent(), mounts it on the root node, and synchronizes it. Asserts that at least one root component has been added. Sets view.isInitialized to true.


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 method first removes any existing root node classes beginning with viewSize, then sets a series of CSS class strings of the form prefixsizeClass. It fails an assertion if curSizeClass is not in sizeClasses. 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, as the computed style value of the root node.

Value

A string including units, for example '0px'.


hasSafeArea

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

Value

A Boolean.


isInitialized

True once view.init() has completed.

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, as the computed style value of the root node.

Value

A string including units, for example '0px'.


The viewport offset from the right of the screen, as the computed style value of the root node.

Value

A string including units, for example '0px'.


top

The viewport offset from the top of the screen, as the computed style value of the root node.

Value

A string including units, for example '0px'.


width

The viewport width in px.

Value

A number.