blob: d3bde8f1e6cf67a715fce11c51a4428828d101dc [file] [log] [blame]
Tyler Scott696d38f2015-08-04 22:41:22 -06001//Run when the document loads AND we have the config loaded.
2(function(){
3 var catalog = null;
4 var config = null;
5 Promise.all([
6 new Promise(function(resolve, reject){
7 $.ajax('config.json').done(function(data){
8 catalog = data.catalogPrefix;
9 config = data.faceConfig;
10 resolve();
11 }).fail(function(){
12 console.error("Failed to get config.");
13 reject()
14 });
15 }),
16 new Promise(function(resolve, reject){
17 var timeout = setTimeout(function(){
18 console.error("Document never loaded? Something bad has happened!");
19 reject();
20 }, 10000);
21 $(function () {
22 clearTimeout(timeout);
23 resolve();
24 });
25 })
26 ]).then(function(){
27 new Atmos(catalog, config);
28 }, function(){
29 console.error("Failed to initialize!");
30 })
31})();
Tyler Scott3c17d5f2015-06-23 17:49:29 -060032
Tyler Scott93cae872015-07-21 14:58:23 -060033var Atmos = (function(){
Tyler Scott7d076e22015-07-06 19:21:50 -060034 "use strict";
Tyler Scott93cae872015-07-21 14:58:23 -060035 /**
36 * Atmos
37 * @version 2.0
Tyler Scott4d951222015-07-31 14:38:27 -060038 *
Tyler Scott93cae872015-07-21 14:58:23 -060039 * Configures an Atmos object. This manages the atmos interface.
Tyler Scott4d951222015-07-31 14:38:27 -060040 *
41 * @constructor
Tyler Scott93cae872015-07-21 14:58:23 -060042 * @param {string} catalog - NDN path
Tyler Scott4d951222015-07-31 14:38:27 -060043 * @param {Object} config - Object of configuration options for a Face.
Tyler Scott93cae872015-07-21 14:58:23 -060044 */
45 function Atmos(catalog, config){
Tyler Scott7d076e22015-07-06 19:21:50 -060046
Tyler Scott93cae872015-07-21 14:58:23 -060047 //Internal variables.
48 this.results = [];
Tyler Scott4e4865a2015-08-06 14:23:21 -060049 this.resultCount = Infinity;
Tyler Scottc55879f2015-07-28 14:56:37 -060050 this.name = null;
51 this.page = 0;
Tyler Scott4e4865a2015-08-06 14:23:21 -060052 this.resultsPerPage = 25;
53 this.retrievedSegments = 0;
Tyler Scotte815d3e2015-07-09 16:56:17 -060054
Tyler Scott93cae872015-07-21 14:58:23 -060055 this.catalog = catalog;
Tyler Scott7d076e22015-07-06 19:21:50 -060056
Tyler Scott93cae872015-07-21 14:58:23 -060057 this.face = new Face(config);
Tyler Scott696d38f2015-08-04 22:41:22 -060058
Tyler Scott93cae872015-07-21 14:58:23 -060059 this.categories = $('#side-menu');
60 this.resultTable = $('#resultTable');
61 this.filters = $('#filters');
Tyler Scotta1530052015-07-24 00:13:28 -060062 this.searchInput = $('#search');
63 this.searchBar = $('#searchBar');
Tyler Scottc55879f2015-07-28 14:56:37 -060064 this.searchButton = $('#searchButton');
65 this.pagers = $('.pager');
Tyler Scott4d951222015-07-31 14:38:27 -060066 this.alerts = $('#alerts');
Tyler Scott7d076e22015-07-06 19:21:50 -060067
Tyler Scott93cae872015-07-21 14:58:23 -060068 var scope = this;
Tyler Scott7d076e22015-07-06 19:21:50 -060069
Tyler Scott93cae872015-07-21 14:58:23 -060070 this.resultTable.on('click', '.interest-button', function(){
71 var button = $(this);
Tyler Scott087aef72015-07-14 14:11:59 -060072
Tyler Scott93cae872015-07-21 14:58:23 -060073 if (button.is(':disabled')){
74 console.warn("Attempt to request again!");
Tyler Scott7d076e22015-07-06 19:21:50 -060075 }
Tyler Scott93cae872015-07-21 14:58:23 -060076
77 var name = button.parent().prev().text();
78 var interest = new Interest(new Name('/retrieve' + name));
79 scope.face.expressInterest(interest, function(){}, function(){});
80
81 button.text("Requested!")
82 .removeClass('btn-primary')
Tyler Scott696d38f2015-08-04 22:41:22 -060083 .addClass('btn-success')
Tyler Scott93cae872015-07-21 14:58:23 -060084 .addClass('disabled')
85 .prop('disabled', true);
Tyler Scott7d076e22015-07-06 19:21:50 -060086 });
Tyler Scott7d076e22015-07-06 19:21:50 -060087
Tyler Scotta1530052015-07-24 00:13:28 -060088 //Filter setup
Tyler Scott93cae872015-07-21 14:58:23 -060089 $.getJSON("search_catagories.json").done(function (data) {
90 $.each(data, function (pageSection, contents) {
91 if (pageSection == "SearchCatagories") {
Tyler Scotta1530052015-07-24 00:13:28 -060092 $.each(contents, function (category, searchOptions) {
93 //Create the category
94 var e = $('<li><a href="#">' + category.replace(/\_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>');
Tyler Scott7d076e22015-07-06 19:21:50 -060095
Tyler Scott93cae872015-07-21 14:58:23 -060096 var sub = e.find('ul.subnav');
97 $.each(searchOptions, function(index, name){
Tyler Scotta1530052015-07-24 00:13:28 -060098 //Create the filter list inside the category
Tyler Scott93cae872015-07-21 14:58:23 -060099 var item = $('<li><a href="#">' + name + '</a></li>');
100 sub.append(item);
Tyler Scotta1530052015-07-24 00:13:28 -0600101 item.click(function(){ //Click on the side menu filters
102 if (item.hasClass('active')){ //Does the filter already exist?
103 item.removeClass('active');
104 scope.filters.find(':contains(' + category + ':' + name + ')').remove();
105 } else { //Add a filter
106 item.addClass('active');
107 var filter = $('<span class="label label-default"></span>');
108 filter.text(category + ':' + name);
109
110 scope.filters.append(filter);
111
112 filter.click(function(){ //Click on a filter
113 filter.remove();
114 item.removeClass('active');
115 });
116 }
Tyler Scott4d951222015-07-31 14:38:27 -0600117
Tyler Scott93cae872015-07-21 14:58:23 -0600118 });
119 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600120
Tyler Scott93cae872015-07-21 14:58:23 -0600121 //Toggle the menus. (Only respond when the immediate tab is clicked.)
122 e.find('> a').click(function(){
123 scope.categories.find('.subnav').slideUp();
124 var t = $(this).siblings('.subnav');
Tyler Scotta1530052015-07-24 00:13:28 -0600125 if ( !t.is(':visible') ){ //If the sub menu is not visible
126 t.slideDown(function(){
127 t.triggerHandler('focus');
128 }); //Make it visible and look at it.
Tyler Scott93cae872015-07-21 14:58:23 -0600129 }
130 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600131
Tyler Scott93cae872015-07-21 14:58:23 -0600132 scope.categories.append(e);
Tyler Scotta1530052015-07-24 00:13:28 -0600133
Tyler Scott93cae872015-07-21 14:58:23 -0600134 });
135 }
136 });
137 });
138
Tyler Scottc55879f2015-07-28 14:56:37 -0600139 this.searchInput.autoComplete(function(field, callback){
140 scope.autoComplete(field, callback);
141 });
142
Tyler Scotta1530052015-07-24 00:13:28 -0600143 this.searchBar.submit(function(e){
Tyler Scott93cae872015-07-21 14:58:23 -0600144 e.preventDefault();
Tyler Scott6b810812015-08-11 18:20:05 -0600145 scope.pathSearch();
Tyler Scottc55879f2015-07-28 14:56:37 -0600146 });
147
148 this.searchButton.click(function(){
149 console.log("Search Button Pressed");
Tyler Scotta1530052015-07-24 00:13:28 -0600150 scope.search();
151 });
152
Tyler Scottc55879f2015-07-28 14:56:37 -0600153 this.pagers.find('.next').click(function(){
154 if (!$(this).hasClass('disabled')){
155 scope.getResults(scope.page + 1);
156 }
157 });
158 this.pagers.find('.previous').click(function(){
159 if (!$(this).hasClass('disabled')){
160 scope.getResults(scope.page - 1);
161 }
Tyler Scotta1530052015-07-24 00:13:28 -0600162 });
Tyler Scott4e4865a2015-08-06 14:23:21 -0600163 this.pagers.find('.pageLength').attr('contentEditable', true)
164 .blur(function(){
165 scope.resultsPerPage = Number($(this).text());
166 scope.pagers.find('.pageLength').text(scope.resultsPerPage);
167 scope.getResults(0); //Reset page to 0;
168 });
Tyler Scotta1530052015-07-24 00:13:28 -0600169
170 }
171
Tyler Scott6b810812015-08-11 18:20:05 -0600172 Atmos.prototype.clearResults = function(){
173 this.results = []; //Drop any old results.
174 this.retrievedSegments = 0;
175 this.resultCount = Infinity;
176 this.page = 0;
177 this.resultTable.empty();
178 }
179
180 Atmos.prototype.pathSearch = function(){
181 var value = this.searchInput.val();
182
183 this.clearResults();
184
185 var scope = this;
186
187 this.query(this.catalog, {"??": value},
188 function(interest, data){
189 console.log("Query response:", interest, data);
190
191 var parameters = JSON.stringify({"??": value});
192
193 var ack = data.getName();
194
195 scope.name = new Name(scope.catalog).append("query-results").append(parameters).append(ack.get(-3)).append(ack.get(-2));
196
197 scope.getResults(0);
198
199 },
200 function(interest){
201 console.warn("Request failed! Timeout", interest);
202 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
203 });
204
205 }
206
Tyler Scotta1530052015-07-24 00:13:28 -0600207 Atmos.prototype.search = function(){
Tyler Scotta1530052015-07-24 00:13:28 -0600208
Tyler Scottc55879f2015-07-28 14:56:37 -0600209 var filters = this.getFilters();
Tyler Scotta1530052015-07-24 00:13:28 -0600210
211 console.log("Search started!", this.searchInput.val(), filters);
212
Tyler Scott4d951222015-07-31 14:38:27 -0600213 console.log("Initiating query");
Tyler Scotta1530052015-07-24 00:13:28 -0600214
Tyler Scott6b810812015-08-11 18:20:05 -0600215 this.clearResults();
Tyler Scottc55879f2015-07-28 14:56:37 -0600216
Tyler Scottd90f84e2015-07-27 12:43:04 -0600217 var scope = this;
218
Tyler Scott4d951222015-07-31 14:38:27 -0600219 this.query(this.catalog, filters,
Tyler Scotta1530052015-07-24 00:13:28 -0600220 function(interest, data){ //Response function
221 console.log("Query Response:", interest, data);
222
Tyler Scottd90f84e2015-07-27 12:43:04 -0600223 var parameters = JSON.stringify(filters);
224
225 var ack = data.getName();
226
Tyler Scottb6e329f2015-07-28 16:08:33 -0600227 scope.name = new Name(scope.catalog).append("query-results").append(parameters).append(ack.get(-3)).append(ack.get(-2));
Tyler Scottd90f84e2015-07-27 12:43:04 -0600228
Tyler Scottc55879f2015-07-28 14:56:37 -0600229 scope.getResults(0);
Tyler Scotta1530052015-07-24 00:13:28 -0600230
231 }, function(interest){ //Timeout function
Tyler Scottb6e329f2015-07-28 16:08:33 -0600232 console.warn("Request failed! Timeout");
Tyler Scott4d951222015-07-31 14:38:27 -0600233 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600234 });
235
236 }
237
238 Atmos.prototype.autoComplete = function(field, callback){
Tyler Scotta1530052015-07-24 00:13:28 -0600239
Tyler Scottc55879f2015-07-28 14:56:37 -0600240 if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){
241 if (!this.searchBar.hasClass('has-error')){
242 this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>');
243 }
244 return;
245 } else {
246 this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()});
247 }
248
Tyler Scottb6e329f2015-07-28 16:08:33 -0600249 var scope = this;
250
Tyler Scott4d951222015-07-31 14:38:27 -0600251 this.query(this.catalog, {"?": field},
Tyler Scotta1530052015-07-24 00:13:28 -0600252 function(interest, data){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600253
254 var ack = data.getName();
255
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600256 var name = new Name(scope.catalog).append('query-results').append(JSON.stringify({"?": field})).append(ack.get(-3)).append(ack.get(-2));
Tyler Scottb6e329f2015-07-28 16:08:33 -0600257
Tyler Scottab822fe2015-08-10 14:38:49 -0600258 var interest = new Interest(name);
259 interest.setInterestLifetimeMilliseconds(5000);
260 interest.setMustBeFresh(true);
261
262 scope.face.expressInterest(interest,
Tyler Scottb6e329f2015-07-28 16:08:33 -0600263 function(interest, data){
Tyler Scott4d951222015-07-31 14:38:27 -0600264
Tyler Scottfe8e4932015-07-28 17:45:45 -0600265 if (data.getContent().length !== 0){
266 var options = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")).next.map(function(element){
Tyler Scottfc990392015-08-10 13:48:03 -0600267 return field + element + "/";
Tyler Scottfe8e4932015-07-28 17:45:45 -0600268 });
269 callback(options);
270 }
271
Tyler Scottb6e329f2015-07-28 16:08:33 -0600272 }, function(interest){
273 console.warn("Interest timed out!", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600274 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottb6e329f2015-07-28 16:08:33 -0600275 });
276
Tyler Scotta1530052015-07-24 00:13:28 -0600277 }, function(interest){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600278 console.error("Request failed! Timeout", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600279 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600280 });
Tyler Scott93cae872015-07-21 14:58:23 -0600281
Tyler Scott424ee102015-07-14 16:50:41 -0600282 }
283
Tyler Scottc55879f2015-07-28 14:56:37 -0600284 Atmos.prototype.showResults = function(resultIndex) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600285
Tyler Scott4e4865a2015-08-06 14:23:21 -0600286 var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1));
287
288 var resultDOM = $(results.reduce(function(prev, current){
Tyler Scott696d38f2015-08-04 22:41:22 -0600289 prev.push('<tr><td><input type="checkbox"></td><td>');
290 prev.push(current);
291 prev.push('</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr>');
292 return prev;
293 }, []).join(''));
Tyler Scott7d076e22015-07-06 19:21:50 -0600294
Tyler Scott4e4865a2015-08-06 14:23:21 -0600295 this.resultTable.empty().append(resultDOM);
Tyler Scott7d076e22015-07-06 19:21:50 -0600296
Tyler Scott4e4865a2015-08-06 14:23:21 -0600297 this.pagers.find('.pageNumber').text(resultIndex + 1);
Tyler Scott7d076e22015-07-06 19:21:50 -0600298
Tyler Scott4e4865a2015-08-06 14:23:21 -0600299 if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) {
Tyler Scottc55879f2015-07-28 14:56:37 -0600300 this.pagers.find('.next').addClass('disabled');
Tyler Scott4e4865a2015-08-06 14:23:21 -0600301 } else if (resultIndex === 0){
302 this.pagers.find('.next').removeClass('disabled');
Tyler Scottc55879f2015-07-28 14:56:37 -0600303 }
Tyler Scott93cae872015-07-21 14:58:23 -0600304
Tyler Scottc55879f2015-07-28 14:56:37 -0600305 if (resultIndex === 0){
Tyler Scottc55879f2015-07-28 14:56:37 -0600306 this.pagers.find('.previous').addClass('disabled');
307 } else if (resultIndex === 1) {
308 this.pagers.find('.previous').removeClass('disabled');
309 }
Tyler Scott93cae872015-07-21 14:58:23 -0600310
Tyler Scottc55879f2015-07-28 14:56:37 -0600311 }
Tyler Scott93cae872015-07-21 14:58:23 -0600312
Tyler Scottc55879f2015-07-28 14:56:37 -0600313 Atmos.prototype.getResults = function(index){
Tyler Scott93cae872015-07-21 14:58:23 -0600314
Tyler Scott4e4865a2015-08-06 14:23:21 -0600315 if ((this.results.length === this.resultCount) || (this.resultsPerPage * (index + 1) < this.results.length)){
Tyler Scottc55879f2015-07-28 14:56:37 -0600316 //console.log("We already have index", index);
317 this.page = index;
318 this.showResults(index);
319 return;
320 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600321
Tyler Scottc55879f2015-07-28 14:56:37 -0600322 if (this.name === null) {
323 console.error("This shouldn't be reached! We are getting results before a search has occured!");
324 throw new Error("Illegal State");
325 }
Tyler Scott424ee102015-07-14 16:50:41 -0600326
Tyler Scott4e4865a2015-08-06 14:23:21 -0600327 var first = new Name(this.name).appendSegment(this.retrievedSegments++);
Tyler Scottc55879f2015-07-28 14:56:37 -0600328
Tyler Scott4e4865a2015-08-06 14:23:21 -0600329 console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri());
Tyler Scottc55879f2015-07-28 14:56:37 -0600330
331 var scope = this;
332
Tyler Scottab822fe2015-08-10 14:38:49 -0600333 var interest = new Interest(first)
334 interest.setInterestLifetimeMilliseconds(5000);
335 interest.setMustBeFresh(true);
336
337 this.face.expressInterest(interest,
Tyler Scottc55879f2015-07-28 14:56:37 -0600338 function(interest, data){ //Response
339
340 if (data.getContent().length === 0){
Tyler Scott03854852015-08-07 15:45:10 -0600341 scope.pagers.find('.totalResults').text(0);
342 scope.pagers.find('.pageNumber').text(0);
Tyler Scottc55879f2015-07-28 14:56:37 -0600343 console.log("Empty response.");
344 return;
345 }
346
Tyler Scottc55879f2015-07-28 14:56:37 -0600347 var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,""));
348
Tyler Scott4e4865a2015-08-06 14:23:21 -0600349 if (!content.results){
350 scope.pagers.find('.totalResults').text(0);
351 scope.pagers.find('.pageNumber').text(0);
Tyler Scottc55879f2015-07-28 14:56:37 -0600352 console.log("No results were found!");
353 return;
354 }
355
Tyler Scott4e4865a2015-08-06 14:23:21 -0600356 scope.results = scope.results.concat(content.results);
357
358 scope.resultCount = content.resultCount;
359
360 scope.pagers.find('.totalResults').text(scope.resultCount);
361
362 scope.page = index;
363
364 scope.getResults(index); //Keep calling this until we have enough data.
Tyler Scottc55879f2015-07-28 14:56:37 -0600365
366 },
367 function(interest){ //Timeout
Tyler Scott4d951222015-07-31 14:38:27 -0600368 console.error("Failed to retrieve results: timeout", interest);
369 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottc55879f2015-07-28 14:56:37 -0600370 }
371 );
372
373 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600374
Tyler Scotta1530052015-07-24 00:13:28 -0600375 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600376
Tyler Scott93cae872015-07-21 14:58:23 -0600377 var queryPrefix = new Name(prefix);
378 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600379
Tyler Scott93cae872015-07-21 14:58:23 -0600380 var jsonString = JSON.stringify(parameters);
381 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600382
Tyler Scott93cae872015-07-21 14:58:23 -0600383 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600384 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scottab822fe2015-08-10 14:38:49 -0600385 queryInterest.setMustBeFresh(true);
Tyler Scott7d076e22015-07-06 19:21:50 -0600386
Tyler Scotta1530052015-07-24 00:13:28 -0600387 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600388
Tyler Scott93cae872015-07-21 14:58:23 -0600389 }
390
Tyler Scott93cae872015-07-21 14:58:23 -0600391 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600392 * This function returns a map of all the categories active filters.
393 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600394 */
Tyler Scotta1530052015-07-24 00:13:28 -0600395 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600396 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600397 var data = $(current).text().split(/:/);
398 prev[data[0]] = data[1];
399 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600400 }, {}); //Collect a map<category, filter>.
401 //TODO Make the return value map<category, Array<filter>>
402 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600403 }
404
Tyler Scott4d951222015-07-31 14:38:27 -0600405 /**
406 * Creates a closable alert for the user.
407 *
408 * @param {string} message
409 * @param {string} type - Override the alert type.
410 */
411 Atmos.prototype.createAlert = function(message, type) {
412
413 var alert = $('<div class="alert"><div>');
414 alert.addClass(type?type:'alert-info');
415 alert.text(message);
416 alert.append(Atmos.closeButton);
417
418 this.alerts.append(alert);
419 }
420 Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
421
Tyler Scott93cae872015-07-21 14:58:23 -0600422 return Atmos;
423
424})();