From 1dbd94d52d9c1a76aca5eae9b55791b9fcafd770 Mon Sep 17 00:00:00 2001 From: zzz <zzz@i2pmail.org> Date: Wed, 18 Jan 2023 11:15:22 -0500 Subject: [PATCH] i2psnark: Search CSS and JS WIP, more polishing todo default theme only, others to follow --- .../org/klomp/snark/web/I2PSnarkServlet.java | 19 +++++--- apps/i2psnark/resources/js/initajax.js | 44 ++++++++++++++++++- apps/i2psnark/resources/js/search.js | 32 ++++++++++++++ .../resources/themes/ubergine/snark.css | 22 ++++++++++ apps/routerconsole/jsp/js/ajax.js | 14 +++++- 5 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 apps/i2psnark/resources/js/search.js diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java b/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java index 25856facd3..2bc68376aa 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java +++ b/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java @@ -342,7 +342,8 @@ public class I2PSnarkServlet extends BasicServlet { "var deleteMessage1 = \"" + _t("Are you sure you want to delete the file \\''{0}\\'' (downloaded data will not be deleted) ?") + "\";\n" + "var deleteMessage2 = \"" + _t("Are you sure you want to delete the torrent \\''{0}\\'' and all downloaded data?") + "\";\n" + "</script>\n" + - "<script src=\".resources/js/delete.js?" + CoreVersion.VERSION + "\" type=\"text/javascript\"></script>\n"); + "<script src=\".resources/js/delete.js?" + CoreVersion.VERSION + "\" type=\"text/javascript\"></script>\n" + + "<script src=\".resources/js/search.js?" + CoreVersion.VERSION + "\" type=\"text/javascript\"></script>\n"); } out.write(HEADER_A + _themePath + HEADER_B); @@ -381,18 +382,24 @@ public class I2PSnarkServlet extends BasicServlet { out.write(" <a href=\"" + t.baseURL + "\" class=\"snarkNav nav_tracker\" target=\"_blank\">" + t.name + "</a>\n"); } } - if (_manager.getTorrents().size() > 1) { + } + // end snarkNavBar + out.write("</div>\n"); + + if (!isConfigure) { + String search = req.getParameter("s"); + if (_manager.getTorrents().size() > 1 || (search != null && search.length() > 0)) { out.write("<form class=\"search\" id = \"search\" action=\"" + _contextPath + "\" method=\"GET\">" + - "<input type=\"text\" name=\"s\" size=\"20\" class=\"search snarkNav\" id=\"searchbox\""); + "<input type=\"text\" name=\"s\" size=\"20\" class=\"search\" id=\"searchbox\""); String s = req.getParameter("s"); if (s != null) out.write(" value=\"" + DataHelper.escapeHTML(s) + '"'); out.write(">" + - "<input type=\"reset\" class=\"cancel snarkNav\" value=\"\">" + + "<input type=\"reset\" class=\"cancel\" id=\"searchcancel\" value=\"\">" + "</form>\n"); } } - out.write("</div>\n"); + String newURL = req.getParameter("newURL"); if (newURL != null && newURL.trim().length() > 0 && req.getMethod().equals("GET")) _manager.addMessage(_t("Click \"Add torrent\" button to fetch torrent")); @@ -846,7 +853,7 @@ public class I2PSnarkServlet extends BasicServlet { * * @param search non-null * @param snarks unmodified - * @return null if no valid search, or matching torrents in same order, empty if no match + * @return null if no valid search, or matching torrents in same order * @since 0.9.58 */ private static List<Snark> search(String search, Collection<Snark> snarks) { diff --git a/apps/i2psnark/resources/js/initajax.js b/apps/i2psnark/resources/js/initajax.js index 3a1d6da397..87de15cd36 100644 --- a/apps/i2psnark/resources/js/initajax.js +++ b/apps/i2psnark/resources/js/initajax.js @@ -4,12 +4,54 @@ // as a formal dedication to the public domain and in circumstances where // a public domain is not usable. +/** + * Use the refresh delay set in head + */ function requestAjax1() { + requestAjax2(ajaxDelay); +} + +/** + * Use the refresh delay specified + * + * @param refreshtime do not refresh if less than or equal to 0 + * @since 0.9.58 + */ +function requestAjax2(refreshtime) { var url = ".ajax/xhr1.html"; var query = window.location.search; + var box = document.getElementById("searchbox"); + if (box != null) { + // put in, remove, or replace value from search form + var search = box.value; + if (search.length > 0) { + if (query == null) { + query = ""; + } + var q = new URLSearchParams(query); + q.set("s", search); + query = "?" + q.toString(); + } else { + if (query != null) { + var q = new URLSearchParams(query); + q.delete("s"); + var newq = q.toString(); + if (newq != null && newq.length > 0) { + query = "?" + newq; + } else { + query = null; + } + } + } + } if (query) url += query; - ajax(url, "mainsection", ajaxDelay); + // tell ajax not to setTimeout(), and do our own + // setTimeout() so we update the URL every time + ajax(url, "mainsection", -1); + if (refreshtime > 0) { + setTimeout(requestAjax1, refreshtime); + } } function initAjax() { diff --git a/apps/i2psnark/resources/js/search.js b/apps/i2psnark/resources/js/search.js new file mode 100644 index 0000000000..67339a5e77 --- /dev/null +++ b/apps/i2psnark/resources/js/search.js @@ -0,0 +1,32 @@ +/* @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 */ +/* see also licenses/LICENSE-GPLv2.txt */ + +/** + * Search form helpers + * + * @since 0.9.58 + */ +function initSearch() +{ + var sch = document.getElementById("search"); + if (sch != null) { + var box = document.getElementById("searchbox"); + sch.addEventListener("reset", function(event) { + if (box.value !== "") { + box.value = ""; + requestAjax2(-1); + } + event.preventDefault(); + }); + + box.addEventListener("input", function(event) { + requestAjax2(-1); + }); + } +} + +document.addEventListener("DOMContentLoaded", function() { + initSearch(); +}, true); + +/* @license-end */ diff --git a/apps/i2psnark/resources/themes/ubergine/snark.css b/apps/i2psnark/resources/themes/ubergine/snark.css index e97682edfb..6394cecea2 100644 --- a/apps/i2psnark/resources/themes/ubergine/snark.css +++ b/apps/i2psnark/resources/themes/ubergine/snark.css @@ -229,6 +229,28 @@ _:-ms-lang(x), .snarkNav:last-child[href="/i2psnark/"] { border-radius: 2px 0 0 2px; } +#search { + display: inline-block; + float: right; + position: absolute; + top: 6px; + right: 3px; +} + +#searchbox { + background: #f60 url(/themes/console/images/buttons/search.png) 7px center no-repeat !important; + margin: 2px 4px 2px 24px !important; + padding: 4px 32px 4px 32px !important; + color: black; +} + +#searchcancel { + background: url(images/cancel.png); + margin: 2px 4px 2px -28px; + color: transparent; + border: none; +} + /* end topnav */ /* screenlogger */ diff --git a/apps/routerconsole/jsp/js/ajax.js b/apps/routerconsole/jsp/js/ajax.js index ae4e582a50..37bf418a73 100644 --- a/apps/routerconsole/jsp/js/ajax.js +++ b/apps/routerconsole/jsp/js/ajax.js @@ -6,6 +6,11 @@ var fails = 0; +/** + * + * @param refresh as of 0.9.58, if less than or equal to zero, do not reschedule + * + */ function ajax(url, target, refresh) { // native XMLHttpRequest object if (window.XMLHttpRequest) { @@ -28,6 +33,11 @@ function ajax(url, target, refresh) { } } +/** + * + * @param refresh as of 0.9.58, if less than or equal to zero, do not reschedule + * + */ function ajaxDone(url, target, refresh) { // only if req is "loaded" if (req.readyState == 4) { @@ -51,7 +61,9 @@ function ajaxDone(url, target, refresh) { graph.style.backgroundImage = "url(/viewstat.jsp?stat=bw.combined&periodCount=20&width=220&height=50&hideLegend=true&hideGrid=true&time=" + new Date().getTime(); } - setTimeout(function() {ajax(url, target, refresh);}, refresh); + if (refresh > 0) { + setTimeout(function() {ajax(url, target, refresh);}, refresh); + } } } -- GitLab