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