Tyler Scott | 696d38f | 2015-08-04 22:41:22 -0600 | [diff] [blame] | 1 | //Run when the document loads AND we have the config loaded. |
| 2 | (function(){ |
| 3 | var catalog = null; |
| 4 | var config = null; |
| 5 | Promise.all([ |
| 6 | new Promise(function(resolve, reject){ |
| 7 | $.ajax('config.json').done(function(data){ |
| 8 | catalog = data.catalogPrefix; |
| 9 | config = data.faceConfig; |
| 10 | resolve(); |
| 11 | }).fail(function(){ |
| 12 | console.error("Failed to get config."); |
| 13 | reject() |
| 14 | }); |
| 15 | }), |
| 16 | new Promise(function(resolve, reject){ |
| 17 | var timeout = setTimeout(function(){ |
| 18 | console.error("Document never loaded? Something bad has happened!"); |
| 19 | reject(); |
| 20 | }, 10000); |
| 21 | $(function () { |
| 22 | clearTimeout(timeout); |
| 23 | resolve(); |
| 24 | }); |
| 25 | }) |
| 26 | ]).then(function(){ |
| 27 | new Atmos(catalog, config); |
| 28 | }, function(){ |
| 29 | console.error("Failed to initialize!"); |
| 30 | }) |
| 31 | })(); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 32 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 33 | var Atmos = (function(){ |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 34 | "use strict"; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 35 | /** |
| 36 | * Atmos |
| 37 | * @version 2.0 |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 38 | * |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 39 | * Configures an Atmos object. This manages the atmos interface. |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 40 | * |
| 41 | * @constructor |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 42 | * @param {string} catalog - NDN path |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 43 | * @param {Object} config - Object of configuration options for a Face. |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 44 | */ |
| 45 | function Atmos(catalog, config){ |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 46 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 47 | //Internal variables. |
| 48 | this.results = []; |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 49 | this.resultCount = Infinity; |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 50 | this.name = null; |
| 51 | this.page = 0; |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 52 | this.resultsPerPage = 25; |
| 53 | this.retrievedSegments = 0; |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 54 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 55 | this.catalog = catalog; |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 56 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 57 | this.face = new Face(config); |
Tyler Scott | 696d38f | 2015-08-04 22:41:22 -0600 | [diff] [blame] | 58 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 59 | this.categories = $('#side-menu'); |
| 60 | this.resultTable = $('#resultTable'); |
| 61 | this.filters = $('#filters'); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 62 | this.searchInput = $('#search'); |
| 63 | this.searchBar = $('#searchBar'); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 64 | this.searchButton = $('#searchButton'); |
| 65 | this.pagers = $('.pager'); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 66 | this.alerts = $('#alerts'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 67 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 68 | var scope = this; |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 69 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 70 | this.resultTable.on('click', '.interest-button', function(){ |
| 71 | var button = $(this); |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 72 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 73 | if (button.is(':disabled')){ |
| 74 | console.warn("Attempt to request again!"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 75 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 76 | |
| 77 | var name = button.parent().prev().text(); |
| 78 | var interest = new Interest(new Name('/retrieve' + name)); |
| 79 | scope.face.expressInterest(interest, function(){}, function(){}); |
| 80 | |
| 81 | button.text("Requested!") |
| 82 | .removeClass('btn-primary') |
Tyler Scott | 696d38f | 2015-08-04 22:41:22 -0600 | [diff] [blame] | 83 | .addClass('btn-success') |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 84 | .addClass('disabled') |
| 85 | .prop('disabled', true); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 86 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 87 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 88 | //Filter setup |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 89 | $.getJSON("search_catagories.json").done(function (data) { |
| 90 | $.each(data, function (pageSection, contents) { |
| 91 | if (pageSection == "SearchCatagories") { |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 92 | $.each(contents, function (category, searchOptions) { |
| 93 | //Create the category |
| 94 | var e = $('<li><a href="#">' + category.replace(/\_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 95 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 96 | var sub = e.find('ul.subnav'); |
| 97 | $.each(searchOptions, function(index, name){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 98 | //Create the filter list inside the category |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 99 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 100 | sub.append(item); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 101 | item.click(function(){ //Click on the side menu filters |
| 102 | if (item.hasClass('active')){ //Does the filter already exist? |
| 103 | item.removeClass('active'); |
| 104 | scope.filters.find(':contains(' + category + ':' + name + ')').remove(); |
| 105 | } else { //Add a filter |
| 106 | item.addClass('active'); |
| 107 | var filter = $('<span class="label label-default"></span>'); |
| 108 | filter.text(category + ':' + name); |
| 109 | |
| 110 | scope.filters.append(filter); |
| 111 | |
| 112 | filter.click(function(){ //Click on a filter |
| 113 | filter.remove(); |
| 114 | item.removeClass('active'); |
| 115 | }); |
| 116 | } |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 117 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 118 | }); |
| 119 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 120 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 121 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
| 122 | e.find('> a').click(function(){ |
| 123 | scope.categories.find('.subnav').slideUp(); |
| 124 | var t = $(this).siblings('.subnav'); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 125 | if ( !t.is(':visible') ){ //If the sub menu is not visible |
| 126 | t.slideDown(function(){ |
| 127 | t.triggerHandler('focus'); |
| 128 | }); //Make it visible and look at it. |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 129 | } |
| 130 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 131 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 132 | scope.categories.append(e); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 133 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 134 | }); |
| 135 | } |
| 136 | }); |
| 137 | }); |
| 138 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 139 | this.searchInput.autoComplete(function(field, callback){ |
| 140 | scope.autoComplete(field, callback); |
| 141 | }); |
| 142 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 143 | this.searchBar.submit(function(e){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 144 | e.preventDefault(); |
Tyler Scott | 6b81081 | 2015-08-11 18:20:05 -0600 | [diff] [blame^] | 145 | scope.pathSearch(); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 146 | }); |
| 147 | |
| 148 | this.searchButton.click(function(){ |
| 149 | console.log("Search Button Pressed"); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 150 | scope.search(); |
| 151 | }); |
| 152 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 153 | this.pagers.find('.next').click(function(){ |
| 154 | if (!$(this).hasClass('disabled')){ |
| 155 | scope.getResults(scope.page + 1); |
| 156 | } |
| 157 | }); |
| 158 | this.pagers.find('.previous').click(function(){ |
| 159 | if (!$(this).hasClass('disabled')){ |
| 160 | scope.getResults(scope.page - 1); |
| 161 | } |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 162 | }); |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 163 | this.pagers.find('.pageLength').attr('contentEditable', true) |
| 164 | .blur(function(){ |
| 165 | scope.resultsPerPage = Number($(this).text()); |
| 166 | scope.pagers.find('.pageLength').text(scope.resultsPerPage); |
| 167 | scope.getResults(0); //Reset page to 0; |
| 168 | }); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 169 | |
| 170 | } |
| 171 | |
Tyler Scott | 6b81081 | 2015-08-11 18:20:05 -0600 | [diff] [blame^] | 172 | Atmos.prototype.clearResults = function(){ |
| 173 | this.results = []; //Drop any old results. |
| 174 | this.retrievedSegments = 0; |
| 175 | this.resultCount = Infinity; |
| 176 | this.page = 0; |
| 177 | this.resultTable.empty(); |
| 178 | } |
| 179 | |
| 180 | Atmos.prototype.pathSearch = function(){ |
| 181 | var value = this.searchInput.val(); |
| 182 | |
| 183 | this.clearResults(); |
| 184 | |
| 185 | var scope = this; |
| 186 | |
| 187 | this.query(this.catalog, {"??": value}, |
| 188 | function(interest, data){ |
| 189 | console.log("Query response:", interest, data); |
| 190 | |
| 191 | var parameters = JSON.stringify({"??": value}); |
| 192 | |
| 193 | var ack = data.getName(); |
| 194 | |
| 195 | scope.name = new Name(scope.catalog).append("query-results").append(parameters).append(ack.get(-3)).append(ack.get(-2)); |
| 196 | |
| 197 | scope.getResults(0); |
| 198 | |
| 199 | }, |
| 200 | function(interest){ |
| 201 | console.warn("Request failed! Timeout", interest); |
| 202 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 203 | }); |
| 204 | |
| 205 | } |
| 206 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 207 | Atmos.prototype.search = function(){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 208 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 209 | var filters = this.getFilters(); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 210 | |
| 211 | console.log("Search started!", this.searchInput.val(), filters); |
| 212 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 213 | console.log("Initiating query"); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 214 | |
Tyler Scott | 6b81081 | 2015-08-11 18:20:05 -0600 | [diff] [blame^] | 215 | this.clearResults(); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 216 | |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 217 | var scope = this; |
| 218 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 219 | this.query(this.catalog, filters, |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 220 | function(interest, data){ //Response function |
| 221 | console.log("Query Response:", interest, data); |
| 222 | |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 223 | var parameters = JSON.stringify(filters); |
| 224 | |
| 225 | var ack = data.getName(); |
| 226 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 227 | scope.name = new Name(scope.catalog).append("query-results").append(parameters).append(ack.get(-3)).append(ack.get(-2)); |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 228 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 229 | scope.getResults(0); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 230 | |
| 231 | }, function(interest){ //Timeout function |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 232 | console.warn("Request failed! Timeout"); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 233 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 234 | }); |
| 235 | |
| 236 | } |
| 237 | |
| 238 | Atmos.prototype.autoComplete = function(field, callback){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 239 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 240 | if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){ |
| 241 | if (!this.searchBar.hasClass('has-error')){ |
| 242 | this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>'); |
| 243 | } |
| 244 | return; |
| 245 | } else { |
| 246 | this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()}); |
| 247 | } |
| 248 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 249 | var scope = this; |
| 250 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 251 | this.query(this.catalog, {"?": field}, |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 252 | function(interest, data){ |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 253 | |
| 254 | var ack = data.getName(); |
| 255 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 256 | var name = new Name(scope.catalog).append('query-results').append(JSON.stringify({"?": field})).append(ack.get(-3)).append(ack.get(-2)); |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 257 | |
Tyler Scott | ab822fe | 2015-08-10 14:38:49 -0600 | [diff] [blame] | 258 | var interest = new Interest(name); |
| 259 | interest.setInterestLifetimeMilliseconds(5000); |
| 260 | interest.setMustBeFresh(true); |
| 261 | |
| 262 | scope.face.expressInterest(interest, |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 263 | function(interest, data){ |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 264 | |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 265 | if (data.getContent().length !== 0){ |
| 266 | var options = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")).next.map(function(element){ |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 267 | return field + element + "/"; |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 268 | }); |
| 269 | callback(options); |
| 270 | } |
| 271 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 272 | }, function(interest){ |
| 273 | console.warn("Interest timed out!", interest); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 274 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 275 | }); |
| 276 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 277 | }, function(interest){ |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 278 | console.error("Request failed! Timeout", interest); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 279 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 280 | }); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 281 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 282 | } |
| 283 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 284 | Atmos.prototype.showResults = function(resultIndex) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 285 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 286 | var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1)); |
| 287 | |
| 288 | var resultDOM = $(results.reduce(function(prev, current){ |
Tyler Scott | 696d38f | 2015-08-04 22:41:22 -0600 | [diff] [blame] | 289 | prev.push('<tr><td><input type="checkbox"></td><td>'); |
| 290 | prev.push(current); |
| 291 | prev.push('</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr>'); |
| 292 | return prev; |
| 293 | }, []).join('')); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 294 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 295 | this.resultTable.empty().append(resultDOM); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 296 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 297 | this.pagers.find('.pageNumber').text(resultIndex + 1); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 298 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 299 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 300 | this.pagers.find('.next').addClass('disabled'); |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 301 | } else if (resultIndex === 0){ |
| 302 | this.pagers.find('.next').removeClass('disabled'); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 303 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 304 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 305 | if (resultIndex === 0){ |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 306 | this.pagers.find('.previous').addClass('disabled'); |
| 307 | } else if (resultIndex === 1) { |
| 308 | this.pagers.find('.previous').removeClass('disabled'); |
| 309 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 310 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 311 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 312 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 313 | Atmos.prototype.getResults = function(index){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 314 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 315 | if ((this.results.length === this.resultCount) || (this.resultsPerPage * (index + 1) < this.results.length)){ |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 316 | //console.log("We already have index", index); |
| 317 | this.page = index; |
| 318 | this.showResults(index); |
| 319 | return; |
| 320 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 321 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 322 | if (this.name === null) { |
| 323 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 324 | throw new Error("Illegal State"); |
| 325 | } |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 326 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 327 | var first = new Name(this.name).appendSegment(this.retrievedSegments++); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 328 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 329 | console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri()); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 330 | |
| 331 | var scope = this; |
| 332 | |
Tyler Scott | ab822fe | 2015-08-10 14:38:49 -0600 | [diff] [blame] | 333 | var interest = new Interest(first) |
| 334 | interest.setInterestLifetimeMilliseconds(5000); |
| 335 | interest.setMustBeFresh(true); |
| 336 | |
| 337 | this.face.expressInterest(interest, |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 338 | function(interest, data){ //Response |
| 339 | |
| 340 | if (data.getContent().length === 0){ |
Tyler Scott | 0385485 | 2015-08-07 15:45:10 -0600 | [diff] [blame] | 341 | scope.pagers.find('.totalResults').text(0); |
| 342 | scope.pagers.find('.pageNumber').text(0); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 343 | console.log("Empty response."); |
| 344 | return; |
| 345 | } |
| 346 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 347 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 348 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 349 | if (!content.results){ |
| 350 | scope.pagers.find('.totalResults').text(0); |
| 351 | scope.pagers.find('.pageNumber').text(0); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 352 | console.log("No results were found!"); |
| 353 | return; |
| 354 | } |
| 355 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 356 | scope.results = scope.results.concat(content.results); |
| 357 | |
| 358 | scope.resultCount = content.resultCount; |
| 359 | |
| 360 | scope.pagers.find('.totalResults').text(scope.resultCount); |
| 361 | |
| 362 | scope.page = index; |
| 363 | |
| 364 | scope.getResults(index); //Keep calling this until we have enough data. |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 365 | |
| 366 | }, |
| 367 | function(interest){ //Timeout |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 368 | console.error("Failed to retrieve results: timeout", interest); |
| 369 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 370 | } |
| 371 | ); |
| 372 | |
| 373 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 374 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 375 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 376 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 377 | var queryPrefix = new Name(prefix); |
| 378 | queryPrefix.append("query"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 379 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 380 | var jsonString = JSON.stringify(parameters); |
| 381 | queryPrefix.append(jsonString); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 382 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 383 | var queryInterest = new Interest(queryPrefix); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 384 | queryInterest.setInterestLifetimeMilliseconds(4000); |
Tyler Scott | ab822fe | 2015-08-10 14:38:49 -0600 | [diff] [blame] | 385 | queryInterest.setMustBeFresh(true); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 386 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 387 | this.face.expressInterest(queryInterest, callback, timeout); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 388 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 389 | } |
| 390 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 391 | /** |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 392 | * This function returns a map of all the categories active filters. |
| 393 | * @return {Object<string, string>} |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 394 | */ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 395 | Atmos.prototype.getFilters = function(){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 396 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 397 | var data = $(current).text().split(/:/); |
| 398 | prev[data[0]] = data[1]; |
| 399 | return prev; |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 400 | }, {}); //Collect a map<category, filter>. |
| 401 | //TODO Make the return value map<category, Array<filter>> |
| 402 | return filters; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 403 | } |
| 404 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 405 | /** |
| 406 | * Creates a closable alert for the user. |
| 407 | * |
| 408 | * @param {string} message |
| 409 | * @param {string} type - Override the alert type. |
| 410 | */ |
| 411 | Atmos.prototype.createAlert = function(message, type) { |
| 412 | |
| 413 | var alert = $('<div class="alert"><div>'); |
| 414 | alert.addClass(type?type:'alert-info'); |
| 415 | alert.text(message); |
| 416 | alert.append(Atmos.closeButton); |
| 417 | |
| 418 | this.alerts.append(alert); |
| 419 | } |
| 420 | Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
| 421 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 422 | return Atmos; |
| 423 | |
| 424 | })(); |