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 += `${column}`; // update #cols document.getElementById("cols").innerHTML += `
`; document.getElementById("col_vals").innerHTML += ``; } // build body bind_to.innerHTML += ''; const body = document.getElementById("table_body"); for (const [i, row] of Object.entries(res.payload)) { let output = ""; for (const column of columns) { const value = row[column]; if (value.length > 100) { // show value in dropdown const clean_value = value .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); output += `
Long Value (${value.length}) ${clean_value}
`; } else { // just show value output += `${value .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """)}`; } } body.innerHTML += `${output}`; } $.payload = res.payload; }); function read_selected() { let text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } return text; } table_view.define("context_menu", function ({ $ }, event) { $.selected = event.target; // move context menu $.ctx.style.position = "absolute"; $.ctx.style.top = `${event.pageY}px`; $.ctx.style.left = `${event.pageX}px`; $.ctx.innerHTML = ""; document.body.appendChild($.ctx); // populate options // text options const selection = read_selected(); if (selection !== "") { $.ctx.innerHTML += ``; } // ... if (event.target) { // button if ( event.target.nodeName === "BUTTON" || event.target.nodeName === "A" ) { $.ctx.innerHTML += ``; } // column if (event.target.getAttribute("data-col")) { if (event.target.getAttribute("data-idx")) { // fill WITH value $.ctx.innerHTML += ``; } else { // fill WITHOUT value $.ctx.innerHTML += ``; } } // default options $.ctx.innerHTML += ``; $.ctx.innerHTML += ``; } }); table_view.define("copy_selection", function (_) { window.navigator.clipboard.writeText(read_selected()); }); table_view.define("click_target", function ({ $ }) { $.selected.click(); });