Skip to content

Commit aed87fe

Browse files
committed
implement logging out
1 parent ad62d45 commit aed87fe

File tree

13 files changed

+243
-195
lines changed

13 files changed

+243
-195
lines changed

src/webpage/channel.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,7 @@ class Channel extends SnowFlake {
414414
div.style.right = window.innerWidth - rect.right + "px";
415415
document.body.append(div);
416416
Contextmenu.keepOnScreen(div);
417-
if (Contextmenu.currentmenu !== "") {
418-
Contextmenu.currentmenu.remove();
419-
}
417+
Contextmenu.declareMenu(div);
420418
this.last_pin_timestamp = this.lastpin;
421419
const l = (e: MouseEvent) => {
422420
if (e.target instanceof HTMLElement && div.contains(e.target)) {

src/webpage/contextmenu.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,40 @@ class Seperator<x, y> implements menuPart<x, y> {
117117
}
118118
}
119119
class Contextmenu<x, y> {
120-
static currentmenu: HTMLElement | "";
120+
static currentmenu: HTMLElement | "" = "";
121+
static prevmenus: HTMLElement[] = [];
121122
name: string;
122123
buttons: menuPart<x, y>[];
123124
div!: HTMLDivElement;
125+
static declareMenu(html: HTMLElement | false = false, keep: false | true | HTMLElement = false) {
126+
if (Contextmenu.currentmenu !== "") {
127+
if (keep === false) {
128+
Contextmenu.currentmenu.remove();
129+
} else if (keep === true) {
130+
this.prevmenus.push(Contextmenu.currentmenu);
131+
} else {
132+
while (Contextmenu.currentmenu && Contextmenu.currentmenu !== keep) {
133+
Contextmenu.currentmenu.remove();
134+
Contextmenu.currentmenu = this.prevmenus.pop() || "";
135+
}
136+
if (Contextmenu.currentmenu) {
137+
this.prevmenus.push(Contextmenu.currentmenu);
138+
}
139+
}
140+
}
141+
if (html) {
142+
Contextmenu.currentmenu = html;
143+
} else {
144+
Contextmenu.currentmenu = this.prevmenus.pop() || "";
145+
}
146+
}
124147
static setup() {
125-
Contextmenu.currentmenu = "";
148+
Contextmenu.declareMenu();
126149
document.addEventListener("click", (event) => {
127-
if (Contextmenu.currentmenu === "") {
128-
return;
129-
}
130-
if (!Contextmenu.currentmenu.contains(event.target as Node)) {
131-
Contextmenu.currentmenu.remove();
132-
Contextmenu.currentmenu = "";
150+
while (Contextmenu.currentmenu) {
151+
if (!Contextmenu.currentmenu.contains(event.target as Node)) {
152+
Contextmenu.declareMenu();
153+
}
133154
}
134155
});
135156
}
@@ -153,7 +174,7 @@ class Contextmenu<x, y> {
153174
addSeperator(visable?: (obj1: x, obj2: y) => boolean) {
154175
this.buttons.push(new Seperator(visable));
155176
}
156-
makemenu(x: number, y: number, addinfo: x, other: y) {
177+
makemenu(x: number, y: number, addinfo: x, other: y, keep: boolean | HTMLElement = false) {
157178
const div = document.createElement("div");
158179
div.classList.add("contextmenu", "flexttb");
159180

@@ -166,9 +187,8 @@ class Contextmenu<x, y> {
166187
//NOTE I don't know if this'll ever actually happen in reality
167188
if (div.childNodes.length === 0) return;
168189

169-
if (Contextmenu.currentmenu !== "") {
170-
Contextmenu.currentmenu.remove();
171-
}
190+
Contextmenu.declareMenu(div, keep);
191+
172192
if (y > 0) {
173193
div.style.top = y + "px";
174194
} else {
@@ -178,7 +198,7 @@ class Contextmenu<x, y> {
178198
document.body.appendChild(div);
179199
Contextmenu.keepOnScreen(div);
180200
console.log(div);
181-
Contextmenu.currentmenu = div;
201+
182202
return this.div;
183203
}
184204
bindContextmenu(
@@ -243,16 +263,17 @@ class Contextmenu<x, y> {
243263
}
244264
static keepOnScreen(obj: HTMLElement) {
245265
const html = document.documentElement.getBoundingClientRect();
246-
const docheight = html.height;
266+
const docheight = window.innerHeight;
247267
const docwidth = html.width;
248268
const box = obj.getBoundingClientRect();
249269
console.log(box, docheight, docwidth);
250270
if (box.right > docwidth) {
251271
console.log("test");
252-
obj.style.left = docwidth - box.width + "px";
272+
obj.style.left = Math.floor(docwidth - box.width) + "px";
253273
}
254274
if (box.bottom > docheight) {
255-
obj.style.top = docheight - box.height + "px";
275+
debugger;
276+
obj.style.top = Math.floor(docheight - box.height) + "px";
256277
}
257278
}
258279
}

src/webpage/emoji.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ class Emoji {
208208

209209
emojiElem.addEventListener("click", () => {
210210
res(emoji);
211-
if (Contextmenu.currentmenu !== "") {
212-
Contextmenu.currentmenu.remove();
213-
}
211+
Contextmenu.declareMenu();
214212
});
215213
}
216214
}
@@ -220,9 +218,7 @@ class Emoji {
220218
search.addEventListener("keyup", (e) => {
221219
if (e.key === "Enter" && topEmoji) {
222220
res(topEmoji);
223-
if (Contextmenu.currentmenu !== "") {
224-
Contextmenu.currentmenu.remove();
225-
}
221+
Contextmenu.declareMenu();
226222
}
227223
});
228224

@@ -291,9 +287,7 @@ class Emoji {
291287

292288
emojiElem.addEventListener("click", () => {
293289
res(emojiClass);
294-
if (Contextmenu.currentmenu !== "") {
295-
Contextmenu.currentmenu.remove();
296-
}
290+
Contextmenu.declareMenu();
297291
});
298292
}
299293
};
@@ -306,11 +300,8 @@ class Emoji {
306300
});
307301
}
308302

309-
if (Contextmenu.currentmenu !== "") {
310-
Contextmenu.currentmenu.remove();
311-
}
303+
Contextmenu.declareMenu(menu);
312304
document.body.append(menu);
313-
Contextmenu.currentmenu = menu;
314305
Contextmenu.keepOnScreen(menu);
315306
let i = 0;
316307
for (const thing of Emoji.emojis) {
@@ -330,9 +321,7 @@ class Emoji {
330321
body.append(emoji);
331322
emoji.onclick = (_) => {
332323
res(emojit);
333-
if (Contextmenu.currentmenu !== "") {
334-
Contextmenu.currentmenu.remove();
335-
}
324+
Contextmenu.declareMenu();
336325
};
337326
}
338327
};

src/webpage/invite.ts

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {I18n} from "./i18n.js";
2+
import {AccountSwitcher} from "./utils/switcher.js";
23
import {getapiurls} from "./utils/utils.js";
34
import {getBulkUsers, Specialuser} from "./utils/utils.js";
45

@@ -77,70 +78,26 @@ import {getBulkUsers, Specialuser} from "./utils/utils.js";
7778
}
7879
});
7980

80-
function showAccounts(): void {
81-
const table = document.createElement("dialog");
82-
for (const user of joinable) {
83-
console.log(user.pfpsrc);
84-
85-
const userinfo = document.createElement("div");
86-
userinfo.classList.add("flexltr", "switchtable");
87-
88-
const pfp = document.createElement("img");
89-
pfp.src = user.pfpsrc;
90-
pfp.classList.add("pfp");
91-
userinfo.append(pfp);
92-
93-
const userDiv = document.createElement("div");
94-
userDiv.classList.add("userinfo");
95-
userDiv.textContent = user.username;
96-
userDiv.append(document.createElement("br"));
97-
98-
const span = document.createElement("span");
99-
span.textContent = user.serverurls.wellknown.replace("https://", "").replace("http://", "");
100-
span.classList.add("serverURL");
101-
userDiv.append(span);
102-
103-
userinfo.append(userDiv);
104-
table.append(userinfo);
105-
106-
userinfo.addEventListener("click", () => {
107-
console.log(user);
108-
fetch(`${urls!.api}/invites/${code}`, {
109-
method: "POST",
110-
headers: {
111-
Authorization: user.token,
112-
},
113-
}).then(() => {
114-
users.currentuser = user.uid;
115-
sessionStorage.setItem("currentuser", user.uid);
116-
localStorage.setItem("userinfos", JSON.stringify(users));
117-
window.location.href = "/channels/" + guildinfo.id;
118-
});
119-
});
120-
}
121-
122-
const td = document.createElement("div");
123-
td.classList.add("switchtable");
124-
td.textContent = I18n.getTranslation("invite.loginOrCreateAccount");
125-
td.addEventListener("click", () => {
126-
const l = new URLSearchParams("?");
127-
l.set("goback", window.location.href);
128-
l.set("instance", well!);
129-
window.location.href = "/login?" + l.toString();
81+
async function showAccounts() {
82+
console.log("showing!");
83+
const user = await new AccountSwitcher((user) => {
84+
return !!(well && user.serverurls.wellknown.includes(well));
85+
}).show();
86+
fetch(`${urls!.api}/invites/${code}`, {
87+
method: "POST",
88+
headers: {
89+
Authorization: user.token,
90+
},
91+
}).then(() => {
92+
users.currentuser = user.uid;
93+
sessionStorage.setItem("currentuser", user.uid);
94+
localStorage.setItem("userinfos", JSON.stringify(users));
95+
window.location.href = "/channels/" + guildinfo.id;
13096
});
131-
132-
if (!joinable.length) {
133-
const l = new URLSearchParams("?");
134-
l.set("goback", window.location.href);
135-
l.set("instance", well!);
136-
window.location.href = "/login?" + l.toString();
137-
}
138-
139-
table.append(td);
140-
table.classList.add("flexttb", "accountSwitcher");
141-
console.log(table);
142-
document.body.append(table);
14397
}
14498

145-
document.getElementById("AcceptInvite")!.addEventListener("click", showAccounts);
99+
document.getElementById("AcceptInvite")!.addEventListener("click", (e) => {
100+
e.stopImmediatePropagation();
101+
showAccounts();
102+
});
146103
})();

0 commit comments

Comments
 (0)