Skip to content

Commit d7a7dfd

Browse files
committed
Add recent sql history component
1 parent b1b7aca commit d7a7dfd

3 files changed

Lines changed: 120 additions & 1 deletion

File tree

debug-db-base/src/main/assets/app.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,88 @@ $( document ).ready(function() {
2828
$(this).addClass('selected');
2929
});
3030

31+
$( document ).on( "click", "#addb-query-history-toggle", function() {
32+
renderQueryHistoryMenu();
33+
});
34+
35+
$( document ).on( "click", ".addb-query-history-item", function(e) {
36+
e.preventDefault();
37+
var query = $(this).data("query");
38+
if (query != null) {
39+
$("#query").val(query).focus();
40+
}
41+
});
3142

3243
});
3344

3445
var isDatabaseSelected = true;
3546
var isDemoModeEnabled = false;
3647

48+
var QUERY_HISTORY_STORAGE_KEY = "addb.queryHistory.v1";
49+
var QUERY_HISTORY_MAX_ITEMS = 10;
50+
51+
function loadQueryHistory() {
52+
try {
53+
if (!window.localStorage) return [];
54+
var raw = window.localStorage.getItem(QUERY_HISTORY_STORAGE_KEY);
55+
if (!raw) return [];
56+
var parsed = JSON.parse(raw);
57+
if (!Array.isArray(parsed)) return [];
58+
return parsed.filter(function (item) { return typeof item === "string" && item.trim().length > 0; });
59+
} catch (e) {
60+
return [];
61+
}
62+
}
63+
64+
function saveQueryHistory(history) {
65+
try {
66+
if (!window.localStorage) return;
67+
window.localStorage.setItem(QUERY_HISTORY_STORAGE_KEY, JSON.stringify(history));
68+
} catch (e) {
69+
// ignore (e.g. storage disabled)
70+
}
71+
}
72+
73+
function addQueryToHistory(query) {
74+
var normalizedQuery = (query == null ? "" : String(query)).trim();
75+
if (!normalizedQuery) return;
76+
77+
var history = loadQueryHistory();
78+
79+
for (var i = history.length - 1; i >= 0; i--) {
80+
if (history[i] === normalizedQuery) {
81+
history.splice(i, 1);
82+
}
83+
}
84+
85+
history.unshift(normalizedQuery);
86+
if (history.length > QUERY_HISTORY_MAX_ITEMS) {
87+
history = history.slice(0, QUERY_HISTORY_MAX_ITEMS);
88+
}
89+
saveQueryHistory(history);
90+
}
91+
92+
function renderQueryHistoryMenu() {
93+
var menu = $("#addb-query-history-menu");
94+
if (!menu.length) return;
95+
96+
menu.empty();
97+
98+
var history = loadQueryHistory();
99+
if (!history.length) {
100+
menu.append($("<li>", { "class": "disabled" }).append($("<a>", { href: "#", tabindex: -1 }).text("No recent queries")));
101+
return;
102+
}
103+
104+
for (var i = 0; i < history.length; i++) {
105+
menu.append(
106+
$("<li>").append(
107+
$("<a>", { href: "#", "class": "addb-query-history-item" }).text(history[i]).data("query", history[i])
108+
)
109+
);
110+
}
111+
}
112+
37113
function shouldUseDemoData() {
38114
try {
39115
var search = window.location.search || "";
@@ -197,6 +273,8 @@ function queryFunction() {
197273
return;
198274
}
199275

276+
addQueryToHistory(query);
277+
200278
$.ajax({url: "query?query="+escape(query), success: function(result){
201279

202280
result = JSON.parse(result);

debug-db-base/src/main/assets/custom.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ body.addb-sidebar-compact #table-list .list-group-item {
4444
margin-right: 8px;
4545
}
4646

47+
.addb-query-history {
48+
display: inline-block;
49+
margin-left: 6px;
50+
}
51+
52+
.addb-query-history-toggle {
53+
padding: 0 4px;
54+
color: #777;
55+
}
56+
57+
.addb-query-history-toggle:hover,
58+
.addb-query-history-toggle:focus {
59+
color: #333;
60+
text-decoration: none;
61+
}
62+
63+
#addb-query-history-menu {
64+
max-width: 720px;
65+
}
66+
67+
#addb-query-history-menu > li > a {
68+
white-space: normal;
69+
word-break: break-word;
70+
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
71+
font-size: 12px;
72+
}
73+
4774
@font-face {
4875
font-family: "AddbGlyphicons";
4976
src: url("fonts/glyphicons-halflings-regular.ttf") format("truetype");

debug-db-base/src/main/assets/index.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,21 @@
109109
<div class="row padding-twenty">
110110
<div class="col-sm-12">
111111
<div class="form-group">
112-
<label for="query">Query</label>
112+
<label for="query">
113+
Query
114+
<span class="dropdown addb-query-history">
115+
<button id="addb-query-history-toggle"
116+
type="button"
117+
class="btn btn-link btn-xs dropdown-toggle addb-query-history-toggle"
118+
data-toggle="dropdown"
119+
aria-haspopup="true"
120+
aria-expanded="false"
121+
title="Recent queries">
122+
<span class="glyphicon glyphicon-time" aria-hidden="true"></span>
123+
</button>
124+
<ul class="dropdown-menu" id="addb-query-history-menu" aria-labelledby="addb-query-history-toggle"></ul>
125+
</span>
126+
</label>
113127
<input class="form-control" id="query">
114128
</div>
115129
<button id="selected-db-download" type="button" onclick="downloadDb()"

0 commit comments

Comments
 (0)