Skip to content

Commit e065831

Browse files
committed
Reimplement feature-matrix filtering
* jQuery removed and replaced with native javascript * Column filtering is now done in pure CSS without javascript. This also means that the initial rendering of the page is correct instead of rendering all column and then removing them (visible on slow connections) * Reduce page size by moving some contents to CSS instead of repeating it
1 parent 2d0a041 commit e065831

File tree

4 files changed

+62
-44
lines changed

4 files changed

+62
-44
lines changed

media/css/main.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,18 +1683,30 @@ td.fm_no {
16831683
background-color: var(--td-fm-no-bg-color);
16841684
color: var(--td-fm-no-fg-color);
16851685
}
1686+
td.fm_no::before {
1687+
content: "No";
1688+
}
16861689
td.fm_yes {
16871690
background-color: var(--td-fm-yes-bg-color);
16881691
color: var(--td-fm-yes-fg-color);
16891692
}
1693+
td.fm_yes::before {
1694+
content: "Yes";
1695+
}
16901696
td.fm_obs {
16911697
background-color: var(--td-fm-obs-bg-color);
16921698
color: var(--td-fm-obs-fg-color);
16931699
}
1700+
td.fm_obs::before {
1701+
content: "Obsolete";
1702+
}
16941703
td.fm_unk {
16951704
background-color: var(--td-fm-unk-bg-color);
16961705
color: var(--td-fm-unk-fg-color);
16971706
}
1707+
td.fm_unk::before {
1708+
content: "?";
1709+
}
16981710

16991711
div#feature-matrix-filter {
17001712
border-color: var(--hr-bdr-color) !important;

