small static cleanup

This commit is contained in:
Evgenii Alekseev 2019-09-11 12:13:43 +03:00
parent c11103a5ba
commit e86f3ee092
10 changed files with 70 additions and 63 deletions

View File

@ -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')
return Response(text=resource_file.read(), content_type=content_type)

View File

@ -4,6 +4,7 @@
<title>Best in slot</title>
<link href="{{ static('styles.css') }}" rel="stylesheet" type="text/css">
<link href=""
</head>
<body>
<h2>best in slot</h2>
@ -67,6 +68,6 @@
{% include "export_to_csv.jinja2" %}
{% include "root.jinja2" %}
{% include "search.jinja2" %}
<script src="/static/table_search.js" type="text/javascript"></script>
</body>
</html>

View File

@ -1,35 +1,2 @@
<button onclick="exportTableToCsv('result.csv')">Export to csv</button>
<script type="application/javascript">
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);
}
</script>
<script src="/static/table_export.js" type="text/javascript"></script>

View File

@ -55,6 +55,6 @@
{% include "export_to_csv.jinja2" %}
{% include "root.jinja2" %}
{% include "search.jinja2" %}
<script src="/static/table_search.js" type="text/javascript"></script>
</body>
</html>

View File

@ -53,6 +53,6 @@
{% include "export_to_csv.jinja2" %}
{% include "root.jinja2" %}
{% include "search.jinja2" %}
<script src="/static/table_search.js" type="text/javascript"></script>
</body>
</html>

View File

@ -55,6 +55,6 @@
{% include "export_to_csv.jinja2" %}
{% include "root.jinja2" %}
{% include "search.jinja2" %}
<script src="/static/table_search.js" type="text/javascript"></script>
</body>
</html>

View File

@ -1,23 +0,0 @@
<script type="text/javascript">
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;
}
}
</script>

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -46,6 +46,6 @@
{% include "export_to_csv.jinja2" %}
{% include "root.jinja2" %}
{% include "search.jinja2" %}
<script src="/static/table_search.js" type="text/javascript"></script>
</body>
</html>