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