-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.js
77 lines (67 loc) · 1.79 KB
/
filter.js
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
//Get a collection of posts
var $posts = jQuery('.post');
var tagsArray = [];
var $tagsNav = $('.tags-nav');
var $tagLink = $('.tag-link');
var $postsTitle = $('.posts-title');
$tagLink.click(function(evt) {
//alert($(evt.target).text());
var linkText = $(evt.target).text();
if (linkText == "all") {
$postsTitle.text("Links");
$posts.show();
} else {
$postsTitle.text("Links tagged with: " + linkText);
$posts.hide()
.filter('.' + linkText)
.show();
}
});
$tagLink.detach(); //hide original from view
//Iterate thru the collection and get the tag names.
$posts.each(function(index) {
var tags = $(this)
.attr("class")
.split(" ");
for (var i = 0; i < tags.length; i++) {
if ((tags[i] != "post") &&
(tags[i] != "tag") &&
(tagsArray.indexOf(tags[i]) < 0)) {
tagsArray.push(tags[i]);
}
}
});
tagsArray.sort();
tagsArray.push("all");
console.log(tagsArray);
for (var i = 0; i < tagsArray.length; i++) {
//clone the link with events
$tagLink.clone(true)
.text(tagsArray[i])
.appendTo($tagsNav);
}
//Add aside element to display post's tags
$posts.each(function(index) {
var tags = $(this)
.attr("class")
.split(" ");
var $tagsAside = $('<aside></aside>');
$tagsAside.addClass('post-tags');
//Remove "post" and "tag" from classes list
tags.splice(tags.indexOf("post"), 1);
tags.splice(tags.indexOf("tag"), 1);
//console.log(tags);
$tagsAside.text("Tagged: #" + tags.join(" #"));
$(this).append($tagsAside);
});
// $posts.hover(
// function(evt) { //mouse in
// $(evt.currentTarget).children('.post-tags')
// .slideDown();
// },
// function(evt) { //mouse out
// $(evt.currentTarget).children('.post-tags')
// .slideUp(function() {
// $(this).finish();
// });
// });