import { _get, _post, _delete } from "../modules/common.js"; interface aWindow extends Window { setNotify(el: HTMLElement): void; deleteInvite(code: string): void; } declare var window: aWindow; const emptyInvite = (): Invite => { return { code: "None", empty: true } as Invite; } function genUsedBy(usedBy: Array>): string { let uB = ""; if (usedBy && usedBy.length != 0) { uB = ` ` } return uB; } function addItem(invite: Invite): void { const links = document.getElementById('invites'); const container = document.createElement('div') as HTMLDivElement; container.id = invite.code; const item = document.createElement('div') as HTMLDivElement; item.classList.add('list-group-item', 'd-flex', 'justify-content-between', 'd-inline-block'); let link = ""; let innerHTML = `None`; if (invite.empty) { item.innerHTML = `
${innerHTML}
`; container.appendChild(item); links.appendChild(container); return; } link = window.location.href.split('#')[0] + "invite/" + invite.code; innerHTML = `
${invite.code.replace(/-/g, '-')} `; if (invite.email) { let email = invite.email; if (!invite.email.includes("Failed to send to")) { email = `Sent to ${email}`; } innerHTML += ` ${email} `; } innerHTML += `
${invite.expiresIn}
`; item.innerHTML = innerHTML; container.appendChild(item); let profiles = ` `; let dateCreated: string; if (invite.created) { dateCreated = `
  • Created: ${invite.created}
  • `; } let middle: string; if (window.notifications_enabled) { middle = `
    `; } let right: string = genUsedBy(invite.usedBy) const dropdown = document.createElement('div') as HTMLDivElement; dropdown.id = `${CSS.escape(invite.code)}_collapse`; dropdown.classList.add("collapse"); dropdown.innerHTML = `
    • ${profiles}
    • ${dateCreated}
    • Remaining uses: ${invite.remainingUses}
    ${middle}
    ${right}
    `; container.appendChild(dropdown); links.appendChild(container); } function parseInvite(invite: Object): Invite { let inv: Invite = { code: invite["code"], empty: false, }; if (invite["email"]) { inv.email = invite["email"]; } let time = "" const f = ["days", "hours", "minutes"]; for (const i in f) { if (invite[f[i]] != 0) { time += `${invite[f[i]]}${f[i][0]} `; } } inv.expiresIn = `Expires in ${time.slice(0, -1)}`; if (invite["no-limit"]) { inv.remainingUses = "∞"; } else if ("remaining-uses" in invite) { inv.remainingUses = invite["remaining-uses"]; } if ("used-by" in invite) { inv.usedBy = invite["used-by"]; } if ("created" in invite) { inv.created = invite["created"]; } if ("notify-expiry" in invite) { inv.notifyExpiry = invite["notify-expiry"]; } if ("notify-creation" in invite) { inv.notifyCreation = invite["notify-creation"]; } if ("profile" in invite) { inv.profile = invite["profile"]; } return inv; } window.setNotify = (el: HTMLElement): void => { let send = {}; let code: string; let notifyType: string; if (el.id.includes("Expiry")) { code = el.id.replace("_notifyExpiry", ""); notifyType = "notify-expiry"; } else if (el.id.includes("Creation")) { code = el.id.replace("_notifyCreation", ""); notifyType = "notify-creation"; } send[code] = {}; send[code][notifyType] = (el as HTMLInputElement).checked; _post("/invites/notify", send, function (): void { if (this.readyState == 4 && this.status != 200) { (el as HTMLInputElement).checked = !(el as HTMLInputElement).checked; } }); } function updateInvite(invite: Invite): void { document.getElementById(invite.code + "_expiry").textContent = invite.expiresIn; const remainingUses: any = document.getElementById(CSS.escape(invite.code) + "_remainingUses"); if (remainingUses) { remainingUses.textContent = `Remaining uses: ${invite.remainingUses}`; } document.getElementById(CSS.escape(invite.code) + "_usersCreated").innerHTML = genUsedBy(invite.usedBy); } // delete invite from DOM const hideInvite = (code: string): void => document.getElementById(CSS.escape(code)).remove(); // delete invite from jfa-go window.deleteInvite = (code: string): void => _delete("/invites", { "code": code }, function (): void { if (this.readyState == 4) { generateInvites(); } }); export function generateInvites(empty?: boolean): void { if (empty) { document.getElementById('invites').textContent = ''; addItem(emptyInvite()); return; } _get("/invites", null, function (): void { if (this.readyState == 4) { let data = this.response; window.availableProfiles = data['profiles']; const Profiles = document.getElementById('inviteProfile') as HTMLSelectElement; let innerHTML = ""; for (let i = 0; i < window.availableProfiles.length; i++) { const profile = window.availableProfiles[i]; innerHTML += ` `; } innerHTML += ` `; Profiles.innerHTML = innerHTML; if (data['invites'] == null || data['invites'].length == 0) { document.getElementById('invites').textContent = ''; addItem(emptyInvite()); return; } let items = document.getElementById('invites').children; for (const i in data['invites']) { let match = false; const inv = parseInvite(data['invites'][i]); for (const x in items) { if (items[x].id == inv.code) { match = true; updateInvite(inv); break; } } if (!match) { addItem(inv); } } // second pass to check for expired invites items = document.getElementById('invites').children; for (let i = 0; i < items.length; i++) { let exists = false; for (const x in data['invites']) { if (items[i].id == data['invites'][x]['code']) { exists = true; break; } } if (!exists) { hideInvite(items[i].id); } } } }); } export const addOptions = (length: number, el: HTMLSelectElement): void => { for (let v = 0; v <= length; v++) { const opt = document.createElement('option'); opt.textContent = ""+v; opt.value = ""+v; el.appendChild(opt); } el.value = "0"; }; export function checkDuration(): void { const boxVals: Array = [+(document.getElementById("days") as HTMLSelectElement).value, +(document.getElementById("hours") as HTMLSelectElement).value, +(document.getElementById("minutes") as HTMLSelectElement).value]; const submit = document.getElementById('generateSubmit') as HTMLButtonElement; if (boxVals.reduce((a: number, b: number): number => a + b) == 0) { submit.disabled = true; } else { submit.disabled = false; } }