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