1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-06-07 16:49:56 +00:00

Updated Functions

Signed-off-by: Florian Bouillon <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2020-09-02 23:05:15 +02:00
parent 611c5d5b47
commit ea3b3e89ad
2 changed files with 8 additions and 4 deletions

View File

@ -18,9 +18,9 @@ export default class DOMElement<T extends HTMLElement = HTMLElement> {
}
public static get<T extends HTMLElement = HTMLElement>(query: string | T, source?: HTMLElement): DOMElement<T> | undefined {
public static get<T extends HTMLElement = HTMLElement>(query: string | T, source?: HTMLElement | DOMElement): DOMElement<T> | undefined {
if (!(query instanceof HTMLElement)) {
const tmp = (source || document).querySelector<T>(query)
const tmp = (source instanceof DOMElement ? source.item : source || document).querySelector<T>(query)
if (!tmp) {
return undefined
}

View File

@ -5,11 +5,15 @@ export default class DOMFleetManager<T extends HTMLElement = HTMLElement> {
public constructor(
private query: string,
private source?: HTMLElement
private source?: HTMLElement | DOMElement
) {
this.refresh()
}
public last() {
return this.items[this.items.length - 1]
}
public each(fn: (item: DOMElement, index: number) => void) {
this.items.forEach((el, index) => fn(el, index))
}
@ -24,7 +28,7 @@ export default class DOMFleetManager<T extends HTMLElement = HTMLElement> {
public refresh() {
this.items = []
;(this.source || document).querySelectorAll<T>(this.query).forEach((item) => {
;(this.source instanceof DOMElement ? this.source.item : this.source || document).querySelectorAll<T>(this.query).forEach((item) => {
const element = DOMElement.get<T>(item)
if (!element) {
return