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