Skip to content

Commit

Permalink
add filter by code in navettes
Browse files Browse the repository at this point in the history
small HTML refacto also (cc @njoyard)
  • Loading branch information
mdamien committed Jun 29, 2018
1 parent fdf2be4 commit 0cf5664
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
4 changes: 4 additions & 0 deletions public/modules/navettes/navettes.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<span href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="selectedchoice">Tous les thèmes</span><b class="caret"></b></span>
<ul class="dropdown-menu" id="themes"></ul>
</span>
<span class="nav-item dropdown noviewonelaw" id="menu-codes">
<span href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="selectedchoice">Tous les codes</span><b class="caret"></b></span>
<ul class="dropdown-menu" id="codes"></ul>
</span>
</span>

<span class="spacer"></span>
Expand Down
62 changes: 51 additions & 11 deletions public/modules/navettes/navettes.viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var drawGantt, navettesScope,
active_filters = {
year: 2018,
theme: "",
code: "",
length: '',
amendments: allAmendments[3]
};
Expand All @@ -37,7 +38,7 @@ var drawGantt, navettesScope,
addBillsFilter(filtype, "");
},
cleanBillsFilter = function () {
active_filters = {year: "", theme: "", length: "", amendments: ""};
active_filters = {year: "", theme: "", code: "", length: "", amendments: ""};
refreshLengthFilter();
};
reset_filters();
Expand All @@ -64,6 +65,7 @@ reset_filters();
steps, laws,
gridrects, gridlines,
allThemes = [], allYears = [],
allCodes = [],
dossiers = [], smallset = [],
stats = {},
mindate, maxdate, maxduration,
Expand Down Expand Up @@ -315,6 +317,9 @@ reset_filters();
if (active_filters['theme'])
$("#menu-themes .selectedchoice").text("Thème : " + active_filters['theme']);
else $("#menu-themes .selectedchoice").text("Tous les thèmes");
if (active_filters['code'])
$("#menu-codes .selectedchoice").text("Code : " + active_filters['code']);
else $("#menu-codes .selectedchoice").text("Tous les codes");
drawLaws();
if (resize && selected_bill) onclick(selected_bill);
drawAxis();
Expand Down Expand Up @@ -420,12 +425,13 @@ reset_filters();
})
}

// Populate themes, years and amendments in filter menu
// Populate codes, themes, years and amendments in filter menu
function computeFilters() {
var y1, hashYears = {};
if (!allYears.length) {
dossiers.forEach(function (l) {
allThemes = allThemes.concat(l.themes);
allCodes = allCodes.concat(l.textes_cites || []);
y1 = l.beginning.substr(0, 4);
hashYears[y1] = true;
y1 = l.end.substr(0, 4);
Expand All @@ -437,6 +443,8 @@ reset_filters();
}
allYears.sort();
allYears.reverse();

// themes
allThemes = allThemes.filter(function (itm, i, a) {
return itm && i == a.indexOf(itm); // unify
});
Expand All @@ -446,19 +454,43 @@ reset_filters();
return (ac === bc ? 0 : (ac < bc ? -1 : 1))
});
allThemes.unshift('Tous les thèmes');

// codes
allCodes = allCodes.filter(function (itm, i, a) {
return itm && i == a.indexOf(itm); // unify
});
allCodes.sort(function (a, b) {
var ac = thelawfactory.utils.clean_accents(a),
bc = thelawfactory.utils.clean_accents(b);
return (ac === bc ? 0 : (ac < bc ? -1 : 1))
});
allCodes.unshift('Tous les codes');
}
var construct_menu_filter = function (filter, cssid, d, i) {
if (!i) {
if (active_filters[filter] == d || !active_filters[filter]) {
$(cssid).append("<li><a class='chosen' onclick=\"rmBillsFilter('" + filter + "','')\">" + d.toLowerCase() + '</a></li>');
} else {
$(cssid).append("<li><a onclick=\"rmBillsFilter('" + filter + "','" + active_filters[filter] + "')\">" + d.toLowerCase() + '</a></li>');
var construct_menu_filter = function (filter, cssid, value, index) {
var li = document.createElement('li');

var a = document.createElement('a');
a.textContent = value.toLowerCase();
li.appendChild(a);

if (!index) {
a.addEventListener("click", function() {
rmBillsFilter(filter);
}, true);
if (active_filters[filter] == value || !active_filters[filter]) {
a.setAttribute("class", "chosen");
}
} else if (active_filters[filter] == d) {
$(cssid).append("<li><a class='chosen' onclick=\"rmBillsFilter('" + filter + "','" + d + "')\">" + d.toLowerCase() + '</a></li>');
} else if (active_filters[filter] == value) {
a.setAttribute("class", "chosen");
a.addEventListener("click", function() {
rmBillsFilter(filter, value);
}, true);
} else {
$(cssid).append("<li><a onclick=\"addBillsFilter('" + filter + "','" + d + "')\">" + d.toLowerCase() + '</a></li>');
a.addEventListener("click", function() {
addBillsFilter(filter, value);
}, true);
}
$(cssid)[0].appendChild(li);
};
$("#years").empty();
allYears.forEach(function (d, i) {
Expand All @@ -468,6 +500,10 @@ reset_filters();
allThemes.forEach(function (d, i) {
construct_menu_filter('theme', '#themes', d, i);
});
$("#codes").empty();
allCodes.forEach(function (d, i) {
construct_menu_filter('code', '#codes', d, i);
});
$("#amendments").empty();
allAmendments.forEach(function (d, i) {
construct_menu_filter('amendments', '#amendments', d, i);
Expand Down Expand Up @@ -545,6 +581,10 @@ reset_filters();
if (!active_filters['theme']) return true;
return (d.themes.join(',').indexOf(active_filters['theme'])) != -1;
})
.filter(function (d) {
if (!active_filters['code']) return true;
return ((d.textes_cites || []).join(',').indexOf(active_filters['code'])) != -1;
})
.filter(function (d) {
if (!active_filters['year']) return true;
return d.beginning.substr(0, 4) <= active_filters['year'] && d.end.substr(0, 4) >= active_filters['year'];
Expand Down

0 comments on commit 0cf5664

Please sign in to comment.