view methods
view properties
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.
The View defines the following custom properties:
--main-text-color--main-text-warning-color--main-text-caution-color--main-text-bg-color--main-text-color-sel--main-text-bg-color-sel--main-fill-color--main-input-color--main-input-bg-color--main-input-border-color--main-output-color--main-disabledInput-color--main-disabledInput-bgColor--main-wrapper-haze--main-button-text-color--main-button-text-bg-color--main-dialog-border-color--main-list-border-color--rounded-corner-bufferThese 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.
view.css also defines the following classes and rules:
.warningColor, .warning: Text in the warning color..cautionColor, .caution: Text in the caution color..okColor, .ok: Text in green..input: Gives an element the border and padding of an input..input-disabled: Gives an element the disabled input colors..inputButton: A small button floated to the right of an input.#root safe-area padding: #root is padded by the device's safe-area insets (constant() before iOS 11.2, env() after)..nativeAndroid #root: Removes the #root padding because the native Android WebView is already inside the safe area. The app sets the nativeAndroid class on the html element in its app.js.#root has a font size of min(calc(10px + 0.7vmin), 17px), which an app may override in its app.js.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:
viewSizemediumviewSizeGE-mediumviewSizeGE-smallviewSizeLT-largeNote that the current-size class has no hyphen: it is viewSize followed directly by the size class name. See the view.setSizeClass() method.
viewimport {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 methodsaddRootComponent()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().
componentOrTemplateappendNodes()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).
parentnodescreateNode()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:
class is converted to classNamerowspan is converted to rowSpancolspan is converted to colSpantagprops (optional)children (optional)view.appenNodes(). If a string, it is used to set the node's innerHTML property.defineComponentTag()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.
tagcomponentdoSync()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()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.
A Component.
getComponentClass()view.getComponentClass(tag)
Return the component class for a given component tag string. See defineComponentTag()
tagA Component.
init()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()view.resize()
Notify the entire View that the viewport has been resized.
setSizeClass()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:
viewSize (no hyphen)viewSizemedium CSS class would apply only when the viewport size falls into the medium size class.viewSizeLT-viewSizeLT-medium CSS class would apply only when the viewport size is smaller than medium.viewSizeGE-viewSizeGE-medium CSS class would apply only when the viewport size is medium or larger.curSizeClasssizeClassessync()view.sync()
Schedule a synchronization of the entire View. This can be called multiple times and the synchronization, using view.doSync()).
toText()view.toText(template)
Extracts the text from anything that the view accepts as a template (strings, arrays or templates).
templateview propertiesbottomThe viewport offset from the bottom of the screen, as the computed style value of the root node.
A string including units, for example '0px'.
hasSafeAreaTrue if the device's viewport has a "safe area": A rectangular area that excludes any intrusions on the viewport.
A Boolean.
isInitializedTrue once view.init() has completed.
A Boolean.
rootNodeThe current root DOM node of the View.
A DOM node
heightThe viewport height in px.
A number
leftThe viewport offset from the left of the screen, as the computed style value of the root node.
A string including units, for example '0px'.
rightThe viewport offset from the right of the screen, as the computed style value of the root node.
A string including units, for example '0px'.
topThe viewport offset from the top of the screen, as the computed style value of the root node.
A string including units, for example '0px'.
widthThe viewport width in px.
A number.