blob: 6412d18f3206f2816573f8c04a2d0cb2eaafcee7 [file] [log] [blame]
Tyler Scott3c17d5f2015-06-23 17:49:29 -06001// {@ @todo: this need to be configured before the document load
2var catalog = "/catalog/myUniqueName";
3var face = new FaceInstance({
4 host: "localhost",
5 port: 9696
6});
7
8// @}
9
10var searchMenuOptions = {};
11var results = [];
12var resultCount = 0;
13var page = 1;
14var totalPages = 1;
15var selectedSearch = {};
16var dropdown = [];
17
18$(function () {
19 var searchMenu = $("#side-menu");
20 var currentPage = $(".page");
21 var resultTable = $(".resultTable");
22 var data = $.getJSON("search_catagories.json", function (data) { //url, success
23 $.each(data, function (pageSection, contents) {
24 if (pageSection == "SearchCatagories") {
25 $.each(contents, function (search, searchOptions) {
26 search = search.replace(/\_/g, " ");
27
28 searchMenu.append('<li id="' + search + '" onclick="getDropDown(this.id)"><a href="#">' + search + '</a></li>');
29 searchMenuOptions[String(search)] = searchOptions;
30 });
31 }
32 });
33 });
34});
35
36function onData(data) {
37 var payloadStr = data.content.toString().split("\n")[0];
38
39 var queryResults = JSON.parse(payloadStr);
40
41 var resultTable = $(".resultTable");
42 $.each(queryResults, function (queryResult, field) {
43
44 if (queryResult == "next") {
45 populateAutocomplete(field);
46 }
47
48 $.each(field, function (entryCount, name) {
49 results.push(name);
50 });
51 });
52
53 // Calculating the current page and the view
54 totalPages = Math.ceil(resultCount / 20);
55 populateResults(0);
56}
57
58var state = {};
59
60function query(prefix, parameters, callback, pipeline) {
61 results = [];
62 dropdown = [];
63
64 var resultTable = $(".resultTable");
65 resultTable.empty();
66 resultTable.append('<tr><td>Results</td></tr>');
67
68 var queryPrefix = new Name(prefix);
69 queryPrefix.add("query");
70
71 queryPrefix.add(JSON.stringify(parameters));
72
73 state = {
74 prefix: new Name(prefix),
75 userOnData: callback,
76 outstanding: {},
77 nextSegment: 0,
78 };
79
80 /*if (state.hasOwnProperty("version")) {
81 console.log("state already has version");
82 }*/
83
84 var queryInterest = new Interest(queryPrefix);
85 queryInterest.setInterestLifetimeMilliseconds(10000);
86
87 face.expressInterest(queryInterest,
88 onQueryData,
89 onQueryTimeout);
90
91 state["outstanding"][queryInterest.getName().toUri()] = 0;
92}
93
94function expressNextInterest() {
95 // @todo pipelines
96 var nextName = new Name(state["results"]);
97 nextName.appendSegment(state["nextSegment"]);
98
99 var nextInterest = new Interest(nextName);
100 nextInterest.setInterestLifetimeMilliseconds(10000);
101
102 face.expressInterest(nextInterest,
103 onQueryResultsData,
104 onQueryResultsTimeout);
105
106 state["nextSegment"] ++;
107 state["outstanding"][nextName.toUri()] = 0;
108}
109
110function onQueryData(interest, data) {
111 var name = data.getName();
112
113 delete state["outstanding"][interest.getName().toUri()];
114
115 state["version"] = name.get(state["prefix"].size() + 2).toVersion();
116
117 state["results"] = new Name(state["prefix"]).append("query-results").appendVersion(state["version"]);
118
119 expressNextInterest();
120}
121
122function onQueryResultsData(interest, data) {
123 var name = data.getName();
124 delete state["outstanding"][interest.getName().toUri()];
125
126 if (!name.get(-1).equals(new Name.Component("END"))) {
127 expressNextInterest();
128 } else {
129 alert("found final block");
130 }
131
132 state["userOnData"](data);
133}
134
135function onQueryTimeout(interest) {
136 var uri = interest.getName().toUri();
137 if (state["outstanding"][uri] < 1) {
138 state["outstanding"][uri] ++;
139 face.expressInterest(interest,
140 onQueryData,
141 onQueryTimeout);
142 } else {
143 delete state["outstanding"][uri];
144
145 // We modify the autocomplete box here because we need to know
146 // we have all of the entries first. Fairly hacky.
147 var autocompleteFullName = autocompleteText.value;
148 for (var i = 0; i < dropdown.length; ++i) {
149 if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) {
150 autocompleteText.value = dropdown[i];
151 }
152 }
153 }
154}
155
156function onQueryResultsTimeout(interest) {
157 var uri = interest.getName().toUri();
158 if (state["outstanding"][uri] < 1) {
159 state["outstanding"][uri] ++;
160 face.expressInterest(interest,
161 onQueryResultsData,
162 onQueryResultsTimeout);
163 } else {
164 delete state["outstanding"][uri];
165 // We modify the autocomplete box here because we need to know
166 // we have all of the entries first. Fairly hacky.
167 var autocompleteFullName = autocompleteText.value;
168 for (var i = 0; i < dropdown.length; ++i) {
169 if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) {
170 autocompleteText.value = dropdown[i];
171 }
172 }
173 }
174}
175
176
177var currentViewIndex = 0;
178
179function populateResults(startIndex) {
180 var resultTable = $(".resultTable");
181 resultTable.empty();
182 resultTable.append('<tr><td>Results</td></tr>');
183
184
185 for (var i = startIndex; i < startIndex + 20 && i < results.length; ++i) {
186 resultTable.append('<tr><td>' + results[i] + '</td></tr>');
187 }
188
189 if (results.length <= 20) {
190 page = 1;
191 } else {
192 page = startIndex / 20 + 1;
193 }
194
195 totalPages = Math.ceil(results.length / 20);
196
197 var currentPage = $(".page");
198 currentPage.empty();
199 if (page != 1) {
200 currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>');
201 }
202 // This section of code creates the paging for the results.
203 // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after
204 // the current page and the total pages (expect users not to really jump around a lot).
205 for (var i = 1; i <= totalPages; ++i) {
206 if (i == 1 || i == totalPages // Min or max
207 || (i <= page && i + 5 >= page) // in our current page range
208 || (i >= page && i - 5 <= page)) { // in our current page range
209 if (i != page) {
210 currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>');
211 if (i == 1 && page > i + 5) {
212 currentPage.append(' ... ');
213 }
214 } else {
215 currentPage.append(' ' + i);
216 }
217 } else { // Need to skip ahead
218 if (i == page + 6) {
219 currentPage.append(' ... ');
220
221 currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>');
222 i = totalPages - 1;
223 }
224 }
225 }
226 currentPage.append(' ' + results.length + ' results');
227}
228
229var dropState = "";
230
231function getDropDown(str) {
232 var searchMenu = $("#side-menu");
233 if (str == dropState) {
234 dropState = "";
235 searchMenu.find("#" + str).find("#options_" + str).empty();
236 } else {
237 dropState = str;
238
239 $.each(searchMenuOptions, function (search, fields) {
240 if (search === str) {
241 searchMenu.find("#" + search).append('<ul id="options_' + search + '" class="sub-menu">');
242 for (var i = 0; i < fields.length; ++i) {
243 searchMenu.find("#options_" + search).append('<li id="' + fields[i] + '"onclick="submitCatalogSearch(this.id)"><a href="#">' + fields[i] + '</a></li>');
244 }
245 searchMenu.append('</ul>');
246 } else {
247 var ul = $("options_" + search);
248 ul.empty();
249 searchMenu.find("#" + search).find("#options_" + search).empty();
250 }
251 });
252 }
253}
254
255function getPage(clickedPage) {
256 console.log(clickedPage);
257
258 var nextPage = clickedPage;
259 if (clickedPage === "<") {
260 nextPage = page - 5;
261 } else if (clickedPage === ">") {
262 console.log("> enabled");
263
264 nextPage = page + 5;
265 }
266
267 nextPage--; // Need to adjust for starting at 0
268
269 if (nextPage < 0 ) {
270 nextPage = 0;
271 console.log("0 enabled");
272 } else if (nextPage > totalPages - 1) {
273 nextPage = totalPages - 1;
274 console.log("total enabled");
275 }
276
277 populateResults(nextPage * 20);
278 return false;
279}
280
281function submitAutoComplete() {
282 if (autocompleteText.value.length > 0) {
283 var selection = autocompleteText.value;
284 $.each(dropdown, function (i, dropdownEntry) {
285 if (dropdownEntry.substr(0, dropdownEntry.length - 1) == selection) {
286 selection = dropdownEntry;
287 }
288 });
289
290 selectedSearch["?"] = selection;
291 query(catalog, selectedSearch, onData, 1);
292 delete selectedSearch["?"];
293 }
294}
295
296function submitCatalogSearch(field) {
297 console.log("Sumbit Catalog Search: " + field);
298 // @todo: this logic isn't quite right
299 var remove = false;
300 $.each(selectedSearch, function (search, f) {
301 if (field == f) {
302 delete selectedSearch[field];
303 remove = true;
304 }
305 });
306 if (!remove) {
307 $.each(searchMenuOptions, function (search, fields) {
308 $.each(fields, function (index, f) {
309 if (f == field) {
310 selectedSearch[search] = field;
311 }
312 });
313 });
314 }
315 query(catalog, selectedSearch, onData, 1);
316 populateCurrentSelections();
317 return false;
318}
319
320function populateAutocomplete(fields) {
321 var isAutocompleteFullName = (autocompleteText.value.charAt(autocompleteText.value.length - 1) === "/");
322 var autocompleteFullName = autocompleteText.value;
323 for (var i = 0; i < fields.length; ++i) {
324 var fieldFullName = fields[i];
325 var entry = autocompleteFullName;
326 var skipahead = "";
327
328 if (isAutocompleteFullName) {
329 skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length);
330 } else {
331 if (fieldFullName.charAt(autocompleteText.value.length) === "/") {
332 entry += "/";
333 skipahead = fieldFullName.substr(autocompleteText.value.length + 1, fieldFullName.length);
334 } else {
335 skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length);
336 }
337 }
338 if (skipahead.indexOf("/") != -1) {
339 entry += skipahead.substr(0, skipahead.indexOf("/") + 1);
340 } else {
341 entry += skipahead;
342 }
343
344 var added = false;
345 for (var j = 0; j < dropdown.length && !added; ++j) {
346 if (dropdown[j] === entry) {
347 added = true;
348 } else if (dropdown[j] > entry) {
349 dropdown.splice(j, 0, entry);
350 added = true;
351 }
352 }
353 if (!added) {
354 dropdown.push(entry);
355 }
356
357 }
358 $("#autocompleteText").autocomplete({
359 source: dropdown
360 });
361}
362
363function populateCurrentSelections() {
364 var currentSelection = $(".currentSelections");
365 currentSelection.empty();
366
367 currentSelection.append("<p>Filtering on:");
368
369 $.each(selectedSearch, function (searchMenuCatagory, selection) {
370 currentSelection.append(' <a href="#" onclick="removeFilter(this.id);" id="' + searchMenuCatagory + ':' + selection + '">[X] ' + searchMenuCatagory + ":" + selection + '</a>');
371 });
372
373 currentSelection.append("</p>");
374}
375
376
377function removeFilter(filter) {
378 console.log("Remove filter" + filter);
379 var searchFilter = filter.split(":");
380
381 var search = "";
382 for (var j = 0; j < searchFilter.length; ++j) {
383 search += searchFilter[j] + " ";
384 }
385 console.log("Split values: '" + search + "'");
386
387 delete selectedSearch[searchFilter[0]];
388 query(catalog, selectedSearch, onData, 1);
389 populateCurrentSelections();
390
391 return false;
392}