mysqlExporter/static/index.html
2020-04-14 14:48:20 +02:00

203 lines
No EOL
6.1 KiB
HTML

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MySQL exporter</title>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap" rel="stylesheet">
</head>
<body>
<div id="app">Loading...</div>
<style>
body {
margin: 30px auto;
max-width: 900px;
line-height: 1.6;
font-size: 18px;
color: #444;
padding: 0 10px;
font-family: 'Noto Sans', sans-serif;
}
h1,
h2,
h3 {
font-family: Simplifica;
line-height: 1.2
}
a {
text-decoration: none;
color: rgb(0, 140, 255);
}
a:visited {
color: rgb(153, 0, 255);
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
word-break: normal;
text-align: center;
}
table {
width: 900px;
width: min(90vw, 900px);
table-layout: fixed;
}
th {
padding: 10px;
}
td {
padding: 5px;
word-break: break-all;
}
tr:hover {
background-color: rgb(214, 214, 214);
}
.privileges {
word-break: normal;
}
.null,
.key {
width: 30px;
}
.default {
width: 50px;
}
</style>
<script>
const app = document.getElementById("app");
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
(async () => {
if (window.location.hostname !== "localhost") {
console.warn("Fetching info supported only from localhost!");
return;
}
var dbRes = await fetch("/database");
var db = await dbRes.json();
if (!dbRes.ok) return console.warn("Couldn't fetch data");
app.innerText = "";
const appTitle = document.createElement("h1");
appTitle.innerText = "Data dictionary of " + db.name;
app.appendChild(appTitle);
const date = document.createElement("p");
date.innerText = "Loading table information...";
app.appendChild(date);
var res = await fetch("/tables");
var tables = await res.json();
var re = await fetch("/fieldsIgnored");
var fieldsIgnored = await re.json();
fieldsIgnored = fieldsIgnored.map(el => el.toLowerCase());
date.innerText = "Generating data dictionary...";
for (var table in tables) {
var data = tables[table];
var tableEl = document.createElement("table");
var title = document.createElement("h2");
title.innerText = table;
app.appendChild(title);
if (!data.columns) {
console.error(data);
app.appendChild(htmlToElement(`<p>${data}<p>`));
continue;
}
try {
var comment = document.createElement("p");
comment.innerText = data.comment[0].table_comment;
app.appendChild(comment);
} catch (e) {}
function addField(fieldName) {
return fieldsIgnored.includes(fieldName.toLowerCase()) ? "" : `<th class='${fieldName.toLowerCase()}'>${fieldName}</th>`;
}
var head = htmlToElement(`
<tr>
${addField("Field")}
${addField("Type")}
${addField("Collation")}
${addField("Null")}
${addField("Key")}
${addField("Default")}
${addField("Extra")}
${addField("Privileges")}
${addField("Comment")}
</tr>
`);
tableEl.appendChild(head);
for (var column of data.columns) {
var el = document.createElement("tr");
for (var field in column) {
if (fieldsIgnored.includes(field.toLowerCase())) continue;
var td = document.createElement("td");
td.className = field.toLowerCase();
switch(field.toLowerCase()) {
case "privileges":
column[field] = column[field].split(",").join(", ");
break;
case "null":
let cb = document.createElement("input");
cb.type = "checkbox";
cb.value = (column[field] === "YES");
cb.disabled = true;
column[field] = cb;
}
if (column[field]) {
if(column[field] instanceof HTMLElement) {
td.appendChild(column[field]);
} else {
td.innerText = column[field];
}
} else if (field == "Default") {
td.innerText = "null";
}
el.appendChild(td);
}
tableEl.appendChild(el);
}
app.appendChild(tableEl);
}
var d = new Date();
date.innerHTML = `Generated ${d.toString()}.
<br>Number of tables: ${Object.keys(tables).length}
<br>Host: ${db.host}`;
})()
</script>
</body>
</html>