Support Menu

Class Dom

Class Dom

This is essentially an helper class for dom manipulation.
This is intended to provide some basic functionality normally offered by jQuery.
To minimize the multiple jQuery conflict we have while integrating in various system, we implemented the very small subset that the framework needs.
See $$, which is a function that wraps this class constructor, for less verbose code.

Index

Methods

addClass

  • addClass(classNames: string[]): void
  • Add a class to the element. Takes care of not adding the same class if the element already has it.

    Parameters

    • classNames: string[]

    Returns void

append

  • append(element: HTMLElement): void
  • Adds the element to the children of the current element

    Parameters

    • element: HTMLElement

      The element to append

    Returns void

children

  • children(): HTMLElement[]

clone

  • clone(deep?: boolean): Dom
  • Clone the node

    Parameters

    • Default value deep: boolean = false

      true if the children of the node should also be cloned, or false to clone only the specified node.

    Returns Dom

closest

  • closest(className: string): HTMLElement
  • Get the first element that matches the classname by testing the element itself and traversing up through its ancestors in the DOM tree.

    Stops at the body of the document

    Parameters

    • className: string

      A CSS classname

    Returns HTMLElement

css

  • css(property: string): string
  • Get the css value of the specified property.

    Parameters

    • property: string

      The property

    Returns string

detach

  • detach(): void

empty

  • empty(): void

find

  • find(selector: string): HTMLElement
  • Find a child element, given a CSS selector

    Parameters

    • selector: string

      A CSS selector, can be a .className or #id

    Returns HTMLElement

findAll

  • findAll(selector: string): HTMLElement[]
  • Find all children that match the given CSS selector

    Parameters

    • selector: string

      A CSS selector, can be a .className

    Returns HTMLElement[]

findClass

  • findClass(className: string): HTMLElement[]
  • Find the child elements using a className

    Parameters

    • className: string

      Class of the childs elements to find

    Returns HTMLElement[]

findId

  • findId(id: string): HTMLElement
  • Find an element using an ID

    Parameters

    • id: string

      ID of the element to find

    Returns HTMLElement

getAttribute

  • getAttribute(name: string): string
  • Returns the value of the specified attribute.

    Parameters

    • name: string

      The name of the attribute

    Returns string

getClass

  • getClass(): string[]
  • Return an array with all the classname on the element. Empty array if the element has not classname

    Returns string[]

hasClass

  • hasClass(className: string): boolean
  • Check if the element has the given class name

    Parameters

    • className: string

      Classname to verify

    Returns boolean

height

  • height(): number

hide

  • hide(): void

insertAfter

  • insertAfter(refNode: HTMLElement): void
  • Insert the current node after the given reference node

    Parameters

    • refNode: HTMLElement

    Returns void

insertBefore

  • insertBefore(refNode: HTMLElement): void
  • Insert the current node before the given reference node

    Parameters

    • refNode: HTMLElement

    Returns void

is

  • is(selector: string): boolean
  • Check if the element match the selector.
    The selector can be a class, an id or a tag.
    Eg : .is('.foo') or .is('#foo') or .is('div').

    Parameters

    • selector: string

    Returns boolean

isDescendant

  • isDescendant(parent: HTMLElement): boolean
  • Check if the element is a descendant of parent

    Parameters

    • parent: HTMLElement

    Returns boolean

isEmpty

  • isEmpty(): boolean
  • Check if the element is "empty" (has no innerHTML content). Whitespace is considered empty

    Returns boolean

nodeListToArray

  • nodeListToArray(nodeList: NodeList): HTMLElement[]

off

  • off(types: string[], eventHandle: function): void
  • Remove an event handler on the element. Accepts either one (a string) or multiple (Array) event type.

    Parameters

    • types: string[]

      The {string} or {Array} of types on which to remove an event handler

    • eventHandle: function

      The function to remove on the element

        • (evt: Event, arg?: any): void
        • Parameters

          • evt: Event
          • Optional arg: any

          Returns void

    Returns void

offset

  • offset(): IOffset

offsetParent

  • offsetParent(): HTMLElement
  • Returns the offset parent. The offset parent is the closest parent that is positioned. An element is positioned when its position property is not 'static', which is the default.

    Returns HTMLElement

