forked from tutorials/tutorials.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar.js
More file actions
166 lines (150 loc) · 5.26 KB
/
sidebar.js
File metadata and controls
166 lines (150 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var tutorials, visible, visible_counts, hidden, dict, filters = [];
$(function() {
// Grab all tutorials
tutorials = $("div.tutorial");
// Mark all tutorials as visible
tutorials.show();
// Render license/source information
_(tutorials).each(function(tutorial){
t = $(tutorial);
d = t.data();
var meta = d.authorGithub || d.author || d.source || d.license;
if(meta) {
var attribution = "";
if(d.authorGithub) {
attribution += '<span>by <a href="http://github.com/' + d.authorGithub + '">' + d.authorGithub + '</a></span>';
} else if(d.author) {
attribution += "<span>by " + d.author + "</span>"
}
if(d.source) {
if (d.authorGithub || d.author) {
attribution += '<span>|</span>';
}
attribution += '<span><a href="' + d.source + '">Original Source</a></span>';
}
if(d.license) {
if (d.authorGithub || d.author || d.source) {
attribution += '<span>|</span>';
}
attribution += '<span><a href="' + d.license + '">License</a></span>';
}
t.prepend("<p class='about'>" + attribution + "</p>");
}
});
dict = _.chain(tutorials)
.reduce(function(lookup, tutorial) {
_($(tutorial).data().facets).each(function(val, key) {
if(!_(lookup).has(key)) { lookup[key] = {}; }
// We want to be able to have multiple values for vertain
// facets, so we convert strings to arrays and iterate over each
if(_(val).isString()) { val = [val]; }
_(val).each(function(v) {
if(!_(lookup[key]).has(v)) { lookup[key][v] = []; }
lookup[key][v].push(tutorial);
});
});
return lookup;
}, {})
.value();
// Draw the sidebar
redraw();
//Fix sidebar
function sidebar_fix() {
var header_height = $('header').height(),
footer_height = 150, //fixed
window_height = $(window).height(),
$nav_sidebar = $('nav#sidebar'),
sidebar_height= $nav_sidebar.height(),
difference = window_height - (header_height + footer_height);
if ($(window).width()>970){
var existence = sidebar_height+difference;
$('header h1').css('paddingTop', 0);
if (existence>250) {
$nav_sidebar.height(existence);
if ($('header').width()==250) {
$('header').css('position', 'fixed');
$('footer').css('position', 'fixed');
}
}
} else {
$nav_sidebar.css('height', 'auto');
$('header').css('position', 'static');
$('footer').css('position', 'static');
$('header h1').css('paddingTop', $('section#main h1').height());
}
}
sidebar_fix();
$(window).resize(function() { sidebar_fix() });
// title fix
var $section = $('section#main'),
$title = $section.find('h1'),
title_height = $title.height(),
set_padding = title_height + 20;
$section.css('paddingTop',set_padding);
// Hide tutorials if the user clicks on a facet
$("nav li").live("click", function(e) {
target = $(this).data();
existing_filter = _(filters).find(function(filter) {
return filter.name === target.name && filter.value === target.value;
});
if(existing_filter) {
_gaq.push(['_trackEvent', 'filters - remove', existing_filter.name, existing_filter.value, filters.size]);
filters = _(filters).without(existing_filter);
} else {
_gaq.push(['_trackEvent', 'filters - apply', target.name, target.value, filters.size]);
filters = _(filters).reject(function(filter) { return filter.name === target.name && filter.value === target.value; });
filters.push(target);
}
redraw();
});
});
function redraw() {
update_visibility();
update_sidebar();
var template = $("#sidebar_template").text();
var list = _.template(template);
$("#sidebar").html(list);
};
function update_visibility() {
// Grab all the tutorials for each facet into an array
all = _.chain(filters).map(function(filter) {
return dict[filter.name][filter.value];
}).flatten().value();
// Only keep the tutorials that are visible
keep = _(tutorials).select(function(tutorial) {
return all.length - _(all).without(tutorial).length === filters.length
});
// Make DOM elements visible or not
$(keep).show();
$(_(tutorials).difference(keep)).hide();
};
function update_sidebar() {
visible = _.chain($("div.tutorial:visible"))
// Collect all the facets
.map(function(tutorial) { return $(tutorial).data().facets; })
// and combine all facets into a nested hash
.reduce(function(counts, facets) {
// by iterating over each pair and aggregating the counts of each
_(facets).each(function(val, key) {
// Initialize the names
if(!_(counts).has(key)) { counts[key] = {}; }
// and the values/counts
if(_(val).isString()) { val = [val]; }
_(val).each(function(v) {
if(!_(counts[key]).has(v)) { counts[key][v] = 0; }
// then increment the counts
counts[key][v] += 1;
});
});
return counts;
// This is where we're storing the new data
}, {})
// Get the final counts
.value();
// Generate counts for the sidebar
var counts = {};
_(visible).each(function(val, key) {
counts[key] = _(val).keys().length;
});
visible_counts = counts;
};