const table_view = reg_ns("table_view");
table_view.define("render", async function ({ $ }, bind_to, table_name) {
// get params
const search = new URLSearchParams(window.location.search);
let query = search.get("query");
let mode = search.get("mode");
if (!query || !mode) {
query = "SELECT * FROM $table LIMIT 100";
mode = "fetch";
}
// create context menu
$.ctx = document.createElement("div");
$.ctx.setAttribute("class", "flex flex-col link-list elevated round");
$.ctx.style.animation = "fadein 0.05s ease-in-out 1 running";
$.ctx.style.width = "15rem";
window.addEventListener("contextmenu", (e) => {
if (e.target && e.target.nodeName === "INPUT") {
return;
}
e.preventDefault();
$.context_menu(e);
});
window.addEventListener("click", () => {
$.ctx.remove();
});
// query builder
globalThis.set_selector = (selector) => {
for (const field of Array.from(
document.querySelectorAll('input[name="selector"]'),
)) {
field.value = selector;
}
document.getElementById("add_to_query_dialog").showModal();
};
globalThis.set_match = (idx, col) => {
const value = $.payload[idx][col]; // get value from [row][col]
for (const field of Array.from(
document.querySelectorAll('input[name="match"]'),
)) {
field.value = value;
}
document.getElementById("add_to_query_dialog").showModal();
};
globalThis.add_to_query = (keyword, e) => {
e.preventDefault();
document.getElementById("query").value +=
` ${keyword.toUpperCase()} ${e.target.value.value}`;
document.getElementById("add_to_query_dialog").close();
e.target.reset();
};
globalThis.add_to_query_eq = (keyword, e) => {
e.preventDefault();
document.getElementById("query").value +=
` ${keyword.toUpperCase()} \"${e.target.selector.value.replaceAll('"', "'")}\" = '${e.target.match.value.replaceAll("'", '"')}'`;
document.getElementById("add_to_query_dialog").close();
e.target.reset();
};
globalThis.add_to_query_neq = (keyword, e) => {
e.preventDefault();
document.getElementById("query").value +=
` ${keyword.toUpperCase()} \"${e.target.selector.value.replaceAll('"', "'")}\" != '${e.target.match.value.replaceAll("'", '"')}'`;
document.getElementById("add_to_query_dialog").close();
e.target.reset();
};
globalThis.update_query = (e) => {
e.preventDefault();
// get selected columns
let selected_columns = [];
for (const col of columns) {
if (document.getElementById(`col:${col}`).checked === false) {
continue;
}
selected_columns.push(col);
}
// build output
let output = "UPDATE $table SET ";
for (const [i, col] of Object.entries(selected_columns)) {
let ending = ",";
if (parseInt(i) === selected_columns.length - 1) {
ending = "";
}
// get value
const value = document.getElementById(`colv:${col}`).value;
if (!value) {
continue;
}
// ...
output += `"${col}" = '${value}'${ending}`;
}
// set query
document.getElementById("query").value = output;
// set mode to execute
document
.querySelector('option[value="execute"]')
.setAttribute("selected", "true");
// hide dialog
document.getElementById("update_builder_dialog").close();
// show specifier dialog
document.getElementById("add_to_query_dialog").showModal();
};
// fill input fields
document.getElementById("query").value = query;
if (mode === "execute") {
document
.querySelector('option[value="execute"]')
.setAttribute("selected", "true");
}
// warnings
if (query.includes("DELETE") && !query.includes("LIMIT")) {
if (
!confirm(
"You're attempting to execute a DELETE query with no limit. Are you sure you want to do this?",
)
) {
trigger("app:gen_secret", [
"note-info",
"Request Canceled (DELETE)",
"Make sure all your requests are safe before attempting to send them.",
]);
return;
}
} else if (query.includes("UPDATE") && !query.includes("LIMIT")) {
if (
!confirm(
"You're attempting to execute an UPDATE query with no limit. Are you sure you want to do this?",
)
) {
trigger("app:gen_secret", [
"note-info",
"Request Canceled (UPDATE)",
"Make sure all your requests are safe before attempting to send them.",
]);
return;
}
}
// send request
const res = await (
await fetch(
`/${globalThis._app_base.nested}/api/sql/${table_name}/${mode}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
}),
},
)
).json();
if (res.success === false) {
trigger("app:gen_secret", [
"note-error",
"Invalid Request",
res.message,
]);
return;
}
if (!res.payload || res.payload.length === 0) {
trigger("app:gen_secret", [
"note-note",
"Request Finished",
"Query completed with no results.",
]);
return;
} else {
trigger("app:gen_secret", [
"note-note",
"Request Finished",
`Query completed with ${res.payload.length} results.`,
]);
}
// build head
bind_to.innerHTML += '';
const head = document.getElementById("table_head");
const columns = Object.keys(res.payload[0]).sort((a, b) =>
a.localeCompare(b),
);
for (const column of columns) {
// update table
head.innerHTML += `