|
1 |
| -var tutorials; |
2 |
| -var sidebar; |
3 |
| -var sidebar_counts; |
| 1 | +var tutorials, visible, visible_counts, hidden, dict, filters = []; |
4 | 2 |
|
5 |
| -$(function(){ |
| 3 | +$(function() { |
6 | 4 | // Grab all tutorials
|
7 | 5 | tutorials = $("section.tutorial");
|
8 | 6 |
|
9 |
| - // Generate the sidebar |
10 |
| - sidebar = _.chain(tutorials) |
11 |
| - .map(function(tutorial) { |
12 |
| - return $(tutorial).data().facets; |
13 |
| - }) |
| 7 | + // Mark all tutorials as visible |
| 8 | + tutorials.show(); |
| 9 | + |
| 10 | + dict = _.chain(tutorials) |
| 11 | + .reduce(function(lookup, tutorial) { |
| 12 | + _($(tutorial).data().facets).each(function(val, key) { |
| 13 | + if(!_(lookup).has(key)) { lookup[key] = {}; } |
| 14 | + if(!_(lookup[key]).has(val)) { lookup[key][val] = []; } |
| 15 | + lookup[key][val].push(tutorial); |
| 16 | + }); |
| 17 | + return lookup; |
| 18 | + }, {}) |
| 19 | + .value(); |
| 20 | + |
| 21 | + // Draw the sidebar |
| 22 | + redraw(); |
| 23 | + |
| 24 | + // Hide tutorials if the user clicks on a facet |
| 25 | + $("nav li").live("click", function(e) { |
| 26 | + target = $(this).data(); |
| 27 | + existing_filter = _(filters).find(function(filter) { |
| 28 | + return filter.name === target.name && filter.value === target.value; |
| 29 | + }); |
| 30 | + if(existing_filter) { |
| 31 | + console.log("Removing filter: " + existing_filter.name); |
| 32 | + filters = _(filters).without(existing_filter); |
| 33 | + } else { |
| 34 | + console.log("Applying filter: " + target.name); |
| 35 | + filters = _(filters).reject(function(filter) { return filter.name === target.name; }); |
| 36 | + filters.push(target); |
| 37 | + } |
| 38 | + redraw(); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +function redraw() { |
| 43 | + update_visibility(); |
| 44 | + update_sidebar(); |
| 45 | + var template = $("#sidebar_template").text(); |
| 46 | + var list = _.template(template); |
| 47 | + $("#sidebar").html(list); |
| 48 | +}; |
| 49 | + |
| 50 | +function update_visibility() { |
| 51 | + // Grab all the tutorials for each facet into an array |
| 52 | + console.log("Calculating all ...") |
| 53 | + all = _.chain(filters).map(function(filter) { |
| 54 | + return dict[filter.name][filter.value]; |
| 55 | + }).flatten().value(); |
| 56 | + |
| 57 | + // Only keep the tutorials that are visible |
| 58 | + console.log("Calculating keep ...") |
| 59 | + keep = _(tutorials).select(function(tutorial) { |
| 60 | + return all.length - _(all).without(tutorial).length === filters.length |
| 61 | + }); |
| 62 | + |
| 63 | + // Make DOM elements visible or not |
| 64 | + $(keep).show(); |
| 65 | + $(_(tutorials).difference(keep)).hide(); |
| 66 | +}; |
| 67 | + |
| 68 | +function update_sidebar() { |
| 69 | + visible = _.chain($("section.tutorial:visible")) |
| 70 | + // Collect all the facets |
| 71 | + .map(function(tutorial) { return $(tutorial).data().facets; }) |
| 72 | + // and combine all facets into a nested hash |
14 | 73 | .reduce(function(counts, facets) {
|
| 74 | + // by iterating over each pair and aggregating the counts of each |
15 | 75 | _(facets).each(function(val, key) {
|
16 |
| - if(!_(counts).has(key)) { |
17 |
| - counts[key] = {}; |
18 |
| - } |
19 |
| - if(!_(counts[key]).has(val)) { |
20 |
| - counts[key][val] = 0; |
21 |
| - } |
| 76 | + // Initialize the names |
| 77 | + if(!_(counts).has(key)) { counts[key] = {}; } |
| 78 | + // and the values/counts |
| 79 | + if(!_(counts[key]).has(val)) { counts[key][val] = 0; } |
| 80 | + // then increment the counts |
22 | 81 | counts[key][val] += 1;
|
23 | 82 | });
|
24 | 83 | return counts;
|
| 84 | + // This is where we're storing the new data |
25 | 85 | }, {})
|
| 86 | + // Get the final counts |
26 | 87 | .value();
|
27 | 88 |
|
28 | 89 | // Generate counts for the sidebar
|
29 |
| - counts = {}; |
30 |
| - _(sidebar).each(function(val, key) { |
| 90 | + var counts = {}; |
| 91 | + _(visible).each(function(val, key) { |
31 | 92 | counts[key] = _(val).keys().length;
|
32 | 93 | });
|
33 |
| - sidebar_counts = counts; |
34 |
| - |
35 |
| - // Draw the sidebar |
36 |
| - redraw(); |
37 |
| -}); |
38 |
| - |
39 |
| -function redraw() { |
40 |
| - template = "<% _(sidebar).each(function(val, key) { %><h3><%= key %> (<%= sidebar_counts[key] %>)</h3><ul><% _(val).each(function(v, k) { %><li><%= k %> (<%= v %>)</li><% }) %></ul><% }) %>"; |
41 |
| - list = _.template(template); |
42 |
| - $("#sidebar").append(list); |
| 94 | + visible_counts = counts; |
43 | 95 | };
|
0 commit comments