- WIP on multiple components to set private vars

This commit is contained in:
Dave M. 2025-11-06 17:16:58 +00:00
parent 14bccd50a8
commit 45095b221d
8 changed files with 91 additions and 58 deletions

View File

@ -1,6 +1,4 @@
import { ElementObserver } from "./element-observer.js";
import { UiTabs } from "./module/ui-tabs.js"; import { UiTabs } from "./module/ui-tabs.js";
window.customElements.define("ui-tabs", UiTabs); window.customElements.define("ui-tabs", UiTabs);

View File

@ -89,7 +89,7 @@ class ElementObserver {
console.log("@todo!", split_selector); console.log("@todo!", split_selector);
} }
else { else {
if ( $element.is( this.listeners[action][j].selector ) ) { if ( element.is( this.listeners[action][j].selector ) ) {
this.listeners[action][j].callback.call(element, element); this.listeners[action][j].callback.call(element, element);
} }
} }

View File

@ -1,43 +1,39 @@
/* const defaultConfig = {
* Really simple timer / timeout object
*
* @author Dave Mc Nicoll
* @version 2.0.0
*
*/
const config = {
timer: 1000, timer: 1000,
start: false, start: false,
loop: true, loop: true,
run: false, // If you need a defined number of code execution, set a number to run and loop to false. occurences: false, // If you need a defined number of code execution, set a number to run and loop to false.
complete: function(){} // This complete callback function is called if the run exec number is reached. complete: function(){} // This complete callback function is called if the run exec number is reached.
}; };
class Hourglass { class Hourglass {
#callback;
#config = {};
constructor(callback, options) { constructor(callback, options) {
this.object_id = null; this.#config = {...this.#config, ...defaultConfig, ...( options ? options : {} ), };
this.counter = null;
this.callback = callback !== undefined ? callback : function() {};
if ( config.start ) { this.objectId = null;
this.counter = null;
this.callback = callback ? callback : function() {};
if ( this.config.start ) {
this.start(); this.start();
} }
} }
start(timeout) { start(timeout) {
let time = ( timeout !== undefined ? timeout : config.timer ); let time = ( timeout !== undefined ? timeout : this.config.timer );
if ( config.loop ) { if ( this.config.loop ) {
if ( this.counter === null ) { if ( this.counter === null ) {
this.counter = config.loop ? 0 : null this.counter = this.config.loop ? 0 : null
} }
this.object_id = window.setInterval( this.call_interval.bind(this), time); this.objectId = window.setInterval( this.callInterval.bind(this), time);
} }
else { else {
this.object_id = window.setTimeout( this.call_timeout.bind(this), time); this.objectId = window.setTimeout( this.callTimeout.bind(this), time);
} }
} }
@ -47,44 +43,45 @@ class Hourglass {
} }
stop() { stop() {
if ( config.loop ) { if ( this.config.loop ) {
this.object_id && window.clearInterval(this.object_id); this.objectId && window.clearInterval(this.objectId);
} }
else { else {
this.object_id && window.clearTimeout(this.object_id); this.objectId && window.clearTimeout(this.objectId);
} }
} }
clear() { clear() {
this.stop(); this.stop();
this.counter = null; this.counter = null;
this.object_id = null; this.objectId = null;
} }
call_timeout() { callTimeout() {
this.callback(); this.callback();
this.clear(); this.clear();
} }
call_interval() { callInterval() {
this.callback(); this.callback();
if ( this.counter !== null && ++this.counter === config.run ) { if ( this.counter !== null && ++this.counter === this.config.occurences ) {
this.clear(); this.clear();
config.complete !== undefined && config.complete();
this.config.complete !== undefined && this.config.complete();
} }
} }
running() { running() {
return !! this.object_id; return !! this.objectId;
} }
get callback() { get callback() {
return this._callback; return this.#callback;
} }
set callback(value) { set callback(value) {
return this._callback = value; return this.#callback = value;
} }
get counter() { get counter() {
@ -95,12 +92,20 @@ class Hourglass {
return this._counter = value; return this._counter = value;
} }
get object_id() { get objectId() {
return this._object_id; return this._objectId;
} }
set object_id(value) { set objectId(value) {
return this._object_id = value; return this._objectId = value;
}
get config() {
return this.#config;
}
set config(value) {
return this.#config = value;
} }
} }

View File

