UNPKG

@medyll/idae-be

Version:

A modern, lightweight, and extensible DOM manipulation library built with TypeScript. Designed for precise element targeting and manipulation using a callback-based approach. Features include advanced DOM traversal, event handling, style management, attri

156 lines (155 loc) 5.27 kB
import { Be, be } from '../be.js'; var timersMethods; (function (timersMethods) { timersMethods["timeout"] = "timeout"; timersMethods["interval"] = "interval"; timersMethods["clearTimeout"] = "clearTimeout"; timersMethods["clearInterval"] = "clearInterval"; })(timersMethods || (timersMethods = {})); export class TimersHandler { beElement; static methods = Object.values(timersMethods); #timer = null; #interval = null; constructor(element) { this.beElement = element; } methods = TimersHandler.methods; valueOf() { return { methods: this.methods, timer: this.#timer, interval: this.#interval }; } handle(actions) { Object.entries(actions).forEach(([method, props]) => { if (method in this) { this[method](props); } }); return this.beElement; } /** * Sets a timeout for the element(s). * @param value - The timeout duration in milliseconds. * @param callback - The callback function to execute after the timeout. * @returns The Be instance for method chaining. * @example * // HTML: <div id="test"></div> * const beInstance = be('#test'); * beInstance.timeout(1000, () => console.log('Timeout executed')); // Sets a 1-second timeout */ timeout(value, callback) { this.beElement.timerOut = setTimeout(() => { callback?.({ method: 'timeout', fragment: this.beElement.timerOut, be: this.beElement, root: this.beElement }); }, value); /* this.beElement.eachNode((el: HTMLElement) => { const aug = be(el); aug.BeTimer = setTimeout(() => { callback?.({ method: 'timeout', fragment: aug.BeTimer, be: be(el), root: this.beElement }); }, value); }); */ return this.beElement; } /** * Sets an interval for the element(s). * @param value - The interval duration in milliseconds. * @param callback - The callback function to execute at each interval. * @returns The Be instance for method chaining. * @example * // HTML: <div id="test"></div> * const beInstance = be('#test'); * beInstance.interval(500, () => console.log('Interval executed')); // Sets a 500ms interval */ interval(value, callback) { this.beElement.timerInterval = setInterval(() => { callback?.({ method: 'interval', fragment: this.beElement.timerInterval, be: this.beElement, root: this.beElement }); }, value); /* this.beElement.eachNode((el: HTMLElement) => { const aug = be(el); aug.BeInterval = setInterval(() => { callback?.({ method: 'interval', fragment: aug.BeInterval, be: be(el), root: this.beElement }); }, value); }); */ return this.beElement; } /** * Clears the timeout for the element(s). * @param callback - Optional callback function. * @returns The Be instance for method chaining. * @example * // HTML: <div id="test"></div> * const beInstance = be('#test'); * beInstance.timeout(1000, () => console.log('Timeout executed')); * beInstance.clearTimeout(); // Clears the timeout */ clearTimeout(callback) { if (this.beElement.timerOut) clearTimeout(this.beElement.timerOut); this.beElement.timerOut = null; /* this.beElement.eachNode((el: HTMLElement) => { const aug = be(el); if (aug.BeTimer !== null) { clearTimeout(aug.BeTimer); aug.BeTimer = null; } }); */ callback?.({ method: 'clearTimeout', fragment: null, be: this.beElement, root: this.beElement }); return this.beElement; } /** * Clears the interval for the element(s). * @param callback - Optional callback function. * @returns The Be instance for method chaining. * @example * // HTML: <div id="test"></div> * const beInstance = be('#test'); * beInstance.interval(500, () => console.log('Interval executed')); * beInstance.clearInterval(); // Clears the interval */ clearInterval(callback) { if (this.beElement.timerInterval) clearInterval(this.beElement.timerInterval); this.beElement.timerInterval = null; /* this.beElement.eachNode((el: HTMLElement) => { const aug = be(el); if (aug.BeInterval !== null) { clearInterval(aug.BeInterval); aug.BeInterval = null; } }); */ callback?.({ method: 'clearInterval', fragment: null, be: this.beElement, root: this.beElement }); return this.beElement; } }