-
Notifications
You must be signed in to change notification settings - Fork 1
/
fd-dev-rss-search.js
116 lines (97 loc) · 3.55 KB
/
fd-dev-rss-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
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
/* vim: textwidth=0 wrapmargin=0 shiftwidth=2 tabstop=2 expandtab softtabstop
*/
var performSearch_inprogress = false;
function performSearch(ev) {
ev.preventDefault();
if (performSearch_inprogress) {
$(".results#status").text("");
$(".results#status").append("<p>Search is <strong>already</strong> in progress, please <em>be patient</em>!</p>");
return;
}
clearResultsOutput();
if (!$('#search_text').val()) {
$(".results#status").text("");
$(".results#status").append("<p>You need to enter some search terms!</p>");
return false;
}
// If there are no ampersands or pipes or exclamation marks, assume someone
// wants a simple "all these words" search and insert the necessary & characters.
var q = $('#search_text').val();
if (q.search('[\&\|\!]') == -1) {
var terms = q.split(' ');
q = terms.join(' & ');
}
search_query = {
search_text: q,
search_in_title: $('#search_in_title').prop('checked') ? 'true' : 'false',
search_in_precis: $('#search_in_precis').prop('checked') ? 'true' : 'false'
};
if (search_query['search_in_title'] != 'true' && search_query['search_in_precis'] != 'true') {
$(".results#status").text("");
$(".results#status").append("<p>At least one of 'In Title' or 'In Precis' must be selected.</p>");
return false;
}
//console.log('performSearch: search_query = %o', search_query);
$.ajax({
url: "search.pl",
type: "GET",
data: search_query,
dataType: "json",
async: true,
success: onSearchResults,
error: onSearchError,
timeout: 20000
});
performSearch_inprogress = true;
return false;
}
function onSearchError(jqXHR, textStatus, errorThrown) {
console.log('onSearchError: %o, %o, %o', jqXHR, textStatus, errorThrown);
performSearch_inprogress = false;
$(".results#status").text("");
$(".results#status").append("<p>Error performing search: " + textStatus + "(" + errorThrown.message + ")</p>");
}
function onSearchResults(data) {
//console.log('onSearchResults: data = %o', data);
performSearch_inprogress = false;
$(".results#status").text("");
if (data.status != 'ok') {
$(".results#status").append("<p>Search failed: " + data.reason + "</p>");
return;
}
displaySearchResults(data);
}
function displaySearchResults(data) {
//console.log('displaySearchResults: For array %o', data.results);
var after;
for (r = 0; r < data.results.length ; r++) {
var new_r = $("#result_X.result_row").clone();
new_r.attr("id", 'result_' + r);
//console.log('displaySearchResults: cloned search result %o', new_r);
if (r == 0) {
after = $("#results_output").find("#result_X");
}
new_r.find(".results_info_item.result_rank").text(data.results[r].rank);
new_r.find(".results_info_item.result_datestamp").text(data.results[r].datestamp);
// <a href="URL">ThreadTitle</a> (Forum)
var new_r_url = new_r.find(".results_info_item.result_thread").html(
$('<a>', {
href: 'https://forums.frontier.co.uk/' + data.results[r].guid_url,
text: data.results[r].threadtitle
})
);
new_r_url.append(' (' + data.results[r].forum + ')');
new_r.find(".results_info_item.result_who").text(data.results[r].who);
var precis = data.results[r].precis.replace(new RegExp('\n', 'g'), '<br/>');
new_r.find(".results_info_item.result_precis").html(precis);
new_r.insertAfter(after);
after = new_r;
}
}
function clearResultsOutput() {
$(".results#status").text("");
$(".result_row").not("#result_X").remove();
}
$(function() {
$("#form_search").submit(performSearch);
});