Skip to content

Commit

Permalink
(BIDS-2624) Add the datatable query to the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisei24 committed Jan 19, 2024
1 parent 84fed6b commit 6030fd4
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
10 changes: 8 additions & 2 deletions handlers/epochs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func EpochsData(w http.ResponseWriter, r *http.Request) {

q := r.URL.Query()

emptySearch := false

if len(q.Get("search[value]")) == 0 {
emptySearch = true
}

search, err := strconv.ParseInt(q.Get("search[value]"), 10, 64)
if err != nil {
search = -1
Expand Down Expand Up @@ -77,7 +83,7 @@ func EpochsData(w http.ResponseWriter, r *http.Request) {
var epochs []*types.EpochsPageData

latestFinalizedEpoch := services.LatestFinalizedEpoch()
if search == -1 {
if emptySearch {
err = db.ReaderDb.Select(&epochs, `
SELECT epoch,
blockscount,
Expand All @@ -96,7 +102,7 @@ func EpochsData(w http.ResponseWriter, r *http.Request) {
FROM epochs
WHERE epoch >= $1 AND epoch <= $2
ORDER BY epoch DESC`, endEpoch, startEpoch, latestFinalizedEpoch)
} else {
} else if search != -1 {
err = db.ReaderDb.Select(&epochs, `
SELECT epoch,
blockscount,
Expand Down
2 changes: 2 additions & 0 deletions templates/deposits.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ordering: true,
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.start = 0
localStorage.setItem("DataTables_" + settings.sInstance, JSON.stringify(data))
},
Expand Down Expand Up @@ -53,6 +54,7 @@
ordering: true,
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.start = 0
localStorage.setItem("DataTables_" + settings.sInstance, JSON.stringify(data))
},
Expand Down
24 changes: 22 additions & 2 deletions templates/epochs.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script type="text/javascript" src="/js/datatable_loader.js"></script>
<script>
$(document).ready(function () {
$("#epochs").DataTable({
var tblOpts = {
searchDelay: 0,
processing: true,
serverSide: true,
Expand All @@ -14,6 +14,7 @@
pagingType: "input",
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.order = []
fetch(`/tables/${settings.sTableId}/state`, {
method: "PUT",
Expand Down Expand Up @@ -44,6 +45,9 @@
console.error("error loading table state, err:", data, "status: ", status)
}
var response = data !== "" ? data : JSON.parse(localStorage.getItem("DataTables_" + settings.sInstance))
if (q) {
response.search.search = q
}
callback(response)
})
.catch((err) => {
Expand Down Expand Up @@ -72,8 +76,24 @@
drawCallback: function (settings) {
formatTimestamps()
},
})
}
// $("div.toolbar").html("<b>Custom tool bar! Text/images etc.</b>")
var usp = new URLSearchParams(window.location.search)
var q = usp.get("q")
var t = $("#epochs").DataTable(tblOpts)
if (q) {
t.search(q)
t.draw()
}

$(document).on("input", "#epochs_filter > label > input", function (ev) {
if (ev && ev.target && ev.target.value) {
var newUrl = window.location.pathname + "?q=" + encodeURIComponent(ev.target.value)
window.history.replaceState(null, "Epochs", newUrl)
} else {
window.history.replaceState(null, "Epochs", window.location.pathname)
}
})
})
</script>
{{ end }}
Expand Down
9 changes: 9 additions & 0 deletions templates/eth1DepositsLeaderboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
searchDelay: 0,
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.start = 0
localStorage.setItem("DataTables_" + settings.sInstance, JSON.stringify(data))
},
Expand Down Expand Up @@ -177,6 +178,14 @@
tbl.search(q);
tbl.draw();
}
$(document).on("input", "#deposits_filter > label > input", function (ev) {
if (ev && ev.target && ev.target.value) {
var newUrl = window.location.pathname + "?q=" + encodeURIComponent(ev.target.value)
window.history.replaceState(null, "", newUrl)
} else {
window.history.replaceState(null, "", window.location.pathname)
}
})
})
</script>
{{ end }}
Expand Down
28 changes: 24 additions & 4 deletions templates/mempoolview.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<script type="text/javascript" src="/js/datatables.min.js"></script>
<script type="text/javascript" src="/js/datatable_input.js"></script>
<script type="text/javascript" src="/js/datatable_num-html.js"></script>

<script>
$("#mempool").DataTable({
$(document).ready(function () {
var tblOpts = {
searchDelay: 0,
searching: true,
pageLength: 25,
Expand All @@ -28,9 +28,29 @@
targets: 5,
orderable: true,
}
],
})
]
}

var usp = new URLSearchParams(window.location.search)
var q = usp.get("q")
var t = $("#mempool").DataTable(tblOpts)

if (q) {
t.search(q)
t.draw()
}

document.getElementById("mempool-body").classList.remove("d-none")

$(document).on("input", "#mempool_filter > label > input", function (ev) {
if (ev && ev.target && ev.target.value) {
var newUrl = window.location.pathname + "?q=" + encodeURIComponent(ev.target.value)
window.history.replaceState(null, "Mempool", newUrl)
} else {
window.history.replaceState(null, "Mempool", window.location.pathname)
}
})
})
</script>
{{ end }}

Expand Down
17 changes: 14 additions & 3 deletions templates/slot/slot.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
}

$(document).ready(function () {

let slotNumber = {{ .Slot }} || 0
var depositsTblOpts = {
searchDelay: 0,
Expand Down Expand Up @@ -195,6 +196,7 @@
ordering: false,
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.start = 0
localStorage.setItem("DataTables_" + settings.sInstance, JSON.stringify(data))
},
Expand Down Expand Up @@ -228,12 +230,21 @@
},
}

var t = $("#block_votes").DataTable(votesTblOpts)

if (q) {
votesTblOpts.search = { search: q }
t.search(q)
t.draw()
}

$("#block_votes").DataTable(votesTblOpts)

$(document).on("input", "#block_votes_filter > label > input", function (ev) {
if (ev && ev.target && ev.target.value) {
var newUrl = window.location.pathname + "?q=" + encodeURIComponent(ev.target.value) + window.location.hash
window.history.replaceState(null, "", newUrl)
} else {
window.history.replaceState(null, "", window.location.pathname + window.location.hash)
}
})

let withdrawalOpts = {
searchDelay: 0,
Expand Down
1 change: 1 addition & 0 deletions templates/slots.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ajax: dataTableLoader("/slots/data"),
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.order = []
fetch(`/tables/${settings.sTableId}/state`, {
method: "PUT",
Expand Down
2 changes: 2 additions & 0 deletions templates/withdrawals.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ordering: false,
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.order = []
fetch(`/tables/${settings.sTableId}/state`, {
method: "PUT",
Expand Down Expand Up @@ -89,6 +90,7 @@
ordering: false,
stateSave: true,
stateSaveCallback: function (settings, data) {
data.search.search = ""
data.order = []
fetch(`/tables/${settings.sTableId}/state`, {
method: "PUT",
Expand Down

0 comments on commit 6030fd4

Please sign in to comment.