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) { |
| 121 | return field + element + (last ? "/" : ""); |
| 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) { |
| 211 | return (path == "/" ? "/" : "") + element + "/"; |
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 | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 349 | prev.push('<tr><td><input class="resultSelector" type="checkbox"></td><td class="popover-container"><a href="#" class="metaDataLink">'); |
| 350 | prev.push(current); |
| 351 | prev.push('</a></td></tr>'); |
| 352 | return prev; |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 353 | }, ['<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] | 354 | resultDOM.find('#resultSelectAll').click(function() { |
| 355 | if ($(this).is(':checked')) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 356 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', true); |
| 357 | } else { |
| 358 | resultDOM.find('.resultSelector:not([disabled])').prop('checked', false); |
| 359 | } |
| 360 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 361 | this.resultTable.hide().empty().append(resultDOM).slideDown('slow'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 362 | this.resultMenu.find('.pageNumber').text(resultIndex + 1); |
| 363 | this.resultMenu.find('.pageLength').text(this.resultsPerPage * resultIndex + results.length); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 364 | if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) { |
| 365 | this.resultMenu.find('.next').addClass('disabled'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 366 | } else if (resultIndex === 0) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 367 | this.resultMenu.find('.next').removeClass('disabled'); |
| 368 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 369 | if (resultIndex === 0) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 370 | this.resultMenu.find('.previous').addClass('disabled'); |
| 371 | } else if (resultIndex === 1) { |
| 372 | this.resultMenu.find('.previous').removeClass('disabled'); |
| 373 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 374 | $.scrollTo("#results", 500, { |
| 375 | interrupt: true |
| 376 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 377 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 378 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 379 | Atmos.prototype.getResults = function(index) { |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 380 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 381 | var scope = this; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 382 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 383 | if ($('#results').hasClass('hidden')) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 384 | $('#results').removeClass('hidden').slideDown(); |
| 385 | } |
| 386 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 387 | 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] | 388 | //console.log("We already have index", index); |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 389 | scope.page = index; |
| 390 | scope.showResults(index); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 394 | if (scope.name === null ) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 395 | console.error("This shouldn't be reached! We are getting results before a search has occured!"); |
| 396 | throw new Error("Illegal State"); |
| 397 | } |
| 398 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 399 | var interestName = new Name(scope.name); |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 400 | // Interest name should be /<catalog-prefix>/query/<query-param>/<version>/<#seq> |
| 401 | if (scope.name.size() === (scope.catalogPrefix.size() + 3)) { |
| 402 | interestName = interestName.appendSegment(scope.retrievedSegments++); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 403 | //console.log("Requesting data index: (", scope.retrievedSegments - 1, ") at ", interestName.toUri()); |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame] | 404 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 405 | |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 406 | this.expressInterest(interestName, function(interest, data) { |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 407 | //Response |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 408 | if (data.getContent().length === 0) { |
| 409 | scope.resultMenu.find('.totalResults').text(0); |
| 410 | scope.resultMenu.find('.pageNumber').text(0); |
| 411 | scope.resultMenu.find('.pageLength').text(0); |
| 412 | console.log("Empty response."); |
| 413 | scope.resultTable.html("<tr><td>Empty response. This usually means no results.</td></tr>"); |
| 414 | return; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 415 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 416 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 417 | var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")); |
| 418 | |
| 419 | if (!content.results) { |
| 420 | scope.resultMenu.find('.totalResults').text(0); |
| 421 | scope.resultMenu.find('.pageNumber').text(0); |
| 422 | scope.resultMenu.find('.pageLength').text(0); |
| 423 | console.log("No results were found!"); |
| 424 | scope.resultTable.html("<tr><td>No Results</td></tr>"); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | scope.results = scope.results.concat(content.results); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 429 | scope.resultCount = content.resultCount; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 430 | scope.resultMenu.find('.totalResults').text(scope.resultCount); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 431 | scope.page = index; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 432 | // reset scope.name |
| 433 | scope.name = new Name(data.getName().getPrefix(scope.catalogPrefix.size() + 3)); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 434 | scope.getResults(index); |
| 435 | //Keep calling this until we have enough data. |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 436 | }, function() {});//Ignore failure |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 437 | |
| 438 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 439 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 440 | var queryPrefix = new Name(prefix); |
| 441 | queryPrefix.append("query"); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 442 | var jsonString = JSON.stringify(parameters); |
| 443 | queryPrefix.append(jsonString); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 444 | this.expressInterest(queryPrefix, callback, timeout); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 445 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 446 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 447 | Atmos.prototype.expressInterest = function(name, success, failure) { |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 448 | var interest = new Interest(name); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 449 | interest.setInterestLifetimeMilliseconds(500); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 450 | interest.setMustBeFresh(true); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 451 | const face = this.face; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 452 | async.retry(4, function(done) { |
| 453 | face.expressInterest(interest, function(interest, data) { |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 454 | done(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 455 | success(interest, data); |
| 456 | }, function(interest) { |
| 457 | done("Interest timed out 4 times.", interest); |
| 458 | }); |
| 459 | }, function(err, interest) { |
| 460 | if (err) { |
| 461 | console.log(err, interest); |
| 462 | failure(interest); |
Tyler Scott | 8724e42 | 2015-10-13 17:59:07 -0600 | [diff] [blame] | 463 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 464 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 465 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * This function returns a map of all the categories active filters. |
| 469 | * @return {Object<string, string>} |
| 470 | */ |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 471 | Atmos.prototype.getFilters = function() { |
| 472 | var filters = this.filters.children().toArray().reduce(function(prev, current) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 473 | var data = $(current).text().split(/:/); |
| 474 | prev[data[0]] = data[1]; |
| 475 | return prev; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 476 | }, {}); |
| 477 | //Collect a map<category, filter>. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 478 | //TODO Make the return value map<category, Array<filter>> |
| 479 | return filters; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 480 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 481 | |
| 482 | /** |
| 483 | * Creates a closable alert for the user. |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 484 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 485 | * @param {string} message |
| 486 | * @param {string} type - Override the alert type. |
| 487 | */ |
| 488 | Atmos.prototype.createAlert = function(message, type) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 489 | var alert = $('<div class="alert"><div>'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 490 | alert.addClass(type ? type : 'alert-info'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 491 | alert.text(message); |
| 492 | alert.append(closeButton); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 493 | this.alerts.append(alert); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 494 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 495 | |
| 496 | /** |
| 497 | * 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] | 498 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 499 | * @param elements {Array<jQuery>} A list of the table row elements |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 500 | * @param subsetFileName {String} If present then do a subsetting request instead. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 501 | */ |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 502 | Atmos.prototype.request = function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 503 | //Pseudo globals. |
| 504 | var keyChain; |
| 505 | var certificateName; |
| 506 | var keyAdded = false; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 507 | return function(elements, subsetFilename) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 508 | |
| 509 | var names = []; |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 510 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 511 | $(elements).find('.metaDataLink').each(function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 512 | var name = $(this).text(); |
| 513 | names.push(name); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 514 | }); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 515 | var subset = false; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 516 | if (!subsetFilename) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 517 | $('#subsetting').hide(); |
| 518 | } else { |
| 519 | $('#subsetting').show(); |
| 520 | subset = true; |
| 521 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 522 | |
| 523 | var scope = this; |
Tyler Scott | 384f37a | 2016-05-05 16:09:05 -0600 | [diff] [blame] | 524 | |
| 525 | //FIXME The following is temporary, it allows people to direct download from |
| 526 | //a single host with a small set of names. It is to demo the functionality but |
| 527 | //could use improvement. (Multiple servers, non static list, etc) |
| 528 | var directDls = $('#directDownloadList').empty(); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 529 | names.forEach(function(name) { |
| 530 | if (scope.conversions[name]) { |
| 531 | //If the name exists in the conversions. |
Tyler Scott | 384f37a | 2016-05-05 16:09:05 -0600 | [diff] [blame] | 532 | var ele = $('<a href="http://atmos-mwsc.ucar.edu/ucar/' + conversions[name] + '" class="list-group-item>' + name + '</a>'); |
| 533 | directDls.append(ele); |
| 534 | } |
| 535 | }); |
| 536 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 537 | this.requestForm.on('submit', function(e) { |
| 538 | //This will be registered for the next submit from the form. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 539 | e.preventDefault(); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 540 | $('#request .alert').remove(); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 541 | var variables = []; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 542 | if (subset) { |
| 543 | $('#subsetVariables .row').each(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 544 | var t = $(this); |
| 545 | var values = {}; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 546 | t.find('.values input').each(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 547 | var t = $(this); |
| 548 | values[t.attr('name')] = t.val(); |
| 549 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 550 | variables.push({ |
| 551 | variable: t.find('.variable').val(), |
| 552 | values: values |
| 553 | }); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 554 | }); |
| 555 | } |
| 556 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 557 | //Form checking |
| 558 | var dest = scope.requestForm.find('#requestDest .active'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 559 | if (dest.length !== 1) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 560 | var alert = $('<div class="alert alert-warning">A destination is required!' + closeButton + '<div>'); |
| 561 | $('#request > .panel-body').append(alert); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 565 | $('#request').modal('hide'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 566 | |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 567 | //Initial params are ok. We can close the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 568 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 569 | |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 570 | $(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] | 571 | |
| 572 | //Key setup |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 573 | if (!keyAdded) { |
| 574 | 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] | 575 | scope.createAlert("This host was not configured to handle retrieval! See console for details.", 'alert-danger'); |
| 576 | console.error("Missing/invalid key! This must be configured in the config on the server.", scope.config.demoKey); |
| 577 | return; |
| 578 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 579 | //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] | 580 | var pub = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.pub)); |
| 581 | //MUST be a Buffer (Buffer != Uint8Array) |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 582 | var priv = new Buffer(base64.toByteArray(scope.config.retrieval.demoKey.priv)); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 583 | var identityStorage = new MemoryIdentityStorage(); |
| 584 | var privateKeyStorage = new MemoryPrivateKeyStorage(); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 585 | keyChain = new KeyChain(new IdentityManager(identityStorage,privateKeyStorage),new SelfVerifyPolicyManager(identityStorage)); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 586 | var keyName = new Name("/retrieve/DSK-123"); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 587 | 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] | 588 | identityStorage.addKey(keyName, KeyType.RSA, new Blob(pub,false)); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 589 | privateKeyStorage.setKeyPairForKeyName(keyName, KeyType.RSA, pub, priv); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 590 | scope.face.setCommandSigningInfo(keyChain, certificateName); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 591 | keyAdded = true; |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | //Retrieval |
| 595 | var retrievePrefix = new Name("/catalog/ui/" + guid()); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 596 | scope.face.registerPrefix(retrievePrefix, function(prefix, interest, face, interestFilterId, filter) { |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 597 | //On Interest |
| 598 | //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] | 599 | var data = new Data(interest.getName()); |
| 600 | var content; |
| 601 | if (subset) { |
| 602 | content = JSON.stringify({ |
| 603 | name: subsetFilename, |
| 604 | subset: variables |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 605 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 606 | } else { |
| 607 | content = JSON.stringify(names); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 608 | } |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 609 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 610 | //Blob breaks the data! Don't use it |
| 611 | data.setContent(content); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 612 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 613 | //TODO Packetize this. |
| 614 | keyChain.sign(data, certificateName); |
| 615 | |
| 616 | try { |
| 617 | face.putData(data); |
| 618 | console.log("Responded for", interest.getName().toUri(), data); |
| 619 | scope.createAlert("Data retrieval has initiated.", "alert-success"); |
| 620 | } catch (e) { |
| 621 | console.error("Failed to respond to", interest.getName().toUri(), data); |
| 622 | scope.createAlert("Data retrieval failed."); |
| 623 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 624 | }, function(prefix) { |
| 625 | //On fail |
| 626 | scope.createAlert("Failed to register the retrieval URI! See console for details.", "alert-danger"); |
| 627 | console.error("Failed to register URI:", prefix.toUri(), prefix); |
| 628 | }, function(prefix, registeredPrefixId) { |
| 629 | //On success |
| 630 | var name = new Name(dest.text()); |
| 631 | name.append(prefix); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 632 | scope.expressInterest(name, function(interest, data) { |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 633 | //Success |
| 634 | console.log("Request for", name.toUri(), "succeeded.", interest, data); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 635 | }, function() { |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 636 | console.warn("Failed to request from retrieve agent."); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 637 | }); |
| 638 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 639 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 640 | $('#request').modal(); |
| 641 | //This forces the form to be the only option. |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 642 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 643 | }(); |
| 644 | |
| 645 | Atmos.prototype.filterSetup = function() { |
| 646 | //Filter setup |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 647 | var prefix = new Name(this.catalog).append("filters-initialization"); |
| 648 | |
| 649 | var scope = this; |
| 650 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 651 | this.getAll(prefix, function(data) { |
| 652 | //Success |
| 653 | var raw = JSON.parse(data.replace(/[\n\0]/g, '')); |
| 654 | //Remove null byte and parse |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 655 | console.log("Filter categories:", raw); |
| 656 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 657 | $.each(raw, function(index, object) { |
| 658 | //Unpack list of objects |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 659 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 660 | $.each(object, function(category, searchOptions) { |
| 661 | //Unpack category from object (We don't know what it is called) |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 662 | //Create the category |
| 663 | 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] | 664 | var sub = e.find('ul.subnav'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 665 | $.each(searchOptions, function(index, name) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 666 | //Create the filter list inside the category |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 667 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 668 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 669 | sub.append(item); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 670 | item.click(function() { |
| 671 | //Click on the side menu filters |
| 672 | if (item.hasClass('active')) { |
| 673 | //Does the filter already exist? |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 674 | item.removeClass('active'); |
| 675 | scope.filters.find(':contains(' + category + ':' + name + ')').remove(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 676 | } else { |
| 677 | //Add a filter |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 678 | item.addClass('active'); |
| 679 | var filter = $('<span class="label label-default"></span>'); |
| 680 | filter.text(category + ':' + name); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 681 | scope.filters.append(filter); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 682 | filter.click(function() { |
| 683 | //Click on a filter |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 684 | filter.remove(); |
| 685 | item.removeClass('active'); |
| 686 | }); |
| 687 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 688 | }); |
| 689 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 690 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 691 | e.find('> a').click(function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 692 | scope.categories.find('.subnav').slideUp(); |
| 693 | var t = $(this).siblings('.subnav'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 694 | if (!t.is(':visible')) { |
| 695 | //If the sub menu is not visible |
| 696 | t.slideDown(function() { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 697 | t.triggerHandler('focus'); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 698 | }); |
| 699 | //Make it visible and look at it. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 700 | } |
| 701 | }); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 702 | scope.categories.append(e); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 703 | }); |
| 704 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 705 | }, function(interest) { |
| 706 | //Timeout |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 707 | scope.createAlert("Failed to initialize the filters!", "alert-danger"); |
| 708 | console.error("Failed to initialize filters!", interest); |
| 709 | ga('send', 'event', 'error', 'filters'); |
| 710 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 711 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 712 | |
| 713 | /** |
| 714 | * This function retrieves all segments in order until it knows it has reached the last one. |
| 715 | * It then returns the final joined result. |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 716 | |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 717 | * @param prefix {String|Name} The ndn name we are retrieving. |
| 718 | * @param callback {function(String)} if successful, will call the callback with a string of data. |
| 719 | * @param failure {function(Interest)} if unsuccessful, will call failure with the last failed interest. |
| 720 | * @param stop {boolean} stop if no finalBlock. |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 721 | */ |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 722 | Atmos.prototype.getAll = function(prefix, callback, failure, stop) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 723 | var scope = this; |
| 724 | var d = []; |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 725 | var name = new Name(prefix); |
| 726 | var segment = 0; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 727 | var request = function() { |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 728 | var n2 = new Name(name); |
| 729 | n2.appendSegment(segment); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 730 | scope.expressInterest(n2, handleData, function(err, interest) { |
| 731 | failure(interest) |
| 732 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 733 | //Forward to handleData and ignore error |
| 734 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 735 | var handleData = function(interest, data) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 736 | d.push(data.getContent().toString()); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 737 | var hasFinalBlock = data.getMetaInfo().getFinalBlockId().value.length === 0; |
| 738 | var finalBlockStop = hasFinalBlock && stop; |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 739 | if (finalBlockStop || (!hasFinalBlock && interest.getName().get(-1).toSegment() == data.getMetaInfo().getFinalBlockId().toSegment())) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 740 | callback(d.join("")); |
| 741 | } else { |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 742 | segment++; |
| 743 | request(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 744 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 745 | } |
Tyler Scott | d61bf83 | 2015-11-30 16:36:17 -0700 | [diff] [blame] | 746 | request(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 747 | } |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 748 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 749 | Atmos.prototype.cleanRequestForm = function() { |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 750 | $('#requestDest').prev().removeClass('btn-success').addClass('btn-default'); |
| 751 | $('#requestDropText').text('Destination'); |
| 752 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 753 | $('#subsetMenu').attr('class', 'collapse'); |
| 754 | $('#subsetVariables').empty(); |
| 755 | $('#request .alert').alert('close').remove(); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 756 | } |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 757 | |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 758 | Atmos.prototype.setupRequestForm = function() { |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 759 | var scope = this; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 760 | this.requestForm.find('#requestCancel').click(function() { |
| 761 | $('#request').unbind('submit')//Removes all event handlers. |
| 762 | .modal('hide'); |
| 763 | //Hides the form. |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 764 | scope.cleanRequestForm(); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 765 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 766 | var dests = $(this.config['retrieval']['destinations'].reduce(function(prev, current) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 767 | prev.push('<li><a href="#">'); |
| 768 | prev.push(current); |
| 769 | prev.push("</a></li>"); |
| 770 | return prev; |
| 771 | }, []).join("")); |
Tyler Scott | 7ae6be0 | 2016-06-22 15:29:26 -0600 | [diff] [blame^] | 772 | this.requestForm.find('#requestDest').append(dests).on('click', 'a', function(e) { |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 773 | $('#requestDest .active').removeClass('active'); |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 774 | var t = $(this); |
| 775 | t.parent().addClass('active'); |
| 776 | $('#requestDropText').text(t.text()); |
| 777 | $('#requestDest').prev().removeClass('btn-default').addClass('btn-success'); |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 778 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 779 | var addVariable = function(selector) { |
| 780 | var ele = $(selector).clone().attr('id', ''); |
| 781 | ele.find('.close').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 782 | ele.remove(); |
| 783 | }); |
| 784 | $('#subsetVariables').append(ele); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 785 | } |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 786 | $('#subsetAddVariableBtn').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 787 | addVariable('#customTemplate'); |
| 788 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 789 | $('#subsetAddTimeVariable').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 790 | addVariable('#timeTemplate'); |
| 791 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 792 | $('#subsetAddLocVariable').click(function() { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 793 | addVariable('#locationTemplate'); |
| 794 | }); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 795 | } |
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 | Atmos.prototype.getMetaData = (function() { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 798 | var cache = {}; |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 799 | return function(element) { |
| 800 | var name = $(element).text(); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 801 | ga('send', 'event', 'request', 'metaData'); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 802 | var subsetButton = '<button class="btn btn-default subsetButton" type="button">Subset</button>'; |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 803 | if (cache[name]) { |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 804 | return [subsetButton, '<pre class="metaData">', cache[name], '</pre>'].join(''); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 805 | } |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 806 | var prefix = new Name(name).append("metadata"); |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 807 | var id = guid(); |
| 808 | //We need an id because the return MUST be a string. |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 809 | 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] | 810 | this.getAll(prefix, function(data) { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 811 | var el = $('<pre class="metaData"></pre>'); |
| 812 | el.text(data); |
Tyler Scott | f355e45 | 2015-11-06 21:35:14 -0700 | [diff] [blame] | 813 | var container = $('<div></div>'); |
| 814 | container.append($(subsetButton)); |
| 815 | container.append(el); |
| 816 | $('#' + id).empty().append(container); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 817 | cache[name] = data; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 818 | }, function(interest) { |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 819 | $('#' + id).text("The metadata is unavailable for this name."); |
| 820 | console.log("Data is unavailable for " + name); |
| 821 | }); |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 822 | return ret; |
Tyler Scott | 66a965d | 2016-02-29 15:41:33 -0700 | [diff] [blame] | 823 | } |
Tyler Scott | 48f92cd | 2015-10-16 18:31:20 -0600 | [diff] [blame] | 824 | })(); |
| 825 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 826 | return Atmos; |
| 827 | |
| 828 | })(); |