Skip to content

Commit 183d06d

Browse files
author
Tom Clark
committed
Allow multiple values for each facet
1 parent b5f5e17 commit 183d06d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

javascripts/sidebar.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ $(function() {
1111
.reduce(function(lookup, tutorial) {
1212
_($(tutorial).data().facets).each(function(val, key) {
1313
if(!_(lookup).has(key)) { lookup[key] = {}; }
14-
if(!_(lookup[key]).has(val)) { lookup[key][val] = []; }
15-
lookup[key][val].push(tutorial);
14+
// We want to be able to have multiple values for vertain
15+
// facets, so we convert strings to arrays and iterate over each
16+
if(_(val).isString()) { val = [val]; }
17+
_(val).each(function(v) {
18+
if(!_(lookup[key]).has(v)) { lookup[key][v] = []; }
19+
lookup[key][v].push(tutorial);
20+
});
1621
});
1722
return lookup;
1823
}, {})
@@ -28,10 +33,8 @@ $(function() {
2833
return filter.name === target.name && filter.value === target.value;
2934
});
3035
if(existing_filter) {
31-
console.log("Removing filter: " + existing_filter.name);
3236
filters = _(filters).without(existing_filter);
3337
} else {
34-
console.log("Applying filter: " + target.name);
3538
filters = _(filters).reject(function(filter) { return filter.name === target.name; });
3639
filters.push(target);
3740
}
@@ -49,13 +52,11 @@ function redraw() {
4952

5053
function update_visibility() {
5154
// Grab all the tutorials for each facet into an array
52-
console.log("Calculating all ...")
5355
all = _.chain(filters).map(function(filter) {
5456
return dict[filter.name][filter.value];
5557
}).flatten().value();
5658

5759
// Only keep the tutorials that are visible
58-
console.log("Calculating keep ...")
5960
keep = _(tutorials).select(function(tutorial) {
6061
return all.length - _(all).without(tutorial).length === filters.length
6162
});
@@ -76,9 +77,12 @@ function update_sidebar() {
7677
// Initialize the names
7778
if(!_(counts).has(key)) { counts[key] = {}; }
7879
// and the values/counts
79-
if(!_(counts[key]).has(val)) { counts[key][val] = 0; }
80-
// then increment the counts
81-
counts[key][val] += 1;
80+
if(_(val).isString()) { val = [val]; }
81+
_(val).each(function(v) {
82+
if(!_(counts[key]).has(v)) { counts[key][v] = 0; }
83+
// then increment the counts
84+
counts[key][v] += 1;
85+
});
8286
});
8387
return counts;
8488
// This is where we're storing the new data

0 commit comments

Comments
 (0)