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'); |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 65 | this.resultMenu = $('.resultMenu'); |
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(){ |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 71 | scope.request(this); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 72 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 73 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 74 | $('.requestSelectedButton').click(function(){ |
| 75 | scope.request( |
| 76 | scope.resultTable.find('.resultSelector:checked:not([disabled])') |
| 77 | .parent().next().find('.interest-button') |
| 78 | ); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 79 | }); |
| 80 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 81 | this.filterSetup(); |
| 82 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 83 | this.searchInput.autoComplete(function(field, callback){ |
| 84 | scope.autoComplete(field, callback); |
| 85 | }); |
| 86 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 87 | this.searchBar.submit(function(e){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 88 | e.preventDefault(); |
Tyler Scott | 6b81081 | 2015-08-11 18:20:05 -0600 | [diff] [blame] | 89 | scope.pathSearch(); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 90 | }); |
| 91 | |
| 92 | this.searchButton.click(function(){ |
| 93 | console.log("Search Button Pressed"); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 94 | scope.search(); |
| 95 | }); |
| 96 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 97 | this.resultMenu.find('.next').click(function(){ |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 98 | if (!$(this).hasClass('disabled')){ |
| 99 | scope.getResults(scope.page + 1); |
| 100 | } |
| 101 | }); |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 102 | this.resultMenu.find('.previous').click(function(){ |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 103 | if (!$(this).hasClass('disabled')){ |
| 104 | scope.getResults(scope.page - 1); |
| 105 | } |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 106 | }); |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 107 | |
| 108 | var rpps = $('.resultsPerPageSelector').click(function(){ |
| 109 | |
| 110 | var t = $(this); |
| 111 | |
| 112 | if (t.hasClass('active')){ |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | rpps.find('.active').removeClass('active'); |
| 117 | t.addClass('active'); |
| 118 | scope.resultsPerPage = Number(t.text()); |
| 119 | scope.getResults(0); //Force return to page 1; |
| 120 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 121 | }); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | |
Tyler Scott | 6b81081 | 2015-08-11 18:20:05 -0600 | [diff] [blame] | 125 | Atmos.prototype.clearResults = function(){ |
| 126 | this.results = []; //Drop any old results. |
| 127 | this.retrievedSegments = 0; |
| 128 | this.resultCount = Infinity; |
| 129 | this.page = 0; |
| 130 | this.resultTable.empty(); |
| 131 | } |
| 132 | |
| 133 | Atmos.prototype.pathSearch = function(){ |
| 134 | var value = this.searchInput.val(); |
| 135 | |
| 136 | this.clearResults(); |
| 137 | |
| 138 | var scope = this; |
| 139 | |
| 140 | this.query(this.catalog, {"??": value}, |
| 141 | function(interest, data){ |
| 142 | console.log("Query response:", interest, data); |
| 143 | |
| 144 | var parameters = JSON.stringify({"??": value}); |
| 145 | |
| 146 | var ack = data.getName(); |
| 147 | |
| 148 | scope.name = new Name(scope.catalog).append("query-results").append(parameters).append(ack.get(-3)).append(ack.get(-2)); |
| 149 | |
| 150 | scope.getResults(0); |
| 151 | |
| 152 | }, |
| 153 | function(interest){ |
| 154 | console.warn("Request failed! Timeout", interest); |
| 155 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 156 | }); |
| 157 | |
| 158 | } |
| 159 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 160 | Atmos.prototype.search = function(){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 161 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 162 | var filters = this.getFilters(); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 163 | |
| 164 | console.log("Search started!", this.searchInput.val(), filters); |
| 165 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 166 | console.log("Initiating query"); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 167 | |
Tyler Scott | 6b81081 | 2015-08-11 18:20:05 -0600 | [diff] [blame] | 168 | this.clearResults(); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 169 | |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 170 | var scope = this; |
| 171 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 172 | this.query(this.catalog, filters, |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 173 | function(interest, data){ //Response function |
| 174 | console.log("Query Response:", interest, data); |
| 175 | |
Tyler Scott | d90f84e | 2015-07-27 12:43:04 -0600 | [diff] [blame] | 176 | var parameters = JSON.stringify(filters); |
| 177 | |
| 178 | var ack = data.getName(); |
| 179 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 180 | 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] | 181 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 182 | scope.getResults(0); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 183 | |
| 184 | }, function(interest){ //Timeout function |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 185 | console.warn("Request failed! Timeout"); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 186 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 187 | }); |
| 188 | |
| 189 | } |
| 190 | |
| 191 | Atmos.prototype.autoComplete = function(field, callback){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 192 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 193 | if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){ |
| 194 | if (!this.searchBar.hasClass('has-error')){ |
| 195 | this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>'); |
| 196 | } |
| 197 | return; |
| 198 | } else { |
| 199 | this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()}); |
| 200 | } |
| 201 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 202 | var scope = this; |
| 203 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 204 | this.query(this.catalog, {"?": field}, |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 205 | function(interest, data){ |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 206 | |
| 207 | var ack = data.getName(); |
| 208 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 209 | 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] | 210 | |
Tyler Scott | ab822fe | 2015-08-10 14:38:49 -0600 | [diff] [blame] | 211 | var interest = new Interest(name); |
| 212 | interest.setInterestLifetimeMilliseconds(5000); |
| 213 | interest.setMustBeFresh(true); |
| 214 | |
| 215 | scope.face.expressInterest(interest, |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 216 | function(interest, data){ |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 217 | |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 218 | if (data.getContent().length !== 0){ |
| 219 | 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] | 220 | return field + element + "/"; |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 221 | }); |
| 222 | callback(options); |
| 223 | } |
| 224 | |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 225 | }, function(interest){ |
| 226 | console.warn("Interest timed out!", interest); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 227 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 228 | }); |
| 229 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 230 | }, function(interest){ |
Tyler Scott | b6e329f | 2015-07-28 16:08:33 -0600 | [diff] [blame] | 231 | console.error("Request failed! Timeout", interest); |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 232 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 233 | }); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 234 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 235 | } |
| 236 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 237 | Atmos.prototype.showResults = function(resultIndex) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 238 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 239 | if ($('#results').hasClass('hidden')){ |
| 240 | $('#results').removeClass('hidden').slideDown(); |
| 241 | } |
| 242 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 243 | var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1)); |
| 244 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 245 | var resultDOM = $( |
| 246 | results.reduce(function(prev, current){ |
| 247 | prev.push('<tr><td><input class="resultSelector" type="checkbox"></td><td>'); |
| 248 | prev.push(current); |
| 249 | prev.push('</td></tr>'); |
| 250 | return prev; |
| 251 | }, ['<tr><th><input id="resultSelectAll" type="checkbox" title="Select All"> Select</th><th>Name</th></tr>']).join('') |
| 252 | ); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 253 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 254 | resultDOM.find('#resultSelectAll').click(function(){ |
| 255 | if ($(this).is(':checked')){ |
| 256 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', true); |
| 257 | } else { |
| 258 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', false); |
| 259 | } |
| 260 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 261 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 262 | this.resultTable.empty().append(resultDOM).slideDown(); |
| 263 | if (this.resultMenu.hasClass('hidden')){ |
| 264 | this.resultMenu.removeClass('hidden').slideDown(); |
| 265 | } |
| 266 | |
| 267 | this.resultMenu.find('.pageNumber').text(resultIndex + 1); |
| 268 | this.resultMenu.find('.pageLength').text(this.resultsPerPage * (resultIndex + 1)); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 269 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 270 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 271 | this.resultMenu.find('.next').addClass('disabled'); |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 272 | } else if (resultIndex === 0){ |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 273 | this.resultMenu.find('.next').removeClass('disabled'); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 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 | if (resultIndex === 0){ |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 277 | this.resultMenu.find('.previous').addClass('disabled'); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 278 | } else if (resultIndex === 1) { |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 279 | this.resultMenu.find('.previous').removeClass('disabled'); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 280 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 281 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 282 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 283 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 284 | Atmos.prototype.getResults = function(index){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 285 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 286 | 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] | 287 | //console.log("We already have index", index); |
| 288 | this.page = index; |
| 289 | this.showResults(index); |
| 290 | return; |
| 291 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 292 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 293 | if (this.name === null) { |
| 294 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 295 | throw new Error("Illegal State"); |
| 296 | } |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 297 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 298 | var first = new Name(this.name).appendSegment(this.retrievedSegments++); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 299 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 300 | console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri()); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 301 | |
| 302 | var scope = this; |
| 303 | |
Tyler Scott | ab822fe | 2015-08-10 14:38:49 -0600 | [diff] [blame] | 304 | var interest = new Interest(first) |
| 305 | interest.setInterestLifetimeMilliseconds(5000); |
| 306 | interest.setMustBeFresh(true); |
| 307 | |
| 308 | this.face.expressInterest(interest, |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 309 | function(interest, data){ //Response |
| 310 | |
| 311 | if (data.getContent().length === 0){ |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 312 | scope.resultMenu.find('.totalResults').text(0); |
| 313 | scope.resultMenu.find('.pageNumber').text(0); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 314 | console.log("Empty response."); |
| 315 | return; |
| 316 | } |
| 317 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 318 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 319 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 320 | if (!content.results){ |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 321 | scope.resultMenu.find('.totalResults').text(0); |
| 322 | scope.resultMenu.find('.pageNumber').text(0); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 323 | console.log("No results were found!"); |
| 324 | return; |
| 325 | } |
| 326 | |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 327 | scope.results = scope.results.concat(content.results); |
| 328 | |
| 329 | scope.resultCount = content.resultCount; |
| 330 | |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 331 | scope.resultMenu.find('.totalResults').text(scope.resultCount); |
Tyler Scott | 4e4865a | 2015-08-06 14:23:21 -0600 | [diff] [blame] | 332 | |
| 333 | scope.page = index; |
| 334 | |
| 335 | scope.getResults(index); //Keep calling this until we have enough data. |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 336 | |
| 337 | }, |
| 338 | function(interest){ //Timeout |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 339 | console.error("Failed to retrieve results: timeout", interest); |
| 340 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 341 | } |
| 342 | ); |
| 343 | |
| 344 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 345 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 346 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 347 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 348 | var queryPrefix = new Name(prefix); |
| 349 | queryPrefix.append("query"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 350 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 351 | var jsonString = JSON.stringify(parameters); |
| 352 | queryPrefix.append(jsonString); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 353 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 354 | var queryInterest = new Interest(queryPrefix); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 355 | queryInterest.setInterestLifetimeMilliseconds(4000); |
Tyler Scott | ab822fe | 2015-08-10 14:38:49 -0600 | [diff] [blame] | 356 | queryInterest.setMustBeFresh(true); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 357 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 358 | this.face.expressInterest(queryInterest, callback, timeout); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 359 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 360 | } |
| 361 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 362 | /** |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 363 | * This function returns a map of all the categories active filters. |
| 364 | * @return {Object<string, string>} |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 365 | */ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 366 | Atmos.prototype.getFilters = function(){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 367 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 368 | var data = $(current).text().split(/:/); |
| 369 | prev[data[0]] = data[1]; |
| 370 | return prev; |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame] | 371 | }, {}); //Collect a map<category, filter>. |
| 372 | //TODO Make the return value map<category, Array<filter>> |
| 373 | return filters; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 374 | } |
| 375 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 376 | /** |
| 377 | * Creates a closable alert for the user. |
| 378 | * |
| 379 | * @param {string} message |
| 380 | * @param {string} type - Override the alert type. |
| 381 | */ |
| 382 | Atmos.prototype.createAlert = function(message, type) { |
| 383 | |
| 384 | var alert = $('<div class="alert"><div>'); |
| 385 | alert.addClass(type?type:'alert-info'); |
| 386 | alert.text(message); |
| 387 | alert.append(Atmos.closeButton); |
| 388 | |
| 389 | this.alerts.append(alert); |
| 390 | } |
Tyler Scott | 3fc0527 | 2015-08-17 18:02:35 -0600 | [diff] [blame^] | 391 | |
| 392 | /** |
| 393 | * Requests all of the names represented by the buttons in the elements list. |
| 394 | * |
| 395 | * @param elements {Array<jQuery>} A list of the interestButton elements |
| 396 | */ |
| 397 | Atmos.prototype.request = function(elements){ |
| 398 | |
| 399 | var scope = this; |
| 400 | $(elements).filter(':not(.disabled)').each(function(){ |
| 401 | var button = $(this); |
| 402 | |
| 403 | if (button.hasClass('disabled')){ |
| 404 | console.warn("An attempt to request a disabled element has occured"); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | var name = button.text(); |
| 409 | var interest = new Interest(new Name('/retrieve' + name)); |
| 410 | scope.face.expressInterest(interest, function(){}, function(){}); |
| 411 | |
| 412 | }) |
| 413 | .append('<span class="badge">Requested!</span>') |
| 414 | .addClass('disabled') |
| 415 | .addClass('label-success') |
| 416 | .parent().prev().find('.resultSelector').prop('disabled', true).prop('checked', false); |
| 417 | |
| 418 | } |
| 419 | |
| 420 | Atmos.prototype.filterSetup = function() { |
| 421 | //Filter setup |
| 422 | |
| 423 | var prefix = new Name(this.catalog).append("filters-initialization"); |
| 424 | |
| 425 | var scope = this; |
| 426 | |
| 427 | this.getAll(prefix, function(data) { //Success |
| 428 | var raw = JSON.parse(data.replace(/[\n\0]/g, '')); //Remove null byte and parse |
| 429 | |
| 430 | console.log("Filter categories:", raw); |
| 431 | |
| 432 | $.each(raw, function(index, object){ //Unpack list of objects |
| 433 | $.each(object, function(category, searchOptions) { //Unpack category from object (We don't know what it is called) |
| 434 | //Create the category |
| 435 | var e = $('<li><a href="#">' + category.replace(/\_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>'); |
| 436 | |
| 437 | var sub = e.find('ul.subnav'); |
| 438 | $.each(searchOptions, function(index, name){ |
| 439 | //Create the filter list inside the category |
| 440 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 441 | sub.append(item); |
| 442 | item.click(function(){ //Click on the side menu filters |
| 443 | if (item.hasClass('active')){ //Does the filter already exist? |
| 444 | item.removeClass('active'); |
| 445 | scope.filters.find(':contains(' + category + ':' + name + ')').remove(); |
| 446 | } else { //Add a filter |
| 447 | item.addClass('active'); |
| 448 | var filter = $('<span class="label label-default"></span>'); |
| 449 | filter.text(category + ':' + name); |
| 450 | |
| 451 | scope.filters.append(filter); |
| 452 | |
| 453 | filter.click(function(){ //Click on a filter |
| 454 | filter.remove(); |
| 455 | item.removeClass('active'); |
| 456 | }); |
| 457 | } |
| 458 | |
| 459 | }); |
| 460 | }); |
| 461 | |
| 462 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
| 463 | e.find('> a').click(function(){ |
| 464 | scope.categories.find('.subnav').slideUp(); |
| 465 | var t = $(this).siblings('.subnav'); |
| 466 | if ( !t.is(':visible') ){ //If the sub menu is not visible |
| 467 | t.slideDown(function(){ |
| 468 | t.triggerHandler('focus'); |
| 469 | }); //Make it visible and look at it. |
| 470 | } |
| 471 | }); |
| 472 | |
| 473 | scope.categories.append(e); |
| 474 | |
| 475 | }); |
| 476 | }); |
| 477 | |
| 478 | }, function(interest){ //Timeout |
| 479 | scope.createAlert("Failed to initialize the filters!", "alert-danger"); |
| 480 | console.error("Failed to initialize filters!", interest); |
| 481 | }); |
| 482 | |
| 483 | } |
| 484 | |
| 485 | Atmos.prototype.getAll = function(prefix, callback, timeout){ |
| 486 | |
| 487 | var scope = this; |
| 488 | var d = []; |
| 489 | |
| 490 | var request = function(segment){ |
| 491 | |
| 492 | var name = new Name(prefix); |
| 493 | name.appendSegment(segment); |
| 494 | |
| 495 | var interest = new Interest(name); |
| 496 | interest.setInterestLifetimeMilliseconds(1000); |
| 497 | interest.setMustBeFresh(true); //Is this needed? |
| 498 | |
| 499 | scope.face.expressInterest(interest, handleData, timeout); |
| 500 | |
| 501 | } |
| 502 | |
| 503 | |
| 504 | var handleData = function(interest, data){ |
| 505 | |
| 506 | d.push(data.getContent().toString()); |
| 507 | |
| 508 | if (interest.getName().get(-1).toSegment() == data.getMetaInfo().getFinalBlockId().toSegment()){ |
| 509 | callback(d.join("")); |
| 510 | } else { |
| 511 | request(interest.getName().toSegment()++); |
| 512 | } |
| 513 | |
| 514 | } |
| 515 | |
| 516 | request(0); |
| 517 | |
| 518 | |
| 519 | } |
| 520 | |
Tyler Scott | 4d95122 | 2015-07-31 14:38:27 -0600 | [diff] [blame] | 521 | Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
| 522 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 523 | return Atmos; |
| 524 | |
| 525 | })(); |