mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-23 19:32:14 +00:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { DOMElement } from '.'
|
|
|
|
export default class DOMFleetManager<T extends HTMLElement = HTMLElement> {
|
|
public items: Array<DOMElement<T>> = []
|
|
|
|
public constructor(
|
|
private query: string,
|
|
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))
|
|
}
|
|
|
|
public on<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => void, options?: boolean | AddEventListenerOptions) {
|
|
this.each((item) => item.on(type, listener, options))
|
|
}
|
|
|
|
public off<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => void) {
|
|
this.each((item) => item.off(type, listener))
|
|
}
|
|
|
|
public refresh() {
|
|
this.items = []
|
|
;(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
|
|
}
|
|
this.items.push(
|
|
element
|
|
)
|
|
})
|
|
}
|
|
|
|
public [Symbol.iterator]() {
|
|
return this.items
|
|
}
|
|
}
|