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 | f95a97c | 2016-06-21 12:09:22 -0600 | [diff] [blame^] | 267 | $('.metaDataLink').not(this).popover('destroy'); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 268 | return scope.getMetaData(this); |
| 269 | }, |
| 270 | title: "Metadata", |
| 271 | html: true, |
| 272 | trigger: 'click', |
| 273 | placement: 'bottom' |
| 274 | }); |
| 275 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 276 | this.resultTable.on('click', '.metaDataLink', function(e) { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 277 | //This prevents the page from scrolling when you click on a name. |
| 278 | e.preventDefault(); |
| 279 | }); |
| 280 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 281 | this.resultTable.on('click', '.subsetButton', function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 282 | var metaData = $(this).siblings('pre').text(); |
| 283 | var exp = /netcdf ([\w-]+)/; |
| 284 | var match = exp.exec(metaData); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 285 | var filename = match[0].replace(/netcdf /, '') + '.nc'; |
| 286 | scope.request(null , filename); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 287 | }); |
| 288 | |
Tyler Scott | f95a97c | 2016-06-21 12:09:22 -0600 | [diff] [blame^] | 289 | //Allow the title to change the tab |
| 290 | $('#brand-title').click(function(){ |
| 291 | //Correct active class on tabs. |
| 292 | $('#explore-tab').removeClass('active'); |
| 293 | $('#search-tab').addClass('active'); |
| 294 | }); |
| 295 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 296 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 297 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 298 | Atmos.prototype.clearResults = function() { |
| 299 | this.results = []; |
| 300 | //Drop any old results. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 301 | this.retrievedSegments = 0; |
| 302 | this.resultCount = Infinity; |
| 303 | this.page = 0; |
| 304 | this.resultTable.empty(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 305 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 306 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 307 | Atmos.prototype.pathSearch = function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 308 | var value = this.searchInput.val(); |
| 309 | |
| 310 | this.clearResults(); |
| 311 | |
| 312 | var scope = this; |
| 313 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 314 | this.query(this.catalog, { |
| 315 | "??": value |
| 316 | }, |
| 317 | function(interest, data) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 318 | console.log("Query response:", interest, data); |
| 319 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 320 | scope.name = interest.getName(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 321 | |
| 322 | scope.getResults(0); |
| 323 | |
| 324 | }, |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 325 | function(interest) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 326 | console.warn("Request failed! Timeout", interest); |
| 327 | scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details."); |
| 328 | }); |
| 329 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 330 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 331 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 332 | Atmos.prototype.search = function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 333 | |
| 334 | var filters = this.getFilters(); |
| 335 | |
| 336 | console.log("Search started!", this.searchInput.val(), filters); |
| 337 | |
| 338 | console.log("Initiating query"); |
| 339 | |
| 340 | this.clearResults(); |
| 341 | |
| 342 | var scope = this; |
| 343 | |
| 344 | this.query(this.catalog, filters, |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 345 | function(interest, data) { |
| 346 | //Response function |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 347 | console.log("Query Response:", interest, data); |
| 348 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 349 | scope.name = interest.getName(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 350 | |
| 351 | scope.getResults(0); |
| 352 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 353 | }, function(interest) { |
| 354 | //Timeout function |
Tyler Scott | d980a29 | 2015-10-13 15:16:34 -0600 | [diff] [blame] | 355 | console.warn("Request failed after 3 attempts!", interest); |
| 356 | 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] | 357 | }); |
| 358 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 359 | } |
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 | Atmos.prototype.autoComplete = function(field, callback) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 362 | |
| 363 | var scope = this; |
| 364 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 365 | var result = {}; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 366 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 367 | const getAll = function(interest, data) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 368 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 369 | if (data.getContent().length !== 0) { |
| 370 | var resp = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")); |
| 371 | if (result.next) { |
| 372 | result.next = result.next.concat(resp.next); |
| 373 | } else { |
| 374 | result = resp; |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 375 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 376 | } else { |
| 377 | callback(result); |
| 378 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 379 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 380 | var name = data.getName(); |
| 381 | var segment = name.components[name.getComponentCount() - 1]; |
| 382 | if (segment.toSegment() !== data.getMetaInfo().getFinalBlockId().toSegment()){ |
| 383 | name = name.getPrefix(-1); //Remove segment |
| 384 | name.appendSegment(segment.toSegment() + 1); |
| 385 | scope.expressInterest(name, getAll, function() { |
| 386 | console.warn("Autocomplete timed out, results may be incomplete."); |
| 387 | callback(result); |
| 388 | //Return if we get a timeout. |
| 389 | }); |
| 390 | } else { |
| 391 | callback(result); |
| 392 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 393 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | this.query(this.catalog, { |
| 397 | "?": field |
| 398 | }, getAll); |
| 399 | |
| 400 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 401 | |
| 402 | Atmos.prototype.showResults = function(resultIndex) { |
| 403 | |
| 404 | var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1)); |
| 405 | |
| 406 | var resultDOM = $( |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 407 | results.reduce(function(prev, current) { |
| 408 | prev.push('<tr><td><input class="resultSelector" type="checkbox"></td><td class="popover-container"><a href="#" class="metaDataLink">'); |
| 409 | prev.push(current); |
| 410 | prev.push('</a></td></tr>'); |
| 411 | return prev; |
| 412 | }, ['<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] | 413 | ); |
| 414 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 415 | resultDOM.find('#resultSelectAll').click(function() { |
| 416 | if ($(this).is(':checked')) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 417 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', true); |
| 418 | } else { |
| 419 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', false); |
| 420 | } |
| 421 | }); |
| 422 | |
| 423 | this.resultTable.hide().empty().append(resultDOM).slideDown('slow'); |
| 424 | |
| 425 | this.resultMenu.find('.pageNumber').text(resultIndex + 1); |
| 426 | this.resultMenu.find('.pageLength').text(this.resultsPerPage * resultIndex + results.length); |
| 427 | |
| 428 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
| 429 | this.resultMenu.find('.next').addClass('disabled'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 430 | } else if (resultIndex === 0) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 431 | this.resultMenu.find('.next').removeClass('disabled'); |
| 432 | } |
| 433 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 434 | if (resultIndex === 0) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 435 | this.resultMenu.find('.previous').addClass('disabled'); |
| 436 | } else if (resultIndex === 1) { |
| 437 | this.resultMenu.find('.previous').removeClass('disabled'); |
| 438 | } |
| 439 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 440 | $.scrollTo("#results", 500, { |
| 441 | interrupt: true |
| 442 | }); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 443 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 444 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 445 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 446 | Atmos.prototype.getResults = function(index) { |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 447 | var scope = this; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 448 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 449 | if ($('#results').hasClass('hidden')) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 450 | $('#results').removeClass('hidden').slideDown(); |
| 451 | } |
| 452 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 453 | 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] | 454 | //console.log("We already have index", index); |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 455 | scope.page = index; |
| 456 | scope.showResults(index); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 460 | if (scope.name === null ) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 461 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 462 | throw new Error("Illegal State"); |
| 463 | } |
| 464 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 465 | var interestName = new Name(scope.name); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 466 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 467 | // Interest name should be /<catalog-prefix>/query/<query-param>/<version>/<#seq> |
| 468 | if (scope.name.size() === (scope.catalogPrefix.size() + 3)) { |
| 469 | interestName = interestName.appendSegment(scope.retrievedSegments++); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 470 | //console.log("Requesting data index: (", scope.retrievedSegments - 1, ") at ", interestName.toUri()); |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 471 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 472 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 473 | this.expressInterest(interestName, |
| 474 | function(interest, data) { |
| 475 | //Response |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 476 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 477 | if (data.getContent().length === 0) { |
| 478 | scope.resultMenu.find('.totalResults').text(0); |
| 479 | scope.resultMenu.find('.pageNumber').text(0); |
| 480 | scope.resultMenu.find('.pageLength').text(0); |
| 481 | console.log("Empty response."); |
| 482 | scope.resultTable.html("<tr><td>Empty response. This usually means no results.</td></tr>"); |
| 483 | return; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 484 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 485 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 486 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")); |
| 487 | |
| 488 | if (!content.results) { |
| 489 | scope.resultMenu.find('.totalResults').text(0); |
| 490 | scope.resultMenu.find('.pageNumber').text(0); |
| 491 | scope.resultMenu.find('.pageLength').text(0); |
| 492 | console.log("No results were found!"); |
| 493 | scope.resultTable.html("<tr><td>No Results</td></tr>"); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | scope.results = scope.results.concat(content.results); |
| 498 | |
| 499 | scope.resultCount = content.resultCount; |
| 500 | |
| 501 | scope.resultMenu.find('.totalResults').text(scope.resultCount); |
| 502 | |
| 503 | scope.page = index; |
| 504 | |
| 505 | // reset scope.name |
| 506 | scope.name = new Name(data.getName().getPrefix(scope.catalogPrefix.size() + 3)); |
| 507 | |
| 508 | scope.getResults(index); |
| 509 | //Keep calling this until we have enough data. |
| 510 | |
| 511 | }, function() {}//Ignore failure |
| 512 | ); |
| 513 | |
| 514 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 515 | |
| 516 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
| 517 | |
| 518 | var queryPrefix = new Name(prefix); |
| 519 | queryPrefix.append("query"); |
| 520 | |
| 521 | var jsonString = JSON.stringify(parameters); |
| 522 | queryPrefix.append(jsonString); |
| 523 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 524 | this.expressInterest(queryPrefix, callback, timeout); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 525 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 526 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 527 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 528 | |
| 529 | Atmos.prototype.expressInterest = function(name, success, failure) { |
| 530 | |
| 531 | var interest = new Interest(name); |
| 532 | interest.setInterestLifetimeMilliseconds(1500); |
| 533 | interest.setMustBeFresh(true); |
| 534 | |
| 535 | const face = this.face; |
| 536 | |
| 537 | async.retry(4, function(done) { |
| 538 | face.expressInterest(interest, function(interest, data) { |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 539 | done(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 540 | success(interest, data); |
| 541 | }, function(interest) { |
| 542 | done("Interest timed out 4 times.", interest); |
| 543 | }); |
| 544 | }, function(err, interest) { |
| 545 | if (err) { |
| 546 | console.log(err, interest); |
| 547 | failure(interest); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 548 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 549 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 550 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 551 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 552 | |
| 553 | /** |
| 554 | * This function returns a map of all the categories active filters. |
| 555 | * @return {Object<string, string>} |
| 556 | */ |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 557 | Atmos.prototype.getFilters = function() { |
| 558 | var filters = this.filters.children().toArray().reduce(function(prev, current) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 559 | var data = $(current).text().split(/:/); |
| 560 | prev[data[0]] = data[1]; |
| 561 | return prev; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 562 | }, {}); |
| 563 | //Collect a map<category, filter>. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 564 | //TODO Make the return value map<category, Array<filter>> |
| 565 | return filters; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 566 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * Creates a closable alert for the user. |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 570 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 571 | * @param {string} message |
| 572 | * @param {string} type - Override the alert type. |
| 573 | */ |
| 574 | Atmos.prototype.createAlert = function(message, type) { |
| 575 | |
| 576 | var alert = $('<div class="alert"><div>'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 577 | alert.addClass(type ? type : 'alert-info'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 578 | alert.text(message); |
| 579 | alert.append(closeButton); |
| 580 | |
| 581 | this.alerts.append(alert); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 582 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * 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] | 586 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 587 | * @param elements {Array<jQuery>} A list of the table row elements |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 588 | * @param subsetFileName {String} If present then do a subsetting request instead. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 589 | */ |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 590 | Atmos.prototype.request = function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 591 | |
| 592 | //Pseudo globals. |
| 593 | var keyChain; |
| 594 | var certificateName; |
| 595 | var keyAdded = false; |
| 596 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 597 | return function(elements, subsetFilename) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 598 | |
| 599 | var names = []; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 600 | $(elements).find('.metaDataLink').each(function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 601 | var name = $(this).text(); |
| 602 | names.push(name); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 603 | }); |
| 604 | |
| 605 | var subset = false; |
| 606 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 607 | if (!subsetFilename) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 608 | $('#subsetting').hide(); |
| 609 | } else { |
| 610 | $('#subsetting').show(); |
| 611 | subset = true; |
| 612 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 613 | |
| 614 | var scope = this; |
Tyler Scott | 384f37a | 2016-05-05 16:09:05 -0600 | [diff] [blame] | 615 | |
| 616 | //FIXME The following is temporary, it allows people to direct download from |
| 617 | //a single host with a small set of names. It is to demo the functionality but |
| 618 | //could use improvement. (Multiple servers, non static list, etc) |
| 619 | var directDls = $('#directDownloadList').empty(); |
| 620 | names.forEach(function(name){ |
| 621 | if (scope.conversions[name]){ //If the name exists in the conversions. |
| 622 | var ele = $('<a href="http://atmos-mwsc.ucar.edu/ucar/' + conversions[name] + '" class="list-group-item>' + name + '</a>'); |
| 623 | directDls.append(ele); |
| 624 | } |
| 625 | }); |
| 626 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 627 | this.requestForm.on('submit', function(e) { |
| 628 | //This will be registered for the next submit from the form. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 629 | e.preventDefault(); |
| 630 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 631 | $('#request .alert').remove(); |
| 632 | |
| 633 | var variables = []; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 634 | if (subset) { |
| 635 | $('#subsetVariables .row').each(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 636 | var t = $(this); |
| 637 | var values = {}; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 638 | t.find('.values input').each(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 639 | var t = $(this); |
| 640 | values[t.attr('name')] = t.val(); |
| 641 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 642 | variables.push({ |
| 643 | variable: t.find('.variable').val(), |
| 644 | values: values |
| 645 | }); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 646 | }); |
| 647 | } |
| 648 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 649 | //Form checking |
| 650 | var dest = scope.requestForm.find('#requestDest .active'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 651 | if (dest.length !== 1) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 652 | var alert = $('<div class="alert alert-warning">A destination is required!' + closeButton + '<div>'); |
| 653 | $('#request > .panel-body').append(alert); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 657 | $('#request').modal('hide'); |
| 658 | //Initial params are ok. We can close the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 659 | |
| 660 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 661 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 662 | $(this).off(e); |
| 663 | //Don't fire this again, the request must be regenerated |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 664 | |
| 665 | //Key setup |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 666 | if (!keyAdded) { |
| 667 | 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] | 668 | scope.createAlert("This host was not configured to handle retrieval! See console for details.", 'alert-danger'); |
| 669 | console.error("Missing/invalid key! This must be configured in the config on the server.", scope.config.demoKey); |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | //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] | 674 | var pub = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.pub)); |
| 675 | //MUST be a Buffer (Buffer != Uint8Array) |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 676 | var priv = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.priv)); |
| 677 | |
| 678 | var identityStorage = new MemoryIdentityStorage(); |
| 679 | var privateKeyStorage = new MemoryPrivateKeyStorage(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 680 | keyChain = new KeyChain(new IdentityManager(identityStorage,privateKeyStorage), |
| 681 | new SelfVerifyPolicyManager(identityStorage)); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 682 | |
| 683 | var keyName = new Name("/retrieve/DSK-123"); |
| 684 | certificateName = keyName.getSubName(0, keyName.size() - 1) |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 685 | .append("KEY").append(keyName.get(-1)) |
| 686 | .append("ID-CERT").append("0"); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 687 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 688 | identityStorage.addKey(keyName, KeyType.RSA, new Blob(pub,false)); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 689 | privateKeyStorage.setKeyPairForKeyName(keyName, KeyType.RSA, pub, priv); |
| 690 | |
| 691 | scope.face.setCommandSigningInfo(keyChain, certificateName); |
| 692 | |
| 693 | keyAdded = true; |
| 694 | |
| 695 | } |
| 696 | |
| 697 | //Retrieval |
| 698 | var retrievePrefix = new Name("/catalog/ui/" + guid()); |
| 699 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 700 | scope.face.registerPrefix(retrievePrefix, |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 701 | function(prefix, interest, face, interestFilterId, filter) { |
| 702 | //On Interest |
| 703 | //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] | 704 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 705 | var data = new Data(interest.getName()); |
| 706 | var content; |
| 707 | if (subset) { |
| 708 | content = JSON.stringify({ |
| 709 | name: subsetFilename, |
| 710 | subset: variables |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 711 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 712 | } else { |
| 713 | content = JSON.stringify(names); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 714 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 715 | //Blob breaks the data! Don't use it |
| 716 | data.setContent(content); |
| 717 | //TODO Packetize this. |
| 718 | keyChain.sign(data, certificateName); |
| 719 | |
| 720 | try { |
| 721 | face.putData(data); |
| 722 | console.log("Responded for", interest.getName().toUri(), data); |
| 723 | scope.createAlert("Data retrieval has initiated.", "alert-success"); |
| 724 | } catch (e) { |
| 725 | console.error("Failed to respond to", interest.getName().toUri(), data); |
| 726 | scope.createAlert("Data retrieval failed."); |
| 727 | } |
| 728 | |
| 729 | }, function(prefix) { |
| 730 | //On fail |
| 731 | scope.createAlert("Failed to register the retrieval URI! See console for details.", "alert-danger"); |
| 732 | console.error("Failed to register URI:", prefix.toUri(), prefix); |
| 733 | }, function(prefix, registeredPrefixId) { |
| 734 | //On success |
| 735 | var name = new Name(dest.text()); |
| 736 | name.append(prefix); |
| 737 | |
| 738 | scope.expressInterest(name, |
| 739 | function(interest, data) { |
| 740 | //Success |
| 741 | console.log("Request for", name.toUri(), "succeeded.", interest, data); |
| 742 | }, |
| 743 | function() { |
| 744 | console.warn("Failed to request from retrieve agent."); |
| 745 | } |
| 746 | ); |
| 747 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 748 | ); |
| 749 | |
| 750 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 751 | $('#request').modal(); |
| 752 | //This forces the form to be the only option. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 753 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 756 | }(); |
| 757 | |
| 758 | Atmos.prototype.filterSetup = function() { |
| 759 | //Filter setup |
| 760 | |
| 761 | var prefix = new Name(this.catalog).append("filters-initialization"); |
| 762 | |
| 763 | var scope = this; |
| 764 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 765 | this.getAll(prefix, function(data) { |
| 766 | //Success |
| 767 | var raw = JSON.parse(data.replace(/[\n\0]/g, '')); |
| 768 | //Remove null byte and parse |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 769 | |
| 770 | console.log("Filter categories:", raw); |
| 771 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 772 | $.each(raw, function(index, object) { |
| 773 | //Unpack list of objects |
| 774 | $.each(object, function(category, searchOptions) { |
| 775 | //Unpack category from object (We don't know what it is called) |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 776 | //Create the category |
| 777 | var e = $('<li><a href="#">' + category.replace(/_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>'); |
| 778 | |
| 779 | var sub = e.find('ul.subnav'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 780 | $.each(searchOptions, function(index, name) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 781 | //Create the filter list inside the category |
| 782 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 783 | sub.append(item); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 784 | item.click(function() { |
| 785 | //Click on the side menu filters |
| 786 | if (item.hasClass('active')) { |
| 787 | //Does the filter already exist? |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 788 | item.removeClass('active'); |
| 789 | scope.filters.find(':contains(' + category + ':' + name + ')').remove(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 790 | } else { |
| 791 | //Add a filter |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 792 | item.addClass('active'); |
| 793 | var filter = $('<span class="label label-default"></span>'); |
| 794 | filter.text(category + ':' + name); |
| 795 | |
| 796 | scope.filters.append(filter); |
| 797 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 798 | filter.click(function() { |
| 799 | //Click on a filter |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 800 | filter.remove(); |
| 801 | item.removeClass('active'); |
| 802 | }); |
| 803 | } |
| 804 | |
| 805 | }); |
| 806 | }); |
| 807 | |
| 808 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 809 | e.find('> a').click(function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 810 | scope.categories.find('.subnav').slideUp(); |
| 811 | var t = $(this).siblings('.subnav'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 812 | if (!t.is(':visible')) { |
| 813 | //If the sub menu is not visible |
| 814 | t.slideDown(function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 815 | t.triggerHandler('focus'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 816 | }); |
| 817 | //Make it visible and look at it. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 818 | } |
| 819 | }); |
| 820 | |
| 821 | scope.categories.append(e); |
| 822 | |
| 823 | }); |
| 824 | }); |
| 825 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 826 | }, function(interest) { |
| 827 | //Timeout |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 828 | scope.createAlert("Failed to initialize the filters!", "alert-danger"); |
| 829 | console.error("Failed to initialize filters!", interest); |
| 830 | ga('send', 'event', 'error', 'filters'); |
| 831 | }); |
| 832 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 833 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 834 | |
| 835 | /** |
| 836 | * This function retrieves all segments in order until it knows it has reached the last one. |
| 837 | * It then returns the final joined result. |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 838 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 839 | * @param prefix {String|Name} The ndn name we are retrieving. |
| 840 | * @param callback {function(String)} if successful, will call the callback with a string of data. |
| 841 | * @param failure {function(Interest)} if unsuccessful, will call failure with the last failed interest. |
| 842 | * @param stop {boolean} stop if no finalBlock. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 843 | */ |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 844 | Atmos.prototype.getAll = function(prefix, callback, failure, stop) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 845 | |
| 846 | var scope = this; |
| 847 | var d = []; |
| 848 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 849 | var name = new Name(prefix); |
| 850 | var segment = 0; |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 851 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 852 | var request = function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 853 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 854 | var n2 = new Name(name); |
| 855 | n2.appendSegment(segment); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 856 | |
Tyler Scott | f95a97c | 2016-06-21 12:09:22 -0600 | [diff] [blame^] | 857 | scope.expressInterest(n2, handleData, function(err, interest){failure(interest)}); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 858 | //Forward to handleData and ignore error |
| 859 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 860 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 861 | var handleData = function(interest, data) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 862 | |
| 863 | d.push(data.getContent().toString()); |
| 864 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 865 | var hasFinalBlock = data.getMetaInfo().getFinalBlockId().value.length === 0; |
| 866 | var finalBlockStop = hasFinalBlock && stop; |
| 867 | |
| 868 | if (finalBlockStop || |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 869 | (!hasFinalBlock && interest.getName().get(-1).toSegment() == data.getMetaInfo().getFinalBlockId().toSegment())) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 870 | callback(d.join("")); |
| 871 | } else { |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 872 | segment++; |
| 873 | request(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 874 | } |
| 875 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 876 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 877 | |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 878 | request(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 879 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 880 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 881 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 882 | Atmos.prototype.cleanRequestForm = function() { |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 883 | $('#requestDest').prev().removeClass('btn-success').addClass('btn-default'); |
| 884 | $('#requestDropText').text('Destination'); |
| 885 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 886 | $('#subsetMenu').attr('class', 'collapse'); |
| 887 | $('#subsetVariables').empty(); |
| 888 | $('#request .alert').alert('close').remove(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 889 | } |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 890 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 891 | Atmos.prototype.setupRequestForm = function() { |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 892 | |
| 893 | var scope = this; |
| 894 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 895 | this.requestForm.find('#requestCancel').click(function() { |
| 896 | $('#request').unbind('submit')//Removes all event handlers. |
| 897 | .modal('hide'); |
| 898 | //Hides the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 899 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 900 | }); |
| 901 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 902 | var dests = $(this.config['retrieval']['destinations'].reduce(function(prev, current) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 903 | prev.push('<li><a href="#">'); |
| 904 | prev.push(current); |
| 905 | prev.push("</a></li>"); |
| 906 | return prev; |
| 907 | }, []).join("")); |
| 908 | |
| 909 | this.requestForm.find('#requestDest').append(dests) |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 910 | .on('click', 'a', function(e) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 911 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 912 | var t = $(this); |
| 913 | t.parent().addClass('active'); |
| 914 | $('#requestDropText').text(t.text()); |
| 915 | $('#requestDest').prev().removeClass('btn-default').addClass('btn-success'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 916 | }); |
| 917 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 918 | var addVariable = function(selector) { |
| 919 | var ele = $(selector).clone().attr('id', ''); |
| 920 | ele.find('.close').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 921 | ele.remove(); |
| 922 | }); |
| 923 | $('#subsetVariables').append(ele); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 924 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 925 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 926 | $('#subsetAddVariableBtn').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 927 | addVariable('#customTemplate'); |
| 928 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 929 | $('#subsetAddTimeVariable').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 930 | addVariable('#timeTemplate'); |
| 931 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 932 | $('#subsetAddLocVariable').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 933 | addVariable('#locationTemplate'); |
| 934 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 935 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 936 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 937 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 938 | Atmos.prototype.getMetaData = (function() { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 939 | |
| 940 | var cache = {}; |
| 941 | |
| 942 | return function(element) { |
| 943 | var name = $(element).text(); |
| 944 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 945 | ga('send', 'event', 'request', 'metaData'); |
| 946 | |
| 947 | var subsetButton = '<button class="btn btn-default subsetButton" type="button">Subset</button>'; |
| 948 | |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 949 | if (cache[name]) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 950 | return [subsetButton, '<pre class="metaData">', cache[name], '</pre>'].join(''); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | var prefix = new Name(name).append("metadata"); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 954 | var id = guid(); |
| 955 | //We need an id because the return MUST be a string. |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 956 | var ret = '<div id="' + id + '"><span class="fa fa-spinner fa-spin"></span></div>'; |
| 957 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 958 | this.getAll(prefix, function(data) { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 959 | var el = $('<pre class="metaData"></pre>'); |
| 960 | el.text(data); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 961 | var container = $('<div></div>'); |
| 962 | container.append($(subsetButton)); |
| 963 | container.append(el); |
| 964 | $('#' + id).empty().append(container); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 965 | cache[name] = data; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 966 | }, function(interest) { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 967 | $('#' + id).text("The metadata is unavailable for this name."); |
| 968 | console.log("Data is unavailable for " + name); |
| 969 | }); |
| 970 | |
| 971 | return ret; |
| 972 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 973 | } |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 974 | })(); |
| 975 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 976 | return Atmos; |
| 977 | |
| 978 | })(); |