Caplin Trader 4.0.3

Class: module:br/util/ElementUtility

module:br/util/ElementUtility

Summary

This class provides static, browser agnostic, utility methods for DOM interactions such as adding / removing event listeners, adjusting CSS classes, finding element positions etc.

Constructor

new module:br/util/ElementUtility()

Methods

(static) addAndRemoveClassNames(element, classesToAdd, classesToRemove)

Adds and/or removes the specified class names from the specified element. This operation is performed in a single DOM action, making this more efficient than adding/removing the classes individually. If a class exists in both the add and remove lists, the class will be added to the element.
Parameters:
Name Type Description
element Element The HTML DOM element to make the class changes to.
classesToAdd Array.<String> The list of class names that will be added to the list of existing classes.
classesToRemove Array.<String> The list of class names that will be removed from the list of existing classes.

(static) addClassName(element, className)

Adds the specified class name to the list of CSS classes on the given element, if the class does not already exist.
Parameters:
Name Type Description
element Element The HTML DOM element to add the CSS class to.
className String The class name that will be added to the list of existing classes.

(static) discardChild(firstElement)

Discards one or more elements (all arguments passed will be discarded) by removing children, and removing it from any parentNode.
Parameters:
Name Type Description
firstElement Element First Element DOM Element

(static) getAncestorElementWithClass(element, className) → {DOMElement}

Returns the first element that contains the given class as part of its className string.
Parameters:
Name Type Description
element Element the element to start the search at.
className String the class name to look for.
Returns:
The ancestor element with the specified class, or null.
Type
DOMElement

(static) getElementsByClassName(domElement, tagName, className) → {Array.<DOMElement>}

Returns an array of DOM elements that match the specified tag name and class name.
Parameters:
Name Type Description
domElement DOMElement The DOM element that should be used as the root of the search.
tagName String The tag name (can be *) of elements to search for.
className String The CSS class name of the elements to search for.
Returns:
An array of elements that match the specified criteria.
Type
Array.<DOMElement>

(static) getNodeIndex(element)

Returns the node index of the element as it exists in the parent.
Parameters:
Name Type Description
element Element The element to get the index for.
Throws:
Error If the specified element does not have a parent.
Returns:
the node index

(static) getPosition(elem)

Returns the absolute position of the element relative to the window in pixels. The position also takes into account any scrolling of parents.
Parameters:
Name Type Description
elem DOMElement The DOM element to calculate the position of
Returns:
\{left:x,top:y\}

(static) getScrollOffset(elem) → {Object}

Returns the scrolled offset of the element (if any) in an object containing a left and top properties.
Parameters:
Name Type Description
elem Element The DOM element to calculate the scrolled offset of.
Returns:
\{left:x,top:y\}
Type
Object

(static) getSize(elem)

Returns the bounding rectangle of the specified element.
Parameters:
Name Type Description
elem Object The element to get the bounding rectangle for.
Returns:
\{left:x,right:y\}

(static) hasClassName(element, className) → {boolean}

Returns TRUE if the specified class name exists on the element.
Parameters:
Name Type Description
element Element The DOMElement to check.
className String The class name to check.
Returns:
TRUE if the specified class name exists on the element.
Type
boolean

(static) insertAfter(element, referenceElement)

Inserts the specified node immediately after the reference element.

This convenience method saves the programmer from having to determine whether to call insertBefore() or appendChild(), depending on whether the reference element is the last child node.

Parameters:
Name Type Description
element Element The element to insert.
referenceElement Element The reference element to insert the element after.

(static) isAncestorOfElement(possibleAncestor, possibleChildElement) → {boolean}

Checks to see if the specified ancestor element contains the specified child element.
Parameters:
Name Type Description
possibleAncestor Element the element that is presumed to be a parent node.
possibleChildElement Element the element to start the search at.
Returns:
TRUE if the specified ancestor element contains the child element.
Type
boolean

(static) removeChild(childElement)

Removes the specified child from its parent.
Parameters:
Name Type Description
childElement Element The child to remove.

(static) removeClassName(element, className)

Removes the CSS class name from the specified element.
Parameters:
Name Type Description
element Element The HTML element that the class name should be removed from.
className String The CSS class to remove from the element.

(static) replaceClassName(element, currentClassName, newClassName)

Replaces the specified CSS class name on the DOM element with another class.
Parameters:
Name Type Description
element Element The HTML DOM element to add the class to.
currentClassName String The class name to replace.
newClassName String The new name of the class.

(static) setInnerHtml(element, htmlToSet)

Sets the innerHTML of the specified element in an efficient way.
Parameters:
Name Type Description
element Element the element on which innerHTML needs to be set.
htmlToSet String The HTML that will be set in the element.

(static) setNodeText(element, textToSet)

Sets the text contents of the specified element in an efficient way. This function should only be used for setting plain text contents.
Parameters:
Name Type Description
element Element The element on which to set the new text content.
textToSet String Text content to set on the element.