added element injection

Now it's possible to actually inject elements. Next up, let's destroy them.
This commit is contained in:
buzz-lightsnack-2007 2024-03-19 11:20:43 +08:00
parent 40693dbc60
commit 951f0a520e

View file

@ -2,8 +2,8 @@
Control the interface!
*/
export default class interface {
static define(element_type, element_content, element_id = ``, element_class = ``) {
export default class UI {
static init(element_type, element_content, element_id, element_class) {
/* Defines a new element.
Parameters:
@ -22,30 +22,43 @@ export default class interface {
element_new.id = element_id;
};
if (Array.isArray(element_class)) {
// for of loop
for (const element_class_one of element_class) {
element_new.classList.add(element_class_one);
if (element_class) {
if (Array.isArray(element_class)) {
// for of loop
for (const element_class_one of element_class) {
element_new.classList.add(element_class_one);
}
} else if ((typeof element_class).includes(`str`)) {
element_new.classList.add(element_class);
}
} else if ((typeof element_class).includes(`str`)) {
element_new.classList.add(element_class);
}
};
return (element_new);
};
static add(element_parent, element_type, element_content, element_id = ``, element_class = ``) {
static add(element_parent, element_new, element_placement = 1) {
/* Adds or injects an element.
Parameters:
element_parent: The parent element
element_type: the element type
element_content: the content of the element
element_id: the element id
element_class: the element class name
element_parent: The parent element; may be ID or the element itself
element_placement: placement
element_new: the new element
Returns: the inserted element
*/
/* If it is the ID */
if ((typeof element_parent).includes(`str`)) {
element_parent = document.getElementById(element_parent);
};
// Add the element
if (element_placement > 0) {
element_parent.appendChild(element_new);
} else if (element_placement < 0) {
element_parent.insertBefore(element_new);
};
// Return the inserted element.
return(element_parent);
}
}