@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
121 lines (120 loc) • 4.49 kB
JavaScript
import { Be } from '../be.js';
var attrMethods;
(function (attrMethods) {
attrMethods["set"] = "set";
attrMethods["get"] = "get";
attrMethods["delete"] = "delete";
})(attrMethods || (attrMethods = {}));
/**
* Handles attribute operations for Be elements.
*/
export class AttrHandler {
beElement;
static methods = Object.values(attrMethods);
/**
* Initializes the AttrHandler with a Be element.
* @param element - The Be element to operate on.
*/
constructor(element) {
this.beElement = element;
}
methods = AttrHandler.methods;
/**
* Handles dynamic method calls for attribute operations.
* @param actions - The actions to perform (e.g., set, get, delete).
* @returns The Be element for chaining.
*/
handle(actions) {
Object.entries(actions).forEach(([method, props]) => {
if (method in this) {
this[method](props);
}
});
return this.beElement;
}
/**
* Retrieves the value of an attribute.
* @param name - The name of the 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.getAttr('data-role');
* console.log(role); // Output: "admin"
*/
get(name) {
if (typeof this.beElement.inputNode === 'string')
return (document.querySelector(this.beElement.inputNode || '')?.getAttribute(name || '') || null);
if (this.beElement.isWhat !== 'element')
return null;
const el = this.beElement.inputNode;
return name ? el.getAttribute(name) : null;
}
/**
* Sets one or more attributes on the element(s).
* @param nameOrObject - 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.setAttr('data-role', 'admin'); // Sets a single attribute
* beInstance.setAttr({ class: 'highlight', title: 'Hello' }); // Sets multiple attributes
*/
set(nameOrObject, value) {
this.beElement.eachNode((el) => {
if (typeof nameOrObject === 'string' && value !== undefined) {
el.setAttribute(nameOrObject, value);
}
else if (typeof nameOrObject === 'object') {
Object.entries(nameOrObject).forEach(([name, val]) => {
el.setAttribute(name, val);
});
}
});
return this.beElement;
}
/**
* Deletes one or more attributes from the element(s).
* @param nameOrObject - A key or an object containing multiple keys to delete.
* @returns The Be element for chaining.
* @example
* // HTML: <div id="test" data-role="admin" class="highlight"></div>
* const beInstance = be('#test');
* beInstance.deleteAttr('data-role'); // Deletes a single attribute
* beInstance.deleteAttr({ class: '' }); // Deletes multiple attributes
*/
delete(nameOrObject) {
this.beElement.eachNode((el) => {
if (typeof nameOrObject === 'string') {
el.removeAttribute(nameOrObject);
}
else if (typeof nameOrObject === 'object') {
Object.entries(nameOrObject).forEach(([name]) => {
el.removeAttribute(name);
});
}
});
return this.beElement;
}
/**
* Retrieves all attributes as a key-value pair object.
* @returns An object containing all attributes, or `null` if not applicable.
* @example
* // HTML: <div id="test" data-role="admin" class="highlight"></div>
* const beInstance = be('#test');
* const attributes = beInstance.attrs.valueOf();
* console.log(attributes); // Output: { id: "test", "data-role": "admin", class: "highlight" }
*/
valueOf() {
if (this.beElement.isWhat !== 'element')
return null;
const el = this.beElement.inputNode;
const attrs = {};
for (let i = 0; i < el.attributes.length; i++) {
const attr = el.attributes[i];
attrs[attr.name] = attr.value;
}
return attrs;
}
}