import HtmlElement from 'elementify/lib/elements/htmlelement.js'
HtmlElement
Extends:
This the common class for all other elements. It provides all functionalities to :
- Explore and manipulate the DOM
- Create, edit, delete properties on the object
- Style the object
- Fade in and out
- Get and set position
Add event with a global events manager
You cannot directly create an instance of the class.
You must use the Element constructor or its alias Q that will then instantiate the right class given the node type (HTML, Form...).
The
options
passed to the Element constructor are then passed to this class constructor.
Constructor Summary
Private Constructor | ||
private |
constructor(node: Element, options: keyValueObject): HtmlElement Constructor for HTMLElement class |
Member Summary
Public Members | ||
public set |
|
|
public get |
bottom: * Get/Set bottom offset in pixels (relative to wiewport) You can set value by assigning any of : |
version 1.0.0 since 1.0.0 |
public |
The underlying DOM node object |
since 1.0.0 |
public |
Convenience property to check if the object is an instance of this class |
since 1.0.0 |
public get |
height: * Get/Set height in pixels You can set value by assigning any of : |
version 1.0.0 since 1.0.0 |
public set |
|
|
public get |
Check if the HtmlElement is present in DOM |
version 1.0.0 since 1.0.0 |
public get |
Get left offset in pixels (relative to wiewport) |
version 1.0.0 since 1.0.0 |
public set |
Set left offset in pixels (relative to wiewport) |
version 1.0.0 since 1.0.0 |
public |
The length property if useful to quickly check what kind of underlying node object have been returned :
|
since 1.0.0 |
public get |
Alias for the element property member that
returns |
version 1.0.0 since 1.0.0 |
public |
The options passed to the constructor |
since 1.0.0 |
public get |
Get the outerHTML string of the underlying node |
version 1.0.0 since 1.0.0 |
public get |
Get the position property of the element. |
version 1.0.0 since 1.0.0 |
public get |
right: * Get/Set right offset in pixels (relative to wiewport) You can set value by assigning any of : |
version 1.0.0 since 1.0.0 |
public set |
|
|
public get |
Get the root Node element for the HtmlElement
The result is not enhanced as an HtmlElement
because it will mostly be a |
version 1.0.0 since 1.0.0 |
public get |
Get scroll X offset of the window in pixels |
version 1.0.0 since 1.0.0 |
public get |
Get scroll Y offset of the window in pixels |
version 1.0.0 since 1.0.0 |
public get |
styles: CSSStyleDeclaration Get the computed styles of the underlying node. |
version 1.0.0 since 1.0.0 |
public get |
top: * Get/Set top offset in pixels (relative to wiewport) You can set value by assigning any of : |
version 1.0.0 since 1.0.0 |
public set |
|
|
public get |
Get the visibility of an HtmlElement. |
version 1.0.0 since 1.0.0 |
public get |
width: * Get/Set width in pixels You can set value by assigning any of : |
version 1.0.0 since 1.0.0 |
public set |
|
Private Members | ||
private |
_metrics: {} |
Method Summary
Public Methods | ||
public |
addClass(cls: String): HtmlElement Add class(es) to an HtmlElement |
version 1.0.0 since 1.0.0 |
public |
after(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement Insert an element before another. |
version 1.0.0 since 1.0.0 |
public |
append(e: String | Node | HtmlElement): HtmlElement Append an element to another |
version 1.0.0 since 1.0.0 |
public |
attr(a: Void | String | Array | keyValueObject, v: Void | Null | Number | String): HtmlElement | Number | String | keyValueObject Get or set given attribute(s) of the element. |
version 1.0.0 since 1.0.0 |
public |
before(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement Insert an element before another. |
version 1.0.0 since 1.0.0 |
public |
child(n: Number): HtmlElement | DocumentFragment Element's nth child finder |
version 1.0.0 since 1.0.0 |
public |
childs(selector: String): Collection | HtmlElement | DocumentFragment Element's childs finder based on a DOM query selector |
version 1.0.0 since 1.0.0 |
public |
clone(): HtmlElement Returns a deep clone of the HtmlElement. |
version 1.0.0 since 1.0.0 |
public |
data(a: undefined | String | Array | Object | ObjectArray, v: Number | String | Object): HtmlElement | Number | String | Object | keyValueObject Get or set data attribute(s) of the element. |
version 1.0.0 since 1.0.0 |
public |
display(v: String): HtmlElement Set the display style property of an HtmlElement |
version 1.0.0 since 1.0.0 |
public |
empty(): HtmlElement Empty an Element |
version 1.0.0 since 1.0.0 |
public |
fadeIn(options: ObjectArray, callback: Callback): this | Promise Fade In element given a linear easing |
version 1.0.0 since 1.0.0 |
public |
fadeOut(options: ObjectArray, callback: Callback): this | Promise Fade Out element given a linear easing |
version 1.0.0 since 1.0.0 |
public |
forEach(callback: forEachCallback): void |
version 1.0.0 since 1.0.0 |
public |
Check if an HtmLElement have given class(es) |
version 1.0.0 since 1.0.0 |
public |
hide(): HtmlElement Hide an HtmlElement by setting its display property to 'none'. |
version 1.0.0 since 1.0.0 |
public |
Get/Set the HTML content of the node |
version 1.0.0 since 1.0.0 |
public |
Matches returns true if the element matches the query selector |
version 1.0.0 since 1.0.0 |
public |
nextSibling(n: Qtype): HtmlElement | DocumentFragment Element's next sibling finder |
version 1.0.0 since 1.0.0 |
public |
nextSiblings(selector: String): Collection | HtmlElement | DocumentFragment Element's next siblings finder based on a DOM query selector |
version 1.0.0 since 1.0.0 |
public |
parent(n: Number | String): HtmlElement | DocumentFragment Element's n-th parent finder |
version 1.0.0 since 1.0.0 |
public |
parents(selector: String): Collection | HtmlElement | DocumentFragment Element's parent finder based on a DOM query selector |
version 1.0.0 since 1.0.0 |
public |
prepend(e: String | Node | HtmlElement): HtmlElement Prepend an element to another |
version 1.0.0 since 1.0.0 |
public |
previousSibling(n: Qtype): HtmlElement | DocumentFragment Element's previous sibling finder |
version 1.0.0 since 1.0.0 |
public |
previousSiblings(selector: String): Collection | HtmlElement | DocumentFragment Element's previous siblings finder based on a DOM query selector |
version 1.0.0 since 1.0.0 |
public |
Remove an Element |
version 1.0.0 since 1.0.0 |
public |
removeClass(cls: String): HtmlElement Remove class(es) from an HtmlElement |
version 1.0.0 since 1.0.0 |
public |
Returns a shallow copy of the HtmlElement |
version 1.0.0 since 1.0.0 |
public |
show(v: String): HtmlElement Show an HtmlElement by setting its display property. |
version 1.0.0 since 1.0.0 |
public |
style(a: String | Array | Object | ObjectArray, v: String): HtmlElement | String | Object Get/Set style properties of an HtmlElement based on its
node |
version 1.0.0 since 1.0.0 |
public |
toggle(v: String): HtmlElement Toggle an HtmlElement visibility by setting its display property. |
version 1.0.0 since 1.0.0 |
public |
toggleClass(cls: String): HtmlElement Toggle class(es) from an HtmlElement |
version 1.0.0 since 1.0.0 |
public |
Unwrap an Element (and its siblings) and delete its parentNode |
version 1.0.0 since 1.0.0 |
public |
wrap(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement Wrap an Element into another. |
version 1.0.0 since 1.0.0 |
Private Methods | ||
private |
_metricsUpdate(): void Refresh position object at first call or after a resize / changeOrientation event |
version 1.0.0 since 1.0.0 |
private |
_parseDims(v: *): * |
|
private |
|
Private Constructors
private constructor(node: Element, options: keyValueObject): HtmlElement source
Constructor for HTMLElement class
You can pass any attributes as options. There are a few reserved keywords for specific options.
Params:
Name | Type | Attribute | Description |
node | Element | Element to enhance |
|
options | keyValueObject |
|
Options for the HTMLElement object and/or attributes for
the underlying native HTMLElement Object. Excepting |
Return:
HtmlElement | New HTMLElement object |
Return Properties:
Name | Type | Attribute | Description |
options.class | String | Array |
|
Class(es) to be set as value for the |
options.style | String | keyValueObject |
|
Style(s) to be set as value for the |
options.data | keyValueObject |
|
An object with key/value pairs |
options.events | keyValueObject |
|
Events to add to the element under the format : |
Public Members
public set bottom source
public get bottom: * version 1.0.0 since 1.0.0 source
Get/Set bottom offset in pixels (relative to wiewport) You can set value by assigning any of :
null
,undefined
: Remove value- {Number} : Value will be interpreted in pixels
- {String} : any css acceptable value for the property (e.g '5vi', '1em'...)
See:
public enhanced: Boolean since 1.0.0 source
Convenience property to check if the object is an instance of this class
public get height: * version 1.0.0 since 1.0.0 source
Get/Set height in pixels You can set value by assigning any of :
null
,undefined
: Remove value- {Number} : Value will be interpreted in pixels
- {String} : any css acceptable value for the property (e.g '5vi', '1em'...)
See:
public set height source
public get isInDom: Boolean version 1.0.0 since 1.0.0 source
Check if the HtmlElement is present in DOM
public get left: Number | undefined version 1.0.0 since 1.0.0 source
Get left offset in pixels (relative to wiewport)
See:
public set left version 1.0.0 since 1.0.0 source
Set left offset in pixels (relative to wiewport)
You can set value by assigning any of :
null
,undefined
: Remove value- {Number} : Value will be interpreted in pixels
- {String} : any css acceptable value for the property (e.g '5vi', '1em'...)
See:
public length: Number since 1.0.0 source
The length property if useful to quickly check what kind of underlying node object have been returned :
- 0 means it's a DocumentFragment
- 1 means it's a node (HTMLElement...)
- More than 1 means it's a Collection (@see elementify.Collection)
public get node: Node version 1.0.0 since 1.0.0 source
Alias for the element property member that
returns null
instead of undefined
public get outerHTML: String version 1.0.0 since 1.0.0 source
Get the outerHTML string of the underlying node
public get position: NodeMetrics version 1.0.0 since 1.0.0 source
Get the position property of the element. The calculation only occurs at first call or when window is resized, rotated or scrolled.
Warning : Calculations can be pretty slow on huge amount of elements
A cached version of the result is available in the _metrics
property of the
HtmlElement. It will be only be generated after a first call to position or another
getter/setter. It will then be regenerated after events that affect window size or scroll
If the element is not in DOM, all values will return undefined.
public get right: * version 1.0.0 since 1.0.0 source
Get/Set right offset in pixels (relative to wiewport) You can set value by assigning any of :
null
,undefined
: Remove value- {Number} : Value will be interpreted in pixels
- {String} : any css acceptable value for the property (e.g '5vi', '1em'...)
See:
public set right source
public get root: Document | Node version 1.0.0 since 1.0.0 source
Get the root Node element for the HtmlElement
The result is not enhanced as an HtmlElement
because it will mostly be a document
object
which is not a Node object.
public get scrollX: Number | undefined version 1.0.0 since 1.0.0 source
Get scroll X offset of the window in pixels
See:
public get scrollY: Number | undefined version 1.0.0 since 1.0.0 source
Get scroll Y offset of the window in pixels
See:
public get styles: CSSStyleDeclaration version 1.0.0 since 1.0.0 source
Get the computed styles of the underlying node. Use with care : It can be painfully slow and must be avoided for huge set of automated ops on style
public get top: * version 1.0.0 since 1.0.0 source
Get/Set top offset in pixels (relative to wiewport) You can set value by assigning any of :
null
,undefined
: Remove value- {Number} : Value will be interpreted in pixels
- {String} : any css acceptable value for the property (e.g '5vi', '1em'...)
See:
public set top source
public get visible: Number version 1.0.0 since 1.0.0 source
Get the visibility of an HtmlElement. It will only return false if the element is out of the DOM or with a display style property set to none.
Use with care : It can be painfully slow and must be avoided for huge set of automated ops on elements if display property is not set inline
public get width: * version 1.0.0 since 1.0.0 source
Get/Set width in pixels You can set value by assigning any of :
null
,undefined
: Remove value- {Number} : Value will be interpreted in pixels
- {String} : any css acceptable value for the property (e.g '5vi', '1em'...)
See:
public set width source
Private Members
private _metrics: {} source
Public Methods
public addClass(cls: String): HtmlElement version 1.0.0 since 1.0.0 source
Add class(es) to an HtmlElement
Params:
Name | Type | Attribute | Description |
cls | String | Class(es) name(s) |
public after(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement version 1.0.0 since 1.0.0 source
Insert an element before another. A Documentfragement wrapper will be inserted if Element have no parent node.
The second parameter can be used to get the parent element for chaining instead of the element itself.
Params:
Name | Type | Attribute | Description |
e | String | Node | HtmlElement | Element to insert |
|
returnself | Boolean |
|
If |
public append(e: String | Node | HtmlElement): HtmlElement version 1.0.0 since 1.0.0 source
Append an element to another
Params:
Name | Type | Attribute | Description |
e | String | Node | HtmlElement | Element to append (enhanced or not) |
public attr(a: Void | String | Array | keyValueObject, v: Void | Null | Number | String): HtmlElement | Number | String | keyValueObject version 1.0.0 since 1.0.0 source
Get or set given attribute(s) of the element.
The getter can be used in two ways :
- A string will return an attribute value
- Array of attributes names will return a keyValueObject
The setter can be used in three ways :
- A string + value will set the attribute
- A string + null will delete the attribute
- A keyValueObject of attributes/values for multiple settings
Return:
HtmlElement | Number | String | keyValueObject | Self for chaining for setter or attributes for getter |
public before(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement version 1.0.0 since 1.0.0 source
Insert an element before another. A Documentfragement wrapper will be inserted if Element have no parent node.
The second parameter can be used to get the parent element for chaining instead of the element itself.
Params:
Name | Type | Attribute | Description |
e | String | Node | HtmlElement | Element to insert |
|
returnself | Boolean |
|
If |
public child(n: Number): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's nth child finder
Params:
Name | Type | Attribute | Description |
n | Number |
|
Distance to the current Element |
Return:
HtmlElement | DocumentFragment | If the child is unreachable, it will returns a DocumentFragment |
public childs(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's childs finder based on a DOM query selector
Params:
Name | Type | Attribute | Description |
selector | String | A valid DOM query selector |
Return:
Collection | HtmlElement | DocumentFragment | HtmlElement or DocumentFragment if no child matches the selector |
public clone(): HtmlElement version 1.0.0 since 1.0.0 source
Returns a deep clone of the HtmlElement. The clone will contains the same child nodes.
public data(a: undefined | String | Array | Object | ObjectArray, v: Number | String | Object): HtmlElement | Number | String | Object | keyValueObject version 1.0.0 since 1.0.0 source
Get or set data attribute(s) of the element.
With no arguments, it will return a keyValueObject with all data attributes.
Data items name will be automatically converted from dash to camel-case if needed.
Return:
HtmlElement | Number | String | Object | keyValueObject | Self for chaining for setter or data attribute(s) values for getter |
public display(v: String): HtmlElement version 1.0.0 since 1.0.0 source
Set the display style property of an HtmlElement
Params:
Name | Type | Attribute | Description |
v | String |
|
Value for display property |
public fadeIn(options: ObjectArray, callback: Callback): this | Promise version 1.0.0 since 1.0.0 source
Fade In element given a linear easing
Params:
Name | Type | Attribute | Description |
options | ObjectArray | Options for the animation |
|
options.display | String |
|
Value for the display CSS property |
options.duration | Number |
|
Duration of the animation in milliseconds |
callback | Callback | Animation callback |
Return:
this | Promise | Chainable or animation promise if available |
public fadeOut(options: ObjectArray, callback: Callback): this | Promise version 1.0.0 since 1.0.0 source
Fade Out element given a linear easing
Params:
Name | Type | Attribute | Description |
options | ObjectArray | Options for the animation |
|
options.display | String |
|
Value for the display CSS property at the end of the animation |
options.duration | Number |
|
Duration of the animation in milliseconds |
callback | Callback | Animation callback |
Return:
this | Promise | Chainable or animation promise if available |
public forEach(callback: forEachCallback): void version 1.0.0 since 1.0.0 source
Params:
Name | Type | Attribute | Description |
callback | forEachCallback | Callback function to be run on each element. |
Return:
void |
public hasClass(cls: String): Boolean version 1.0.0 since 1.0.0 source
Check if an HtmLElement have given class(es)
Params:
Name | Type | Attribute | Description |
cls | String | Class(es) name(s) |
public hide(): HtmlElement version 1.0.0 since 1.0.0 source
Hide an HtmlElement by setting its display property to 'none'.
public html(v: String): HTMLElement | string version 1.0.0 since 1.0.0 source
Get/Set the HTML content of the node
Params:
Name | Type | Attribute | Description |
v | String |
|
Value for HTML content |
public matches(selector: String): Boolean version 1.0.0 since 1.0.0 source
Matches returns true if the element matches the query selector
Params:
Name | Type | Attribute | Description |
selector | String | Valid DOM query selector |
public nextSibling(n: Qtype): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's next sibling finder
Params:
Name | Type | Attribute | Description |
n | Qtype |
|
Distance to current Element |
Return:
HtmlElement | DocumentFragment | If the sibling is unreachable, it will returns a DocumentFragment |
public nextSiblings(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's next siblings finder based on a DOM query selector
Params:
Name | Type | Attribute | Description |
selector | String | A valid DOM query selector |
Return:
Collection | HtmlElement | DocumentFragment | DocumentFragment if no siblings matches the selector |
public parent(n: Number | String): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's n-th parent finder
Return:
HtmlElement | DocumentFragment | If the parent level is above top level, it will returns a DocumentFragment |
public parents(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's parent finder based on a DOM query selector
Params:
Name | Type | Attribute | Description |
selector | String | A valid DOM query selector |
Return:
Collection | HtmlElement | DocumentFragment | HtmlElement or DocumentFragment if no parent matches the selector |
public prepend(e: String | Node | HtmlElement): HtmlElement version 1.0.0 since 1.0.0 source
Prepend an element to another
Params:
Name | Type | Attribute | Description |
e | String | Node | HtmlElement | Element to prepend (enhanced or not) |
public previousSibling(n: Qtype): HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's previous sibling finder
Params:
Name | Type | Attribute | Description |
n | Qtype |
|
Distance to current Element |
Return:
HtmlElement | DocumentFragment | If the sibling is unreachable, it will returns a DocumentFragment |
public previousSiblings(selector: String): Collection | HtmlElement | DocumentFragment version 1.0.0 since 1.0.0 source
Element's previous siblings finder based on a DOM query selector
Params:
Name | Type | Attribute | Description |
selector | String | A valid DOM query selector |
Return:
Collection | HtmlElement | DocumentFragment | DocumentFragment if no siblings matches the selector |
public removeClass(cls: String): HtmlElement version 1.0.0 since 1.0.0 source
Remove class(es) from an HtmlElement
Params:
Name | Type | Attribute | Description |
cls | String | Class(es) name(s) |
public shallow(): HtmlElement version 1.0.0 since 1.0.0 source
Returns a shallow copy of the HtmlElement
public show(v: String): HtmlElement version 1.0.0 since 1.0.0 source
Show an HtmlElement by setting its display property. The property value can be override for custom results (e.g. 'flex')
Params:
Name | Type | Attribute | Description |
v | String |
|
Value for display property |
public style(a: String | Array | Object | ObjectArray, v: String): HtmlElement | String | Object version 1.0.0 since 1.0.0 source
Get/Set style properties of an HtmlElement based on its
node style
attribute.
Properties name will be camelized when read from style string and dashed when set to style string.
Many syntaxes are available. See examples.
You can remove a style property by setting its value to null
Params:
Name | Type | Attribute | Description |
a | String | Array | Object | ObjectArray |
|
get/set request on style attribute
|
v | String |
|
Value of the style or enable replace mode |
Return:
HtmlElement | String | Object | Returns self for chaining for set cases, String or Object {styleKey: styleValue, ...} for get cases |
Example:
var e = new Element('<div style="color:red;margin:1em"></div>');
e.style('color'); // returns 'red'
e.style('color', 'yellow'); // Set color to yellow and returns e for chaining
e.style(['color', 'margin']); // returns {color:'red', margin: '1em'}
e.style({color: 'yellow', padding: '5px'}); // Set color to yellow, padding to 5px and returns e for chaining
e.style({paddingLeft: '10px', margin: '5px'}); // Set color to yellow, padding to 5px and returns e for chaining
public toggle(v: String): HtmlElement version 1.0.0 since 1.0.0 source
Toggle an HtmlElement visibility by setting its display property. The "show" property value can be override for custom results (e.g. 'flex')
Use with care : It can be painfully slow and must be avoided for huge set of automated ops on elements if display property is not set inline
Params:
Name | Type | Attribute | Description |
v | String |
|
Value for display property when set to visible |
public toggleClass(cls: String): HtmlElement version 1.0.0 since 1.0.0 source
Toggle class(es) from an HtmlElement
Params:
Name | Type | Attribute | Description |
cls | String | Class(es) name(s) |
public unwrap(): HtmlElement version 1.0.0 since 1.0.0 source
Unwrap an Element (and its siblings) and delete its parentNode
public wrap(e: String | Node | HtmlElement, returnself: Boolean): HtmlElement version 1.0.0 since 1.0.0 source
Wrap an Element into another. If no Element provided, the wrapper will be set to a DocumentFragment node.
The second parameter can be used to get the parent element for chaining instead of the element itself.
Params:
Name | Type | Attribute | Description |
e | String | Node | HtmlElement |
|
Wrapping Element |
returnself | Boolean |
|
If |
Private Methods
private _metricsUpdate(): void version 1.0.0 since 1.0.0 source
Refresh position object at first call or after a resize / changeOrientation event
Return:
void |