import { DOMElement } from '.' export default class DOMFleetManager { public items: Array> = [] 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(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => void, options?: boolean | AddEventListenerOptions) { this.each((item) => item.on(type, listener, options)) } public off(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(this.query).forEach((item) => { const element = DOMElement.get(item) if (!element) { return } this.items.push( element ) }) } public [Symbol.iterator]() { return this.items } }