-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathsearch.html
135 lines (114 loc) · 3.96 KB
/
search.html
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
<!DOCTYPE html>
<head>
{% include head.html %}
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
{% if page.datatable == true %}
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function(){
$('table.datatable').DataTable( {
paging: false,
stateSave: true
}
);
});
</script>
{% endif %}
</head>
<body>
{% include topnav.html %}
<!-- Page Content -->
<div class="container">
<!-- Content Row -->
<div class="row">
<!-- Content Column -->
<div class="col-md-8 col-md-offset-2 col-sm-12">
<h1>Search Results</h1>
<div class="results">
<img src="/images/LB_Preloader.gif" alt="Loading">
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<script>
// List of HTML entities for escaping.
var htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
};
// Regex containing the keys listed immediately above.
var htmlEscaper = /[&<>"'\/]/g;
// Escape a string for HTML interpolation.
function escapeHTML(string) {
string = ('' + string).replace(htmlEscaper, function(match) {
return htmlEscapes[match];
});
// Undo <em></em> -- used to highlight matches
string = string.replace(/<em>/g, '<em>');
return string.replace(/</em>/g, '</em>');
};
$(document).ready(function() {
var query = window.location.href.slice(window.location.href.indexOf('?') + 1);
if (query && query.length > 0) {
var split = query.split('&');
var queryObj = {};
split.forEach(function(s) {
queryObj[s.split('=')[0]] = s.split('=')[1];
});
}
$('.search-bar').val(queryObj.q.replace('+', ' '));
if(queryObj.sidebar && queryObj.sidebar !== "") {
$('input[name="sidebar"]').val(queryObj.sidebar);
}
var urlBase = "https://us-south.functions.cloud.ibm.com/api/v1/web/dhmlau%40ca.ibm.com_dev/default/search.json?";
var url = urlBase + query;
$.get(url).done(function(data) {
if (!data.status) {
$('.results').html('<h3>' + escapeHTML(data.msg) + '</h3>');
} else {
var html = '<ul class="resultList">'
data.results.forEach(function(res) {
if (!res.highlight.text) {
res.highlight.text = []
}
res.highlight.text.forEach(function(t) {
t = escapeHTML(t);
});
var displayedTitle = escapeHTML(res.extracted_metadata.title);
displayedTitle = displayedTitle.slice(0, displayedTitle.indexOf('|')!=-1? displayedTitle.indexOf('|'): displayedTitle.length);
html += '<li><a class="title" href="' + escapeHTML(res.metadata.source.url) + '">' + displayedTitle + '</a><br/>';
html += '<a class="link" href="' + escapeHTML(res.metadata.source.url) + '">' + escapeHTML(res.metadata.source.url) + '</a>';
html += '<p>' + res.highlight.text.splice(0, 3).join('<br/>') + '</p></li>';
});
html += '</ul>';
// Page Index
var pages = Math.floor(data.matching_results / 10);
var offset = 0;
if (queryObj.offset) {
offset = Number(queryObj.offset);
}
html += '<ul class="pagination">'
for (var i=0; i<pages; i++) {
queryObj.offset = i*10;
var page = i + 1;
var active = Math.floor(offset / 10) == i ? 'active' : '';
html += '<li class="' + active + '"><a href="/search?' + $.param(queryObj) + '">' + page + '</a></li>';
}
$('.results').html(html);
}
});
})
</script>
</body>
</html>