2024-07-01 03:51:25 +00:00
|
|
|
import util from "./util"
|
2024-07-16 19:43:35 +00:00
|
|
|
import {Settings} from "./settings"
|
2024-07-01 03:51:25 +00:00
|
|
|
|
|
|
|
export class TabBarElement extends HTMLElement {
|
|
|
|
static observedAttributes = [ "data-adapters", "data-currentadapter" ]
|
|
|
|
|
2024-08-23 01:18:30 +00:00
|
|
|
private _adapters: string[] = [];
|
2024-07-01 03:51:25 +00:00
|
|
|
private _currentAdapter: string | null = null;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
if (this._currentAdapter) {
|
|
|
|
this.showAdapterFunc(this, this._currentAdapter)();
|
|
|
|
} else {
|
|
|
|
this.showSettings(this)();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-23 01:18:30 +00:00
|
|
|
attributeChangedCallback(attr: string, prev: string, next: string) {
|
|
|
|
switch (attr) {
|
|
|
|
case "data-adapters":
|
|
|
|
if (next) {
|
|
|
|
this._adapters = next.split(",")
|
|
|
|
} else {
|
|
|
|
this._adapters = [];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "data-currentadapter":
|
|
|
|
this._currentAdapter = next || null;
|
|
|
|
break;
|
2024-07-01 03:51:25 +00:00
|
|
|
}
|
|
|
|
|
2024-08-23 01:18:30 +00:00
|
|
|
let html = "<ul><li><a id='tabbar_settings' href='#settings'>settings</a></li>";
|
2024-07-01 03:51:25 +00:00
|
|
|
|
|
|
|
html = this._adapters.reduce((self: string, a: string)=>{
|
|
|
|
self += `<li><a id="tabbar_${a}" href="#${a}">${a}</a></li>`;
|
|
|
|
return self;
|
|
|
|
}, html);
|
|
|
|
html += "</ul>";
|
|
|
|
|
|
|
|
this.innerHTML = html;
|
|
|
|
// now we can query the child elements and add click handlers to them
|
2024-07-16 19:43:35 +00:00
|
|
|
var s = util.$("tabbar_settings");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (s) {
|
|
|
|
s.addEventListener("click", this.showSettings(this), false);
|
|
|
|
if (!this._currentAdapter) {
|
|
|
|
s.classList.add("tabbar_current");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i of this._adapters) {
|
2024-07-16 19:43:35 +00:00
|
|
|
var a = util.$(`tabbar_${i}`);
|
2024-07-01 03:51:25 +00:00
|
|
|
if (a) {
|
|
|
|
a.addEventListener("click", this.showAdapterFunc(this, i), false);
|
|
|
|
if (this._currentAdapter == i) {
|
|
|
|
a.classList.add("tabbar_current");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
showSettings(self: TabBarElement): ()=>void {
|
|
|
|
return () => {
|
2024-07-16 19:43:35 +00:00
|
|
|
let x = util.$("mainarea_injectparent");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (x) {
|
2024-07-16 19:43:35 +00:00
|
|
|
x.innerHTML = `<underbbs-settings data-adapters=${Settings._instance.adapters.map(a=>a.nickname).join(",") ?? []}></underbbs-settings>`;
|
2024-07-01 03:51:25 +00:00
|
|
|
self.setAttribute("data-currentadapter", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showAdapterFunc(self: TabBarElement, adapter: string): ()=>void {
|
|
|
|
return ()=>{
|
2024-07-16 19:43:35 +00:00
|
|
|
let x = util.$("mainarea_injectparent");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (x) {
|
2024-08-17 00:21:10 +00:00
|
|
|
x.innerHTML = `<underbbs-adapter id="adapter_${adapter}" data-name="${adapter}" data-view=""></underbbs-adapter>`;
|
2024-07-01 03:51:25 +00:00
|
|
|
self.setAttribute("data-currentadapter", adapter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|