-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.js
More file actions
237 lines (205 loc) · 5.29 KB
/
search.js
File metadata and controls
237 lines (205 loc) · 5.29 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
# ***** BEGIN LICENSE BLOCK *****
#
# Noviny2
# Theme by Pierre Van Glabeke
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# ***** END LICENSE BLOCK *****
*/
// Tags list display
$(function() {
var tags = $('#custom .tags li:has(a[class^=tag])').filter(function() {
var c = $('a',this).attr('class').substr(3);
return c <= 10; // We display only tags with % > 10
}).hide();
if (tags.length > 0) {
var all_tags = $('<a href="#">' + noviny2.all_tags + '</a>').click(function() {
$('#custom .tags li:hidden').show().css('opacity',0.2).fadeTo(1600,1);
$('#custom .tags p').remove();
return false;
});
$('#custom .tags').append($('<p></p>').append(all_tags));
}
});
// Search suggest
$(function() {
$('#q').searchSuggest({
source: noviny2.ajaxsearch,
forcePosition: {
position: 'absolute',
top: '20px',
left: 0
}
});
});
// Modified search suggest code from Peter Vulgaris (www.vulgarisoip.com)
(function($) {
$.searchSuggest = function(input,params) {
var defaults = {
source: null,
delay: 150,
minchars: 2,
selectClass: 'suggest-select',
maxCacheSize: 65536,
forcePosition: false
};
params = $.extend(defaults,params);
if (params.source == null) {
throw 'No source given';
}
input = $(input).attr("autocomplete", "off");
var results = $(document.createElement('div'));
var timeout = false; // hold timeout ID for suggestion results to appear
var prevLength = 0; // last recorded length of $input.val()
var cache = []; // cache MRU list
var cacheSize = 0; // size of cache in chars (bytes?)
results.attr('id','search-suggest').appendTo(input.parent()).hide();
resetPosition();
$(window).load(resetPosition).resize(resetPosition);
input.blur(function() {
setTimeout(hideList,100);
function hideList() {
results.hide('fast');
};
});
try {
results.bgIframe();
} catch (e) {}
if ((navigator.userAgent.indexOf("Mozilla") > 0)) {
input.keypress(processKey); // onkeypress repeats arrow keys in Mozilla/Opera
} else {
input.keydown(processKey); // onkeydown repeats arrow keys in IE/Safari
}
function hideList() {
results.hide('fast');
};
function resetPosition() {
var offset = input.offset();
if (!params.forcePosition) {
results.attr({
top: (offset.top + input.height()),
left: offset.left - input.width()
});
} else {
results.css(params.forcePosition);
}
};
function processKey(e) {
// handling up/down/escape requires results to be visible
// handling enter requires that AND a result to be selected
if ((/(27|38|40)$/.test(e.keyCode) && results.is(':visible')) || (e.keyCode == 13 && getCurrentResult()))
{
if (e.preventDefault)
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
switch(e.keyCode) {
case 38: // up
prevResult();
break;
case 40: // down
nextResult();
break;
case 27: // escape
results.hide('fast');
break;
case 13: // return
document.location.href = $('a',getCurrentResult()).attr('href');
break;
}
}
else if (input.val().length != prevLength)
{
if (timeout)
clearTimeout(timeout);
timeout = setTimeout(suggest, params.delay);
prevLength = input.val().length;
}
};
function suggest() {
var q = $.trim(input.val());
if (q.length >= params.minchars) {
var cached = checkCache(q);
if (cached) {
displayItems(cached['data']);
} else {
$.get(params.source + q,function(data) {
displayItems(data);
addToCache(q,data,data.length);
});
}
} else {
results.hide('fast');
}
};
function checkCache(q) {
for (var i = 0; i < cache.length; i++) {
if (cache[i]['q'] == q) {
cache.unshift(cache.splice(i, 1)[0]);
return cache[0];
}
}
return false;
};
function addToCache(q,data,size) {
while (cache.length && (cacheSize + size > params.maxCacheSize)) {
var cached = cache.pop();
cacheSize -= cached['size'];
}
cache.push({
q: q,
size: size,
data: data
});
cacheSize += size;
};
function displayItems(data) {
if (data.length == 0) {
results.hide('fast');
return;
}
results.empty().append(data).show('fast');
$('li',results).mouseover(function() {
$('li',results).removeClass(params.selectClass);
$(this).addClass(params.selectClass);
});
};
function getCurrentResult() {
if (!results.is(':visible')) {
return false;
}
var res = $('li.'+params.selectClass,results);
if (res.length == 0) {
return false;
}
return res;
};
function nextResult() {
var res = getCurrentResult();
if (res) {
res.removeClass(params.selectClass).next().addClass(params.selectClass);
} else {
$('li:first-child',results).addClass(params.selectClass);
}
};
function prevResult() {
var res = getCurrentResult();
if (res) {
res.removeClass(params.selectClass).prev().addClass(params.selectClass);
} else {
$('li:last-child',results).addClass(params.selectClass);
}
};
};
$.fn.searchSuggest = function(params) {
this.each(function() {
new $.searchSuggest(this,params);
});
return this;
};
})(jQuery);