";
html += "";
self.innerHTML = html;
let create = util.$("settings_adapter_create_btn");
if (create) {
create.addEventListener("click", self.showCreateAdapter(self), false);
}
for (let a of this._adapters) {
let edit = util.$(`settings_adapter_edit_${a}`);
if (edit) {
edit.addEventListener("click", self.showEditAdapterFunc(a, self), false);
}
}
let connect = util.$("settings_connect_btn");
if (connect) {
connect.addEventListener("click", DatagramSocket.connect, false);
}
}
}
showCreateAdapter(self: SettingsElement): ()=>void {
return ()=>{
// dropdown for protocol
let html = "";
// nostr is the first protocol, so show its options by default
html += "
";
html += " ";
html += " ";
html += " ";
html += "
";
html += "";
html += "";
self.innerHTML = html;
let protocolSelect = util.$("settings_newadapter_protocolselect");
if (protocolSelect) {
protocolSelect.addEventListener("change", self.fillAdapterProtocolOptions, false);
}
let save = util.$("settings_adapter_create_save_btn");
if (save) {
save.addEventListener("click", self.saveAdapter(self), false);
}
let back = util.$("settings_adapter_create_back_btn");
if (back) {
back.addEventListener("click", self.showSettings(self), false);
}
}
}
saveAdapter(self: SettingsElement): ()=>void {
return ()=>{
let adapterdata: any = {};
// get selected adapter protocol
const proto = util.$("settings_newadapter_protocolselect") as HTMLSelectElement;
const nickname = (util.$("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
// switch protocol
switch (proto.options[proto.selectedIndex].value) {
case "nostr":
const privkey = (util.$("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? "";
const relays = (util.$("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? "";
adapterdata = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
break;
case "mastodon":
case "misskey":
const server = (util.$("settings_newadapter_masto_server") as HTMLInputElement)?.value ?? "";
const apiKey = (util.$("settings_newadapter_masto_apikey") as HTMLInputElement)?.value ?? "";
adapterdata = { nickname: nickname, protocol: proto.options[proto.selectedIndex].value, server: server, apiKey: apiKey };
break;
}
const settings = Settings._instance;
if (settings) {
if (!settings.adapters) {
settings.adapters = [];
}
settings.adapters.push(adapterdata);
self._adapters.push(adapterdata.nickname);
localStorage.setItem("settings", JSON.stringify(settings));
self.showSettings(self)();
}
}
}
fillAdapterProtocolOptions() {
const proto = util.$("settings_newadapter_protocolselect") as HTMLSelectElement;
let html = "";
switch(proto?.options[proto.selectedIndex].value) {
case "nostr":
html += " ";
html += " ";
html += " ";
break;
case "mastodon":
case "misskey":
html += " ";
html += " ";
html += " ";
break;
}
const div = util.$("settings_newadapter_protocoloptions");
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 ()=>{};
}
}