@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
93 lines (92 loc) • 3.79 kB
TypeScript
import { Be } from '../be.js';
import type { CommonHandler } from '../types.js';
declare enum dataMethods {
set = "set",
get = "get",
delete = "delete",
getKey = "getKey"
}
/**
* Type definition for DataHandler actions.
*/
export type DataHandlerHandle = {
get: (keyOrObject: string | Record<string, string>, value?: string) => Be;
set: (keyOrObject: string | Record<string, string>, value?: string) => Be;
delete: (keyOrObject: string | Record<string, string>, value?: string) => Be;
getKey: (keyOrObject: string | Record<string, string>, value?: string) => Be;
};
/**
* Handles operations on `data-*` attributes for Be elements.
*/
export declare class DataHandler implements CommonHandler<DataHandler> {
private beElement;
static methods: dataMethods[];
/**
* Initializes the DataHandler with a Be element.
* @param element - The Be element to operate on.
*/
constructor(element: Be);
methods: string[] | keyof DataHandler;
/**
* Handles dynamic method calls for data operations.
* @param actions - The action to perform (e.g., set, get, delete).
* @returns The Be element for chaining.
*/
handle(actions: Partial<DataHandlerHandle>): Be;
/**
* Retrieves the value of a `data-*` attribute.
* @param key - The key of the `data-*` attribute to retrieve.
* @returns The value of the attribute, or `null` if not found.
* @example
* // HTML: <div id="test" data-role="admin"></div>
* const beInstance = be('#test');
* const role = beInstance.getData('role');
* console.log(role); // Output: "admin"
*/
get(key: string): string | null;
/**
* Sets one or more `data-*` attributes.
* @param keyOrObject - A key-value pair or an object containing multiple key-value pairs.
* @param value - The value to set if a single key is provided.
* @returns The Be element for chaining.
* @example
* // HTML: <div id="test"></div>
* const beInstance = be('#test');
* beInstance.setData('role', 'admin'); // Sets a single `data-role` attribute
* beInstance.setData({ role: 'admin', type: 'user' }); // Sets multiple `data-*` attributes
*/
set(keyOrObject: string | Record<string, string>, value?: string): Be;
/**
* Deletes one or more `data-*` attributes.
* @param keyOrObject - A key or an object containing multiple keys to delete.
* @returns The Be element for chaining.
* @example
* // HTML: <div id="test" data-role="admin" data-type="user"></div>
* const beInstance = be('#test');
* beInstance.deleteData('role'); // Deletes the `data-role` attribute
* beInstance.deleteData({ type: '' }); // Deletes the `data-type` attribute
*/
delete(keyOrObject: string | Record<string, string>): Be;
/**
* Retrieves the value of a specific `data-*` attribute.
* @param key - The key of the `data-*` attribute to retrieve.
* @returns The value of the attribute, or `null` if not found.
* @example
* // HTML: <div id="test" data-role="admin"></div>
* const beInstance = be('#test');
* const role = beInstance.getKey('role');
* console.log(role); // Output: "admin"
*/
getKey(key: string | string[]): string | null;
/**
* Retrieves all `data-*` attributes as a DOMStringMap.
* @returns A DOMStringMap containing all `data-*` attributes, or `null` if not applicable.
* @example
* // HTML: <div id="test" data-role="admin" data-type="user"></div>
* const beInstance = be('#test');
* const dataAttributes = beInstance.data.valueOf();
* console.log(dataAttributes); // Output: { role: "admin", type: "user" }
*/
valueOf(): DOMStringMap | null;
}
export {};