Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 1 | //Run when the document loads AND we have the config loaded. |
| 2 | (function(){ |
| 3 | "use strict"; |
| 4 | var config; |
| 5 | Promise.all([ |
| 6 | new Promise(function(resolve, reject){ |
| 7 | $.ajax('config.json').done(function(data){ |
| 8 | config = data; |
| 9 | resolve(); |
| 10 | }).fail(function(){ |
| 11 | console.error("Failed to get config."); |
| 12 | ga('send', 'event', 'error', '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(config); |
| 28 | }, function(){ |
| 29 | console.error("Failed to initialize!"); |
| 30 | ga('send', 'event', 'error', 'init'); |
| 31 | }); |
| 32 | })(); |
| 33 | |
| 34 | var Atmos = (function(){ |
| 35 | "use strict"; |
| 36 | |
| 37 | var closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
| 38 | |
| 39 | var guid = function(){ |
| 40 | var d = new Date().getTime(); |
| 41 | var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
| 42 | var r = (d + Math.random()*16)%16 | 0; |
| 43 | d = Math.floor(d/16); |
| 44 | return (c=='x' ? r : (r&0x3|0x8)).toString(16); |
| 45 | }); |
| 46 | return uuid; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Atmos |
| 51 | * @version 2.0 |
| 52 | * |
| 53 | * Configures an Atmos object. This manages the atmos interface. |
| 54 | * |
| 55 | * @constructor |
| 56 | * @param {string} catalog - NDN path |
| 57 | * @param {Object} config - Object of configuration options for a Face. |
| 58 | */ |
| 59 | var Atmos = function(config){ |
| 60 | |
| 61 | //Internal variables. |
| 62 | this.results = []; |
| 63 | this.resultCount = Infinity; |
| 64 | this.name = null; |
| 65 | this.page = 0; |
| 66 | this.resultsPerPage = 25; |
| 67 | this.retrievedSegments = 0; |
| 68 | |
| 69 | //Config/init |
| 70 | this.config = config; |
| 71 | |
| 72 | this.catalog = config['global']['catalogPrefix']; |
| 73 | |
| 74 | this.face = new Face(config['global']['faceConfig']); |
| 75 | |
| 76 | //Easy access dom variables |
| 77 | this.categories = $('#side-menu'); |
| 78 | this.resultTable = $('#resultTable'); |
| 79 | this.filters = $('#filters'); |
| 80 | this.searchInput = $('#search'); |
| 81 | this.searchBar = $('#searchBar'); |
| 82 | this.searchButton = $('#searchButton'); |
| 83 | this.resultMenu = $('.resultMenu'); |
| 84 | this.alerts = $('#alerts'); |
| 85 | this.requestForm = $('#requestForm'); |
| 86 | |
| 87 | var scope = this; |
| 88 | |
| 89 | $('.requestSelectedButton').click(function(){ |
| 90 | ga('send', 'event', 'button', 'click', 'request'); |
| 91 | scope.request(scope.resultTable.find('.resultSelector:checked:not([disabled])').parent().parent()); |
| 92 | }); |
| 93 | |
| 94 | this.filterSetup(); |
| 95 | |
| 96 | //Init autocomplete |
| 97 | this.searchInput.autoComplete(function(field, callback){ |
| 98 | ga('send', 'event', 'search', 'autocomplete'); |
Tyler Scott | 9445899 | 2015-09-24 14:16:28 -0700 | [diff] [blame] | 99 | scope.autoComplete(field, function(data){ |
| 100 | var list = data.next; |
| 101 | var last = data.lastComponent === true; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 102 | callback(list.map(function(element){ |
Tyler Scott | 9445899 | 2015-09-24 14:16:28 -0700 | [diff] [blame] | 103 | return field + element + (last?"/":""); //Don't add trailing slash for last component. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 104 | })); |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | //Handle search |
| 109 | this.searchBar.submit(function(e){ |
| 110 | ga('send', 'event', 'search', 'submit'); |
| 111 | e.preventDefault(); |
| 112 | if (scope.searchInput.val().length === 0){ |
| 113 | if (!scope.searchBar.hasClass('has-error')){ |
| 114 | scope.searchBar.addClass('has-error').append('<span class="help-block">Search path is required!</span>'); |
| 115 | } |
| 116 | return; |
| 117 | } else { |
| 118 | scope.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()}); |
| 119 | } |
| 120 | scope.pathSearch(); |
| 121 | }); |
| 122 | |
| 123 | this.searchButton.click(function(){ |
| 124 | console.log("Search Button Pressed"); |
| 125 | ga('send', 'event', 'button', 'click', 'search'); |
| 126 | scope.search(); |
| 127 | }); |
| 128 | |
| 129 | //Result navigation handlers |
| 130 | this.resultMenu.find('.next').click(function(){ |
| 131 | ga('send', 'event', 'button', 'click', 'next'); |
| 132 | if (!$(this).hasClass('disabled')){ |
| 133 | scope.getResults(scope.page + 1); |
| 134 | } |
| 135 | }); |
| 136 | this.resultMenu.find('.previous').click(function(){ |
| 137 | ga('send', 'event', 'button', 'click', 'previous'); |
| 138 | if (!$(this).hasClass('disabled')){ |
| 139 | scope.getResults(scope.page - 1); |
| 140 | } |
| 141 | }); |
Tyler Scott | e8dac70 | 2015-10-13 14:33:25 -0600 | [diff] [blame^] | 142 | this.resultMenu.find('.clearResults').click(function(){ |
| 143 | ga('send', 'event', 'button', 'click', 'resultClear'); |
| 144 | scope.clearResults(); |
| 145 | $('#results').fadeOut(function(){ |
| 146 | $(this).addClass('hidden'); |
| 147 | }); |
| 148 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 149 | |
| 150 | //Change the number of results per page handler |
| 151 | var rpps = $('.resultsPerPageSelector').click(function(){ |
| 152 | |
| 153 | var t = $(this); |
| 154 | |
| 155 | if (t.hasClass('active')){ |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | rpps.find('.active').removeClass('active'); |
| 160 | t.addClass('active'); |
| 161 | scope.resultsPerPage = Number(t.text()); |
| 162 | scope.getResults(0); //Force return to page 1; |
| 163 | |
| 164 | }); |
| 165 | |
| 166 | //Init tree search |
| 167 | $('#treeSearch div').treeExplorer(function(path, callback){ |
| 168 | console.log("Tree Explorer request", path); |
| 169 | ga('send', 'event', 'tree', 'request'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 170 | scope.autoComplete(path, function(data){ |
| 171 | var list = data.next; |
| 172 | var last = (data.lastComponent === true); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 173 | console.log("Autocomplete response", list); |
| 174 | callback(list.map(function(element){ |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 175 | return (path == "/"?"/":"") + element + (!last?"/":""); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 176 | })); |
| 177 | }) |
| 178 | }); |
| 179 | |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 180 | $('#treeSearch').on('click', '.treeSearch', function(){ |
| 181 | var t = $(this); |
| 182 | |
| 183 | scope.clearResults(); |
| 184 | |
| 185 | var path = t.parent().parent().attr('id'); |
| 186 | |
| 187 | console.log("Stringing tree search:", path); |
| 188 | |
| 189 | scope.query(scope.catalog, {'??': path}, |
| 190 | function(interest, data){ //Success |
| 191 | console.log("Tree search response", interest, data); |
| 192 | |
| 193 | scope.name = data.getContent().toString().replace(/[\n\0]+/g,''); |
| 194 | |
| 195 | scope.getResults(0); |
| 196 | }, |
| 197 | function(interest){ //Failure |
| 198 | console.warn("Request failed! Timeout", interest); |
| 199 | scope.createAlert("Request timed out.\""+ interest.getName().toUri() + "\" See console for details."); |
| 200 | }); |
| 201 | |
| 202 | }); |
| 203 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 204 | this.setupRequestForm(); |
| 205 | |
| 206 | } |
| 207 | |
| 208 | Atmos.prototype.clearResults = function(){ |
| 209 | this.results = []; //Drop any old results. |
| 210 | this.retrievedSegments = 0; |
| 211 | this.resultCount = Infinity; |
| 212 | this.page = 0; |
| 213 | this.resultTable.empty(); |
| 214 | } |
| 215 | |
| 216 | Atmos.prototype.pathSearch = function(){ |
| 217 | var value = this.searchInput.val(); |
| 218 | |
| 219 | this.clearResults(); |
| 220 | |
| 221 | var scope = this; |
| 222 | |
| 223 | this.query(this.catalog, {"??": value}, |
| 224 | function(interest, data){ |
| 225 | console.log("Query response:", interest, data); |
| 226 | |
| 227 | scope.name = data.getContent().toString().replace(/[\n\0]/g,""); |
| 228 | |
| 229 | scope.getResults(0); |
| 230 | |
| 231 | }, |
| 232 | function(interest){ |
| 233 | console.warn("Request failed! Timeout", interest); |
| 234 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 235 | }); |
| 236 | |
| 237 | } |
| 238 | |
| 239 | Atmos.prototype.search = function(){ |
| 240 | |
| 241 | var filters = this.getFilters(); |
| 242 | |
| 243 | console.log("Search started!", this.searchInput.val(), filters); |
| 244 | |
| 245 | console.log("Initiating query"); |
| 246 | |
| 247 | this.clearResults(); |
| 248 | |
| 249 | var scope = this; |
| 250 | |
| 251 | this.query(this.catalog, filters, |
| 252 | function(interest, data){ //Response function |
| 253 | console.log("Query Response:", interest, data); |
| 254 | |
| 255 | scope.name = data.getContent().toString().replace(/[\n\0]/g,""); |
| 256 | |
| 257 | scope.getResults(0); |
| 258 | |
| 259 | }, function(interest){ //Timeout function |
| 260 | console.warn("Request failed! Timeout"); |
| 261 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 262 | }); |
| 263 | |
| 264 | } |
| 265 | |
| 266 | Atmos.prototype.autoComplete = function(field, callback){ |
| 267 | |
| 268 | var scope = this; |
| 269 | |
| 270 | this.query(this.catalog, {"?": field}, |
| 271 | function(interest, data){ |
| 272 | |
| 273 | var name = new Name(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 274 | |
| 275 | var interest = new Interest(name); |
| 276 | interest.setInterestLifetimeMilliseconds(5000); |
| 277 | interest.setMustBeFresh(true); |
| 278 | |
| 279 | scope.face.expressInterest(interest, |
| 280 | function(interest, data){ |
| 281 | |
| 282 | if (data.getContent().length !== 0){ |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 283 | callback(JSON.parse(data.getContent().toString().replace(/[\n\0]/g, ""))); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 284 | } else { |
| 285 | callback([]); |
| 286 | } |
| 287 | |
| 288 | }, function(interest){ |
| 289 | console.warn("Interest timed out!", interest); |
| 290 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 291 | }); |
| 292 | |
| 293 | }, function(interest){ |
| 294 | console.error("Request failed! Timeout", interest); |
| 295 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 296 | }); |
| 297 | |
| 298 | } |
| 299 | |
| 300 | Atmos.prototype.showResults = function(resultIndex) { |
| 301 | |
| 302 | var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1)); |
| 303 | |
| 304 | var resultDOM = $( |
| 305 | results.reduce(function(prev, current){ |
| 306 | prev.push('<tr><td><input class="resultSelector" type="checkbox"></td><td>'); |
| 307 | prev.push(current); |
| 308 | prev.push('</td></tr>'); |
| 309 | return prev; |
| 310 | }, ['<tr><th><input id="resultSelectAll" type="checkbox" title="Select All"> Select</th><th>Name</th></tr>']).join('') |
| 311 | ); |
| 312 | |
| 313 | resultDOM.find('#resultSelectAll').click(function(){ |
| 314 | if ($(this).is(':checked')){ |
| 315 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', true); |
| 316 | } else { |
| 317 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', false); |
| 318 | } |
| 319 | }); |
| 320 | |
| 321 | this.resultTable.hide().empty().append(resultDOM).slideDown('slow'); |
| 322 | |
| 323 | this.resultMenu.find('.pageNumber').text(resultIndex + 1); |
| 324 | this.resultMenu.find('.pageLength').text(this.resultsPerPage * resultIndex + results.length); |
| 325 | |
| 326 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
| 327 | this.resultMenu.find('.next').addClass('disabled'); |
| 328 | } else if (resultIndex === 0){ |
| 329 | this.resultMenu.find('.next').removeClass('disabled'); |
| 330 | } |
| 331 | |
| 332 | if (resultIndex === 0){ |
| 333 | this.resultMenu.find('.previous').addClass('disabled'); |
| 334 | } else if (resultIndex === 1) { |
| 335 | this.resultMenu.find('.previous').removeClass('disabled'); |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | |
| 340 | Atmos.prototype.getResults = function(index){ |
| 341 | |
| 342 | if ($('#results').hasClass('hidden')){ |
| 343 | $('#results').removeClass('hidden').slideDown(); |
| 344 | } |
| 345 | |
Tyler Scott | e8dac70 | 2015-10-13 14:33:25 -0600 | [diff] [blame^] | 346 | $.scrollTo("#results", 500, {interrupt: true}); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 347 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 348 | if ((this.results.length === this.resultCount) || (this.resultsPerPage * (index + 1) < this.results.length)){ |
| 349 | //console.log("We already have index", index); |
| 350 | this.page = index; |
| 351 | this.showResults(index); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | if (this.name === null) { |
| 356 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 357 | throw new Error("Illegal State"); |
| 358 | } |
| 359 | |
| 360 | var first = new Name(this.name).appendSegment(this.retrievedSegments++); |
| 361 | |
| 362 | console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri()); |
| 363 | |
| 364 | var scope = this; |
| 365 | |
| 366 | var interest = new Interest(first) |
| 367 | interest.setInterestLifetimeMilliseconds(5000); |
| 368 | interest.setMustBeFresh(true); |
| 369 | |
| 370 | this.face.expressInterest(interest, |
| 371 | function(interest, data){ //Response |
| 372 | |
| 373 | if (data.getContent().length === 0){ |
| 374 | scope.resultMenu.find('.totalResults').text(0); |
| 375 | scope.resultMenu.find('.pageNumber').text(0); |
| 376 | scope.resultMenu.find('.pageLength').text(0); |
| 377 | console.log("Empty response."); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 382 | |
| 383 | if (!content.results){ |
| 384 | scope.resultMenu.find('.totalResults').text(0); |
| 385 | scope.resultMenu.find('.pageNumber').text(0); |
| 386 | scope.resultMenu.find('.pageLength').text(0); |
| 387 | console.log("No results were found!"); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | scope.results = scope.results.concat(content.results); |
| 392 | |
| 393 | scope.resultCount = content.resultCount; |
| 394 | |
| 395 | scope.resultMenu.find('.totalResults').text(scope.resultCount); |
| 396 | |
| 397 | scope.page = index; |
| 398 | |
| 399 | scope.getResults(index); //Keep calling this until we have enough data. |
| 400 | |
| 401 | }, |
| 402 | function(interest){ //Timeout |
| 403 | console.error("Failed to retrieve results: timeout", interest); |
| 404 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 405 | } |
| 406 | ); |
| 407 | |
| 408 | } |
| 409 | |
| 410 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
| 411 | |
| 412 | var queryPrefix = new Name(prefix); |
| 413 | queryPrefix.append("query"); |
| 414 | |
| 415 | var jsonString = JSON.stringify(parameters); |
| 416 | queryPrefix.append(jsonString); |
| 417 | |
| 418 | var queryInterest = new Interest(queryPrefix); |
| 419 | queryInterest.setInterestLifetimeMilliseconds(4000); |
| 420 | queryInterest.setMustBeFresh(true); |
| 421 | |
| 422 | this.face.expressInterest(queryInterest, callback, timeout); |
| 423 | |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * This function returns a map of all the categories active filters. |
| 428 | * @return {Object<string, string>} |
| 429 | */ |
| 430 | Atmos.prototype.getFilters = function(){ |
| 431 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
| 432 | var data = $(current).text().split(/:/); |
| 433 | prev[data[0]] = data[1]; |
| 434 | return prev; |
| 435 | }, {}); //Collect a map<category, filter>. |
| 436 | //TODO Make the return value map<category, Array<filter>> |
| 437 | return filters; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Creates a closable alert for the user. |
| 442 | * |
| 443 | * @param {string} message |
| 444 | * @param {string} type - Override the alert type. |
| 445 | */ |
| 446 | Atmos.prototype.createAlert = function(message, type) { |
| 447 | |
| 448 | var alert = $('<div class="alert"><div>'); |
| 449 | alert.addClass(type?type:'alert-info'); |
| 450 | alert.text(message); |
| 451 | alert.append(closeButton); |
| 452 | |
| 453 | this.alerts.append(alert); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Requests all of the names represented by the buttons in the elements list. |
| 458 | * |
| 459 | * @param elements {Array<jQuery>} A list of the table row elements |
| 460 | */ |
| 461 | Atmos.prototype.request = function(){ |
| 462 | |
| 463 | //Pseudo globals. |
| 464 | var keyChain; |
| 465 | var certificateName; |
| 466 | var keyAdded = false; |
| 467 | |
| 468 | return function(elements){ |
| 469 | |
| 470 | var names = []; |
| 471 | var destination = $('#requestDest .active').text(); |
| 472 | $(elements).find('>*:nth-child(2)').each(function(){ |
| 473 | var name = $(this).text(); |
| 474 | names.push(name); |
| 475 | });//.append('<span class="badge">Requested!</span>') |
| 476 | //Disabling the checkbox doesn't make sense anymore with the ability to request to multiple destinations. |
| 477 | //$(elements).find('.resultSelector').prop('disabled', true).prop('checked', false); |
| 478 | |
| 479 | var scope = this; |
| 480 | this.requestForm.on('submit', function(e){ //This will be registered for the next submit from the form. |
| 481 | e.preventDefault(); |
| 482 | |
| 483 | //Form checking |
| 484 | var dest = scope.requestForm.find('#requestDest .active'); |
| 485 | if (dest.length !== 1){ |
| 486 | $('#requestForm').append($('<div class="alert alert-warning">A destination is required!' + closeButton + '<div>')); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | $('#request').modal('hide')//Initial params are ok. We can close the form. |
| 491 | .remove('.alert') //Remove any alerts |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 492 | |
| 493 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 494 | |
| 495 | $(this).off(e); //Don't fire this again, the request must be regenerated |
| 496 | |
| 497 | //Key setup |
| 498 | if (!keyAdded){ |
| 499 | if (!scope.config.retrieval.demoKey || !scope.config.retrieval.demoKey.pub || !scope.config.retrieval.demoKey.priv){ |
| 500 | scope.createAlert("This host was not configured to handle retrieval! See console for details.", 'alert-danger'); |
| 501 | console.error("Missing/invalid key! This must be configured in the config on the server.", scope.config.demoKey); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | //FIXME base64 may or may not exist in other browsers. Need a new polyfill. |
| 506 | var pub = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.pub)); //MUST be a Buffer (Buffer != Uint8Array) |
| 507 | var priv = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.priv)); |
| 508 | |
| 509 | var identityStorage = new MemoryIdentityStorage(); |
| 510 | var privateKeyStorage = new MemoryPrivateKeyStorage(); |
| 511 | keyChain = new KeyChain(new IdentityManager(identityStorage, privateKeyStorage), |
| 512 | new SelfVerifyPolicyManager(identityStorage)); |
| 513 | |
| 514 | var keyName = new Name("/retrieve/DSK-123"); |
| 515 | certificateName = keyName.getSubName(0, keyName.size() - 1) |
| 516 | .append("KEY").append(keyName.get(-1)) |
| 517 | .append("ID-CERT").append("0"); |
| 518 | |
| 519 | identityStorage.addKey(keyName, KeyType.RSA, new Blob(pub, false)); |
| 520 | privateKeyStorage.setKeyPairForKeyName(keyName, KeyType.RSA, pub, priv); |
| 521 | |
| 522 | scope.face.setCommandSigningInfo(keyChain, certificateName); |
| 523 | |
| 524 | keyAdded = true; |
| 525 | |
| 526 | } |
| 527 | |
| 528 | //Retrieval |
| 529 | var retrievePrefix = new Name("/catalog/ui/" + guid()); |
| 530 | |
| 531 | //Due to a lack of success callback in the register prefix function, we have to pretend we |
| 532 | //know it succeeded with an arbitrary timeout. |
| 533 | var sendTimer = setTimeout(function(){ |
| 534 | var prefix = new Name(dest.text()); |
| 535 | prefix.append(retrievePrefix); |
| 536 | var interest = new Interest(prefix); |
| 537 | interest.setInterestLifetimeMilliseconds(3000); |
| 538 | scope.face.expressInterest(interest, |
| 539 | function(interest, data){ //Success |
| 540 | console.log("Request for", prefix.toUri(), "succeeded.", interest, data); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 541 | scope.createAlert("Data retrieval has initiated.", "alert-success"); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 542 | }, function(interest){ //Failure |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 543 | console.error("Request for", prefix.toUri(), "timed out.", interest); |
| 544 | scope.createAlert("Request for " + prefix.toUri() + " timed out. This means that the retrieve failed! See console for more details."); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 545 | } |
| 546 | ); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 547 | }, 5000); //Wait 5 seconds |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 548 | |
| 549 | scope.face.registerPrefix(retrievePrefix, |
| 550 | function(prefix, interest, face, interestFilterId, filter){ //On Interest |
| 551 | //This function will exist until the page exits but will likely only be used once. |
| 552 | |
| 553 | var data = new Data(interest.getName()); |
| 554 | var content = JSON.stringify(names); |
| 555 | data.setContent(content); |
| 556 | keyChain.sign(data, certificateName); |
| 557 | |
| 558 | try { |
| 559 | face.putData(data); |
| 560 | console.log("Responded for", interest.getName().toUri(), data); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 561 | } catch (e) { |
| 562 | console.error("Failed to respond to", interest.getName().toUri(), data); |
| 563 | scope.createAlert("Data retrieval failed."); |
| 564 | } |
| 565 | |
| 566 | }, function(prefix){ //On fail |
| 567 | clearTimeout(sendTimer); //Cancel the earlier request timer |
| 568 | scope.createAlert("Failed to register the retrieval URI! See console for details.", "alert-danger"); |
| 569 | console.error("Failed to register URI:", prefix.toUri(), prefix); |
| 570 | |
| 571 | } |
| 572 | ); |
| 573 | |
| 574 | }); |
| 575 | $('#request').modal(); //This forces the form to be the only option. |
| 576 | |
| 577 | } |
| 578 | }(); |
| 579 | |
| 580 | Atmos.prototype.filterSetup = function() { |
| 581 | //Filter setup |
| 582 | |
| 583 | var prefix = new Name(this.catalog).append("filters-initialization"); |
| 584 | |
| 585 | var scope = this; |
| 586 | |
| 587 | this.getAll(prefix, function(data) { //Success |
| 588 | var raw = JSON.parse(data.replace(/[\n\0]/g, '')); //Remove null byte and parse |
| 589 | |
| 590 | console.log("Filter categories:", raw); |
| 591 | |
| 592 | $.each(raw, function(index, object){ //Unpack list of objects |
| 593 | $.each(object, function(category, searchOptions) { //Unpack category from object (We don't know what it is called) |
| 594 | //Create the category |
| 595 | var e = $('<li><a href="#">' + category.replace(/_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>'); |
| 596 | |
| 597 | var sub = e.find('ul.subnav'); |
| 598 | $.each(searchOptions, function(index, name){ |
| 599 | //Create the filter list inside the category |
| 600 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 601 | sub.append(item); |
| 602 | item.click(function(){ //Click on the side menu filters |
| 603 | if (item.hasClass('active')){ //Does the filter already exist? |
| 604 | item.removeClass('active'); |
| 605 | scope.filters.find(':contains(' + category + ':' + name + ')').remove(); |
| 606 | } else { //Add a filter |
| 607 | item.addClass('active'); |
| 608 | var filter = $('<span class="label label-default"></span>'); |
| 609 | filter.text(category + ':' + name); |
| 610 | |
| 611 | scope.filters.append(filter); |
| 612 | |
| 613 | filter.click(function(){ //Click on a filter |
| 614 | filter.remove(); |
| 615 | item.removeClass('active'); |
| 616 | }); |
| 617 | } |
| 618 | |
| 619 | }); |
| 620 | }); |
| 621 | |
| 622 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
| 623 | e.find('> a').click(function(){ |
| 624 | scope.categories.find('.subnav').slideUp(); |
| 625 | var t = $(this).siblings('.subnav'); |
| 626 | if ( !t.is(':visible') ){ //If the sub menu is not visible |
| 627 | t.slideDown(function(){ |
| 628 | t.triggerHandler('focus'); |
| 629 | }); //Make it visible and look at it. |
| 630 | } |
| 631 | }); |
| 632 | |
| 633 | scope.categories.append(e); |
| 634 | |
| 635 | }); |
| 636 | }); |
| 637 | |
| 638 | }, function(interest){ //Timeout |
| 639 | scope.createAlert("Failed to initialize the filters!", "alert-danger"); |
| 640 | console.error("Failed to initialize filters!", interest); |
| 641 | ga('send', 'event', 'error', 'filters'); |
| 642 | }); |
| 643 | |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * This function retrieves all segments in order until it knows it has reached the last one. |
| 648 | * It then returns the final joined result. |
| 649 | */ |
| 650 | Atmos.prototype.getAll = function(prefix, callback, timeout){ |
| 651 | |
| 652 | var scope = this; |
| 653 | var d = []; |
| 654 | |
| 655 | var request = function(segment){ |
| 656 | |
| 657 | var name = new Name(prefix); |
| 658 | name.appendSegment(segment); |
| 659 | |
| 660 | var interest = new Interest(name); |
| 661 | interest.setInterestLifetimeMilliseconds(1000); |
| 662 | interest.setMustBeFresh(true); //Is this needed? |
| 663 | |
| 664 | scope.face.expressInterest(interest, handleData, timeout); |
| 665 | |
| 666 | } |
| 667 | |
| 668 | |
| 669 | var handleData = function(interest, data){ |
| 670 | |
| 671 | d.push(data.getContent().toString()); |
| 672 | |
| 673 | if (interest.getName().get(-1).toSegment() == data.getMetaInfo().getFinalBlockId().toSegment()){ |
| 674 | callback(d.join("")); |
| 675 | } else { |
| 676 | request(interest.getName().toSegment()++); |
| 677 | } |
| 678 | |
| 679 | } |
| 680 | |
| 681 | request(0); |
| 682 | |
| 683 | } |
| 684 | |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 685 | Atmos.prototype.cleanRequestForm = function(){ |
| 686 | $('#requestDest').prev().removeClass('btn-success').addClass('btn-default'); |
| 687 | $('#requestDropText').text('Destination'); |
| 688 | $('#requestDest .active').removeClass('active'); |
| 689 | } |
| 690 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 691 | Atmos.prototype.setupRequestForm = function(){ |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 692 | |
| 693 | var scope = this; |
| 694 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 695 | this.requestForm.find('#requestCancel').click(function(){ |
| 696 | $('#request').unbind('submit') //Removes all event handlers. |
| 697 | .modal('hide'); //Hides the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 698 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 699 | }); |
| 700 | |
| 701 | var dests = $(this.config['retrieval']['destinations'].reduce(function(prev, current){ |
| 702 | prev.push('<li><a href="#">'); |
| 703 | prev.push(current); |
| 704 | prev.push("</a></li>"); |
| 705 | return prev; |
| 706 | }, []).join("")); |
| 707 | |
| 708 | this.requestForm.find('#requestDest').append(dests) |
| 709 | .on('click', 'a', function(e){ |
| 710 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 711 | var t = $(this); |
| 712 | t.parent().addClass('active'); |
| 713 | $('#requestDropText').text(t.text()); |
| 714 | $('#requestDest').prev().removeClass('btn-default').addClass('btn-success'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 715 | }); |
| 716 | |
| 717 | //This code will remain unused until users must use their own keys instead of the demo key. |
| 718 | // var scope = this; |
| 719 | |
| 720 | // var warning = '<div class="alert alert-warning">' + closeButton + '<div>'; |
| 721 | |
| 722 | // var handleFile = function(e){ |
| 723 | // var t = $(this); |
| 724 | // if (e.target.files.length > 1){ |
| 725 | // var el = $(warning); |
| 726 | // t.append(el.append("We are looking for a single file, we will try the first only!")); |
| 727 | // } else if (e.target.files.length === 0) { |
| 728 | // var el = $(warning.replace("alert-warning", "alert-danger")); |
| 729 | // t.append(el.append("No file was supplied!")); |
| 730 | // return; |
| 731 | // } |
| 732 | |
| 733 | // var reader = new FileReader(); |
| 734 | // reader.onload = function(e){ |
| 735 | // var key; |
| 736 | // try { |
| 737 | // key = JSON.parse(e.target.result); |
| 738 | // } catch (e) { |
| 739 | // console.error("Could not parse the key! (", key, ")"); |
| 740 | // var el = $(warning.replace("alert-warning", "alert-danger")); |
| 741 | // t.append(el.append("Failed to parse the key file, is it a valid json key?")); |
| 742 | // } |
| 743 | |
| 744 | // if (!key.DEFAULT_RSA_PUBLIC_KEY_DER || !key.DEFAULT_RSA_PRIVATE_KEY_DER) { |
| 745 | // console.warn("Invalid key", key); |
| 746 | // var el = $(warning.replace("alert-warning", "alert-danger")); |
| 747 | // t.append(el.append("Failed to parse the key file, it is missing required attributes.")); |
| 748 | // } |
| 749 | |
| 750 | |
| 751 | // }; |
| 752 | |
| 753 | // } |
| 754 | |
| 755 | // this.requestForm.find('#requestDrop').on('dragover', function(e){ |
| 756 | // e.dataTransfer.dropEffect = 'copy'; |
| 757 | // }).on('drop', handleFile); |
| 758 | |
| 759 | // this.requestForm.find('input[type=file]').change(handleFile); |
| 760 | |
| 761 | } |
| 762 | |
| 763 | return Atmos; |
| 764 | |
| 765 | })(); |