DomHelper
Helps with dom querying and manipulation.
DomHelper.createElement({
tag : 'div',
className : 'parent',
style : 'background: red',
children : [
{ tag: 'div', className: 'child' },
{ tag: 'div', className: 'child' }
]
});
Properties
6
Properties
6Returns active element checking shadow dom too
Set to false to not show focus renditions when using keyboard
Check if the currently used theme is a dark theme, by checking the color-scheme property of .b-widget.
Get the current themes primary color (by reading it from document.body)
Measures the scrollbar width using a hidden div. Caches result
A theme information object about the current theme. Note, when using Bryntum widgets inside a shadowRoot context, you need to use getThemeInfo and pass a context element.
Functions
67
Functions
67Creation
Appends element to parentElement.
| Parameter | Type | Description |
|---|---|---|
parentElement | HTMLElement | Parent element |
elementOrConfig | HTMLElement | DomConfig | String | Element to insert, or an element config passed on to
|
Creates an Element, accepts a DomConfig object. Example usage:
DomHelper.createElement({
tag : 'table', // defaults to 'div'
className : 'nacho',
html : 'I am a nacho',
children : [ { tag: 'tr', ... }, myDomElement ],
parent : myExistingElement // Or its id
style : 'font-weight: bold;color: red',
dataset : { index: 0, size: 10 },
tooltip : 'Yay!',
ns : 'http://www.w3.org/1999/xhtml'
});
| Parameter | Type | Description |
|---|---|---|
config | DomConfig | Element config object |
options | Object | An object specifying creation options. If this is a boolean value, it is
understood to be the |
options.ignoreRefs | Boolean | Pass |
options.returnAll | Boolean | Specify true to return all elements & child elements created as an array. |
Single element or array of elements returnAll was set to true.
If any elements had a reference property, this will be an object containing a reference to
all those elements, keyed by the reference name.
Inserts an element before beforeElement in into.
| Parameter | Type | Description |
|---|---|---|
into | HTMLElement | Parent element |
element | HTMLElement | Element to insert, or an element config passed on to createElement() |
beforeElement | HTMLElement | Element before which passed element should be inserted |
Inserts an element at first position in into.
| Parameter | Type | Description |
|---|---|---|
into | HTMLElement | Parent element |
element | HTMLElement | Element to insert, or an element config passed on to createElement() |
CSS
Adds a CSS class to an element during the specified duration
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Target element |
cls | String | CSS class to add temporarily |
duration | Number | Duration in ms, 0 means cls will not be applied |
delayable | Delayable | Window | The delayable to tie the setTimeout call to |
Applies specified style to the passed element. Style can be an object or a string.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Target element |
style | String | Object | Style to apply, 'border: 1px solid black' or { border: '1px solid black' } |
overwrite | Boolean | Specify |
Adds or removes the classes contained in the specified object based on the truthy or falsy value of that key.
For example:
DomHelper.assignClasses(element, {
'b-class-one' : 1 // class will be added
'b-class-two' : 0 // class will be removed
});
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
classes | Object | An object whose truthy keys will be added as classes and whose falsy keys will be removed. |
Returns a style value or values for the passed element.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to read styles from |
propName | String | String[] | The property or properties to read |
inline | Boolean | Pass as |
The value or an object containing the values keyed by the requested property name.
Replaces the passed element's className with the class names
passed in either Array or String format or Object.
This method compares the existing class set with the incoming class set and avoids mutating the element's class name set if possible.
This can avoid browser style invalidations.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element whose class list to synchronize. |
newClasses | String[] | String | Object | The incoming class names to set on the element. |
true if the DOM class list was changed.
Toggle multiple classes in elements classList. Helper for toggling multiple classes at once.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
classes | String[] | |
force | Boolean | Specify true to add classes, false to remove. Leave blank to toggle |
Applies the key state of the passed object or DomClassList to the passed element.
Properties with a falsy value mean that property name is removed as a class name.
Properties with a truthy value mean that property name is added as a class name.
This is different from syncClassList. That sets the className of the element to the
sum of all its truthy keys, regardless of what the pre-existing value of the className was, and ignoring falsy
keys.
This selectively updates the classes in the className. If there is a truthy key, the name is added. If there
is a falsy key, the name is removed.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to apply the class list to . |
classes | Object | DomClassList | The classes to add or remove. |
true if the DOM class list was changed.
Measure
Measures a relative size, such as a size specified in em units for the passed element.
| Parameter | Type | Description |
|---|---|---|
size | String | The CSS size value to measure. |
sourceElement | HTMLElement | |
round | Boolean | Pass true to return exact width, not rounded value |
size The size in pixels of the passed relative measurement.
Measures the text width using a hidden div
| Parameter | Type | Description |
|---|---|---|
text | String | |
sourceElement | HTMLElement |
width
Other
Adds the passed string as a space-separated value to the passed attribute in the passed element.
| Parameter | Type | Description |
|---|---|---|
elementOrSelector | String | Element | The element to manipulate |
attribute | String | The attruibute name |
value | String | The value to add |
Align the passed element with the passed target according to the align spec.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to align. |
target | HTMLElement | Rectangle | The target element or rectangle to align to |
alignSpec | AlignSpec | See alignTo Defaults to |
round | Boolean | Round the calculated Rectangles (for example if dealing with scrolling which is integer based). |
method | String | The method to use to align the element. Can be |
Resolves element from point, checking shadow DOM if required
| Parameter | Type | Description |
|---|---|---|
x | Number | |
y | Number |
Returns element animations
| Parameter | Type | Description |
|---|---|---|
element | Element | Document | |
args | * |
Returns an object with the parse style values for the top, right, bottom, and left components of the given edge style.
The return value is an object with top, right, bottom, and left properties
for the respective components of the edge style, as well as width (the sum of
left and right) and height (the sum of top and bottom).
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
edgeStyle | String | The element's desired edge style such as 'padding', 'margin', or 'border'. |
edges | String | A string with one character codes for each edge. Only those edges will be populated in the returned object. By default, all edges will be populated. |
Returns the specified element of the given event. If the event is an Element, it is returned. Otherwise,
the eventName argument is used to retrieve the desired element property from event (this defaults to the
'target' property).
| Parameter | Type | Description |
|---|---|---|
event | Event | Element | |
elementName | String |
Returns the id of the passed element. Generates a unique id if the element does not have one.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to return the |
Gets theme information about the currently active theme by reading CSS custom properties from the
.b-theme-info class.
The method creates a temporary DOM element with the b-theme-info class, reads all CSS custom properties
that start with --b-theme-, converts them to camelCase property names, and returns them as an object.
Example theme CSS:
.b-theme-info {
--b-theme-name : "SvalbardDark";
--b-theme-filename : "svalbard-dark";
--b-theme-button-rendition : "text";
--b-theme-label-position : "align-before";
--b-theme-overlap-label : "false";
}
This would result in:
{
name : "SvalbardDark",
filename : "svalbard-dark",
buttonRendition : "text",
labelPosition : "align-before",
overlapLabel : false // String "true"/"false" values are converted to boolean
}
| Parameter | Type | Description |
|---|---|---|
contextElement | HTMLElement | The element for which to find the theme. If using a web component / shadowRoot, the theme will be encapsulated in the web component's encapsulated style so a context element is required. If no web components are in use, this may be omitted. |
Current theme information object with properties read from CSS custom properties
This method checks that the passed target is visible in all viewports
| Parameter | Type | Description |
|---|---|---|
target | HTMLElement | Event | The HTML element or Event to check for intersection with all ancestor viewports. |
docRect | Rectangle | Window rectangle |
method | contains | intersect | 'contains' or 'intersect' |
Highlights the passed element or Rectangle according to the theme's highlighting rules. Usually an animated framing effect.
The framing effect is achieved by adding the CSS class b-fx-highlight which references
a keyframes animation named b-fx-highlight-animation. You may override the animation
name referenced, or the animation itself in your own CSS.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Rectangle | The element or Rectangle to highlight. |
Checks if an element clips its content.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to check. |
Returns true if the element clips its content.
Returns true if element has opened shadow root
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Element to check |
Returns true if the passed element accepts keystrokes to edit its contents.
Returns true if the passed element is editable.
Returns true if the provided value is likely a DOM element. If the element can be assured to be from the
same document, instanceof Element is more reliable.
| Parameter | Type | Description |
|---|---|---|
value | * |
Returns true if the passed element is focusable either programmatically or through pointer gestures.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to test. |
Returns true if the passed element is focusable
Returns the rectangle of the element or event which is currently visible in the browser viewport,
i.e. user can find it on screen, or false if it is scrolled out of view.
| Parameter | Type | Description |
|---|---|---|
target | HTMLElement | Event | The element or event to test. |
whole | Boolean | Whether to check that whole element is visible, not just part of it.
If this is passed as true, the result will be a boolean, |
Returns the rectangle of the element which is currently visible in
the browser viewport, or false if it is out of view.
Returns true if the provided value is likely a DOM node. If the node can be assured to be from the same
document, instanceof Node is more reliable.
| Parameter | Type | Description |
|---|---|---|
value | * |
Returns true if the passed element is deeply visible. Meaning it is not hidden using display
or visibility and no ancestor node is hidden.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to test. |
true if deeply visible.
Converts the passed id to an id valid for usage as id on a DOM element.
| Parameter | Type | Description |
|---|---|---|
id | String |
Splits a style string up into object form. For example 'font-weight:bold;font-size:150%'
would convert to
{
font-weight : 'bold',
font-size : '150%'
}
| Parameter | Type | Description |
|---|---|---|
style | String | A DOM style string |
the style declaration in object form.
Removed the passed string as a space-separated value from the passed attribute in the passed element.
| Parameter | Type | Description |
|---|---|---|
elementOrSelector | String | Element | The element to manipulate |
attribute | String | The attruibute name |
value | String | The value to remove |
Removes the passed CSS classes from all descendants of the passed element.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Outermost element |
classes | String | CSS classes to remove |
Resets DomHelper.scrollBarWidth cache, triggering a new measurement next time it is read
Sets a CSS length style value.
| Parameter | Type | Description |
|---|---|---|
element | String | HTMLElement | The element to set the style in, or, if just the result is required, the style magnitude to return with units added. If a nullish value is passed, an empty string is returned. |
style | String | The name of a style property which specifies a length |
value | Number | String | The magnitude. If a number is used, the value will be set in |
The style value string.
Changes the theme to the passed theme name if possible.
Theme names are case-insensitive. The href used is all lower case.
To use this method, the <link rel="stylesheet"> must use the default, Bryntum-supplied CSS files where the
href end with <themeName>.css, so that it can be found in the document, and switched out for a new link with
the modified href. The new href will use the same path, just with the themeName portion substituted for
the new name.
If no <link> with that name pattern can be found, an error will be thrown.
If you use this method, you must ensure that the theme files are all accessible on your server.
Because this is an asynchronous operation, a Promise is returned. The theme change event is passed to the
success function. If the theme was not changed, because the theme name passed is the current theme, nothing is
passed to the success function.
The theme change event contains two properties:
prevThe previous Theme name.themeThe new Theme name.
| Parameter | Type | Description |
|---|---|---|
newThemeName | String | the name of the theme that should be applied |
A promise whose success callback receives the theme change event if the theme in fact changed.
If the theme href could not be loaded, the failure callback is called, passing the error event caught.
Animates the specified element to slide it into view within the visible viewport of its parentElement from the direction of movement.
So in a left-to-right Widget, direction 1 means it slides in from the right
and direction -1 means it slides in from the left. RTL reverses the movement.
See the forward/backward navigations in DatePicker for an example of this in action.
If "next" should arrive from below and "previous" should arrive from above, add the
class b-slide-vertical to the element.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The element to slide in. |
direction | Number |
If the element is inside an RTL widget the directions are reversed. |
Sync one source element attributes, children etc. to a target element. Source element can be specified as a html string or an actual HTMLElement.
NOTE: This function is superseded by DomSync.sync(), which works with DOM configs. For most usecases, use it instead.
| Parameter | Type | Description |
|---|---|---|
sourceElement | String | HTMLElement | Source "element" to copy from |
targetElement | HTMLElement | Target element to apply to, can also be specified as part of the config object |
Returns the updated targetElement (which is also updated in place)
Toggles between light and dark themes. Does not check if the theme is actually available, only does a string replace on the current themes filename for light/dark.
Position, get
Get elements X offset within a containing element
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
container | HTMLElement |
X offset
Gets elements X and Y offset within containing element as an array [x, y]
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
container | HTMLElement |
[x, y]
Get elements Y offset within a containing element
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
container | HTMLElement |
Y offset
Get elements X position on page
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement |
Get elements Y position on page
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement |
Returns the x from the element's translate: x y value in pixels.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement |
X translate
Gets both X and Y coordinates as an array [x, y]
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement |
[x, y]
Returns the y from the element's translate: x y value in pixels.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement |
Y coordinate
Position, set
Increase X translation
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
x | Number | The number of pixels by which to increase the element's |
Increase Y position
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
y | Number | The number of pixels by which to increase the element's |
Set element's style left.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
x | Number | String | The top position. If numeric, |
Set element's style top.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
y | Number | String | The top position. If numeric, |
Set element's X translation in pixels (keepin Y translation as is).
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
x | Number | The value by which the element should be translated from its default position. |
Set elements X and Y translation in pixels.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
x | Number | The `X translation. |
y | Number | The `Y translation. |
Set element's Y translation in pixels (keeping X translation as is).
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
y | Number | The value by which the element should be translated from its default position. |
Query children
Returns all child elements (not necessarily direct children) that matches selector.
If selector starts with '>' or '# ', then all components of the selector must match inside of element.
The scope selector, :scope is prepended to the selector (and if # was used, it is removed).
These are equivalent:
DomHelper.children(el, '# .foo .bar');
el.querySelectorAll(':scope .foo .bar');
These are also equivalent:
DomHelper.children(el, '> .foo .bar');
el.querySelectorAll(':scope > .foo .bar');
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The parent element |
selector | String | The CSS selector |
Matched elements, somewhere below element
Looks at the specified element and all of its children for the one that first matches selector.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Parent element |
selector | String | CSS selector |
Matched element, either element or an element below it
Iterates over the direct child elements of the specified element.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Parent element |
fn | function | Function called for each child element |
Iterates over each result returned from element.querySelectorAll(selector). Can also be called with only two
arguments, in which case the first argument is used as selector and document is used as the element.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Parent element |
selector | String | CSS selector |
fn | function | Function called for each found element |
Gets the first direct child of element that matches selector.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Parent element |
selector | String | CSS selector |
Checks if element has any child that matches selector.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Parent element |
selector | String | CSS selector |
true if any child matches selector
Checks if childElement is a descendant of parentElement (contained in it or a sub element)
| Parameter | Type | Description |
|---|---|---|
parentElement | HTMLElement | Parent element |
childElement | HTMLElement | Child element, at any level below parent (includes nested shadow roots) |
Removes each element returned from element.querySelectorAll(selector).
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | |
selector | String |
Query parents
Retrieves all parents to the specified element.
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Element |
All parent elements, bottom up
Typedefs
3
Typedefs
3An object that describes a DOM element. Used for example by createElement() and by DomSync.sync().
DomHelper.createElement({
class : {
big : true,
small : false
},
children : [
{ tag : 'img', src : 'img.png' },
{ html : '<b style="color: red">Red text</b>' }
]
});
| Parameter | Type | Description |
|---|---|---|
tag | String | Tag name, for example 'span'. If this is passed as |
parent | HTMLElement | Parent element |
nextSibling | HTMLElement | Element's next sibling in the parent element |
class | String | Object | CSS classes, as a string or an object (truthy keys will be applied) |
className | String | Object | Alias for |
style | String | Object | Style, as a string or an object (keys will be hyphenated) |
elementData | Object | Data object stored as an expando on the resulting element |
dataset | Object | Dataset applied to the resulting element |
children | DomConfig[] | Object<String, DomConfig> | String[] | HTMLElement[] | Child elements, as an array that can include
DomConfigs that will be turned into elements, plain strings that will be used as text nodes or existing elements that
will be moved. Also accepts an object map of DomConfigs, but please note that it cannot be used with
|
html | String | Html string, used as the resulting elements |
tooltip | TooltipConfig | String | Tooltip config applied to the resulting element |
text | String | Text content, XSS safe when you want to display text in the element. Mutually exclusive with the |
id | String | Element's |
href | String | Element's |
ns | String | Element's namespace |
src | String | Element's |
A theme information object about the current theme.
Theme information object containing metadata about the active theme.
The theme information is read from CSS custom properties defined in the .b-theme-info class.
Example CSS:
.b-theme-info {
--b-theme-name : "SvalbardDark";
--b-theme-filename : "svalbard-dark";
--b-theme-button-rendition : "text";
--b-theme-label-position : "align-before";
--b-theme-overlap-label : "false";
}
| Parameter | Type | Description |
|---|---|---|
name | String | The display name of the current theme (e.g., "SvalbardDark") |
filename | String | The filename/identifier of the theme (e.g., "svalbard-dark") |
buttonRendition | String | The button rendering style for the theme (e.g., "text", "outlined", "filled") |
labelPosition | String | The default label position for form fields (e.g., "align-before", "above") |
overlapLabel | Boolean | Whether labels should overlap with fields |
An object that describes a vue component configuration.
{
vue : true
is : 'VueWidget'
bind : { prop1: 'value1', prop2: 'value2' }
on : { myclick: handleMyClick }
}
| Parameter | Type | Description |
|---|---|---|
vue | Boolean | Mandatory flag to mark as vue configuration. Should be set to |
is | String | Name of the Vue component |
bind | Object | Object with properties to bind to the Vue Component |
on | Object | Object with event listeners. Events emitted by Vue Component |