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! Control the interface!
*/ */
export default class interface { export default class UI {
static define(element_type, element_content, element_id = ``, element_class = ``) { static init(element_type, element_content, element_id, element_class) {
/* Defines a new element. /* Defines a new element.
Parameters: Parameters:
@ -22,6 +22,7 @@ export default class interface {
element_new.id = element_id; element_new.id = element_id;
}; };
if (element_class) {
if (Array.isArray(element_class)) { if (Array.isArray(element_class)) {
// for of loop // for of loop
for (const element_class_one of element_class) { for (const element_class_one of element_class) {
@ -30,22 +31,34 @@ export default class interface {
} else if ((typeof element_class).includes(`str`)) { } else if ((typeof element_class).includes(`str`)) {
element_new.classList.add(element_class); element_new.classList.add(element_class);
} }
};
return (element_new); 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. /* Adds or injects an element.
Parameters: Parameters:
element_parent: The parent element element_parent: The parent element; may be ID or the element itself
element_type: the element type element_placement: placement
element_content: the content of the element element_new: the new element
element_id: the element id
element_class: the element class name
Returns: the inserted 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);
} }
} }