";
html += "";
self.innerHTML = html;
let create = $("settings_adapter_create_btn");
if (create) {
create.addEventListener("click", self.showCreateAdapter(self), false);
}
for (let a of this._adapters) {
let edit = $(`settings_adapter_edit_${a}`);
if (edit) {
edit.addEventListener("click", self.showEditAdapterFunc(a, self), false);
}
}
let connect = $("settings_connect_btn");
if (connect) {
connect.addEventListener("click", websocket.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 = $("settings_newadapter_protocolselect");
if (protocolSelect) {
protocolSelect.addEventListener("change", self.fillAdapterProtocolOptions, false);
}
let save = $("settings_adapter_create_save_btn");
if (save) {
save.addEventListener("click", self.saveAdapter(self), false);
}
let back = $("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 = $("settings_newadapter_protocolselect") as HTMLSelectElement;
const nickname = ($("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
// switch protocol
switch (proto.options[proto.selectedIndex].value) {
case "nostr":
const privkey = ($("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? "";
const relays = ($("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 = ($("settings_newadapter_masto_server") as HTMLInputElement)?.value ?? "";
const apiKey = ($("settings_newadapter_masto_apikey") as HTMLInputElement)?.value ?? "";
adapterdata = { nickname: nickname, protocol: proto.options[proto.selectedIndex].value, server: server, apiKey: apiKey };
break;
}
const settings = _("settings");
if (settings) {
if (!settings.adapters) {
settings.adapters = [];
}
settings.adapters.push(adapterdata);
self._adapters.push(adapterdata.nickname);
localStorage.setItem("settings", JSON.stringify(settings));
self.setAttribute("adapters", self._adapters.join(","));
}
}
}
fillAdapterProtocolOptions() {
const proto = $("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 = $("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 ()=>{};
}
}