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(){ |
Tyler Scott | 2118a41 | 2015-10-29 15:17:05 -0600 | [diff] [blame] | 27 | |
| 28 | var getParameterByName = function(name){ |
| 29 | name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); |
| 30 | var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), |
| 31 | results = regex.exec(location.search); |
| 32 | return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); |
| 33 | } |
| 34 | |
| 35 | //Overwrite config if present. Any failure will just cause this to be skipped. |
| 36 | try{ |
| 37 | var configParam = JSON.parse(getParameterByName('config')); |
| 38 | config = jQuery.extend(true, config, configParam); |
| 39 | } catch(e){ |
| 40 | console.warn("Failure in config overwrite, skipping.", e); |
| 41 | } |
| 42 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 43 | new Atmos(config); |
| 44 | }, function(){ |
| 45 | console.error("Failed to initialize!"); |
| 46 | ga('send', 'event', 'error', 'init'); |
| 47 | }); |
| 48 | })(); |
| 49 | |
| 50 | var Atmos = (function(){ |
| 51 | "use strict"; |
| 52 | |
| 53 | var closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'; |
| 54 | |
| 55 | var guid = function(){ |
| 56 | var d = new Date().getTime(); |
| 57 | var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
| 58 | var r = (d + Math.random()*16)%16 | 0; |
| 59 | d = Math.floor(d/16); |
| 60 | return (c=='x' ? r : (r&0x3|0x8)).toString(16); |
| 61 | }); |
| 62 | return uuid; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Atmos |
| 67 | * @version 2.0 |
| 68 | * |
| 69 | * Configures an Atmos object. This manages the atmos interface. |
| 70 | * |
| 71 | * @constructor |
| 72 | * @param {string} catalog - NDN path |
| 73 | * @param {Object} config - Object of configuration options for a Face. |
| 74 | */ |
| 75 | var Atmos = function(config){ |
| 76 | |
| 77 | //Internal variables. |
| 78 | this.results = []; |
| 79 | this.resultCount = Infinity; |
| 80 | this.name = null; |
| 81 | this.page = 0; |
| 82 | this.resultsPerPage = 25; |
| 83 | this.retrievedSegments = 0; |
| 84 | |
| 85 | //Config/init |
| 86 | this.config = config; |
| 87 | |
| 88 | this.catalog = config['global']['catalogPrefix']; |
| 89 | |
| 90 | this.face = new Face(config['global']['faceConfig']); |
| 91 | |
| 92 | //Easy access dom variables |
| 93 | this.categories = $('#side-menu'); |
| 94 | this.resultTable = $('#resultTable'); |
| 95 | this.filters = $('#filters'); |
| 96 | this.searchInput = $('#search'); |
| 97 | this.searchBar = $('#searchBar'); |
| 98 | this.searchButton = $('#searchButton'); |
| 99 | this.resultMenu = $('.resultMenu'); |
| 100 | this.alerts = $('#alerts'); |
| 101 | this.requestForm = $('#requestForm'); |
| 102 | |
| 103 | var scope = this; |
| 104 | |
| 105 | $('.requestSelectedButton').click(function(){ |
| 106 | ga('send', 'event', 'button', 'click', 'request'); |
| 107 | scope.request(scope.resultTable.find('.resultSelector:checked:not([disabled])').parent().parent()); |
| 108 | }); |
| 109 | |
| 110 | this.filterSetup(); |
| 111 | |
| 112 | //Init autocomplete |
| 113 | this.searchInput.autoComplete(function(field, callback){ |
| 114 | ga('send', 'event', 'search', 'autocomplete'); |
Tyler Scott | 9445899 | 2015-09-24 14:16:28 -0700 | [diff] [blame] | 115 | scope.autoComplete(field, function(data){ |
| 116 | var list = data.next; |
| 117 | var last = data.lastComponent === true; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 118 | callback(list.map(function(element){ |
Tyler Scott | 9445899 | 2015-09-24 14:16:28 -0700 | [diff] [blame] | 119 | return field + element + (last?"/":""); //Don't add trailing slash for last component. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 120 | })); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | //Handle search |
| 125 | this.searchBar.submit(function(e){ |
| 126 | ga('send', 'event', 'search', 'submit'); |
| 127 | e.preventDefault(); |
| 128 | if (scope.searchInput.val().length === 0){ |
| 129 | if (!scope.searchBar.hasClass('has-error')){ |
| 130 | scope.searchBar.addClass('has-error').append('<span class="help-block">Search path is required!</span>'); |
| 131 | } |
| 132 | return; |
| 133 | } else { |
| 134 | scope.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()}); |
| 135 | } |
| 136 | scope.pathSearch(); |
| 137 | }); |
| 138 | |
| 139 | this.searchButton.click(function(){ |
| 140 | console.log("Search Button Pressed"); |
| 141 | ga('send', 'event', 'button', 'click', 'search'); |
| 142 | scope.search(); |
| 143 | }); |
| 144 | |
| 145 | //Result navigation handlers |
| 146 | this.resultMenu.find('.next').click(function(){ |
| 147 | ga('send', 'event', 'button', 'click', 'next'); |
| 148 | if (!$(this).hasClass('disabled')){ |
| 149 | scope.getResults(scope.page + 1); |
| 150 | } |
| 151 | }); |
| 152 | this.resultMenu.find('.previous').click(function(){ |
| 153 | ga('send', 'event', 'button', 'click', 'previous'); |
| 154 | if (!$(this).hasClass('disabled')){ |
| 155 | scope.getResults(scope.page - 1); |
| 156 | } |
| 157 | }); |
Tyler Scott | e8dac70 | 2015-10-13 14:33:25 -0600 | [diff] [blame] | 158 | this.resultMenu.find('.clearResults').click(function(){ |
| 159 | ga('send', 'event', 'button', 'click', 'resultClear'); |
| 160 | scope.clearResults(); |
| 161 | $('#results').fadeOut(function(){ |
| 162 | $(this).addClass('hidden'); |
| 163 | }); |
| 164 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 165 | |
| 166 | //Change the number of results per page handler |
| 167 | var rpps = $('.resultsPerPageSelector').click(function(){ |
| 168 | |
| 169 | var t = $(this); |
| 170 | |
| 171 | if (t.hasClass('active')){ |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | rpps.find('.active').removeClass('active'); |
| 176 | t.addClass('active'); |
| 177 | scope.resultsPerPage = Number(t.text()); |
| 178 | scope.getResults(0); //Force return to page 1; |
| 179 | |
| 180 | }); |
| 181 | |
| 182 | //Init tree search |
| 183 | $('#treeSearch div').treeExplorer(function(path, callback){ |
| 184 | console.log("Tree Explorer request", path); |
| 185 | ga('send', 'event', 'tree', 'request'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 186 | scope.autoComplete(path, function(data){ |
| 187 | var list = data.next; |
| 188 | var last = (data.lastComponent === true); |
Tyler Scott | bb42ed2 | 2015-10-21 17:02:56 -0600 | [diff] [blame] | 189 | |
| 190 | if (last) { |
| 191 | console.log("Redirecting last element request to a search."); |
| 192 | scope.clearResults(); |
| 193 | scope.query(scope.catalog, {'??': path}, |
| 194 | function(interest, data){ |
| 195 | console.log("Search response", interest, data); |
| 196 | scope.name = data.getContent().toString().replace(/[\n\0]+/g, ''); |
| 197 | scope.getResults(0); |
| 198 | }, function(interest){ |
| 199 | console.warn("Failed to retrieve final component.", interest, path); |
| 200 | scope.createAlert("Failed to request final component. " + path + " See console for details."); |
| 201 | }); |
| 202 | return; //Don't call the callback |
| 203 | } |
| 204 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 205 | console.log("Autocomplete response", list); |
| 206 | callback(list.map(function(element){ |
Tyler Scott | bb42ed2 | 2015-10-21 17:02:56 -0600 | [diff] [blame] | 207 | return (path == "/"?"/":"") + element + "/"; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 208 | })); |
| 209 | }) |
| 210 | }); |
| 211 | |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 212 | $('#treeSearch').on('click', '.treeSearch', function(){ |
| 213 | var t = $(this); |
| 214 | |
| 215 | scope.clearResults(); |
| 216 | |
| 217 | var path = t.parent().parent().attr('id'); |
| 218 | |
Tyler Scott | bb42ed2 | 2015-10-21 17:02:56 -0600 | [diff] [blame] | 219 | console.log("Tree search:", path); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 220 | |
| 221 | scope.query(scope.catalog, {'??': path}, |
| 222 | function(interest, data){ //Success |
| 223 | console.log("Tree search response", interest, data); |
| 224 | |
| 225 | scope.name = data.getContent().toString().replace(/[\n\0]+/g,''); |
| 226 | |
| 227 | scope.getResults(0); |
| 228 | }, |
| 229 | function(interest){ //Failure |
| 230 | console.warn("Request failed! Timeout", interest); |
| 231 | scope.createAlert("Request timed out.\""+ interest.getName().toUri() + "\" See console for details."); |
| 232 | }); |
| 233 | |
| 234 | }); |
| 235 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 236 | this.setupRequestForm(); |
| 237 | |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 238 | this.resultTable.popover({ |
| 239 | selector : ".metaDataLink", |
| 240 | content: function(){ |
| 241 | return scope.getMetaData(this); |
| 242 | }, |
| 243 | title: "Metadata", |
| 244 | html: true, |
| 245 | trigger: 'click', |
| 246 | placement: 'bottom' |
| 247 | }); |
| 248 | |
| 249 | this.resultTable.on('click', '.metaDataLink', function(e){ |
| 250 | //This prevents the page from scrolling when you click on a name. |
| 251 | e.preventDefault(); |
| 252 | }); |
| 253 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 254 | this.resultTable.on('click', '.subsetButton', function(){ |
| 255 | var metaData = $(this).siblings('pre').text(); |
| 256 | var exp = /netcdf ([\w-]+)/; |
| 257 | var match = exp.exec(metaData); |
| 258 | var filename = match[0].replace(/netcdf /,'') + '.nc'; |
| 259 | scope.request(null, filename); |
| 260 | }); |
| 261 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | Atmos.prototype.clearResults = function(){ |
| 265 | this.results = []; //Drop any old results. |
| 266 | this.retrievedSegments = 0; |
| 267 | this.resultCount = Infinity; |
| 268 | this.page = 0; |
| 269 | this.resultTable.empty(); |
| 270 | } |
| 271 | |
| 272 | Atmos.prototype.pathSearch = function(){ |
| 273 | var value = this.searchInput.val(); |
| 274 | |
| 275 | this.clearResults(); |
| 276 | |
| 277 | var scope = this; |
| 278 | |
| 279 | this.query(this.catalog, {"??": value}, |
| 280 | function(interest, data){ |
| 281 | console.log("Query response:", interest, data); |
| 282 | |
| 283 | scope.name = data.getContent().toString().replace(/[\n\0]/g,""); |
| 284 | |
| 285 | scope.getResults(0); |
| 286 | |
| 287 | }, |
| 288 | function(interest){ |
| 289 | console.warn("Request failed! Timeout", interest); |
| 290 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 291 | }); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | Atmos.prototype.search = function(){ |
| 296 | |
| 297 | var filters = this.getFilters(); |
| 298 | |
| 299 | console.log("Search started!", this.searchInput.val(), filters); |
| 300 | |
| 301 | console.log("Initiating query"); |
| 302 | |
| 303 | this.clearResults(); |
| 304 | |
| 305 | var scope = this; |
| 306 | |
| 307 | this.query(this.catalog, filters, |
| 308 | function(interest, data){ //Response function |
| 309 | console.log("Query Response:", interest, data); |
| 310 | |
| 311 | scope.name = data.getContent().toString().replace(/[\n\0]/g,""); |
| 312 | |
| 313 | scope.getResults(0); |
| 314 | |
| 315 | }, function(interest){ //Timeout function |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 316 | console.warn("Request failed after 3 attempts!", interest); |
| 317 | scope.createAlert("Request failed after 3 attempts. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 318 | }); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | Atmos.prototype.autoComplete = function(field, callback){ |
| 323 | |
| 324 | var scope = this; |
| 325 | |
| 326 | this.query(this.catalog, {"?": field}, |
| 327 | function(interest, data){ |
| 328 | |
| 329 | var name = new Name(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 330 | |
| 331 | var interest = new Interest(name); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 332 | interest.setInterestLifetimeMilliseconds(1500); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 333 | interest.setMustBeFresh(true); |
| 334 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 335 | async.retry(4, function(done){ |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 336 | scope.face.expressInterest(interest, |
| 337 | function(interest, data){ |
| 338 | |
| 339 | if (data.getContent().length !== 0){ |
| 340 | callback(JSON.parse(data.getContent().toString().replace(/[\n\0]/g, ""))); |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 341 | done(); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 342 | } else { |
| 343 | callback([]); |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 344 | done(); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 345 | } |
| 346 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 347 | }, function(){ |
| 348 | done("Failed attempt to request data.") |
| 349 | }); |
| 350 | }, function(err, results){ |
| 351 | if (err){ |
| 352 | console.warn("Interest timed out!", interest); |
| 353 | scope.createAlert("Request failed after 3 attempts. \"" + interest.getName().toUri() + "\" See console for details."); |
| 354 | } |
| 355 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 356 | |
| 357 | }, function(interest){ |
| 358 | console.error("Request failed! Timeout", interest); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 359 | scope.createAlert("Request failed after 3 attempts. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 360 | }); |
| 361 | |
| 362 | } |
| 363 | |
| 364 | Atmos.prototype.showResults = function(resultIndex) { |
| 365 | |
| 366 | var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1)); |
| 367 | |
| 368 | var resultDOM = $( |
| 369 | results.reduce(function(prev, current){ |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 370 | prev.push('<tr><td><input class="resultSelector" type="checkbox"></td><td class="popover-container"><a href="#" class="metaDataLink">'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 371 | prev.push(current); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 372 | prev.push('</a></td></tr>'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 373 | return prev; |
| 374 | }, ['<tr><th><input id="resultSelectAll" type="checkbox" title="Select All"> Select</th><th>Name</th></tr>']).join('') |
| 375 | ); |
| 376 | |
| 377 | resultDOM.find('#resultSelectAll').click(function(){ |
| 378 | if ($(this).is(':checked')){ |
| 379 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', true); |
| 380 | } else { |
| 381 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', false); |
| 382 | } |
| 383 | }); |
| 384 | |
| 385 | this.resultTable.hide().empty().append(resultDOM).slideDown('slow'); |
| 386 | |
| 387 | this.resultMenu.find('.pageNumber').text(resultIndex + 1); |
| 388 | this.resultMenu.find('.pageLength').text(this.resultsPerPage * resultIndex + results.length); |
| 389 | |
| 390 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
| 391 | this.resultMenu.find('.next').addClass('disabled'); |
| 392 | } else if (resultIndex === 0){ |
| 393 | this.resultMenu.find('.next').removeClass('disabled'); |
| 394 | } |
| 395 | |
| 396 | if (resultIndex === 0){ |
| 397 | this.resultMenu.find('.previous').addClass('disabled'); |
| 398 | } else if (resultIndex === 1) { |
| 399 | this.resultMenu.find('.previous').removeClass('disabled'); |
| 400 | } |
| 401 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 402 | $.scrollTo("#results", 500, {interrupt: true}); |
| 403 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | Atmos.prototype.getResults = function(index){ |
| 407 | |
| 408 | if ($('#results').hasClass('hidden')){ |
| 409 | $('#results').removeClass('hidden').slideDown(); |
| 410 | } |
| 411 | |
| 412 | if ((this.results.length === this.resultCount) || (this.resultsPerPage * (index + 1) < this.results.length)){ |
| 413 | //console.log("We already have index", index); |
| 414 | this.page = index; |
| 415 | this.showResults(index); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | if (this.name === null) { |
| 420 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 421 | throw new Error("Illegal State"); |
| 422 | } |
| 423 | |
| 424 | var first = new Name(this.name).appendSegment(this.retrievedSegments++); |
| 425 | |
| 426 | console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri()); |
| 427 | |
| 428 | var scope = this; |
| 429 | |
| 430 | var interest = new Interest(first) |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 431 | interest.setInterestLifetimeMilliseconds(1500); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 432 | interest.setMustBeFresh(true); |
| 433 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 434 | async.retry(4, function(done){ |
| 435 | this.face.expressInterest(interest, |
| 436 | function(interest, data){ //Response |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 437 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 438 | if (data.getContent().length === 0){ |
| 439 | scope.resultMenu.find('.totalResults').text(0); |
| 440 | scope.resultMenu.find('.pageNumber').text(0); |
| 441 | scope.resultMenu.find('.pageLength').text(0); |
| 442 | console.log("Empty response."); |
| 443 | scope.resultTable.html("<tr><td>Empty response. This usually means no results.</td></tr>"); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,"")); |
| 448 | |
| 449 | if (!content.results){ |
| 450 | scope.resultMenu.find('.totalResults').text(0); |
| 451 | scope.resultMenu.find('.pageNumber').text(0); |
| 452 | scope.resultMenu.find('.pageLength').text(0); |
| 453 | console.log("No results were found!"); |
| 454 | scope.resultTable.html("<tr><td>No Results</td></tr>"); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | scope.results = scope.results.concat(content.results); |
| 459 | |
| 460 | scope.resultCount = content.resultCount; |
| 461 | |
| 462 | scope.resultMenu.find('.totalResults').text(scope.resultCount); |
| 463 | |
| 464 | scope.page = index; |
| 465 | |
| 466 | scope.getResults(index); //Keep calling this until we have enough data. |
| 467 | |
| 468 | done(); |
| 469 | |
| 470 | }, |
| 471 | function(interest){ //Timeout |
| 472 | done("Failed to request results."); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 473 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 474 | ); |
| 475 | }, function(err){ |
| 476 | if (err){ |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 477 | console.error("Failed to retrieve results: timeout", interest); |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 478 | scope.createAlert("Request failed after 3 attempts. \"" + interest.getName().toUri() + "\" See console for details."); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 479 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 480 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 481 | |
| 482 | } |
| 483 | |
| 484 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
| 485 | |
| 486 | var queryPrefix = new Name(prefix); |
| 487 | queryPrefix.append("query"); |
| 488 | |
| 489 | var jsonString = JSON.stringify(parameters); |
| 490 | queryPrefix.append(jsonString); |
| 491 | |
| 492 | var queryInterest = new Interest(queryPrefix); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 493 | queryInterest.setInterestLifetimeMilliseconds(1500); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 494 | queryInterest.setMustBeFresh(true); |
| 495 | |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 496 | var face = this.face; |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 497 | |
| 498 | async.retry(4, function(done){ |
| 499 | face.expressInterest(queryInterest, function(interest, data){ |
| 500 | callback(interest, data); |
| 501 | done(); |
| 502 | }, function(interest){ |
| 503 | done("Failed attempt to query results", interest); |
| 504 | }); |
| 505 | }, function(err, interest){ |
| 506 | if (err){ |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 507 | timeout(interest); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 508 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 509 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 510 | |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * This function returns a map of all the categories active filters. |
| 515 | * @return {Object<string, string>} |
| 516 | */ |
| 517 | Atmos.prototype.getFilters = function(){ |
| 518 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
| 519 | var data = $(current).text().split(/:/); |
| 520 | prev[data[0]] = data[1]; |
| 521 | return prev; |
| 522 | }, {}); //Collect a map<category, filter>. |
| 523 | //TODO Make the return value map<category, Array<filter>> |
| 524 | return filters; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Creates a closable alert for the user. |
| 529 | * |
| 530 | * @param {string} message |
| 531 | * @param {string} type - Override the alert type. |
| 532 | */ |
| 533 | Atmos.prototype.createAlert = function(message, type) { |
| 534 | |
| 535 | var alert = $('<div class="alert"><div>'); |
| 536 | alert.addClass(type?type:'alert-info'); |
| 537 | alert.text(message); |
| 538 | alert.append(closeButton); |
| 539 | |
| 540 | this.alerts.append(alert); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Requests all of the names represented by the buttons in the elements list. |
| 545 | * |
| 546 | * @param elements {Array<jQuery>} A list of the table row elements |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 547 | * @param subsetFileName {String} If present then do a subsetting request instead. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 548 | */ |
| 549 | Atmos.prototype.request = function(){ |
| 550 | |
| 551 | //Pseudo globals. |
| 552 | var keyChain; |
| 553 | var certificateName; |
| 554 | var keyAdded = false; |
| 555 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 556 | return function(elements, subsetFilename){ |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 557 | |
| 558 | var names = []; |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 559 | $(elements).find('.metaDataLink').each(function(){ |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 560 | var name = $(this).text(); |
| 561 | names.push(name); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 562 | }); |
| 563 | |
| 564 | var subset = false; |
| 565 | |
| 566 | if (!subsetFilename){ |
| 567 | $('#subsetting').hide(); |
| 568 | } else { |
| 569 | $('#subsetting').show(); |
| 570 | subset = true; |
| 571 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 572 | |
| 573 | var scope = this; |
| 574 | this.requestForm.on('submit', function(e){ //This will be registered for the next submit from the form. |
| 575 | e.preventDefault(); |
| 576 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 577 | $('#request .alert').remove(); |
| 578 | |
| 579 | var variables = []; |
| 580 | if (subset){ |
| 581 | $('#subsetVariables .row').each(function(){ |
| 582 | var t = $(this); |
| 583 | var values = {}; |
| 584 | t.find('.values input').each(function(){ |
| 585 | var t = $(this); |
| 586 | values[t.attr('name')] = t.val(); |
| 587 | }); |
| 588 | variables.push({variable: t.find('.variable').val(), values: values}); |
| 589 | }); |
| 590 | } |
| 591 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 592 | //Form checking |
| 593 | var dest = scope.requestForm.find('#requestDest .active'); |
| 594 | if (dest.length !== 1){ |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 595 | var alert = $('<div class="alert alert-warning">A destination is required!' + closeButton + '<div>'); |
| 596 | $('#request > .panel-body').append(alert); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 600 | $('#request').modal('hide');//Initial params are ok. We can close the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 601 | |
| 602 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 603 | |
| 604 | $(this).off(e); //Don't fire this again, the request must be regenerated |
| 605 | |
| 606 | //Key setup |
| 607 | if (!keyAdded){ |
| 608 | if (!scope.config.retrieval.demoKey || !scope.config.retrieval.demoKey.pub || !scope.config.retrieval.demoKey.priv){ |
| 609 | scope.createAlert("This host was not configured to handle retrieval! See console for details.", 'alert-danger'); |
| 610 | console.error("Missing/invalid key! This must be configured in the config on the server.", scope.config.demoKey); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | //FIXME base64 may or may not exist in other browsers. Need a new polyfill. |
| 615 | var pub = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.pub)); //MUST be a Buffer (Buffer != Uint8Array) |
| 616 | var priv = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.priv)); |
| 617 | |
| 618 | var identityStorage = new MemoryIdentityStorage(); |
| 619 | var privateKeyStorage = new MemoryPrivateKeyStorage(); |
| 620 | keyChain = new KeyChain(new IdentityManager(identityStorage, privateKeyStorage), |
| 621 | new SelfVerifyPolicyManager(identityStorage)); |
| 622 | |
| 623 | var keyName = new Name("/retrieve/DSK-123"); |
| 624 | certificateName = keyName.getSubName(0, keyName.size() - 1) |
| 625 | .append("KEY").append(keyName.get(-1)) |
| 626 | .append("ID-CERT").append("0"); |
| 627 | |
| 628 | identityStorage.addKey(keyName, KeyType.RSA, new Blob(pub, false)); |
| 629 | privateKeyStorage.setKeyPairForKeyName(keyName, KeyType.RSA, pub, priv); |
| 630 | |
| 631 | scope.face.setCommandSigningInfo(keyChain, certificateName); |
| 632 | |
| 633 | keyAdded = true; |
| 634 | |
| 635 | } |
| 636 | |
| 637 | //Retrieval |
| 638 | var retrievePrefix = new Name("/catalog/ui/" + guid()); |
| 639 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 640 | scope.face.registerPrefix(retrievePrefix, |
| 641 | function(prefix, interest, face, interestFilterId, filter){ //On Interest |
| 642 | //This function will exist until the page exits but will likely only be used once. |
| 643 | |
| 644 | var data = new Data(interest.getName()); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 645 | var content; |
| 646 | if (subset){ |
| 647 | content = JSON.stringify({name: subsetFilename, subset: variables}); |
| 648 | } else { |
| 649 | content = JSON.stringify(names); |
| 650 | } |
| 651 | //Blob breaks the data! Don't use it |
| 652 | data.setContent(content); //TODO Packetize this. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 653 | keyChain.sign(data, certificateName); |
| 654 | |
| 655 | try { |
| 656 | face.putData(data); |
| 657 | console.log("Responded for", interest.getName().toUri(), data); |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 658 | scope.createAlert("Data retrieval has initiated.", "alert-success"); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 659 | } catch (e) { |
| 660 | console.error("Failed to respond to", interest.getName().toUri(), data); |
| 661 | scope.createAlert("Data retrieval failed."); |
| 662 | } |
| 663 | |
| 664 | }, function(prefix){ //On fail |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 665 | scope.createAlert("Failed to register the retrieval URI! See console for details.", "alert-danger"); |
| 666 | console.error("Failed to register URI:", prefix.toUri(), prefix); |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 667 | }, function(prefix, registeredPrefixId){ //On success |
| 668 | var name = new Name(dest.text()); |
| 669 | name.append(prefix); |
| 670 | var interest = new Interest(name); |
| 671 | interest.setInterestLifetimeMilliseconds(1500); |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 672 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 673 | async.retry(4, function(done){ |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 674 | scope.face.expressInterest(interest, |
| 675 | function(interest, data){ //Success |
| 676 | console.log("Request for", name.toUri(), "succeeded.", interest, data); |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 677 | done(); |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 678 | }, |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 679 | function(){ |
| 680 | done("Failed to request from retrieve agent."); |
| 681 | } |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 682 | ); |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 683 | }, function(err){ |
| 684 | if (err){ |
| 685 | console.error("Request for", name.toUri(), "timed out (3 times).", interest); |
| 686 | scope.createAlert("Request for " + name.toUri() + " timed out after 3 attempts. This means that the retrieve failed! See console for more details."); |
| 687 | } |
| 688 | }); |
| 689 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 690 | } |
| 691 | ); |
| 692 | |
| 693 | }); |
| 694 | $('#request').modal(); //This forces the form to be the only option. |
| 695 | |
| 696 | } |
| 697 | }(); |
| 698 | |
| 699 | Atmos.prototype.filterSetup = function() { |
| 700 | //Filter setup |
| 701 | |
| 702 | var prefix = new Name(this.catalog).append("filters-initialization"); |
| 703 | |
| 704 | var scope = this; |
| 705 | |
| 706 | this.getAll(prefix, function(data) { //Success |
| 707 | var raw = JSON.parse(data.replace(/[\n\0]/g, '')); //Remove null byte and parse |
| 708 | |
| 709 | console.log("Filter categories:", raw); |
| 710 | |
| 711 | $.each(raw, function(index, object){ //Unpack list of objects |
| 712 | $.each(object, function(category, searchOptions) { //Unpack category from object (We don't know what it is called) |
| 713 | //Create the category |
| 714 | var e = $('<li><a href="#">' + category.replace(/_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>'); |
| 715 | |
| 716 | var sub = e.find('ul.subnav'); |
| 717 | $.each(searchOptions, function(index, name){ |
| 718 | //Create the filter list inside the category |
| 719 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 720 | sub.append(item); |
| 721 | item.click(function(){ //Click on the side menu filters |
| 722 | if (item.hasClass('active')){ //Does the filter already exist? |
| 723 | item.removeClass('active'); |
| 724 | scope.filters.find(':contains(' + category + ':' + name + ')').remove(); |
| 725 | } else { //Add a filter |
| 726 | item.addClass('active'); |
| 727 | var filter = $('<span class="label label-default"></span>'); |
| 728 | filter.text(category + ':' + name); |
| 729 | |
| 730 | scope.filters.append(filter); |
| 731 | |
| 732 | filter.click(function(){ //Click on a filter |
| 733 | filter.remove(); |
| 734 | item.removeClass('active'); |
| 735 | }); |
| 736 | } |
| 737 | |
| 738 | }); |
| 739 | }); |
| 740 | |
| 741 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
| 742 | e.find('> a').click(function(){ |
| 743 | scope.categories.find('.subnav').slideUp(); |
| 744 | var t = $(this).siblings('.subnav'); |
| 745 | if ( !t.is(':visible') ){ //If the sub menu is not visible |
| 746 | t.slideDown(function(){ |
| 747 | t.triggerHandler('focus'); |
| 748 | }); //Make it visible and look at it. |
| 749 | } |
| 750 | }); |
| 751 | |
| 752 | scope.categories.append(e); |
| 753 | |
| 754 | }); |
| 755 | }); |
| 756 | |
| 757 | }, function(interest){ //Timeout |
| 758 | scope.createAlert("Failed to initialize the filters!", "alert-danger"); |
| 759 | console.error("Failed to initialize filters!", interest); |
| 760 | ga('send', 'event', 'error', 'filters'); |
| 761 | }); |
| 762 | |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * This function retrieves all segments in order until it knows it has reached the last one. |
| 767 | * It then returns the final joined result. |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 768 | * |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 769 | * @param prefix {String|Name} The ndn name we are retrieving. |
| 770 | * @param callback {function(String)} if successful, will call the callback with a string of data. |
| 771 | * @param failure {function(Interest)} if unsuccessful, will call failure with the last failed interest. |
| 772 | * @param stop {boolean} stop if no finalBlock. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 773 | */ |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 774 | Atmos.prototype.getAll = function(prefix, callback, failure, stop){ |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 775 | |
| 776 | var scope = this; |
| 777 | var d = []; |
| 778 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 779 | var name = new Name(prefix); |
| 780 | var segment = 0; |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 781 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 782 | var request = function(){ |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 783 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 784 | var n2 = new Name(name); |
| 785 | n2.appendSegment(segment); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 786 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 787 | var interest = new Interest(n2); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 788 | interest.setInterestLifetimeMilliseconds(1500); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 789 | interest.setMustBeFresh(true); //Is this needed? |
| 790 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 791 | async.retry(4, function(done){ |
| 792 | scope.face.expressInterest(interest, |
| 793 | function(interest, data){ |
| 794 | handleData(interest, data); |
| 795 | done(); |
| 796 | }, function(){ |
| 797 | done("Failed to get segment."); |
| 798 | } |
| 799 | ); |
| 800 | }, function(err){ |
| 801 | if (err){ |
| 802 | console.log("Failed after 3 attempts:", err); |
| 803 | } |
| 804 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 805 | |
| 806 | } |
| 807 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 808 | var handleData = function(interest, data){ |
| 809 | |
| 810 | d.push(data.getContent().toString()); |
| 811 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 812 | var hasFinalBlock = data.getMetaInfo().getFinalBlockId().value.length === 0; |
| 813 | var finalBlockStop = hasFinalBlock && stop; |
| 814 | |
| 815 | if (finalBlockStop || |
| 816 | (!hasFinalBlock && interest.getName().get(-1).toSegment() == data.getMetaInfo().getFinalBlockId().toSegment())){ |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 817 | callback(d.join("")); |
| 818 | } else { |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 819 | segment++; |
| 820 | request(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | } |
| 824 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame^] | 825 | request(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 826 | |
| 827 | } |
| 828 | |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 829 | Atmos.prototype.cleanRequestForm = function(){ |
| 830 | $('#requestDest').prev().removeClass('btn-success').addClass('btn-default'); |
| 831 | $('#requestDropText').text('Destination'); |
| 832 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 833 | $('#subsetMenu').attr('class', 'collapse'); |
| 834 | $('#subsetVariables').empty(); |
| 835 | $('#request .alert').alert('close').remove(); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 836 | } |
| 837 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 838 | Atmos.prototype.setupRequestForm = function(){ |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 839 | |
| 840 | var scope = this; |
| 841 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 842 | this.requestForm.find('#requestCancel').click(function(){ |
| 843 | $('#request').unbind('submit') //Removes all event handlers. |
| 844 | .modal('hide'); //Hides the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 845 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 846 | }); |
| 847 | |
| 848 | var dests = $(this.config['retrieval']['destinations'].reduce(function(prev, current){ |
| 849 | prev.push('<li><a href="#">'); |
| 850 | prev.push(current); |
| 851 | prev.push("</a></li>"); |
| 852 | return prev; |
| 853 | }, []).join("")); |
| 854 | |
| 855 | this.requestForm.find('#requestDest').append(dests) |
| 856 | .on('click', 'a', function(e){ |
| 857 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 858 | var t = $(this); |
| 859 | t.parent().addClass('active'); |
| 860 | $('#requestDropText').text(t.text()); |
| 861 | $('#requestDest').prev().removeClass('btn-default').addClass('btn-success'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 862 | }); |
| 863 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 864 | var addVariable = function(selector){ |
| 865 | var ele = $(selector).clone().attr('id',''); |
| 866 | ele.find('.close').click(function(){ |
| 867 | ele.remove(); |
| 868 | }); |
| 869 | $('#subsetVariables').append(ele); |
| 870 | }; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 871 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 872 | $('#subsetAddVariableBtn').click(function(){ |
| 873 | addVariable('#customTemplate'); |
| 874 | }); |
| 875 | $('#subsetAddTimeVariable').click(function(){ |
| 876 | addVariable('#timeTemplate'); |
| 877 | }); |
| 878 | $('#subsetAddLocVariable').click(function(){ |
| 879 | addVariable('#locationTemplate'); |
| 880 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 881 | |
| 882 | } |
| 883 | |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 884 | Atmos.prototype.getMetaData = (function(){ |
| 885 | |
| 886 | var cache = {}; |
| 887 | |
| 888 | return function(element) { |
| 889 | var name = $(element).text(); |
| 890 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 891 | ga('send', 'event', 'request', 'metaData'); |
| 892 | |
| 893 | var subsetButton = '<button class="btn btn-default subsetButton" type="button">Subset</button>'; |
| 894 | |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 895 | if (cache[name]) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 896 | return [subsetButton, '<pre class="metaData">', cache[name], '</pre>'].join(''); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | var prefix = new Name(name).append("metadata"); |
| 900 | var id = guid(); //We need an id because the return MUST be a string. |
| 901 | var ret = '<div id="' + id + '"><span class="fa fa-spinner fa-spin"></span></div>'; |
| 902 | |
| 903 | this.getAll(prefix, function(data){ |
| 904 | var el = $('<pre class="metaData"></pre>'); |
| 905 | el.text(data); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 906 | var container = $('<div></div>'); |
| 907 | container.append($(subsetButton)); |
| 908 | container.append(el); |
| 909 | $('#' + id).empty().append(container); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 910 | cache[name] = data; |
| 911 | }, function(interest){ |
| 912 | $('#' + id).text("The metadata is unavailable for this name."); |
| 913 | console.log("Data is unavailable for " + name); |
| 914 | }); |
| 915 | |
| 916 | return ret; |
| 917 | |
| 918 | } |
| 919 | })(); |
| 920 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 921 | return Atmos; |
| 922 | |
| 923 | })(); |