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 | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 145 | console.warn("This feature is incomplete."); |
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 | |
| 172 | Atmos.prototype.search = function(){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 173 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 174 | var filters = this.getFilters(); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 175 | |
| 176 | console.log("Search started!", this.searchInput.val(), filters); |
| 177 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 178 | console.log("Initiating query"); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 179 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 180 | this.results = []; //Drop any old results. |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 181 | this.retrievedSegments = 0; |
| 182 | this.resultCount = Infinity; |
| 183 | this.page = 0; |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 184 | this.resultTable.empty(); |
| 185 | |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 186 | var scope = this; |
| 187 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 188 | this.query(this.catalog, filters, |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 189 | function(interest, data){ //Response function |
| 190 | console.log("Query Response:", interest, data); |
| 191 | |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 192 | var parameters = JSON.stringify(filters); |
| 193 | |
| 194 | var ack = data.getName(); |
| 195 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 196 | 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] | 197 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 198 | scope.getResults(0); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 199 | |
| 200 | }, function(interest){ //Timeout function |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 201 | console.warn("Request failed! Timeout"); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 202 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 203 | }); |
| 204 | |
| 205 | } |
| 206 | |
| 207 | Atmos.prototype.autoComplete = function(field, callback){ |
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 | if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){ |
| 210 | if (!this.searchBar.hasClass('has-error')){ |
| 211 | this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>'); |
| 212 | } |
| 213 | return; |
| 214 | } else { |
| 215 | this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()}); |
| 216 | } |
| 217 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 218 | var scope = this; |
| 219 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 220 | this.query(this.catalog, {"?": field}, |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 221 | function(interest, data){ |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 222 | |
| 223 | var ack = data.getName(); |
| 224 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 225 | 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] | 226 | |
| 227 | scope.face.expressInterest(new Interest(name).setInterestLifetimeMilliseconds(5000), |
| 228 | function(interest, data){ |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 229 | |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 230 | if (data.getContent().length !== 0){ |
| 231 | var options = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")).next.map(function(element){ |
| 232 | return field + element; |
| 233 | }); |
| 234 | callback(options); |
| 235 | } |
| 236 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 237 | }, function(interest){ |
| 238 | console.warn("Interest timed out!", interest); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 239 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 240 | }); |
| 241 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 242 | }, function(interest){ |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 243 | console.error("Request failed! Timeout", interest); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 244 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 245 | }); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 246 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 247 | } |
| 248 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 249 | Atmos.prototype.showResults = function(resultIndex) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 250 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 251 | var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1)); |
| 252 | |
| 253 | var resultDOM = $(results.reduce(function(prev, current){ |
Tyler Scott | 696d38f | 2015-08-04 22:41:22 -0600 | [diff] [blame] | 254 | prev.push('<tr><td><input type="checkbox"></td><td>'); |
| 255 | prev.push(current); |
| 256 | prev.push('</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr>'); |
| 257 | return prev; |
| 258 | }, []).join('')); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 259 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 260 | this.resultTable.empty().append(resultDOM); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 261 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 262 | this.pagers.find('.pageNumber').text(resultIndex + 1); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 263 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 264 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 265 | this.pagers.find('.next').addClass('disabled'); |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 266 | } else if (resultIndex === 0){ |
| 267 | this.pagers.find('.next').removeClass('disabled'); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 268 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 269 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 270 | if (resultIndex === 0){ |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 271 | this.pagers.find('.previous').addClass('disabled'); |
| 272 | } else if (resultIndex === 1) { |
| 273 | this.pagers.find('.previous').removeClass('disabled'); |
| 274 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 275 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 276 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 277 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 278 | Atmos.prototype.getResults = function(index){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 279 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 280 | 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] | 281 | //console.log("We already have index", index); |
| 282 | this.page = index; |
| 283 | this.showResults(index); |
| 284 | return; |
| 285 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 286 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 287 | if (this.name === null) { |
| 288 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 289 | throw new Error("Illegal State"); |
| 290 | } |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 291 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 292 | var first = new Name(this.name).appendSegment(this.retrievedSegments++); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 293 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 294 | console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri()); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 295 | |
| 296 | var scope = this; |
| 297 | |
| 298 | this.face.expressInterest(new Interest(first).setInterestLifetimeMilliseconds(5000), |
| 299 | function(interest, data){ //Response |
| 300 | |
| 301 | if (data.getContent().length === 0){ |
| 302 | console.log("Empty response."); |
| 303 | return; |
| 304 | } |
| 305 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 306 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 307 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 308 | if (!content.results){ |
| 309 | scope.pagers.find('.totalResults').text(0); |
| 310 | scope.pagers.find('.pageNumber').text(0); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 311 | console.log("No results were found!"); |
| 312 | return; |
| 313 | } |
| 314 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 315 | scope.results = scope.results.concat(content.results); |
| 316 | |
| 317 | scope.resultCount = content.resultCount; |
| 318 | |
| 319 | scope.pagers.find('.totalResults').text(scope.resultCount); |
| 320 | |
| 321 | scope.page = index; |
| 322 | |
| 323 | scope.getResults(index); //Keep calling this until we have enough data. |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 324 | |
| 325 | }, |
| 326 | function(interest){ //Timeout |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 327 | console.error("Failed to retrieve results: timeout", interest); |
| 328 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 329 | } |
| 330 | ); |
| 331 | |
| 332 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 333 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 334 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 335 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 336 | var queryPrefix = new Name(prefix); |
| 337 | queryPrefix.append("query"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 338 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 339 | var jsonString = JSON.stringify(parameters); |
| 340 | queryPrefix.append(jsonString); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 341 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 342 | var queryInterest = new Interest(queryPrefix); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 343 | queryInterest.setInterestLifetimeMilliseconds(4000); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 344 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 345 | this.face.expressInterest(queryInterest, callback, timeout); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 346 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 347 | } |
| 348 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 349 | /** |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 350 | * This function returns a map of all the categories active filters. |
| 351 | * @return {Object<string, string>} |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 352 | */ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 353 | Atmos.prototype.getFilters = function(){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 354 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 355 | var data = $(current).text().split(/:/); |
| 356 | prev[data[0]] = data[1]; |
| 357 | return prev; |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 358 | }, {}); //Collect a map<category, filter>. |
| 359 | //TODO Make the return value map<category, Array<filter>> |
| 360 | return filters; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 361 | } |
| 362 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 363 | /** |
| 364 | * Creates a closable alert for the user. |
| 365 | * |
| 366 | * @param {string} message |
| 367 | * @param {string} type - Override the alert type. |
| 368 | */ |
| 369 | Atmos.prototype.createAlert = function(message, type) { |
| 370 | |
| 371 | var alert = $('<div class="alert"><div>'); |
| 372 | alert.addClass(type?type:'alert-info'); |
| 373 | alert.text(message); |
| 374 | alert.append(Atmos.closeButton); |
| 375 | |
| 376 | this.alerts.append(alert); |
| 377 | } |
| 378 | Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
| 379 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 380 | return Atmos; |
| 381 | |
| 382 | })(); |
| 383 | |
| 384 | |