-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaikido.js
117 lines (114 loc) · 2.52 KB
/
aikido.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
112
113
114
115
116
117
var old_query = [];
function search() {
var query = $('#search').val();
if (query == old_query) {
return;
}
old_query = query;
if (query.match(/^ *$/)) {
$('li').show();
delay_set_title_hash('');
return;
}
var query_words = query.toLowerCase().split(" ");
for (var i=0; i<query_words.length; ++i) {
query_words[i] = query_words[i].replace(/\./g, ' ');
}
var count = 0;
$('li').hide();
$('li').each(function() {
var $li = $(this);
var $path_li = $.merge($li, $li.parents('li'));
var path = ' ';
$path_li.each(function() {
var $li = $(this);
var $a = $li.children('a').first();
path += $a.text() + ' ';
});
path = path.toLowerCase();
var match = true;
for (var i=0; i<query_words.length; ++i) {
var w = query_words[i];
var exclude = w.indexOf('-') == 0;
if (exclude) {
w = w.substr(1);
if (w.length == 0) {
continue;
}
}
var found = path.indexOf(w) >= 0;
if (exclude == found) {
match = false;
break;
}
}
if (match) {
$li.show();
$li.parents('li').show();
++count;
}
});
if (count) {
delay_set_title_hash(query);
} else {
clearTimeout(set_title_hash_timeout);
}
}
var new_hash;
var new_title;
var set_title_hash_timeout;
function delay_set_title_hash(query) {
new_hash = '#'+query.replace(/ /g, '+');
new_title = query_to_title(query);
if (set_title_hash_timeout) {
clearTimeout(set_title_hash_timeout);
}
set_title_hash_timeout = setTimeout(set_title_hash, 2000);
}
function query_to_title(query) {
var new_title = $('title').text();
new_title = new_title.replace(/.* - |^/, '');
if (query != '') {
new_title = query + ' - ' + new_title;
}
return new_title;
}
function set_title_hash() {
location.hash = new_hash;
$('title').text(new_title);
}
function on_hash_change() {
$('title').text(query_to_title(hash_to_query(location.hash)));
if (location.hash != new_hash) {
var query = hash_to_query(location.hash);
$('#search').val(query);
search();
}
}
function hash_to_query(hash) {
var query = hash.replace(/\+/g, ' ');
query = decodeURIComponent(query);
if (query.length) {
query = query.substr(1);
}
return query;
}
function search_clear() {
$('#search').focus();
$('#search').val('');
search();
}
$('#search').on('input', search);
$('#search').on('change', search);
$('#search').on('keyup', function(event) {
event.preventDefault();
if (event.which == 13) {
search();
}
});
$('#search_clear').on('click', search_clear);
$(window).hashchange(on_hash_change);
$(function() {
$('#search').focus();
on_hash_change();
});