@serenity-is/corelib / Fluent
Interface: Fluent<TElement>
Represents a Fluent object, which is similar to jQuery but works for only one element.
It implements the ArrayLike
interface and can have 0 (null) or 1 element.
Typeparam
TElement The type of the underlying HTML element.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement = HTMLElement |
Hierarchy
ArrayLike
<TElement
>↳
Fluent
Indexable
▪ [n: number
]: TElement
Gets the element at the specified index.
Param
The index of the element.
Table of contents
Constructors
Properties
Methods
- addClass
- after
- append
- appendTo
- attr
- before
- children
- class
- click
- closest
- data
- each
- empty
- findAll
- findEach
- findFirst
- focus
- getNode
- getWidget
- hasClass
- hide
- insertAfter
- insertBefore
- matches
- nextSibling
- off
- on
- one
- parent
- prepend
- prependTo
- prevSibling
- remove
- removeAttr
- removeClass
- show
- style
- text
- toggle
- toggleClass
- trigger
- tryGetWidget
- val
- byId
- findAll
- findEach
- findFirst
- ready
Constructors
constructor
• constructor: Object
Inherited from
ArrayLike<TElement>.constructor
Properties
[iterator]
• [iterator]: TElement
[]
Gets an iterator for the elements in the Fluent object.
Defined in
length
• Readonly
length: number
Gets the number of elements in the Fluent object. Can only be 1 or 0.
Overrides
ArrayLike.length
Defined in
Methods
addClass
▸ addClass(value
): this
Adds one or more classes to the element. Any falsy value is ignored.
Parameters
Name | Type | Description |
---|---|---|
value |
string | boolean | (string | boolean )[] |
The class or classes to add. It can be a string, boolean, or an array of strings or booleans. |
Returns
this
The Fluent object itself.
Defined in
after
▸ after(content
): this
Inserts content after the element.
Parameters
Name | Type | Description |
---|---|---|
content |
string | Node | Fluent <any > |
The content to insert. It can be a string, a Node object, or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
append
▸ append(child
): this
Appends content to the element.
Parameters
Name | Type | Description |
---|---|---|
child |
string | Node | Fluent <any > |
The content to append. It can be a string, a Node object, or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
appendTo
▸ appendTo(parent
): this
Appends the element to the specified parent element.
Parameters
Name | Type | Description |
---|---|---|
parent |
Element | Fluent <any > |
The parent element to append to. It can be an Element object or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
attr
▸ attr(name
): string
Gets the value of the specified attribute.
Parameters
Name | Type | Description |
---|---|---|
name |
string |
The name of the attribute. |
Returns
string
The value of the attribute.
Defined in
▸ attr(name
, value
): this
Sets the value of the specified attribute.
Parameters
Name | Type | Description |
---|---|---|
name |
string |
The name of the attribute. |
value |
string | number | boolean |
The value of the attribute. If the value is falsy the attribute is removed. |
Returns
this
The Fluent object itself if a value is provided.
Defined in
before
▸ before(content
): this
Inserts content before the element.
Parameters
Name | Type | Description |
---|---|---|
content |
string | Node | Fluent <any > |
The content to insert. It can be a string, a Node object, or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
children
▸ children(selector?
): HTMLElement
[]
Gets the children of the element as an array (not Fluent)
Parameters
Name | Type | Description |
---|---|---|
selector? |
string |
Optional. A CSS selector to filter the children. |
Returns
HTMLElement
[]
An array of HTMLElement objects representing the children.
Defined in
class
▸ class(value
): this
Sets (overrides) the class attribute of the element. Any falsy value is ignored.
Parameters
Name | Type | Description |
---|---|---|
value |
string | boolean | (string | boolean )[] |
The class or classes to add. It can be a string, boolean, or an array of strings or booleans. |
Returns
this
The Fluent object itself.
Defined in
click
▸ click(): this
Triggers a click event on the element.
Returns
this
The Fluent object itself.
Defined in
▸ click(listener
): this
Adds a click event listener on the element.
Parameters
Name | Type | Description |
---|---|---|
listener |
(e : MouseEvent ) => void |
A callback function to execute when the click event is triggered. |
Returns
this
The Fluent object itself.
Defined in
closest
▸ closest(selector
): Fluent
<HTMLElement
>
Gets the closest ancestor of the element that matches the specified selector.
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
A CSS selector to match against. |
Returns
Fluent
<HTMLElement
>
A Fluent object representing the closest ancestor element.
Defined in
data
▸ data(name
): string
Gets or sets the value of the specified data attribute.
Parameters
Name | Type | Description |
---|---|---|
name |
string |
The name of the data attribute. |
Returns
string
The value of the data attribute if no value is provided, or the Fluent object itself if a value is provided.
Defined in
▸ data(name
, value
): this
Parameters
Name | Type |
---|---|
name |
string |
value |
string |
Returns
this
Defined in
each
▸ each(callback
): this
Executes a callback function for the element in the Fluent object if it is not null.
Parameters
Name | Type | Description |
---|---|---|
callback |
(el : TElement ) => void |
The callback function to execute for each element. |
Returns
this
The Fluent object itself.
Defined in
empty
▸ empty(): this
Removes all child nodes from the element. It also clears event handlers attached via Fluent, and disposes any attached widgets.
Returns
this
The Fluent object itself.
Defined in
findAll
▸ findAll<TElement
>(selector
): TElement
[]
Finds all elements that match the specified selector within the element.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement = HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
A CSS selector to match against. |
Returns
TElement
[]
An array of elements that match the selector.
Typeparam
TElement The type of the found elements.
Defined in
findEach
▸ findEach<TElement
>(selector
, callback
): Fluent
<HTMLElement
>
Finds each element that matches the specified selector within the element and executes a callback function for each found element as a Fluent object.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement = HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
A CSS selector to match against. |
callback |
(el : Fluent <TElement >) => void |
The callback function to execute for each found element. It receives a Fluent object for each element. |
Returns
Fluent
<HTMLElement
>
The Fluent object itself.
Typeparam
TElement The type of the found elements.
Defined in
findFirst
▸ findFirst<TElement
>(selector
): Fluent
<TElement
>
Finds the first element that matches the specified selector within the element.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement = HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
A CSS selector to match against. |
Returns
Fluent
<TElement
>
A Fluent object representing the first element that matches the selector.
Typeparam
TElement The type of the found element.
Defined in
focus
▸ focus(): this
Sets focus on the element.
Returns
this
The Fluent object itself.
Defined in
getNode
▸ getNode(): TElement
Gets the underlying HTML element.
Returns
TElement
The underlying HTML element.
Defined in
getWidget
▸ getWidget<TWidget
>(type?
): TWidget
Gets the widget associated with the element.
Type parameters
Name |
---|
TWidget |
Parameters
Name | Type | Description |
---|---|---|
type? |
(...args : any []) => TWidget |
Optional. The constructor function of the widget. |
Returns
TWidget
The widget associated with the element.
Typeparam
TWidget The type of the widget.
Defined in
hasClass
▸ hasClass(klass
): boolean
Checks if the element has the specified class.
Parameters
Name | Type | Description |
---|---|---|
klass |
string |
The class to check for. |
Returns
boolean
true
if the element has the class, false
otherwise.
Defined in
hide
▸ hide(): this
Hides the element by setting its display property to "none".
Returns
this
The Fluent object itself.
Defined in
insertAfter
▸ insertAfter(referenceNode
): this
Inserts the element after the specified reference element.
Parameters
Name | Type | Description |
---|---|---|
referenceNode |
HTMLElement | Fluent <HTMLElement > |
The reference element to insert after. It can be an HTMLElement object or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
insertBefore
▸ insertBefore(referenceNode
): this
Inserts the element before the specified reference element.
Parameters
Name | Type | Description |
---|---|---|
referenceNode |
HTMLElement | Fluent <HTMLElement > |
The reference element to insert before. It can be an HTMLElement object or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
matches
▸ matches(selector
): boolean
Checks if the element matches the specified selector.
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
A CSS selector to match against. |
Returns
boolean
true
if the element matches the selector, false
otherwise.
Defined in
nextSibling
▸ nextSibling(selector?
): Fluent
<any
>
Gets the next sibling element that matches the specified selector, or the first sibling if no selector is provided..
Parameters
Name | Type | Description |
---|---|---|
selector? |
string |
Optional. A CSS selector to filter the next sibling. |
Returns
Fluent
<any
>
A Fluent object representing the next sibling element.
Defined in
off
▸ off<K
>(type
, listener
): this
Removes an event listener from the element.
Type parameters
Name | Type |
---|---|
K |
extends keyof HTMLElementEventMap |
Parameters
Name | Type | Description |
---|---|---|
type |
K |
The type of the event. It can include a ".namespace" similar to jQuery. |
listener |
(this : HTMLElement , ev : HTMLElementEventMap [K ]) => any |
The event listener to remove. |
Returns
this
The Fluent object itself.
Typeparam
K The type of the event.
Defined in
▸ off(type
): this
Parameters
Name | Type |
---|---|
type |
string |
Returns
this
Defined in
▸ off(type
, listener
): this
Parameters
Name | Type |
---|---|
type |
string |
listener |
EventListener |
Returns
this
Defined in
▸ off(type
, selector
, delegationHandler
): this
Parameters
Name | Type |
---|---|
type |
string |
selector |
string |
delegationHandler |
Function |
Returns
this
Defined in
on
▸ on<K
>(type
, listener
): this
Adds an event listener to the element. It is possible to use delegated events like jQuery.
Type parameters
Name | Type |
---|---|
K |
extends keyof HTMLElementEventMap |
Parameters
Name | Type | Description |
---|---|---|
type |
K |
The type of the event. It can include a ".namespace" similar to jQuery. |
listener |
(this : HTMLElement , ev : HTMLElementEventMap [K ]) => any |
The event listener to add. |
Returns
this
The Fluent object itself.
Typeparam
K The type of the event.
Defined in
▸ on(type
, listener
): this
Parameters
Name | Type |
---|---|
type |
string |
listener |
EventListener |
Returns
this
Defined in
▸ on(type
, selector
, delegationHandler
): this
Parameters
Name | Type |
---|---|
type |
string |
selector |
string |
delegationHandler |
Function |
Returns
this
Defined in
one
▸ one<K
>(type
, listener
): this
Adds a one-time event listener to the element. It is possible to use delegated events like jQuery.
Type parameters
Name | Type |
---|---|
K |
extends keyof HTMLElementEventMap |
Parameters
Name | Type | Description |
---|---|---|
type |
K |
The type of the event. It can include a ".namespace" similar to jQuery. |
listener |
(this : HTMLElement , ev : HTMLElementEventMap [K ]) => any |
The event listener to add. |
Returns
this
The Fluent object itself.
Typeparam
K The type of the event.
Defined in
▸ one(type
, listener
): this
Parameters
Name | Type |
---|---|
type |
string |
listener |
EventListener |
Returns
this
Defined in
▸ one(type
, selector
, delegationHandler
): this
Parameters
Name | Type |
---|---|
type |
string |
selector |
string |
delegationHandler |
Function |
Returns
this
Defined in
parent
▸ parent(): Fluent
<HTMLElement
>
Gets the parent element of the element.
Returns
Fluent
<HTMLElement
>
A Fluent object representing the parent element.
Defined in
prepend
▸ prepend(child
): this
Prepends content to the element.
Parameters
Name | Type | Description |
---|---|---|
child |
string | Node | Fluent <any > |
The content to prepend. It can be a string, a Node object, or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
prependTo
▸ prependTo(parent
): this
Prepends the element to the specified parent element.
Parameters
Name | Type | Description |
---|---|---|
parent |
Element | Fluent <any > |
The parent element to prepend to. It can be an Element object or another Fluent object. |
Returns
this
The Fluent object itself.
Defined in
prevSibling
▸ prevSibling(selector?
): Fluent
<any
>
Gets the previous sibling element that matches the specified selector, or the first sibling if no selector is provided.
Parameters
Name | Type | Description |
---|---|---|
selector? |
string |
Optional. A CSS selector to filter the previous sibling. |
Returns
Fluent
<any
>
A Fluent object representing the previous sibling element.
Defined in
remove
▸ remove(): this
Removes the element from the DOM. It also removes event handlers and disposes widgets by calling "remove" event handlers.
Returns
this
The Fluent object itself.
Defined in
removeAttr
▸ removeAttr(name
): this
Removes the specified attribute from the element.
Parameters
Name | Type | Description |
---|---|---|
name |
string |
The name of the attribute to remove. |
Returns
this
The Fluent object itself.
Defined in
removeClass
▸ removeClass(value
): this
Removes one or more classes from the element. Any falsy value is ignored.
Parameters
Name | Type | Description |
---|---|---|
value |
string | boolean | (string | boolean )[] |
The class or classes to remove. It can be a string, boolean, or an array of strings or booleans. |
Returns
this
The Fluent object itself.
Defined in
show
▸ show(): this
Shows the element by setting its display property to empty string.
Returns
this
The Fluent object itself.
Defined in
style
▸ style(callback
): this
Executes a callback function to modify the inline style of the element.
Parameters
Name | Type | Description |
---|---|---|
callback |
(css : CSSStyleDeclaration ) => void |
The callback function to modify the inline style. |
Returns
this
The Fluent object itself.
Defined in
text
▸ text(): string
Gets or sets the text content of the element.
Returns
string
The text content of the element if no value is provided, or the Fluent object itself if a value is provided.
Defined in
▸ text(value
): this
Parameters
Name | Type |
---|---|
value |
string |
Returns
this
Defined in
toggle
▸ toggle(flag?
): this
Toggles the visibility of the element.
Parameters
Name | Type | Description |
---|---|---|
flag? |
boolean |
Optional. A flag indicating whether to show or hide the element. If not provided, the visibility will be toggled. |
Returns
this
The Fluent object itself.
Defined in
toggleClass
▸ toggleClass(value
, add?
): this
Toggles one or more classes on the element. If the class exists, it is removed; otherwise, it is added.
Parameters
Name | Type | Description |
---|---|---|
value |
string | boolean | (string | boolean )[] |
The class or classes to toggle. It can be a string, boolean, or an array of strings or booleans. |
add? |
boolean |
- |
Returns
this
The Fluent object itself.
Defined in
trigger
▸ trigger(type
, args?
): this
Triggers a specified event on the element.
Parameters
Name | Type | Description |
---|---|---|
type |
string |
The type of the event to trigger. |
args? |
any |
Optional. An object that specifies event-specific initialization properties. |
Returns
this
The Fluent object itself.
Defined in
tryGetWidget
▸ tryGetWidget<TWidget
>(type?
): TWidget
Tries to get the widget associated with the element.
Type parameters
Name |
---|
TWidget |
Parameters
Name | Type | Description |
---|---|---|
type? |
(...args : any []) => TWidget |
Optional. The constructor function of the widget. |
Returns
TWidget
The widget associated with the element, or null
if no widget is found.
Typeparam
TWidget The type of the widget.
Defined in
val
▸ val(value
): this
Gets or sets the value of the element.
Parameters
Name | Type | Description |
---|---|---|
value |
string |
The value to set. If no value is provided, returns the current value of the element. |
Returns
this
The value of the element if no value is provided, or the Fluent object itself if a value is provided.
Defined in
▸ val(): string
Returns
string
Defined in
byId
▸ byId<TElement
>(id
): Fluent
<TElement
>
Finds the first element having the specified ID within the document.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
id |
string |
The ID |
Returns
Fluent
<TElement
>
A Fluent instance representing the first matching element, or null if no match is found.
Defined in
findAll
▸ findAll<TElement
>(selector
): TElement
[]
Finds all elements matching the specified selector within the document.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
The CSS selector to search for. |
Returns
TElement
[]
An array of HTML elements matching the selector.
Defined in
findEach
▸ findEach<TElement
>(selector
, callback
): void
Iterates over all elements matching the specified selector within the document.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
The CSS selector to search for. |
callback |
(el : Fluent <TElement >) => void |
A function to execute for each matching element. Receives a Fluent object. |
Returns
void
Defined in
findFirst
▸ findFirst<TElement
>(selector
): Fluent
<TElement
>
Finds the first element matching the specified selector within the document.
Type parameters
Name | Type |
---|---|
TElement |
extends HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
selector |
string |
The CSS selector to search for. |
Returns
Fluent
<TElement
>
A Fluent instance representing the first matching element, or null if no match is found.
Defined in
ready
▸ ready(callback
): void
Parameters
Name | Type |
---|---|
callback |
() => void |
Returns
void