Skip to content

Commit 0cfa562

Browse files
author
Tom Clark
committed
Navigation works properly
1 parent 0ac7f8f commit 0cfa562

File tree

3 files changed

+96
-27
lines changed

3 files changed

+96
-27
lines changed

_config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pygments: true
22
markdown: kramdown
3+
4+
tracking_id: UA-30727234-1

javascripts/sidebar.js

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,95 @@
1-
var tutorials;
2-
var sidebar;
3-
var sidebar_counts;
1+
var tutorials, visible, visible_counts, hidden, dict, filters = [];
42

5-
$(function(){
3+
$(function() {
64
// Grab all tutorials
75
tutorials = $("section.tutorial");
86

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
1473
.reduce(function(counts, facets) {
74+
// by iterating over each pair and aggregating the counts of each
1575
_(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
2281
counts[key][val] += 1;
2382
});
2483
return counts;
84+
// This is where we're storing the new data
2585
}, {})
86+
// Get the final counts
2687
.value();
2788

2889
// Generate counts for the sidebar
29-
counts = {};
30-
_(sidebar).each(function(val, key) {
90+
var counts = {};
91+
_(visible).each(function(val, key) {
3192
counts[key] = _(val).keys().length;
3293
});
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;
4395
};

pages/test.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,26 @@ should show the following sidebar:
4545

4646
This allows users to filter the document by those attributes which are relevant to them.
4747

48+
If you clicked on the "Source (1)" `li` then you should see this:
49+
50+
Operating System (1)
51+
Ubuntu (1)
52+
53+
Package Management (3)
54+
Source (Selected)
55+
Homebrew (1)
56+
Macports (1)
57+
58+
That is, "Source" goes to the top of the list and the other lists are filtered.
59+
4860
<section class="tutorial" markdown="1" data-facets='{"Operating System": "OS X", "Package Management": "Homebrew"}'>
61+
<hr />
4962
This is the tutorial written for _OS X_ and _Homebrew_.
5063
It has data attributes on the preceeding `hr` tag.
5164
</section>
5265

5366
<section class="tutorial" markdown="1" data-facets='{"Operating System": "OS X", "Package Management": "Macports"}'>
67+
<hr />
5468
This is the tutorial written for _OS X_ and _Source_.
5569
It has data attributes on a surrounding section tag (written in html).
5670
It uses kramdown's `markdown="1"` attribute to enable markdown processing within an html tag.
@@ -65,5 +79,6 @@ Yay!
6579
</section>
6680

6781
<section class="tutorial" markdown="1" data-facets='{"Operating System": "Ubuntu", "Package Management": "Source"}'>
82+
<hr />
6883
This is the tutorial written for _Ubuntu_ and _Source_.
6984
</section>

0 commit comments

Comments
 (0)