From e86f3ee092bd7babe4b56411e6dfa86deeb7f330 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Wed, 11 Sep 2019 12:13:43 +0300 Subject: [PATCH] small static cleanup --- src/service/api/views/html/static.py | 12 +++++++++- templates/bis.jinja2 | 3 ++- templates/export_to_csv.jinja2 | 35 +--------------------------- templates/loot.jinja2 | 2 +- templates/loot_suggest.jinja2 | 2 +- templates/party.jinja2 | 2 +- templates/search.jinja2 | 23 ------------------ templates/static/table_export.js | 31 ++++++++++++++++++++++++ templates/static/table_search.js | 21 +++++++++++++++++ templates/users.jinja2 | 2 +- 10 files changed, 70 insertions(+), 63 deletions(-) delete mode 100644 templates/search.jinja2 create mode 100644 templates/static/table_export.js create mode 100644 templates/static/table_search.js diff --git a/src/service/api/views/html/static.py b/src/service/api/views/html/static.py index ad58c4d..cbfb456 100644 --- a/src/service/api/views/html/static.py +++ b/src/service/api/views/html/static.py @@ -12,11 +12,21 @@ from aiohttp.web import HTTPNotFound, Response, View class StaticHtmlView(View): + def __get_content_type(self, filename: str) -> str: + _, ext = os.path.splitext(filename) + print(ext) + if ext == '.css': + return 'text/css' + elif ext == '.js': + return 'text/javascript' + return 'text/plain' + async def get(self) -> Response: resource_name = self.request.match_info['resource_id'] resource_path = os.path.join(self.request.app['templates_root'], 'static', resource_name) if not os.path.exists(resource_path) or os.path.isdir(resource_path): return HTTPNotFound() + content_type = self.__get_content_type(resource_name) with open(resource_path) as resource_file: - return Response(text=resource_file.read(), content_type='text/css') \ No newline at end of file + return Response(text=resource_file.read(), content_type=content_type) \ No newline at end of file diff --git a/templates/bis.jinja2 b/templates/bis.jinja2 index 99ed6a5..62fb10a 100644 --- a/templates/bis.jinja2 +++ b/templates/bis.jinja2 @@ -4,6 +4,7 @@ Best in slot +

best in slot

@@ -67,6 +68,6 @@ {% include "export_to_csv.jinja2" %} {% include "root.jinja2" %} - {% include "search.jinja2" %} + diff --git a/templates/export_to_csv.jinja2 b/templates/export_to_csv.jinja2 index 9087d5d..37013a4 100644 --- a/templates/export_to_csv.jinja2 +++ b/templates/export_to_csv.jinja2 @@ -1,35 +1,2 @@ - - + diff --git a/templates/loot.jinja2 b/templates/loot.jinja2 index 433fea8..d16a4c4 100644 --- a/templates/loot.jinja2 +++ b/templates/loot.jinja2 @@ -55,6 +55,6 @@ {% include "export_to_csv.jinja2" %} {% include "root.jinja2" %} - {% include "search.jinja2" %} + diff --git a/templates/loot_suggest.jinja2 b/templates/loot_suggest.jinja2 index 3d96f59..391acc3 100644 --- a/templates/loot_suggest.jinja2 +++ b/templates/loot_suggest.jinja2 @@ -53,6 +53,6 @@ {% include "export_to_csv.jinja2" %} {% include "root.jinja2" %} - {% include "search.jinja2" %} + diff --git a/templates/party.jinja2 b/templates/party.jinja2 index 92cc889..304ff9e 100644 --- a/templates/party.jinja2 +++ b/templates/party.jinja2 @@ -55,6 +55,6 @@ {% include "export_to_csv.jinja2" %} {% include "root.jinja2" %} - {% include "search.jinja2" %} + diff --git a/templates/search.jinja2 b/templates/search.jinja2 deleted file mode 100644 index 4fd7810..0000000 --- a/templates/search.jinja2 +++ /dev/null @@ -1,23 +0,0 @@ - diff --git a/templates/static/table_export.js b/templates/static/table_export.js new file mode 100644 index 0000000..93ec8d7 --- /dev/null +++ b/templates/static/table_export.js @@ -0,0 +1,31 @@ +function downloadCsv(csv, filename) { + var csvFile = new Blob([csv], {"type": "text/csv"}); + + var downloadLink = document.createElement("a"); + downloadLink.download = filename; + downloadLink.href = window.URL.createObjectURL(csvFile); + downloadLink.style.display = "none"; + + document.body.appendChild(downloadLink); + downloadLink.click(); +} + +function exportTableToCsv(filename) { + var table = document.getElementById("result"); + var rows = table.getElementsByTagName("tr"); + + var csv = []; + for (var i = 0; i < rows.length; i++) { + if (rows[i].style.display === "none") + continue + var cols = rows[i].querySelectorAll("td, th"); + + var row = []; + for (var j = 0; j < cols.length; j++) + row.push(cols[j].innerText); + + csv.push(row.join(",")); + } + + downloadCsv(csv.join("\n"), filename); +} \ No newline at end of file diff --git a/templates/static/table_search.js b/templates/static/table_search.js new file mode 100644 index 0000000..e269f2c --- /dev/null +++ b/templates/static/table_search.js @@ -0,0 +1,21 @@ +function searchTable() { + var input = document.getElementById("search"); + var filter = input.value.toLowerCase(); + var table = document.getElementById("result"); + var tr = table.getElementsByTagName("tr"); + + // from 1 coz of header + for (var i = 1; i < tr.length; i++) { + var td = tr[i].getElementsByClassName("include_search"); + var display = "none"; + for (var j = 0; j < td.length; j++) { + if (td[j].tagName.toLowerCase() === "td") { + if (td[j].innerHTML.toLowerCase().indexOf(filter) > -1) { + display = ""; + break; + } + } + } + tr[i].style.display = display; + } +} \ No newline at end of file diff --git a/templates/users.jinja2 b/templates/users.jinja2 index 02024ba..e57d35d 100644 --- a/templates/users.jinja2 +++ b/templates/users.jinja2 @@ -46,6 +46,6 @@ {% include "export_to_csv.jinja2" %} {% include "root.jinja2" %} - {% include "search.jinja2" %} +