-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.js
65 lines (62 loc) · 1.83 KB
/
search.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
function initSearch(events, dl) {
$( "#search-source" ).autocomplete({
minLength: 0,
source: function(request, response) {
var re = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(events, function(item) {
return re.test(item.sentence);
}));
},
focus: function( event, ui ) {
$( "#source" ).val( ui.item.text );
return false;
},
select: function( event, ui ) {
$( "#source" ).val( ui.item.text );
scrollTo(dl, ui.item.id)
return false;
}
})
.autocomplete( "instance" )._renderItem = function( ul, item ) {
var re = new RegExp("(" + $.ui.autocomplete.escapeRegex(this.term) + ")", "i");
var text = htmlize(item.sentence).replace(re, "<span class='ui-match'>$1</span>");
return $( "<li>" )
.append(
"<div>" +
"<span class='ui-source'>" + item.text + "</span> " +
"<span class='ui-meta'>" + item.start.toLocaleDateString(undefined, {month:"long", day:"numeric", year:"numeric"}) + "</span>" +
"<br/>" +
"<span class='ui-sent'>" + text + "</span>" +
"</div>" )
.appendTo( ul );
};
}
/*
* If the URL contains an event id as fragment, scroll to the
* event and show its info box. */
function scrollToFragment(dl) {
if (document.location.hash)
scrollTo(dl, document.location.hash.substr(1));
}
/*
* Scroll to the event with the given id and show its info box.
*/
function scrollTo(dl, itemid) {
var evt = dl.find(itemid);
// hack: after some delay (!) show info bubble
setTimeout(function(){
for (var i=0; i < evt.elements.length; i++) {
var elem = evt.elements[i];
if (elem.classList.contains("d-event")) {
simulateClick(elem);
}
}
}, 2000);
}
function simulateClick(cb) {
cb.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
}));
}