diff --git a/webui/src/main/js/browse.js b/webui/src/main/js/browse.js index 7e420608..13035f9a 100644 --- a/webui/src/main/js/browse.js +++ b/webui/src/main/js/browse.js @@ -29,6 +29,18 @@ class Browse { this.receivedResults = receivedResults this.status = status this.revision = revision + this.key = null + this.descending = null + } + + setSort(key, descending) { + this.key = key + this.descending = descending + } + + getBrowseLink() { + return "" + this.host + "" } } @@ -103,7 +115,7 @@ function refreshActive() { var tableHtml = ""; for (var [host, browse] of browsesByHost) { - var browseLink = getBrowseLink(host, host) + var browseLink = browse.getBrowseLink() tableHtml += "" tableHtml += "" @@ -122,7 +134,7 @@ function refreshActive() { if (currentBrowse != null) { var newBrowse = browsesByHost.get(currentHost) if (currentBrowse.revision < newBrowse.revision) - showResults(currentHost) + showResults(currentHost, currentBrowse.key, currentBrowse.descending) } } } @@ -130,11 +142,7 @@ function refreshActive() { xmlhttp.send() } -function getBrowseLink(host, text) { - return "" + text + "" -} - -function showResults(host) { +function showResults(host, key, descending) { var browse = browsesByHost.get(host) var xmlhttp = new XMLHttpRequest() @@ -163,49 +171,68 @@ function showResults(host) { resultsByInfoHash.set(infoHash, result) } - var tableHtml = "
" + _t("Host") + "" + _t("Status") + "" + _t("Results") + "
" + browseLink + "
" + if (descending == "descending") + descending = "ascending" + else + descending = "ascending" + + var table = new Table(["Name", "Size", "Download"], "sort", key, descending) for (var [infoHash, result] of resultsByInfoHash) { - tableHtml += "" - var showComments = "" if (result.comment != null) { showComments = "
" + getShowCommentLink(infoHash) + "" showComments += "
" } - tableHtml += "" - tableHtml += "" + var nameCell = result.name + showComments + result.getCertificateBlock() + var sizeCell = result.size + var downloadCell = null + if (result.downloading == "true") - tableHtml += "" + downloadCell = _t("Downloading") else - tableHtml += "" - // TODO: show comment link - tableHtml += "" + downloadCell = getDownloadLink(host, infoHash) + + var mapping = new Map() + mapping.set("Name", nameCell) + mapping.set("Size", sizeCell) + mapping.set("Download", downloadCell) + + table.addRow(mapping) } - tableHtml += "
" + _t("Name") + "" + _t("Size") + "" + _t("Download") + "
" + result.name + showComments + result.getCertificateBlock() + "" + result.size + "" + _t("Downloading") + "" + getDownloadLink(host, infoHash) + "
" - var tableDiv = document.getElementById("resultsTable") - tableDiv.innerHTML = tableHtml + tableDiv.innerHTML = table.render() } } - xmlhttp.open("GET", "/MuWire/Browse?section=results&host="+browse.hostB64, true) + var paramString = "/MuWire/Browse?section=results&host=" + browse.hostB64 + if (key != null) + paramString += "&key=" + key + "&order=" + descending + + xmlhttp.open("GET", paramString, true) xmlhttp.send() } +function sort(key, descending) { + var currentBrowse = browsesByHost.get(currentHost) + currentBrowse.setSort(key, descending) + showResults(currentHost, key, descending) +} + function getDownloadLink(host, infoHash) { return "" + _t("Download") + "" } function download(host,infoHash) { + var currentBrowse = browsesByHost.get(host) var result = resultsByInfoHash.get(infoHash) - var hostB64 = browsesByHost.get(host).hostB64 + var hostB64 = currentBrowse.hostB64 var xmlhttp = new XMLHttpRequest() xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { - showResults(host) + showResults(host, currentBrowse.key, currentBrowse.descending) } } xmlhttp.open("POST", "/MuWire/Browse", true) diff --git a/webui/src/main/js/tables.js b/webui/src/main/js/tables.js new file mode 100644 index 00000000..5f9a9b08 --- /dev/null +++ b/webui/src/main/js/tables.js @@ -0,0 +1,53 @@ +class Column { + constructor(key) { + this.key = key + } + + render(descending, callback) { + var html = "" + var linkText = _t(this.key) + var link = "" + linkText + "" + html += link + "" + return html + } +} + +class Table { + constructor(columns, callback, key, descending) { + this.columns = columns.map(x => new Column(x)) + this.callback = callback + this.rows = [] + this.key = key + this.descending = descending + } + + addRow(mapping) { + this.rows.push(mapping) + } + + render() { + var html = "" + var i + for (i = 0;i < this.columns.length; i++) { + if (this.columns[i].key == this.key) + html += this.columns[i].render(this.descending, this.callback) + else + html += this.columns[i].render("descending", this.callback) + } + html += "" + + for (i = 0; i < this.rows.length; i++) { + html += "" + var j + for (j = 0; j < this.columns.length; j++) { + var key = this.columns[j].key + var value = this.rows[i].get(key) + html += "" + } + html += "" + } + + html += "
" + value + "
" + return html + } +} \ No newline at end of file diff --git a/webui/src/main/webapp/BrowseHost.jsp b/webui/src/main/webapp/BrowseHost.jsp index 2fde71d0..c5fc40ce 100644 --- a/webui/src/main/webapp/BrowseHost.jsp +++ b/webui/src/main/webapp/BrowseHost.jsp @@ -13,6 +13,7 @@ String pagetitle="Browse Host"; <%@ include file="css.jsi"%> +