Skip to content

Commit 5b76013

Browse files
authored
Merge pull request #69 from shiftlab/tutorial-cards
Tutorial cards
2 parents 3a59e6d + 12eb451 commit 5b76013

File tree

6 files changed

+680
-7
lines changed

6 files changed

+680
-7
lines changed

js/filter-tutorial-tags.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
window.filterTags = {
2+
bind: function() {
3+
var options = {
4+
valueNames: [{ data: ["tags"] }],
5+
page: "18"
6+
};
7+
8+
var tutorialList = new List("tutorial-cards", options);
9+
10+
function filterSelectedTags(cardTags, selectedTags) {
11+
return cardTags.some(function(tag) {
12+
return selectedTags.some(function(selectedTag) {
13+
return selectedTag == tag;
14+
});
15+
});
16+
}
17+
18+
function updateList() {
19+
var selectedTags = [];
20+
21+
$(".selected").each(function() {
22+
selectedTags.push($(this).data("tag"));
23+
});
24+
25+
tutorialList.filter(function(item) {
26+
var cardTags;
27+
28+
if (item.values().tags == null) {
29+
cardTags = [""];
30+
} else {
31+
cardTags = item.values().tags.split(",");
32+
}
33+
34+
if (selectedTags.length == 0) {
35+
return true;
36+
} else {
37+
return filterSelectedTags(cardTags, selectedTags);
38+
}
39+
});
40+
$("[data-tags='null']").remove();
41+
}
42+
43+
$(".filter-btn").on("click", function() {
44+
if ($(this).data("tag") == "all") {
45+
$(this).addClass("all-tag-selected");
46+
$(".filter").removeClass("selected");
47+
} else {
48+
$(this).toggleClass("selected");
49+
$("[data-tag='all']").removeClass("all-tag-selected");
50+
}
51+
52+
// If no tags are selected then highlight the 'All' tag
53+
54+
if (!$(".selected")[0]) {
55+
$("[data-tag='all']").addClass("all-tag-selected");
56+
}
57+
58+
updateList();
59+
});
60+
}
61+
};

js/theme.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,52 @@ if ($("p.caption:first").text() == "Notes") {
286286
$("p.caption:first").next("ul").toggle();
287287
}
288288
}
289+
290+
// Get the card link from the card's link attribute
291+
292+
$(".tutorials-card").on("click", function() {
293+
window.location = $(this).attr("link");
294+
});
295+
296+
// Build an array from each tag that's present
297+
298+
var tagList = $(".tutorials-card-container").map(function() {
299+
return $(this).data("tags").split(",").map(function(item) {
300+
return item.trim();
301+
});
302+
}).get();
303+
304+
function unique(value, index, self) {
305+
  return self.indexOf(value) == index && value != ""
306+
}
307+
308+
// Only return unique tags
309+
310+
var tags = tagList.filter(unique);
311+
312+
// Add filter buttons to the top of the page for each tag
313+
314+
function createTagMenu() {
315+
tags.forEach(function(item){
316+
$(".tutorial-filter-menu").append(" <div class='tutorial-filter filter-btn filter' data-tag='" + item + "'>" + item + "</div>")
317+
})
318+
};
319+
320+
createTagMenu();
321+
322+
// Remove hyphens if they are present in the filter buttons
323+
324+
$(".tags").each(function(){
325+
var tags = $(this).text().split(",");
326+
tags.forEach(function(tag, i ) {
327+
tags[i] = tags[i].replace(/-/, ' ')
328+
})
329+
$(this).html(tags.join(", "));
330+
});
331+
332+
// Remove hyphens if they are present in the card body
333+
334+
$(".tutorial-filter").each(function(){
335+
var tag = $(this).text();
336+
$(this).html(tag.replace(/-/, ' '))
337+
})

pytorch_sphinx_theme/layout.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315

316316
<script type="text/javascript" src="{{ pathto('_static/js/vendor/popper.min.js', 1) }}"></script>
317317
<script type="text/javascript" src="{{ pathto('_static/js/vendor/bootstrap.min.js', 1) }}"></script>
318+
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
318319
<script type="text/javascript" src="{{ pathto('_static/js/theme.js', 1) }}"></script>
319320

320321
<script type="text/javascript">
@@ -502,6 +503,10 @@ <h2>Resources</h2>
502503
scrollToAnchor.bind();
503504
highlightNavigation.bind();
504505
mainMenuDropdown.bind();
506+
filterTags.bind();
507+
508+
// Remove any empty p tags that Sphinx adds
509+
$("[data-tags='null']").remove();
505510

506511
// Add class to links that have code blocks, since we cannot create links in code blocks
507512
$("article.pytorch-article a span.pre").each(function(e) {

pytorch_sphinx_theme/static/css/theme.css

Lines changed: 227 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)