-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.js
111 lines (96 loc) · 2.76 KB
/
assets.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
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
// Case-insensitive contains()
$(function () {
$.expr[':'].Contains = function (a, i, m) {
return (a.textContent || a.innerText || '').toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};
/**
* Initialize a filterable list.
*/
function Filter(list) {
this.el = list;
// Filter input
var form = $('<form>').attr({
'action': '#'
});
var input = $('<input>').attr({
'type': 'text',
'placeholder': 'Filter by keyword'
});
$(form).append(input).prependTo(this.el);
// Filter function
var self = this;
$(input).change(function () {
var filter = $(this).val();
if (filter) {
$(self.el).find('a:not(:Contains(' + filter + '))').parent().hide();
$(self.el).find('a:Contains(' + filter + ')').parent().show();
} else {
$(self.el).find('li').show();
}
// Hide titles when group is empty
$(self.el).find('ul').each(function () {
if (!$(this).find('li:visible').length) {
$(this).prev('h2').hide();
} else {
$(this).prev('h2').show();
}
});
return false;
})
.keyup(function () {
$(this).change();
});
return this;
}
// Collapsible articles
$('article').each(function () {
var that = $(this);
var header = that.children('a');
var body = that.children('.body');
body.hide();
header.on('click',
function (e) {
if (body.data('active') != false) {
body.slideDown('fast');
that.addClass('active');
body.data('active', false)
} else {
body.slideUp('fast');
that.removeClass('active');
body.data('active', true)
}
e.preventDefault();
}
);
});
var anchor = window.location.hash.substring(1);
if (anchor) $('article a[name="' + anchor + '"]').trigger('click');
// Expanding the article on link click and scrolling down to it
$('#sidebar a').each(function () {
var that = $(this);
var id = that.attr('href').substring(1);
that.on('click', function (e) {
var header = $('article a[name="' + id + '"]')
if (!header.parent().hasClass('active')) header.trigger('click');
$('html, body').animate({
scrollTop: header.offset().top
}, 'fast');
});
// If we find a link in the body with similar anchor, add the same behavior
$('.body a[href="#' + id + '"]').on('click', function (e) {
$('#sidebar a[href="#' + id + '"]').trigger('click');
});
});
// Hide all/Show all links
var show = $('<a class="show">Show all</a>');
show.on('click', function () {
$('#content article:not(".active") > a').trigger('click');
});
var hide = $('<a class="hide">Hide all</a>');
hide.on('click', function () {
$('#content article.active > a').trigger('click');
});
$('#sidebar > ul').prepend($('<div class="control"></div>').append(show).append(hide));
// Making our navigation sticky
new Filter($('#sidebar > ul'));
})