Snootalogue/wwwroot/js/script.ts

61 lines
1.7 KiB
TypeScript

interface MessageBoxButton {
message: string,
onClick: (data: any) => any,
};
var standardCancelButton: MessageBoxButton = {
message: "Cancel",
onClick: function () { }
}
function DoHTTPRequest(url: string, method: string = "GET"): { success: boolean, response: string } {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
// console.log(this);
if (this.readyState == 4) {
if (this.status == 200) {
return { success: true, response: this.responseText };
} else {
return { success: false, response: `HTTP status ${this.status}` }
}
}
}
request.open(method, url);
request.send();
return { success: false, response: "Unknown error" };
}
function dgel(id: string): HTMLElement {
return document.getElementById(id);
}
function MessageBox(title: string, message: string, buttons: Array<MessageBoxButton>, data?: any) {
dgel("msgbox-title").textContent = title;
dgel("msgbox-message").textContent = message;
dgel("msgbox-buttons").innerHTML = "";
buttons.forEach(button => {
var htmlButton = document.createElement("button");
htmlButton.className = "inverted";
htmlButton.textContent = button.message;
htmlButton.addEventListener("click", function () { dgel("msgbox-holder").style.display = "none"; button.onClick(data) });
dgel("msgbox-buttons").appendChild(htmlButton);
});
dgel("msgbox-holder").style.display = "flex";
}
function PromptForDeletion(id: string, filename: string) {
MessageBox(
"Delete document?",
`Are you sure you want to delete ${filename}?`,
[{ message: "Delete", onClick: DeleteDocument }, standardCancelButton],
id
);
}
function DeleteDocument(id: string) {
DoHTTPRequest(`/api/Document/${id}`, "DELETE");
window.location = window.location;
}