-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path06-searchresults.js
205 lines (164 loc) · 5.6 KB
/
06-searchresults.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
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
// Get URL parameters as an object
// USAGE:
// getAllUrlParams().product; // 'shirt'
// getAllUrlParams().color; // 'blue'
// getAllUrlParams().newuser; // true
// getAllUrlParams().nonexistent; // undefined
// getAllUrlParams('http://test.com/?a=abc').a; // 'abc'
function getAllUrlParams(url) {
// get query string from url (optional) or window
var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
// we'll store the parameters here
var obj = {};
// if query string exists
if (queryString) {
// stuff after # is not part of query string, so get rid of it
queryString = queryString.split('#')[0];
// split our query string into its component parts
var arr = queryString.split('&');
for (var i=0; i<arr.length; i++) {
// separate the keys and the values
var a = arr[i].split('=');
// in case params look like: list[]=thing1&list[]=thing2
var paramNum = undefined;
var paramName = a[0].replace(/\[\d*\]/, function(v) {
paramNum = v.slice(1,-1);
return '';
});
// set parameter value (use 'true' if empty)
var paramValue = typeof(a[1])==='undefined' ? true : a[1];
// (optional) keep case consistent
paramName = paramName.toLowerCase();
paramValue = paramValue.toLowerCase();
// if parameter name already exists
if (obj[paramName]) {
// convert value to array (if still string)
if (typeof obj[paramName] === 'string') {
obj[paramName] = [obj[paramName]];
}
// if no array index number specified...
if (typeof paramNum === 'undefined') {
// put the value on the end of the array
obj[paramName].push(paramValue);
}
// if array index number specified...
else {
// put the value at that index number
obj[paramName][paramNum] = paramValue;
}
}
// if param name doesn't exist yet, set it
else {
obj[paramName] = paramValue;
}
}
}
return obj;
}
// Strip the + delimeters
function stripDelimeters(string) {
return string.split('+').join(' ');
}
// specialCombos()
// if the query matches the predefined combination word terms, use a
// keyword search
function specialCombos(query) {
var specials = new Array();
specials[0] = "delivery manager";
specials[1] = "service designer";
specials[2] = "service manager";
specials[3] = "product manager";
specials[4] = "interaction designer";
specials[5] = "visual designer";
specials[6] = "service design";
specials[7] = "technical architect";
specials[8] = "user researcher";
specials[9] = "content designer";
specials[10] = "performance analyst";
var words = query.split(' ');
for (var i = 0; i < specials.length ; i++) {
if (query == specials[i]) {
return "+keywords:" + words[0] + " " + "+keywords:" + words[1];
continue;
}
}
return query;
}
// only run Lunr code on the search page
// as Lunr.min.js is not loaded by default
if (window.location.pathname == "/search/" ) {
var rawQuery = getAllUrlParams().q;
// strip + delimeters from query
var query = stripDelimeters(rawQuery);
//var query = rawQuery.replace("+"," ");
var searchresults__count = document.getElementById("searchresults__count");
var searchresults__resultslist = document.getElementById("searchresults__resultslist");
var searchresults__query = document.getElementById("searchresults__query");
var resultsObj = new Object();
var htmlstring = "";
function reqListener() {
var obj = JSON.parse(this.responseText);
var index = lunr.Index.load(obj);
var searchresults_json = index.search(specialCombos(query), {});
if (searchresults_json.length > 0) {
resultsObj = searchresults_json;
} else { //fuzzy search now
searchresults_json = index.search(specialCombos(query) + "~1", {});
if (searchresults_json.length > 0) {
resultsObj = searchresults_json;
} else {
searchresults_json = index.search(specialCombos(query) + "~2", {});
resultsObj = searchresults_json;
}
}
if (resultsObj.length == 0){
searchresults__count.innerHTML = "No";
}
else {
searchresults__count.innerHTML = resultsObj.length;
}
searchresults__query.innerHTML = "<strong>" + query + "</strong>";
//console.log(JSON.stringify(searchresults_json));
// fetch the path map to lookup title and form the link
var load_pathmap = new XMLHttpRequest();
load_pathmap.onload = pmListener;
load_pathmap.onerror = reqError;
load_pathmap.open('get', '../pathmap.json', true);
load_pathmap.send();
}
function pmListener() {
var documentsjson = JSON.parse(this.responseText);
var resultcount = 0;
// form the results list
resultsObj.forEach (function (result) {
resultcount ++;
// lookup the title
var pagetitle = "";
var pagedescription = "";
var url = "";
for (var i = 0; i < documentsjson.length; i++ ) {
if (documentsjson[i].id == result.ref) {
pagetitle = documentsjson[i].title;
pagedescription = documentsjson[i].description;
url = documentsjson[i].path;
continue;
}
}
// form the HTML list result item
var rel = "";
if (url.substring(0,1) == "h") {
rel = "rel='external'";
}
htmlstring = htmlstring + "<li><h3><a " + rel + "href='" + url +"'>" + pagetitle + "</a></h3><p>" + pagedescription + "</p></li>";
});
searchresults__resultslist.innerHTML = htmlstring;
}
function reqError(err) {
console.log('Fetch Error :-S', err);
}
var load_index = new XMLHttpRequest();
load_index.onload = reqListener;
load_index.onerror = reqError;
load_index.open('get', '../search_index.json', true);
load_index.send();
}