-
diff --git a/static/js/app.js b/static/js/app.js
index a3db64c..4b38c23 100644
--- a/static/js/app.js
+++ b/static/js/app.js
@@ -1,21 +1,21 @@
-var editor = null;
-var connected = false;
-var bookmarks = {};
-var default_rows_limit = 100;
-var currentObject = null;
+var editor = null;
+var connected = false;
+var bookmarks = {};
+var default_rows_limit = 100;
+var currentObject = null;
var autocompleteObjects = [];
var filterOptions = {
- "equal": "= 'DATA'",
- "not_equal": "!= 'DATA'",
- "greater": "> 'DATA'" ,
- "greater_eq": ">= 'DATA'",
- "less": "< 'DATA'",
- "less_eq": "<= 'DATA'",
- "like": "LIKE 'DATA'",
- "ilike": "ILIKE 'DATA'",
- "null": "IS NULL",
- "not_null": "IS NOT NULL"
+ equal: "= 'DATA'",
+ not_equal: "!= 'DATA'",
+ greater: "> 'DATA'",
+ greater_eq: ">= 'DATA'",
+ less: "< 'DATA'",
+ less_eq: "<= 'DATA'",
+ like: "LIKE 'DATA'",
+ ilike: "ILIKE 'DATA'",
+ null: "IS NULL",
+ not_null: "IS NOT NULL",
};
function getSessionId() {
@@ -38,7 +38,7 @@ function getRowsLimit() {
}
function getPaginationOffset() {
- var page = $(".current-page").data("page");
+ var page = $(".current-page").data("page");
var limit = getRowsLimit();
return (page - 1) * limit;
}
@@ -47,7 +47,7 @@ function getPagesCount(rowsCount) {
var limit = getRowsLimit();
var num = parseInt(rowsCount / limit);
- if ((num * limit) < rowsCount) {
+ if (num * limit < rowsCount) {
num++;
}
@@ -64,75 +64,125 @@ function apiCall(method, path, params, cb) {
cache: false,
data: params,
headers: {
- "x-session-id": getSessionId()
+ "x-session-id": getSessionId(),
},
- success: function(data) {
+ success: function (data) {
cb(data);
},
- error: function(xhr, status, data) {
+ error: function (xhr, status, data) {
if (status == "timeout") {
- return cb({ error: "Query timeout after " + (timeout / 1000) + "s" });
+ return cb({ error: "Query timeout after " + timeout / 1000 + "s" });
}
cb(jQuery.parseJSON(xhr.responseText));
- }
+ },
});
}
-function getInfo(cb) { apiCall("get", "/info", {}, cb); }
-function getObjects(cb) { apiCall("get", "/objects", {}, cb); }
-function getTables(cb) { apiCall("get", "/tables", {}, cb); }
-function getTableRows(table, opts, cb) { apiCall("get", "/tables/" + table + "/rows", opts, cb); }
-function getTableStructure(table, opts, cb) { apiCall("get", "/tables/" + table, opts, cb); }
-function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); }
-function getTableConstraints(table, cb) { apiCall("get", "/tables/" + table + "/constraints", {}, cb); }
-function getHistory(cb) { apiCall("get", "/history", {}, cb); }
-function getBookmarks(cb) { apiCall("get", "/bookmarks", {}, cb); }
-function executeQuery(query, cb) { apiCall("post", "/query", { query: query }, cb); }
-function explainQuery(query, cb) { apiCall("post", "/explain", { query: query }, cb); }
-function analyzeQuery(query, cb) { apiCall("post", "/analyze", { query: query }, cb); }
-function disconnect(cb) { apiCall("post", "/disconnect", {}, cb); }
+function getInfo(cb) {
+ apiCall("get", "/info", {}, cb);
+}
+function getObjects(cb) {
+ apiCall("get", "/objects", {}, cb);
+}
+function getTables(cb) {
+ apiCall("get", "/tables", {}, cb);
+}
+function getTableRows(table, opts, cb) {
+ apiCall("get", "/tables/" + table + "/rows", opts, cb);
+}
+function getTableStructure(table, opts, cb) {
+ apiCall("get", "/tables/" + table, opts, cb);
+}
+function getTableIndexes(table, cb) {
+ apiCall("get", "/tables/" + table + "/indexes", {}, cb);
+}
+function getTableConstraints(table, cb) {
+ apiCall("get", "/tables/" + table + "/constraints", {}, cb);
+}
+function getHistory(cb) {
+ apiCall("get", "/history", {}, cb);
+}
+function getBookmarks(cb) {
+ apiCall("get", "/bookmarks", {}, cb);
+}
+function executeQuery(query, cb) {
+ apiCall("post", "/query", { query: query }, cb);
+}
+function explainQuery(query, cb) {
+ apiCall("post", "/explain", { query: query }, cb);
+}
+function analyzeQuery(query, cb) {
+ apiCall("post", "/analyze", { query: query }, cb);
+}
+function disconnect(cb) {
+ apiCall("post", "/disconnect", {}, cb);
+}
function encodeQuery(query) {
- return Base64.encode(query).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ".");
+ return Base64.encode(query)
+ .replace(/\+/g, "-")
+ .replace(/\//g, "_")
+ .replace(/=/g, ".");
}
function buildSchemaSection(name, objects) {
var section = "";
var titles = {
- "table": "Tables",
- "view": "Views",
- "materialized_view": "Materialized Views",
- "sequence": "Sequences"
+ table: "Tables",
+ view: "Views",
+ materialized_view: "Materialized Views",
+ sequence: "Sequences",
};
var icons = {
- "table": '',
- "view": '',
- "materialized_view": '',
- "sequence": ''
+ table: '',
+ view: '',
+ materialized_view: '',
+ sequence: '',
};
var klass = "";
if (name == "public") klass = "expanded";
section += "
- " + icons[group] + " " + item + " "; + section += + "
- " + + icons[group] + + " " + + item + + " "; }); section += "
";
- section += "
" + name + "
";
+ section +=
+ " " +
+ name +
+ "
";
section += "";
- ["table", "view", "materialized_view", "sequence"].forEach(function(group) {
+ ["table", "view", "materialized_view", "sequence"].forEach(function (group) {
group_klass = "";
if (name == "public" && group == "table") group_klass = "expanded";
section += "" + col + " " + sortArrow(sortOrder) + " ";
- }
- else {
- cols += "" + col + " ";
+ cols +=
+ "" +
+ col +
+ " " +
+ sortArrow(sortOrder) +
+ " ";
+ } else {
+ cols +=
+ "" + col + " ";
}
});
@@ -343,17 +426,27 @@ function buildTable(results, sortColumn, sortOrder, options) {
action.dataColumn = results.columns.indexOf(action.data);
}
- results.rows.forEach(function(row) {
+ results.rows.forEach(function (row) {
var r = "";
// Add all actual row data here
for (i in row) {
- r += " ";
+ r +=
+ " ";
}
// Add row action button
if (action) {
- r += "" + action.title + " ";
+ r +=
+ "" +
+ action.title +
+ " ";
}
rows += "" + r + " ";
@@ -380,10 +473,10 @@ function setCurrentTab(id) {
}
function showQueryHistory() {
- getHistory(function(data) {
+ getHistory(function (data) {
var rows = [];
- for(i in data) {
+ for (i in data) {
rows.unshift([parseInt(i) + 1, data[i].query, data[i].timestamp]);
}
@@ -404,7 +497,7 @@ function showTableIndexes() {
return;
}
- getTableIndexes(name, function(data) {
+ getTableIndexes(name, function (data) {
setCurrentTab("table_indexes");
buildTable(data);
@@ -422,7 +515,7 @@ function showTableConstraints() {
return;
}
- getTableConstraints(name, function(data) {
+ getTableConstraints(name, function (data) {
setCurrentTab("table_constraints");
buildTable(data);
@@ -440,7 +533,7 @@ function showTableInfo() {
return;
}
- apiCall("get", "/tables/" + name + "/info", {}, function(data) {
+ apiCall("get", "/tables/" + name + "/info", {}, function (data) {
$(".table-information .lines").show();
$("#table_total_size").text(data.total_size);
$("#table_data_size").text(data.data_size);
@@ -460,21 +553,19 @@ function updatePaginator(pagination) {
return;
}
- $(".current-page").
- data("page", pagination.page).
- data("pages", pagination.pages_count);
+ $(".current-page")
+ .data("page", pagination.page)
+ .data("pages", pagination.pages_count);
if (pagination.page > 1) {
$(".prev-page").prop("disabled", "");
- }
- else {
+ } else {
$(".prev-page").prop("disabled", "disabled");
}
if (pagination.pages_count > 1 && pagination.page < pagination.pages_count) {
$(".next-page").prop("disabled", "");
- }
- else {
+ } else {
$(".next-page").prop("disabled", "disabled");
}
@@ -492,29 +583,29 @@ function showTableContent(sortColumn, sortOrder) {
}
var opts = {
- limit: getRowsLimit(),
- offset: getPaginationOffset(),
+ limit: getRowsLimit(),
+ offset: getPaginationOffset(),
sort_column: sortColumn,
- sort_order: sortOrder
+ sort_order: sortOrder,
};
var filter = {
column: $(".filters select.column").val(),
- op: $(".filters select.filter").val(),
- input: $(".filters input").val()
+ op: $(".filters select.filter").val(),
+ input: $(".filters input").val(),
};
// Apply filtering only if column is selected
if (filter.column && filter.op) {
var where = [
'"' + filter.column + '"',
- filterOptions[filter.op].replace("DATA", filter.input)
+ filterOptions[filter.op].replace("DATA", filter.input),
].join(" ");
opts["where"] = where;
}
- getTableRows(name, opts, function(data) {
+ getTableRows(name, opts, function (data) {
$("#input").hide();
$("#body").prop("class", "with-pagination");
@@ -552,7 +643,7 @@ function showTableStructure() {
$("#input").hide();
$("#body").prop("class", "full");
- getTableStructure(name, { type: getCurrentObject().type }, function(data) {
+ getTableStructure(name, { type: getCurrentObject().type }, function (data) {
buildTable(data);
$("#results").addClass("no-crop");
});
@@ -567,22 +658,22 @@ function showQueryPanel() {
editor.focus();
$("#input").show();
- $("#body").prop("class", "")
+ $("#body").prop("class", "");
}
function showConnectionPanel() {
setCurrentTab("table_connection");
- apiCall("get", "/connection", {}, function(data) {
+ apiCall("get", "/connection", {}, function (data) {
var rows = [];
- for(key in data) {
+ for (key in data) {
rows.push([key, data[key]]);
}
buildTable({
columns: ["attribute", "value"],
- rows: rows
+ rows: rows,
});
$("#input").hide();
@@ -596,12 +687,12 @@ function showActivityPanel() {
name: "stop_query",
title: "stop",
data: "pid",
- style: "danger"
- }
- }
+ style: "danger",
+ },
+ };
setCurrentTab("table_activity");
- apiCall("get", "/activity", {}, function(data) {
+ apiCall("get", "/activity", {}, function (data) {
buildTable(data, null, null, options);
$("#input").hide();
$("#body").addClass("full");
@@ -615,7 +706,10 @@ function showQueryProgressMessage() {
}
function hideQueryProgressMessage() {
- $("#run, #explain-dropdown-toggle, #csv, #json, #xml").prop("disabled", false);
+ $("#run, #explain-dropdown-toggle, #csv, #json, #xml").prop(
+ "disabled",
+ false
+ );
$("#query_progress").hide();
}
@@ -638,7 +732,7 @@ function getEditorSelection() {
editor.selection.setSelectionRange({
start: { row: subquery.startRow, column: 0 },
end: { row: subquery.endRow, column: 0 },
- })
+ });
}
return subquery.text;
@@ -680,7 +774,7 @@ function getSubquery(text, cursor) {
text: lines.slice(ranges[0][0], ranges[0][1]).join("\n"),
startRow: ranges[0][0],
endRow: ranges[0][1],
- numChunks: numChunks
+ numChunks: numChunks,
};
}
}
@@ -695,7 +789,7 @@ function runQuery() {
return;
}
- executeQuery(query, function(data) {
+ executeQuery(query, function (data) {
buildTable(data);
hideQueryProgressMessage();
@@ -724,7 +818,7 @@ function runExplain() {
return;
}
- explainQuery(query, function(data) {
+ explainQuery(query, function (data) {
buildTable(data);
hideQueryProgressMessage();
@@ -744,7 +838,7 @@ function runAnalyze() {
return;
}
- analyzeQuery(query, function(data) {
+ analyzeQuery(query, function (data) {
buildTable(data);
hideQueryProgressMessage();
@@ -760,8 +854,15 @@ function exportTo(format) {
return;
}
- var url = window.location.href.split("#")[0] + "api/query?format=" + format + "&query=" + encodeQuery(query) + "&_session_id=" + getSessionId();
- var win = window.open(url, '_blank');
+ var url =
+ window.location.href.split("#")[0] +
+ "api/query?format=" +
+ format +
+ "&query=" +
+ encodeQuery(query) +
+ "&_session_id=" +
+ getSessionId();
+ var win = window.open(url, "_blank");
setCurrentTab("table_query");
win.focus();
@@ -774,10 +875,17 @@ function showUniqueColumnsValues(table, column, showCounts) {
// Display results ordered by counts.
// This could be slow on large sets without an index.
if (showCounts) {
- query = 'SELECT DISTINCT "' + column + '", COUNT(1) AS total_count FROM ' + table + ' GROUP BY "' + column + '" ORDER BY total_count DESC';
+ query =
+ 'SELECT DISTINCT "' +
+ column +
+ '", COUNT(1) AS total_count FROM ' +
+ table +
+ ' GROUP BY "' +
+ column +
+ '" ORDER BY total_count DESC';
}
- executeQuery(query, function(data) {
+ executeQuery(query, function (data) {
$("#input").hide();
$("#body").prop("class", "full");
$("#results").data("mode", "query");
@@ -787,9 +895,17 @@ function showUniqueColumnsValues(table, column, showCounts) {
// Show numeric stats on the field
function showFieldNumStats(table, column) {
- var query = 'SELECT count(1), min(' + column + '), max(' + column + '), avg(' + column + ') FROM ' + table;
-
- executeQuery(query, function(data) {
+ var query =
+ "SELECT count(1), min(" +
+ column +
+ "), max(" +
+ column +
+ "), avg(" +
+ column +
+ ") FROM " +
+ table;
+
+ executeQuery(query, function (data) {
$("#input").hide();
$("#body").prop("class", "full");
$("#results").data("mode", "query");
@@ -798,15 +914,16 @@ function showFieldNumStats(table, column) {
}
function buildTableFilters(name, type) {
- getTableStructure(name, { type: type }, function(data) {
+ getTableStructure(name, { type: type }, function (data) {
if (data.rows.length == 0) {
$("#pagination .filters").hide();
- }
- else {
+ } else {
$("#pagination .filters").show();
}
- $("#pagination select.column").html("");
+ $("#pagination select.column").html(
+ ""
+ );
for (var i = 0; i < data.rows.length; i++) {
var row = data.rows[i];
@@ -820,8 +937,8 @@ function buildTableFilters(name, type) {
var objectAutocompleter = {
getCompletions: function (editor, session, pos, prefix, callback) {
callback(null, autocompleteObjects);
- }
-}
+ },
+};
function initEditor() {
var writeQueryTimeout = null;
@@ -840,32 +957,35 @@ function initEditor() {
editor.getSession().setTabSize(2);
editor.getSession().setUseSoftTabs(true);
- editor.commands.addCommands([{
- name: "run_query",
- bindKey: {
- win: "Ctrl-Enter",
- mac: "Command-Enter"
+ editor.commands.addCommands([
+ {
+ name: "run_query",
+ bindKey: {
+ win: "Ctrl-Enter",
+ mac: "Command-Enter",
+ },
+ exec: function (editor) {
+ runQuery();
+ },
},
- exec: function(editor) {
- runQuery();
- }
- }, {
- name: "explain_query",
- bindKey: {
- win: "Ctrl-E",
- mac: "Command-E"
+ {
+ name: "explain_query",
+ bindKey: {
+ win: "Ctrl-E",
+ mac: "Command-E",
+ },
+ exec: function (editor) {
+ runExplain();
+ },
},
- exec: function(editor) {
- runExplain();
- }
- }]);
+ ]);
- editor.on("change", function() {
+ editor.on("change", function () {
if (writeQueryTimeout) {
clearTimeout(writeQueryTimeout);
}
- writeQueryTimeout = setTimeout(function() {
+ writeQueryTimeout = setTimeout(function () {
localStorage.setItem("pgweb_query", editor.getValue());
}, 1000);
});
@@ -881,8 +1001,7 @@ function addShortcutTooltips() {
if (navigator.userAgent.indexOf("OS X") > 0) {
$("#run").attr("title", "Shortcut: ⌘+Enter");
$("#explain").attr("title", "Shortcut: ⌘+E");
- }
- else {
+ } else {
$("#run").attr("title", "Shortcut: Ctrl+Enter");
$("#explain").attr("title", "Shortcut: Ctrl+E");
}
@@ -891,26 +1010,35 @@ function addShortcutTooltips() {
// Get the latest release from Github API
function getLatestReleaseInfo(current) {
try {
- $.get("https://api.github.com/repos/sosedoff/pgweb/releases/latest", function(release) {
- if (release.name != current.version) {
- var message = "Update available. Check out " + release.tag_name + " on Github";
- $(".connection-settings .update").html(message).fadeIn();
+ $.get(
+ "https://api.github.com/repos/sosedoff/pgweb/releases/latest",
+ function (release) {
+ if (release.name != current.version) {
+ var message =
+ "Update available. Check out " +
+ release.tag_name +
+ " on Github";
+ $(".connection-settings .update").html(message).fadeIn();
+ }
}
- });
- }
- catch(error) {
+ );
+ } catch (error) {
console.log("Cant get last release from github:", error);
}
}
function showConnectionSettings() {
// Fetch server info
- getInfo(function(data) {
+ getInfo(function (data) {
if (data.error) return;
if (!data.version) return;
// Show the current postgres version
- $(".connection-settings .version").text("v" + data.version).show();
+ $(".connection-settings .version")
+ .text("v" + data.version)
+ .show();
// Check for updates if running the actual release from Github
if (data.git_sha == "") {
@@ -918,7 +1046,7 @@ function showConnectionSettings() {
}
});
- getBookmarks(function(data) {
+ getBookmarks(function (data) {
// Do not add any bookmarks if we've got an error
if (data.error) {
return;
@@ -936,12 +1064,13 @@ function showConnectionSettings() {
// Add all available bookmarks
for (key in data) {
- $("").appendTo("#connection_bookmarks");
+ $("").appendTo(
+ "#connection_bookmarks"
+ );
}
$(".bookmarks").show();
- }
- else {
+ } else {
$(".bookmarks").hide();
}
});
@@ -950,25 +1079,37 @@ function showConnectionSettings() {
}
function getConnectionString() {
- var url = $.trim($("#connection_url").val());
+ var url = $.trim($("#connection_url").val());
var mode = $(".connection-group-switch button.active").attr("data");
- var ssl = $("#connection_ssl").val();
+ var ssl = $("#connection_ssl").val();
if (mode == "standard" || mode == "ssh") {
var host = $("#pg_host").val();
var port = $("#pg_port").val();
var user = $("#pg_user").val();
var pass = encodeURIComponent($("#pg_password").val());
- var db = $("#pg_db").val();
+ var db = $("#pg_db").val();
if (port.length == 0) {
port = "5432";
}
- url = "postgres://" + user + ":" + pass + "@" + host + ":" + port + "/" + db + "?sslmode=" + ssl;
- }
- else {
- var local = url.indexOf("localhost") != -1 || url.indexOf("127.0.0.1") != -1;
+ url =
+ "postgres://" +
+ user +
+ ":" +
+ pass +
+ "@" +
+ host +
+ ":" +
+ port +
+ "/" +
+ db +
+ "?sslmode=" +
+ ssl;
+ } else {
+ var local =
+ url.indexOf("localhost") != -1 || url.indexOf("127.0.0.1") != -1;
if (local && url.indexOf("sslmode") == -1) {
url += "?sslmode=" + ssl;
@@ -983,7 +1124,7 @@ function bindTableHeaderMenu() {
$("#results_header").contextmenu({
scopes: "th",
target: "#results_header_menu",
- before: function(e, element, target) {
+ before: function (e, element, target) {
// Enable menu for browsing table rows view only.
if ($("#results").data("mode") != "browse") {
e.preventDefault();
@@ -991,10 +1132,10 @@ function bindTableHeaderMenu() {
return false;
}
},
- onItem: function(context, e) {
+ onItem: function (context, e) {
var menuItem = $(e.target);
- switch(menuItem.data("action")) {
+ switch (menuItem.data("action")) {
case "copy_name":
copyToClipboard($(context).data("name"));
break;
@@ -1002,28 +1143,28 @@ function bindTableHeaderMenu() {
case "unique_values":
showUniqueColumnsValues(
$("#results").data("table"), // table name
- $(context).data("name"), // column name
- menuItem.data("counts") // display counts
+ $(context).data("name"), // column name
+ menuItem.data("counts") // display counts
);
break;
case "num_stats":
showFieldNumStats(
$("#results").data("table"), // table name
- $(context).data("name") // column name
+ $(context).data("name") // column name
);
break;
}
- }
+ },
});
$("#results_body").contextmenu({
scopes: "td",
target: "#results_row_menu",
- before: function(e, element, target) {
+ before: function (e, element, target) {
var browseMode = $("#results").data("mode");
- var isEmpty = $("#results").hasClass("empty");
- var isAllowed = browseMode == "browse" || browseMode == "query";
+ var isEmpty = $("#results").hasClass("empty");
+ var isAllowed = browseMode == "browse" || browseMode == "query";
if (isEmpty || !isAllowed) {
e.preventDefault();
@@ -1031,41 +1172,44 @@ function bindTableHeaderMenu() {
return false;
}
},
- onItem: function(context, e) {
+ onItem: function (context, e) {
var menuItem = $(e.target);
- switch(menuItem.data("action")) {
+ switch (menuItem.data("action")) {
case "copy_value":
copyToClipboard($(context).text());
break;
case "filter_by_value":
- var colIdx = $(context).data("col");
+ var colIdx = $(context).data("col");
var colValue = $(context).text();
- var colName = $("#results_header th").eq(colIdx).data("name");
+ var colName = $("#results_header th").eq(colIdx).data("name");
$("select.column").val(colName);
$("select.filter").val("equal");
$("#table_filter_value").val(colValue);
$("#rows_filter").submit();
}
- }
+ },
});
}
function bindCurrentDatabaseMenu() {
$("#current_database").contextmenu({
target: "#current_database_context_menu",
- onItem: function(context, e) {
+ onItem: function (context, e) {
var menuItem = $(e.target);
- switch(menuItem.data("action")) {
+ switch (menuItem.data("action")) {
case "export":
- var url = window.location.href.split("#")[0] + "api/export?_session_id=" + getSessionId();
- var win = window.open(url, "_blank");
+ var url =
+ window.location.href.split("#")[0] +
+ "api/export?_session_id=" +
+ getSessionId();
+ var win = window.open(url, "_blank");
win.focus();
break;
}
- }
+ },
});
}
@@ -1087,11 +1231,11 @@ function bindDatabaseObjectsFilter() {
$(".schema-group").addClass("expanded");
filterTimeout = setTimeout(function () {
- filterObjectsByName(val)
+ filterObjectsByName(val);
}, 200);
});
- $(".clear-objects-filter").on("click", function(e) {
+ $(".clear-objects-filter").on("click", function (e) {
resetObjectsFilter();
});
}
@@ -1118,7 +1262,13 @@ function filterObjectsByName(query) {
function getQuotedSchemaTableName(table) {
if (typeof table === "string" && table.indexOf(".") > -1) {
var schemaTableComponents = table.split(".");
- return ['"', schemaTableComponents[0], '"."', schemaTableComponents[1], '"'].join('');
+ return [
+ '"',
+ schemaTableComponents[0],
+ '"."',
+ schemaTableComponents[1],
+ '"',
+ ].join("");
}
return table;
}
@@ -1127,19 +1277,19 @@ function bindContextMenus() {
bindTableHeaderMenu();
bindCurrentDatabaseMenu();
- $(".schema-group ul").each(function(id, el) {
+ $(".schema-group ul").each(function (id, el) {
var group = $(el).data("group");
if (group == "table") {
$(el).contextmenu({
target: "#tables_context_menu",
scopes: "li.schema-table",
- onItem: function(context, e) {
- var el = $(e.target);
- var table = getQuotedSchemaTableName($(context[0]).data("id"));
- var action = el.data("action");
+ onItem: function (context, e) {
+ var el = $(e.target);
+ var table = getQuotedSchemaTableName($(context[0]).data("id"));
+ var action = el.data("action");
performTableAction(table, action, el);
- }
+ },
});
}
@@ -1147,12 +1297,12 @@ function bindContextMenus() {
$(el).contextmenu({
target: "#view_context_menu",
scopes: "li.schema-view",
- onItem: function(context, e) {
- var el = $(e.target);
- var table = getQuotedSchemaTableName($(context[0]).data("id"));
- var action = el.data("action");
+ onItem: function (context, e) {
+ var el = $(e.target);
+ var table = getQuotedSchemaTableName($(context[0]).data("id"));
+ var action = el.data("action");
performViewAction(table, action, el);
- }
+ },
});
}
});
@@ -1173,68 +1323,85 @@ function enableDatabaseSearch(data) {
minLength: 0,
items: "all",
autoSelect: false,
- fitToElement: true
+ fitToElement: true,
});
input.typeahead("lookup").focus();
- input.on("focusout", function(e){
+ input.on("focusout", function (e) {
toggleDatabaseSearch();
input.off("focusout");
});
}
-$(document).ready(function() {
- $("#table_content").on("click", function() { showTableContent(); });
- $("#table_structure").on("click", function() { showTableStructure(); });
- $("#table_indexes").on("click", function() { showTableIndexes(); });
- $("#table_constraints").on("click", function() { showTableConstraints(); });
- $("#table_history").on("click", function() { showQueryHistory(); });
- $("#table_query").on("click", function() { showQueryPanel(); });
- $("#table_connection").on("click", function() { showConnectionPanel(); });
- $("#table_activity").on("click", function() { showActivityPanel(); });
+function start() {
+ $("body").removeClass("hidden");
+ $("#table_content").on("click", function () {
+ showTableContent();
+ });
+ $("#table_structure").on("click", function () {
+ showTableStructure();
+ });
+ $("#table_indexes").on("click", function () {
+ showTableIndexes();
+ });
+ $("#table_constraints").on("click", function () {
+ showTableConstraints();
+ });
+ $("#table_history").on("click", function () {
+ showQueryHistory();
+ });
+ $("#table_query").on("click", function () {
+ showQueryPanel();
+ });
+ $("#table_connection").on("click", function () {
+ showConnectionPanel();
+ });
+ $("#table_activity").on("click", function () {
+ showActivityPanel();
+ });
- $("#run").on("click", function() {
+ $("#run").on("click", function () {
runQuery();
});
- $("#explain").on("click", function() {
+ $("#explain").on("click", function () {
runExplain();
});
- $("#analyze").on("click", function() {
+ $("#analyze").on("click", function () {
runAnalyze();
});
- $("#csv").on("click", function() {
+ $("#csv").on("click", function () {
exportTo("csv");
});
- $("#json").on("click", function() {
+ $("#json").on("click", function () {
exportTo("json");
});
- $("#xml").on("click", function() {
+ $("#xml").on("click", function () {
exportTo("xml");
});
- $("#results").on("click", "tr", function(e) {
+ $("#results").on("click", "tr", function (e) {
$("#results tr.selected").removeClass();
$(this).addClass("selected");
});
- $("#objects").on("click", ".schema-group-title", function(e) {
+ $("#objects").on("click", ".schema-group-title", function (e) {
$(this).parent().toggleClass("expanded");
});
- $("#objects").on("click", ".schema-name", function(e) {
+ $("#objects").on("click", ".schema-name", function (e) {
$(this).parent().toggleClass("expanded");
});
- $("#objects").on("click", "li", function(e) {
+ $("#objects").on("click", "li", function (e) {
currentObject = {
name: $(this).data("id"),
- type: $(this).data("type")
+ type: $(this).data("type"),
};
$("#objects li").removeClass("active");
@@ -1244,7 +1411,7 @@ $(document).ready(function() {
showTableInfo();
- switch(sessionStorage.getItem("tab")) {
+ switch (sessionStorage.getItem("tab")) {
case "table_content":
showTableContent();
break;
@@ -1262,46 +1429,46 @@ $(document).ready(function() {
}
});
- $("#results").on("click", "a.row-action", function(e) {
+ $("#results").on("click", "a.row-action", function (e) {
e.preventDefault();
var action = $(this).data("action");
- var value = $(this).data("value");
+ var value = $(this).data("value");
performRowAction(action, value);
- })
+ });
- $("#results").on("click", "th", function(e) {
+ $("#results").on("click", "th", function (e) {
if (!$("#table_content").hasClass("selected")) return;
var sortColumn = $(this).data("name");
- var sortOrder = $(this).data("order") === "ASC" ? "DESC" : "ASC";
+ var sortOrder = $(this).data("order") === "ASC" ? "DESC" : "ASC";
$(this).data("order", sortOrder);
showTableContent(sortColumn, sortOrder);
});
- $("#refresh_tables").on("click", function() {
+ $("#refresh_tables").on("click", function () {
loadSchemas();
});
- $("#rows_filter").on("submit", function(e) {
+ $("#rows_filter").on("submit", function (e) {
e.preventDefault();
$(".current-page").data("page", 1);
var column = $(this).find("select.column").val();
var filter = $(this).find("select.filter").val();
- var query = $.trim($(this).find("input").val());
+ var query = $.trim($(this).find("input").val());
if (filter && filterOptions[filter].indexOf("DATA") > 0 && query == "") {
alert("Please specify filter query");
- return
+ return;
}
showTableContent();
});
- $(".change-limit").on("click", function() {
+ $(".change-limit").on("click", function () {
var limit = prompt("Please specify a new rows limit", getRowsLimit());
if (limit && limit >= 1) {
@@ -1311,33 +1478,32 @@ $(document).ready(function() {
}
});
- $("select.filter").on("change", function(e) {
+ $("select.filter").on("change", function (e) {
var val = $(this).val();
if (["null", "not_null"].indexOf(val) >= 0) {
$(".filters input").hide().val("");
- }
- else {
+ } else {
$(".filters input").show();
}
});
- $("button.reset-filters").on("click", function() {
+ $("button.reset-filters").on("click", function () {
$(".filters select, .filters input").val("");
showTableContent();
});
// Automatically prefill the filter if it's not set yet
- $("select.column").on("change", function() {
+ $("select.column").on("change", function () {
if ($("select.filter").val() == "") {
$("select.filter").val("equal");
$("#table_filter_value").focus();
}
});
- $("#pagination .next-page").on("click", function() {
+ $("#pagination .next-page").on("click", function () {
var current = $(".current-page").data("page");
- var total = $(".current-page").data("pages");
+ var total = $(".current-page").data("pages");
if (total > current) {
$(".current-page").data("page", current + 1);
@@ -1353,7 +1519,7 @@ $(document).ready(function() {
}
});
- $("#pagination .prev-page").on("click", function() {
+ $("#pagination .prev-page").on("click", function () {
var current = $(".current-page").data("page");
if (current > 1) {
@@ -1367,27 +1533,27 @@ $(document).ready(function() {
}
});
- $("#current_database").on("click", function(e) {
- apiCall("get", "/databases", {}, function(resp) {
+ $("#current_database").on("click", function (e) {
+ apiCall("get", "/databases", {}, function (resp) {
toggleDatabaseSearch();
enableDatabaseSearch(resp);
});
});
- $("#database_search").change(function(e) {
+ $("#database_search").change(function (e) {
var current = $("#database_search").typeahead("getActive");
if (current && current == $("#database_search").val()) {
- apiCall("post", "/switchdb", { db: current }, function(resp) {
+ apiCall("post", "/switchdb", { db: current }, function (resp) {
if (resp.error) {
alert(resp.error);
return;
- };
+ }
window.location.reload();
});
- };
+ }
});
- $("#edit_connection").on("click", function() {
+ $("#edit_connection").on("click", function () {
if (connected) {
$("#close_connection_window").show();
}
@@ -1395,27 +1561,27 @@ $(document).ready(function() {
showConnectionSettings();
});
- $("#close_connection").on("click", function() {
+ $("#close_connection").on("click", function () {
if (!confirm("Are you sure you want to disconnect?")) return;
- disconnect(function() {
+ disconnect(function () {
showConnectionSettings();
resetTable();
$("#close_connection_window").hide();
});
});
- $("#close_connection_window").on("click", function() {
+ $("#close_connection_window").on("click", function () {
$("#connection_window").hide();
});
- $("#connection_url").on("change", function() {
+ $("#connection_url").on("change", function () {
if ($(this).val().indexOf("localhost") != -1) {
$("#connection_ssl").val("disable");
}
});
- $("#pg_host").on("change", function() {
+ $("#pg_host").on("change", function () {
var value = $(this).val();
if (value.indexOf("localhost") != -1 || value.indexOf("127.0.0.1") != -1) {
@@ -1423,11 +1589,11 @@ $(document).ready(function() {
}
});
- $(".connection-group-switch button").on("click", function() {
+ $(".connection-group-switch button").on("click", function () {
$(".connection-group-switch button").removeClass("active");
$(this).addClass("active");
- switch($(this).attr("data")) {
+ switch ($(this).attr("data")) {
case "scheme":
$(".connection-scheme-group").show();
$(".connection-standard-group").hide();
@@ -1446,7 +1612,7 @@ $(document).ready(function() {
}
});
- $("#connection_bookmarks").on("change", function(e) {
+ $("#connection_bookmarks").on("change", function (e) {
var name = $.trim($(this).val());
if (name == "") return;
@@ -1475,8 +1641,7 @@ $(document).ready(function() {
$("#ssh_key").val(item.ssh.key);
$("#ssh_key_password").val(item.ssh.keypassword);
$("#connection_ssh").click();
- }
- else {
+ } else {
$("#ssh_host").val("");
$("#ssh_port").val("");
$("#ssh_user").val("");
@@ -1488,12 +1653,12 @@ $(document).ready(function() {
}
});
- $("#connection_form").on("submit", function(e) {
+ $("#connection_form").on("submit", function (e) {
e.preventDefault();
var button = $(this).find("button.open-connection");
var params = {
- url: getConnectionString()
+ url: getConnectionString(),
};
if (params.url.length == 0) {
@@ -1501,26 +1666,25 @@ $(document).ready(function() {
}
if ($(".connection-group-switch button.active").attr("data") == "ssh") {
- params["ssh"] = 1
- params["ssh_host"] = $("#ssh_host").val();
- params["ssh_port"] = $("#ssh_port").val();
- params["ssh_user"] = $("#ssh_user").val();
- params["ssh_password"] = $("#ssh_password").val();
- params["ssh_key"] = $("#ssh_key").val();
- params["ssh_key_password"] = $("#ssh_key_password").val()
+ params["ssh"] = 1;
+ params["ssh_host"] = $("#ssh_host").val();
+ params["ssh_port"] = $("#ssh_port").val();
+ params["ssh_user"] = $("#ssh_user").val();
+ params["ssh_password"] = $("#ssh_password").val();
+ params["ssh_key"] = $("#ssh_key").val();
+ params["ssh_key_password"] = $("#ssh_key_password").val();
}
$("#connection_error").hide();
button.prop("disabled", true).text("Please wait...");
- apiCall("post", "/connect", params, function(resp) {
+ apiCall("post", "/connect", params, function (resp) {
button.prop("disabled", false).text("Connect");
if (resp.error) {
connected = false;
$("#connection_error").text(resp.error).show();
- }
- else {
+ } else {
connected = true;
loadSchemas();
@@ -1543,13 +1707,12 @@ $(document).ready(function() {
window.history.pushState({}, document.title, window.location.pathname);
}
- apiCall("get", "/connection", {}, function(resp) {
+ apiCall("get", "/connection", {}, function (resp) {
if (resp.error) {
connected = false;
showConnectionSettings();
$(".connection-actions").show();
- }
- else {
+ } else {
connected = true;
loadSchemas();
@@ -1563,4 +1726,40 @@ $(document).ready(function() {
});
bindDatabaseObjectsFilter();
+}
+
+function goAuthorize(data, cb) {
+ const id = data.id;
+ const token = data.token;
+ if (!id || !token) {
+ console.error("用户信息错误!");
+ }
+ $.ajax({
+ type: "POST",
+ url: "",
+ data: "",
+ success: function () {
+ cb();
+ },
+ error: function () {
+ console.error("client用户信息错误!");
+ },
+ });
+}
+
+var isAuthentic = false;
+
+function handleMessage(e) {
+ if (!isAuthentic) {
+ goAuthorize(e.data, function () {
+ isAuthentic = true;
+ window.removeEventListener("message", handleMessage);
+ start();
+ });
+ }
+}
+
+$(document).ready(function () {
+ window.addEventListener("message", handleMessage);
+ // start();
});
--
Gitee
";
- section += "
";
}
@@ -146,13 +196,13 @@ function buildSchemaSection(name, objects) {
function loadSchemas() {
$("#objects").html("");
- getObjects(function(data) {
+ getObjects(function (data) {
if (Object.keys(data).length == 0) {
data["public"] = {
table: [],
view: [],
materialized_view: [],
- sequence: []
+ sequence: [],
};
}
@@ -168,14 +218,16 @@ function loadSchemas() {
autocompleteObjects = [];
for (schema in data) {
for (kind in data[schema]) {
- if (!(kind == "table" || kind == "view" || kind == "materialized_view")) {
- continue
+ if (
+ !(kind == "table" || kind == "view" || kind == "materialized_view")
+ ) {
+ continue;
}
for (item in data[schema][kind]) {
autocompleteObjects.push({
caption: data[schema][kind][item],
value: data[schema][kind][item],
- meta: kind
+ meta: kind,
});
}
}
@@ -193,7 +245,7 @@ function escapeHtml(str) {
return "null";
}
-function unescapeHtml(str){
+function unescapeHtml(str) {
var e = document.createElement("div");
e.innerHTML = str;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
@@ -204,10 +256,7 @@ function getCurrentObject() {
}
function resetTable() {
- $("#results").
- data("mode", "").
- removeClass("empty").
- removeClass("no-crop");
+ $("#results").data("mode", "").removeClass("empty").removeClass("no-crop");
$("#results_header").html("");
$("#results_body").html("");
@@ -215,19 +264,20 @@ function resetTable() {
function performTableAction(table, action, el) {
if (action == "truncate" || action == "delete") {
- var message = "Are you sure you want to " + action + " table " + table + " ?";
+ var message =
+ "Are you sure you want to " + action + " table " + table + " ?";
if (!confirm(message)) return;
}
- switch(action) {
+ switch (action) {
case "truncate":
- executeQuery("TRUNCATE TABLE " + table, function(data) {
+ executeQuery("TRUNCATE TABLE " + table, function (data) {
if (data.error) alert(data.error);
resetTable();
});
break;
case "delete":
- executeQuery("DROP TABLE " + table, function(data) {
+ executeQuery("DROP TABLE " + table, function (data) {
if (data.error) alert(data.error);
loadSchemas();
resetTable();
@@ -238,17 +288,31 @@ function performTableAction(table, action, el) {
var db = $("#current_database").text();
var filename = db + "." + table + "." + format;
var query = window.encodeURI("SELECT * FROM " + table);
- var url = window.location.href.split("#")[0] + "api/query?format=" + format + "&filename=" + filename + "&query=" + query + "&_session_id=" + getSessionId();
- var win = window.open(url, "_blank");
+ var url =
+ window.location.href.split("#")[0] +
+ "api/query?format=" +
+ format +
+ "&filename=" +
+ filename +
+ "&query=" +
+ query +
+ "&_session_id=" +
+ getSessionId();
+ var win = window.open(url, "_blank");
win.focus();
break;
case "dump":
- var url = window.location.href.split("#")[0] + "api/export?table=" + table + "&_session_id=" + getSessionId();
- var win = window.open(url, "_blank");
+ var url =
+ window.location.href.split("#")[0] +
+ "api/export?table=" +
+ table +
+ "&_session_id=" +
+ getSessionId();
+ var win = window.open(url, "_blank");
win.focus();
break;
case "copy":
- copyToClipboard(table.split('.')[1]);
+ copyToClipboard(table.split(".")[1]);
break;
}
}
@@ -259,9 +323,9 @@ function performViewAction(view, action, el) {
if (!confirm(message)) return;
}
- switch(action) {
+ switch (action) {
case "delete":
- executeQuery("DROP VIEW " + view, function(data) {
+ executeQuery("DROP VIEW " + view, function (data) {
if (data.error) alert(data.error);
loadSchemas();
resetTable();
@@ -272,12 +336,21 @@ function performViewAction(view, action, el) {
var db = $("#current_database").text();
var filename = db + "." + view + "." + format;
var query = window.encodeURI("SELECT * FROM " + view);
- var url = window.location.href.split("#")[0] + "api/query?format=" + format + "&filename=" + filename + "&query=" + query + "&_session_id=" + getSessionId();
- var win = window.open(url, "_blank");
+ var url =
+ window.location.href.split("#")[0] +
+ "api/query?format=" +
+ format +
+ "&filename=" +
+ filename +
+ "&query=" +
+ query +
+ "&_session_id=" +
+ getSessionId();
+ var win = window.open(url, "_blank");
win.focus();
break;
case "copy":
- copyToClipboard(view.split('.')[1]);
+ copyToClipboard(view.split(".")[1]);
break;
}
}
@@ -285,7 +358,7 @@ function performViewAction(view, action, el) {
function performRowAction(action, value) {
if (action == "stop_query") {
if (!confirm("Are you sure you want to stop the query?")) return;
- executeQuery("SELECT pg_cancel_backend(" + value + ");", function(data) {
+ executeQuery("SELECT pg_cancel_backend(" + value + ");", function (data) {
if (data.error) alert(data.error);
setTimeout(showActivityPanel, 1000);
});
@@ -326,12 +399,22 @@ function buildTable(results, sortColumn, sortOrder, options) {
var cols = "";
var rows = "";
- results.columns.forEach(function(col) {
+ results.columns.forEach(function (col) {
if (col === sortColumn) {
- cols += " " + titles[group] + " " + objects[group].length + "
";
+ section +=
+ " " +
+ titles[group] +
+ " " +
+ objects[group].length +
+ "
";
section += "- ";
if (objects[group]) {
- objects[group].forEach(function(item) {
+ objects[group].forEach(function (item) {
var id = name + "." + item;
- section += "
" + escapeHtml(row[i]) + "
" + escapeHtml(row[i]) + "