media/js/featurematrix.js

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,37 @@
11
/*
22
* Filter feature matrix
33
*/
4-
$(document).ready(function(){
5-
// Show/hide column based on whether supplied checkbox is checked.
6-
function filter_version(checkbox)
7-
{
8-
var total_checked = $('form#featurematrix_version_filter .featurematrix_version:checked').length;
9-
var column=$("table tr:first th").index($("table tr:first th:contains('" + checkbox.val() + "')")) + 1;
10-
if (total_checked) {
11-
$('.feature-version-col').css('width', (70 / total_checked) + '%');
4+
5+
function hide_unchanged(toggled) {
6+
/* Unfortunately, can't figure out how to do this part in pure CSS */
7+
8+
if (!document.getElementById('hide_unchanged').checked) {
9+
/* Unchanged filter is unchecked. If we just made it unchecked we have to display everything, otherwise we have nothing to do here. */
10+
if (toggled) {
11+
document.querySelectorAll('table.matrix tbody tr').forEach((tr) => {
12+
tr.style.display = 'table-row';
13+
});
1214
}
13-
$("table th:nth-child(" + column + "), table td:nth-child(" + column + ")").toggle(checkbox.is(":checked")).toggleClass('hidden');
14-
hide_unchanged();
15-
// Lastly, if at this point an entire row is obsolete, then hide
16-
$('tbody tr').each(function(i, el) {
17-
var $tr = $(el),
18-
visible_count = $tr.find('td:not(.hidden)').length,
19-
obsolete_count = $tr.find('td.fm_obs:not(.hidden)').length;
20-
// if visible count matches obsolete count, then hide this row
21-
$tr.toggle(visible_count !== obsolete_count);
22-
});
15+
return;
2316
}
2417

25-
// Show/hide rows if all values are the same in displayed cells
26-
function hide_unchanged()
27-
{
28-
var hide_unchanged=$('form#featurematrix_version_filter input#hide_unchanged').is(':checked');
29-
$('table tr').has('td').each(function(){
30-
var row_values=$(this).children('td').not('.colFirst, .hidden');
31-
var yes_count=row_values.filter(":contains('Yes')").length;
32-
var no_count=row_values.filter(":contains('No')").length;
33-
var obsolete_count=row_values.filter(":contains('Obsolete')").length;
34-
$(this).toggle(hide_unchanged == false || (yes_count != row_values.length && no_count != row_values.length && obsolete_count != row_values.length));
35-
});
36-
}
18+
/* Get indexes of checked version checkboxes */
19+
const vercols = [...document.querySelectorAll('#featurematrix_version_filter input.featurematrix_version')].map((cb, i) => cb.checked ? i : null).filter((i) => i != null);
3720

38-
// Upon loading the page, apply the filter based on EOL versions that are
39-
// unchecked.
40-
$('form#featurematrix_version_filter input.featurematrix_version').not(':checked').each(function(){
41-
filter_version($(this));
21+
document.querySelectorAll('table.matrix tbody tr').forEach((tr) => {
22+
/* Get classes of all relevant td's (based on vercols), and see if there is more than one unique class. Assumes each td only has one explicit class. */
23+
const changed = new Set([...tr.querySelectorAll('td')].filter((td, i) => vercols.indexOf(i) > -1).map((td) => td.classList[0])).size > 1;
24+
tr.style.display = changed ? "table-row" : "none";
4225
});
26+
}
4327

44-
// Apply filter based on change in check status of clicked version filter.
45-
$('form#featurematrix_version_filter input.featurematrix_version').on("change", function(){
46-
filter_version($(this));
28+
document.addEventListener('DOMContentLoaded', () => {
29+
document.getElementById('hide_unchanged').addEventListener('change', (e) => {
30+
hide_unchanged(true);
4731
});
48-
49-
// Show/hide unchanged feature rows when checkbox clicked.
50-
$('form#featurematrix_version_filter input#hide_unchanged').on("change", function(){
51-
hide_unchanged();
32+
document.querySelectorAll('input.featurematrix_version').forEach((c) => {
33+
c.addEventListener('change', (e) => {
34+
hide_unchanged(false);
35+
});
5236
});
5337
});

pgweb/featurematrix/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from django.shortcuts import get_object_or_404
22

33
from pgweb.util.contexts import render_pgweb
4+
from pgweb.util.decorators import content_sources
45

56
from pgweb.core.models import Version
67
from .models import Feature
78

89

10+
@content_sources('style', "'unsafe-inline'")
911
def root(request):
1012
features = Feature.objects.all().select_related().order_by('group__groupsort', 'group__groupname', 'featurename')
1113
groups = []

templates/featurematrix/featurematrix.html

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
{%extends "base/page.html"%}
22
{%block title%}Feature Matrix{%endblock%}
33

4+
{% block extrahead %}
5+
{{ block.super }}
6+
<style>
7+
table.matrix tr th,
8+
table.matrix tr td {
9+
display: none;
10+
}
11+
table.matrix tr th:nth-child(1),
12+
table.matrix tr td:nth-child(1) {
13+
display: table-cell;
14+
}
15+
{% for version in versions %}
16+
body:has(input#toggle_{{ version.numtree|cut:"." }}:checked) table.matrix th:nth-child({{forloop.counter|add:1}}),
17+
body:has(input#toggle_{{ version.numtree|cut:"." }}:checked) table.matrix td:nth-child({{forloop.counter|add:1}}) {
18+
display: table-cell;
19+
}
20+
{% endfor %}
21+
</style>
22+
{%endblock%}
23+
424
{%block extrascript%}
525
<script type="text/javascript" src="/media/js/featurematrix.js?{{gitrev}}"></script>
626
{%endblock%}
@@ -38,12 +58,12 @@ <h3>Groups</h3>
3858
<h2>
3959
<a name="{{group.group.groupname|slugify}}">{{ group.group.groupname }}</a>
4060
</h2>
41-
<table class="table table-striped table-sm">
61+
<table class="table table-striped table-sm matrix">
4262
<thead>
4363
<tr>
4464
<th scope="col" width="30%">&nbsp;</th>
4565
{% for col in group.group.columns %}
46-
<th scope="col" class="feature-version-col">{{col}}</th>
66+
<th scope="col">{{col}}</th>
4767
{% endfor %}
4868
</tr>
4969
</thead>
@@ -58,7 +78,7 @@ <h2>
5878
{%endif%}
5979
</th>
6080
{%for col in feature.columns%}
61-
<td class="fm_{{col.class}}">{{col.str}}</td>
81+
<td class="fm_{{col.class}}"></td>
6282
{%endfor%}
6383
</tr>
6484
{%endfor%}

0 commit comments

Comments
 (0)