@ -9,7 +9,6 @@ class UiPopup extends Webcomponent {
constructor() { constructor() {
super(); super();
this.fetch_template();
this.message = this.querySelector('.message'); this.message = this.querySelector('.message');
this.title = this.querySelector('.title'); this.title = this.querySelector('.title');

View File

@ -15,8 +15,6 @@ class UiSelect extends Webcomponent {
constructor() { constructor() {
super(); super();
this.fetch_template();
this.wrapper = this.shadowRoot.querySelector(config.content.wrapper); this.wrapper = this.shadowRoot.querySelector(config.content.wrapper);
this.list = this.shadowRoot.querySelector(config.content.list); this.list = this.shadowRoot.querySelector(config.content.list);
this.input = this.querySelector('input:not([type="hidden"])'); this.input = this.querySelector('input:not([type="hidden"])');

View File

@ -5,11 +5,14 @@ const config = {
item: ".tab-item", item: ".tab-item",
content: ".tab-content", content: ".tab-content",
activeClass: "ui-tab-active", activeClass: "ui-tab-active",
template: {
fetch : false
}
}; };
class UiTabs extends Webcomponent { class UiTabs extends Webcomponent {
constructor(parameters) { constructor() {
super(); super(config);
this.init(); this.init();
} }
@ -17,7 +20,7 @@ class UiTabs extends Webcomponent {
init() { init() {
window.addEventListener("hashchange", this.hashChange.bind(this), false); window.addEventListener("hashchange", this.hashChange.bind(this), false);
//this.querySelectorAll(config.item).forEach(ele => ele.addEventListener("click", this.clickTab.bind(this))); //this.querySelectorAll(this.config.item).forEach(ele => ele.addEventListener("click", this.clickTab.bind(this)));
if (window.location.hash) { if (window.location.hash) {
let eve = new Event("click"); let eve = new Event("click");
@ -29,16 +32,16 @@ class UiTabs extends Webcomponent {
hashChange(e) { hashChange(e) {
let anchor = e.newURL.split('#')[1]; let anchor = e.newURL.split('#')[1];
document.querySelectorAll("#" + anchor + config.content).forEach(function(content) { document.querySelectorAll("#" + anchor + this.config.content).forEach(function(content) {
content.parentNode.querySelectorAll(config.content + "." + config.activeClass).forEach(ele => ele.classList.remove(config.activeClass)); content.parentNode.querySelectorAll(this.config.content + "." + this.config.activeClass).forEach(ele => ele.classList.remove(this.config.activeClass));
content.classList.add(config.activeClass); content.classList.add(this.config.activeClass);
}); }.bind(this));
let element = this.querySelector("." + config.activeClass); let element = this.querySelector("." + this.config.activeClass);
element ? element.classList.remove(config.activeClass) : null; element ? element.classList.remove(this.config.activeClass) : null;
element = this.querySelector(config.item + `[href="#${anchor}"]`); element = this.querySelector(this.config.item + `[href="#${anchor}"]`);
element ? element.closest('div').classList.add(config.activeClass) : null; element ? element.closest('div').classList.add(this.config.activeClass) : null;
} }
clickTab(e) { clickTab(e) {

View File

@ -24,8 +24,7 @@ class UiTextarea extends Webcomponent {
constructor() { constructor() {
super(); super();
this.fetch_template();
this.wrapper = this.shadowRoot.querySelector(domElements.wrapper); this.wrapper = this.shadowRoot.querySelector(domElements.wrapper);
this.content = this.shadowRoot.querySelector(domElements.boilerplate); this.content = this.shadowRoot.querySelector(domElements.boilerplate);
this.toolbar = this.shadowRoot.querySelector(domElements.toolbar); this.toolbar = this.shadowRoot.querySelector(domElements.toolbar);

View File

@ -1,21 +1,38 @@
class Webcomponent extends HTMLElement { class Webcomponent extends HTMLElement {
#config = {
template: {
fetch : true
}
};
fetch_template() { constructor(config) {
super();
if (config) {
this.#config = {...this.#config, ...( config ? config : {} ) };
}
if (this.config.template.fetch) {
this.fetchTemplate();
}
}
fetchTemplate() {
this.attachShadow({mode: 'open'}).appendChild( this.attachShadow({mode: 'open'}).appendChild(
document.querySelector("template#" + this.tagName.toLowerCase()).content.cloneNode(true) document.querySelector("template#" + this.tagName.toLowerCase()).content.cloneNode(true)
); );
} }
get_name() { getName() {
return Object.getPrototypeOf(this).constructor.name; return Object.getPrototypeOf(this).constructor.name;
} }
get_slot(name) { getSlot(name) {
return this.querySelector('[slot="' + name + '"]'); return this.querySelector('[slot="' + name + '"]');
} }
empty_element(element) { emptyElement(element) {
while (element.lastChild) { while (element.lastChild) {
element.removeChild(element.lastChild); element.removeChild(element.lastChild);
} }
@ -23,11 +40,25 @@ class Webcomponent extends HTMLElement {
return element; return element;
} }
build_url(url, data) { buildUrl(url, data) {
return url + ( Object.entries(data).length ? "?" + Object.keys(data).map(function(key) { return url + ( Object.entries(data).length ? "?" + Object.keys(data).map(function(key) {
return [key, data[key]].map(encodeURIComponent).join("="); return [key, data[key]].map(encodeURIComponent).join("=");
}).join("&") : "" ); }).join("&") : "" );
} }
on(type, listener, options) {
this.addEventListener(type, listener, options);
return this;
}
get config() {
return this.#config;
}
set config(value) {
return this.#config = value;
}
} }
export { Webcomponent }; export { Webcomponent };