pgweb/static/js/app.js

240 lines
5.5 KiB
JavaScript
Raw Normal View History

function apiCall(method, path, params, cb) {
2014-10-13 22:55:19 +04:00
$.ajax({
url: path,
method: method,
2014-10-13 22:55:19 +04:00
cache: false,
data: params,
2014-10-13 22:55:19 +04:00
success: function(data) {
cb(data);
},
error: function(xhr, status, data) {
cb(jQuery.parseJSON(xhr.responseText));
}
});
}
function getTables(cb) { apiCall("get", "/tables", {}, cb); }
function getTableStructure(table, cb) { apiCall("get", "/tables/" + table, {}, cb); }
function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); }
function getHistory(cb) { apiCall("get", "/history", {}, cb); }
function executeQuery(query, cb) {
apiCall("post", "/query", { query: query }, cb);
}
function explainQuery(query, cb) {
apiCall("post", "/explain", { query: query }, cb);
}
2014-10-13 22:55:19 +04:00
function loadTables() {
getTables(function(data) {
data.forEach(function(item) {
$("<li>" + item + "</li>").appendTo("#tables");
});
});
}
2014-10-14 04:49:43 +04:00
function escapeHtml(str) {
if (str != null || str != undefined) {
return jQuery("<div/>").text(str).html();
}
return "<span class='null'>null</span>";
}
2014-10-13 22:55:19 +04:00
function buildTable(results) {
$("#results").text("").removeClass("empty");
if (results.error) {
$("<tr><td>ERROR: " + results.error + "</tr></tr>").appendTo("#results");
$("#results").addClass("empty");
return;
}
if (!results.rows) {
$("<tr><td>No records found</tr></tr>").appendTo("#results");
$("#results").addClass("empty");
return;
}
var cols = "";
var rows = ""
results.columns.forEach(function(col) {
cols += "<th>" + col + "</th>";
});
results.rows.forEach(function(row) {
var r = "";
2014-10-14 04:49:43 +04:00
for (i in row) { r += "<td><div>" + escapeHtml(row[i]) + "</div></td>"; }
2014-10-13 22:55:19 +04:00
rows += "<tr>" + r + "</tr>";
});
$("<thead>" + cols + "</thead><tbody>" + rows + "</tobdy>").appendTo("#results");
}
function setCurrentTab(id) {
$("#nav ul li.selected").removeClass("selected");
$("#" + id).addClass("selected");
}
function showQueryHistory() {
getHistory(function(data) {
var rows = [];
for(i in data) {
rows.unshift([parseInt(i) + 1, data[i]]);
}
buildTable({ columns: ["id", "query"], rows: rows });
setCurrentTab("table_history");
$("#input").hide();
$("#output").addClass("full");
});
}
function showTableIndexes() {
var name = $("#tables li.selected").text();
if (name.length == 0) {
alert("Please select a table!");
return;
}
getTableIndexes(name, function(data) {
setCurrentTab("table_indexes");
buildTable(data);
$("#input").hide();
$("#output").addClass("full");
});
}
function showTableContent() {
var name = $("#tables li.selected").text();
if (name.length == 0) {
alert("Please select a table!");
return;
}
var query = "SELECT * FROM " + name + " LIMIT 100;";
executeQuery(query, function(data) {
buildTable(data);
setCurrentTab("table_content");
$("#input").hide();
$("#output").addClass("full");
});
}
function showTableStructure() {
var name = $("#tables li.selected").text();
if (name.length == 0) {
alert("Please select a table!");
return;
}
getTableStructure(name, function(data) {
setCurrentTab("table_structure");
buildTable(data);
});
}
function runQuery() {
setCurrentTab("table_query");
$("#run").attr("disabled", "disabled");
2014-10-15 06:32:46 +04:00
$("#explain").attr("disabled", "disabled");
$("#query_progress").show();
2014-10-15 06:32:46 +04:00
var query = $.trim(editor.getValue());
if (query.length == 0) {
$("#run").removeAttr("disabled");
$("#explain").removeAttr("disabled");
$("#query_progress").hide();
2014-10-15 06:32:46 +04:00
return;
}
executeQuery(query, function(data) {
2014-10-13 22:55:19 +04:00
buildTable(data);
$("#run").removeAttr("disabled");
2014-10-15 06:32:46 +04:00
$("#explain").removeAttr("disabled");
$("#query_progress").hide();
$("#input").show();
$("#output").removeClass("full");
});
}
function runExplain() {
setCurrentTab("table_query");
$("#run").attr("disabled", "disabled");
$("#explain").attr("disabled", "disabled");
$("#query_progress").show();
var query = $.trim(editor.getValue());
if (query.length == 0) {
$("#run").removeAttr("disabled");
$("#explain").removeAttr("disabled");
$("#query_progress").hide();
2014-10-15 06:32:46 +04:00
return;
}
explainQuery(query, function(data) {
2014-10-15 06:32:46 +04:00
buildTable(data);
$("#run").removeAttr("disabled");
$("#explain").removeAttr("disabled");
$("#query_progress").hide();
2014-10-13 22:55:19 +04:00
$("#input").show();
$("#output").removeClass("full");
});
}
var editor;
$(document).ready(function() {
editor = ace.edit("custom_query");
editor.getSession().setMode("ace/mode/pgsql");
editor.getSession().setTabSize(2);
editor.getSession().setUseSoftTabs(true);
2014-10-17 01:47:37 +04:00
$("#table_content").on("click", function() { showTableContent(); });
$("#table_structure").on("click", function() { showTableStructure(); });
$("#table_indexes").on("click", function() { showTableIndexes(); });
$("#table_history").on("click", function() { showQueryHistory(); });
2014-10-13 22:55:19 +04:00
$("#table_query").on("click", function() {
setCurrentTab("table_query");
$("#input").show();
$("#output").removeClass("full");
});
$("#run").on("click", function() {
runQuery();
});
2014-10-15 06:32:46 +04:00
$("#explain").on("click", function() {
runExplain();
});
2014-10-13 22:55:19 +04:00
$("#results").on("click", "tr", function() {
$("#results tr.selected").removeClass();
$(this).addClass("selected");
});
$("#tables").on("click", "li", function() {
$("#tables li.selected").removeClass("selected");
$(this).addClass("selected");
showTableContent();
});
loadTables();
});