on

  • on(types: string[], eventHandle: function): void
  • Bind an event handler on the element. Accepts either one (a string) or multiple (Array) event type.

    Parameters

    • types: string[]

      The {string} or {Array} of types on which to bind an event handler

    • eventHandle: function

      The function to execute when the event is triggered

        • (evt: Event, data: any): void
        • Parameters

          • evt: Event
          • data: any

          Returns void

    Returns void

one

  • one(types: string[], eventHandle: function): void
  • Bind an event handler on the element. Accepts either one (a string) or multiple (Array) event type.
    The event handler will execute only ONE time.

    Parameters

    • types: string[]

      The {string} or {Array} of types on which to bind an event handler

    • eventHandle: function

      The function to execute when the event is triggered

        • (evt: Event, args?: any): void
        • Parameters

          • evt: Event
          • Optional args: any

          Returns void

    Returns void

parent

  • parent(className: string): HTMLElement
  • Get the first element that matches the classname by testing the element itself and traversing up through its ancestors in the DOM tree.

    Stops at the body of the document

    Parameters

    • className: string

    Returns HTMLElement

parents

  • parents(className: string): HTMLElement[]
  • Get all the ancestors of the current element that match the given className

    Return an empty array if none found.

    Parameters

    • className: string

    Returns HTMLElement[]

position

  • position(): IOffset

prepend

  • prepend(toPrepend: HTMLElement): void
  • Insert the given node as the first child of the current node

    Parameters

    • toPrepend: HTMLElement

    Returns void

remove

  • remove(): void

removeClass

  • removeClass(className: string): void
  • Remove the class on the element. Works even if the element does not possess the class.

    Parameters

    • className: string

      Classname to remove on the the element

    Returns void

replaceWith

  • replaceWith(otherElem: HTMLElement): void
  • Replace the current element with the other element, then detach the current element

    Parameters

    • otherElem: HTMLElement

    Returns void

setAttribute

  • setAttribute(name: string, value: string): void
  • Sets the value of the specified attribute.

    Parameters

    • name: string

      The name of the attribute

    • value: string

      The value to set

    Returns void

setHtml

  • setHtml(html: string): void
  • Sets the inner html of the element

    Parameters

    • html: string

      The html to set

    Returns void

show

  • show(): void

siblings

  • siblings(selector: string): HTMLElement[]

text

  • text(txt?: string): string
  • Get or set the text content of the HTMLElement.

    Parameters

    • Optional txt: string

      Optional. If given, this will set the text content of the element. If not, will return the text content.

    Returns string

toggle

  • toggle(visible?: boolean): void
  • Toggle the element visibility.
    Optional visible parameter, if specified will set the element visibility

    Parameters

    • Optional visible: boolean

      Optional parameter to display or hide the element

    Returns void

toggleClass

  • toggleClass(className: string, swtch?: boolean): void
  • Toggle the class on the element.

    swtch

    If true, add the class regardless and if false, remove the class

    Parameters

    • className: string

      Classname to toggle

    • Optional swtch: boolean

    Returns void

trigger

  • trigger(type: string, data?: object): void
  • Trigger an event on the element.

    Parameters

    • type: string

      The event type to trigger

    • Optional data: object
      • [key: string]: any

    Returns void

width

  • width(): number

Static createElement

  • createElement(type: string, props?: Object, ...children: Array<string | HTMLElement | Dom>): HTMLElement
  • Helper function to quickly create an HTMLElement

    Parameters

    • type: string

      The type of the element (e.g. div, span)

    • Optional props: Object

      The props (id, className, attributes) of the element
      Can be either specified in dashed-case strings ('my-attribute') or camelCased keys (myAttribute), the latter of which will automatically get replaced to dash-case.

    • Rest ...children: Array<string | HTMLElement | Dom>

    Returns HTMLElement

Constructors

constructor

  • new Dom(el: HTMLElement): Dom
  • Create a new Dom object with the given HTMLElement

    Parameters

    • el: HTMLElement

      The HTMLElement to wrap in a Dom object

    Returns Dom

Hierarchy

  • Dom