2024-07-01 03:51:25 +00:00
|
|
|
import util from "./util"
|
2024-07-16 19:43:35 +00:00
|
|
|
import {DatagramSocket} from "./websocket"
|
|
|
|
import {Settings} from "./settings"
|
2024-07-01 03:51:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
export class SettingsElement extends HTMLElement {
|
|
|
|
static observedAttributes = [ "data-adapters" ]
|
|
|
|
|
|
|
|
private _adapters: string[] = [];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
}
|
|
|
|
|
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(",");
|
|
|
|
}
|
2024-07-16 19:43:35 +00:00
|
|
|
}
|
2024-07-01 03:51:25 +00:00
|
|
|
this.showSettings(this)();
|
|
|
|
}
|
|
|
|
|
|
|
|
showSettings(self: SettingsElement): ()=>void {
|
|
|
|
return ()=>{
|
|
|
|
let html = "";
|
|
|
|
html += "<button id='settings_adapter_create_btn'>New</button>";
|
|
|
|
html += self._adapters.reduce((self: string, a: string) => {
|
|
|
|
self += `<li><a id='settings_adapter_edit_${a}' href='#editadapter'>${a}</a></li>`
|
|
|
|
return self;
|
|
|
|
}, "<ul id='settings_adapterlist'>");
|
|
|
|
html += "</ul>";
|
2024-07-05 01:27:29 +00:00
|
|
|
html += "<button id='settings_connect_btn'>connect</button>";
|
2024-07-01 03:51:25 +00:00
|
|
|
self.innerHTML = html;
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
let create = util.$("settings_adapter_create_btn");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (create) {
|
|
|
|
create.addEventListener("click", self.showCreateAdapter(self), false);
|
|
|
|
}
|
|
|
|
for (let a of this._adapters) {
|
2024-07-16 19:43:35 +00:00
|
|
|
let edit = util.$(`settings_adapter_edit_${a}`);
|
2024-07-01 03:51:25 +00:00
|
|
|
if (edit) {
|
|
|
|
edit.addEventListener("click", self.showEditAdapterFunc(a, self), false);
|
|
|
|
}
|
|
|
|
}
|
2024-07-16 19:43:35 +00:00
|
|
|
let connect = util.$("settings_connect_btn");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (connect) {
|
2024-07-16 19:43:35 +00:00
|
|
|
connect.addEventListener("click", DatagramSocket.connect, false);
|
2024-07-01 03:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showCreateAdapter(self: SettingsElement): ()=>void {
|
|
|
|
return ()=>{
|
|
|
|
// dropdown for protocol
|
|
|
|
let html = "<select id='settings_newadapter_protocolselect'>";
|
|
|
|
html += [ "nostr", "mastodon", "misskey" ].reduce((self, p)=>{
|
|
|
|
self += `<option value='${p}'>${p}</option>`;
|
|
|
|
return self;
|
|
|
|
}, "");
|
|
|
|
html += "</select>";
|
|
|
|
|
|
|
|
// nostr is the first protocol, so show its options by default
|
|
|
|
html += "<div id='settings_newadapter_protocoloptions'>";
|
|
|
|
html += " <label>nickname<input id='settings_newadapter_nickname'/></label>";
|
|
|
|
html += " <label>privkey<input id='settings_newadapter_nostr_privkey'/></label>";
|
|
|
|
html += " <label>default relays<input id='settings_newadapter_nostr_default_relays'/></label>";
|
|
|
|
html += "</div>";
|
|
|
|
|
|
|
|
html += "<button id='settings_adapter_create_save_btn'>add</button>";
|
|
|
|
html += "<button id='settings_adapter_create_back_btn'>back</button>";
|
|
|
|
|
|
|
|
self.innerHTML = html;
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
let protocolSelect = util.$("settings_newadapter_protocolselect");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (protocolSelect) {
|
|
|
|
protocolSelect.addEventListener("change", self.fillAdapterProtocolOptions, false);
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
let save = util.$("settings_adapter_create_save_btn");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (save) {
|
|
|
|
save.addEventListener("click", self.saveAdapter(self), false);
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
let back = util.$("settings_adapter_create_back_btn");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (back) {
|
|
|
|
back.addEventListener("click", self.showSettings(self), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveAdapter(self: SettingsElement): ()=>void {
|
|
|
|
return ()=>{
|
|
|
|
let adapterdata: any = {};
|
|
|
|
// get selected adapter protocol
|
2024-07-16 19:43:35 +00:00
|
|
|
const proto = util.$("settings_newadapter_protocolselect") as HTMLSelectElement;
|
2024-07-01 03:51:25 +00:00
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
const nickname = (util.$("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
|
2024-07-01 03:51:25 +00:00
|
|
|
|
|
|
|
// switch protocol
|
|
|
|
switch (proto.options[proto.selectedIndex].value) {
|
|
|
|
case "nostr":
|
2024-07-16 19:43:35 +00:00
|
|
|
const privkey = (util.$("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? "";
|
|
|
|
const relays = (util.$("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? "";
|
2024-07-01 03:51:25 +00:00
|
|
|
adapterdata = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
|
|
|
|
break;
|
|
|
|
case "mastodon":
|
|
|
|
case "misskey":
|
2024-07-16 19:43:35 +00:00
|
|
|
const server = (util.$("settings_newadapter_masto_server") as HTMLInputElement)?.value ?? "";
|
|
|
|
const apiKey = (util.$("settings_newadapter_masto_apikey") as HTMLInputElement)?.value ?? "";
|
2024-07-01 03:51:25 +00:00
|
|
|
adapterdata = { nickname: nickname, protocol: proto.options[proto.selectedIndex].value, server: server, apiKey: apiKey };
|
|
|
|
break;
|
|
|
|
}
|
2024-07-16 19:43:35 +00:00
|
|
|
const settings = Settings._instance;
|
2024-07-01 03:51:25 +00:00
|
|
|
if (settings) {
|
|
|
|
if (!settings.adapters) {
|
|
|
|
settings.adapters = [];
|
|
|
|
}
|
|
|
|
settings.adapters.push(adapterdata);
|
|
|
|
self._adapters.push(adapterdata.nickname);
|
|
|
|
localStorage.setItem("settings", JSON.stringify(settings));
|
|
|
|
|
2024-08-03 16:52:33 +00:00
|
|
|
self.showSettings(self)();
|
2024-07-01 03:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fillAdapterProtocolOptions() {
|
2024-07-16 19:43:35 +00:00
|
|
|
const proto = util.$("settings_newadapter_protocolselect") as HTMLSelectElement;
|
2024-07-01 03:51:25 +00:00
|
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
|
|
switch(proto?.options[proto.selectedIndex].value) {
|
|
|
|
case "nostr":
|
|
|
|
html += " <label>nickname<input id='settings_newadapter_nickname'/></label>";
|
|
|
|
html += " <label>privkey<input id='settings_newadapter_nostr_privkey'/></label>";
|
|
|
|
html += " <label>default relays<input id='settings_newadapter_nostr_default_relays'/></label>";
|
|
|
|
break;
|
|
|
|
case "mastodon":
|
|
|
|
case "misskey":
|
|
|
|
html += " <label>nickname<input id='settings_newadapter_nickname'/></label>";
|
|
|
|
html += " <label>server<input id='settings_newadapter_masto_server'/></label>";
|
|
|
|
html += " <label>API key<input id='settings_newadapter_masto_apikey'/></label>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
const div = util.$("settings_newadapter_protocoloptions");
|
2024-07-01 03:51:25 +00:00
|
|
|
if (div) {
|
|
|
|
div.innerHTML = html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
showEditAdapterFunc(adapter: string, self: SettingsElement): ()=>void {
|
|
|
|
// this UI has to be able to edit an exiting adapter or delete it
|
|
|
|
return ()=>{};
|
|
|
|
}
|
|
|
|
}
|