| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import { types } from "assets/types.js";
- class widget {
- #node;
- constructor(node, classname, name) {
- types.check_html_element(node);
- types.check_string(name, true);
- types.check_string(classname);
- node.classList.add(classname);
- if (name !== undefined && name !== null) {
- types.block_strings(name, [" ", "#", "\t", "\n"]);
-
- node.classList.add(name);
- node.classList.add(classname + "-" + name);
- }
- this.#node = node;
- }
- get node() {
- if (!(this.#node instanceof HTMLElement)) {
- throw new ReferenceError("Widget had been removed.");
- }
- return this.#node;
- }
- _add_event(name, callback) {
- types.check_string(name);
- types.check_callback(callback);
- this.node.addEventListener(name, (event) => {
- callback(event, this);
- });
- }
- set _content(target) {
- types.check_string(target);
- const node = this.node;
- if (node instanceof HTMLInputElement) {
- node.value = target
- return;
- }
- if (node instanceof HTMLImageElement) {
- node.src = target;
- return;
- }
- node.innetText = target;
- }
- _drop_content() {
- const node = this.node;
- if (node instanceof HTMLInputElement) {
- node.value = "";
- return;
- }
- while (node.childElementCound) {
- node.lastChild.remove();
- }
- }
- place(target) {
- types.check_html_element(target);
- target.appendChild(this.node);
- }
- _append(target) {
- if (this.node instanceof HTMLInputElement) {
- throw new ReferenceError("Can not append into input element.");
- }
- if (target instanceof HTMLElement) {
- this.node.appendChild(target);
- return;
- }
- if (target instanceof widget) {
- this.node.appendChild(target.node);
- return;
- }
- throw new TypeError("New item must be HTML element or widget.");
- }
- add_click(callback) {
- this._add_event("click", callback);
- }
- get style() {
- return this.node.style;
- }
- }
- export { widget };
|