blob: 682c504a62338066bfcabc423ed5d9d9bcb38bfc [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 Scottfc990392015-08-10 13:48:03 -0600145 scope.createAlert("This feature is currently incomplete.");
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
172 Atmos.prototype.search = function(){
Tyler Scotta1530052015-07-24 00:13:28 -0600173
Tyler Scottc55879f2015-07-28 14:56:37 -0600174 var filters = this.getFilters();
Tyler Scotta1530052015-07-24 00:13:28 -0600175
176 console.log("Search started!", this.searchInput.val(), filters);
177
Tyler Scott4d951222015-07-31 14:38:27 -0600178 console.log("Initiating query");
Tyler Scotta1530052015-07-24 00:13:28 -0600179
Tyler Scottc55879f2015-07-28 14:56:37 -0600180 this.results = []; //Drop any old results.
Tyler Scott4e4865a2015-08-06 14:23:21 -0600181 this.retrievedSegments = 0;
182 this.resultCount = Infinity;
183 this.page = 0;
Tyler Scottc55879f2015-07-28 14:56:37 -0600184 this.resultTable.empty();
185
Tyler Scottd90f84e2015-07-27 12:43:04 -0600186 var scope = this;
187
Tyler Scott4d951222015-07-31 14:38:27 -0600188 this.query(this.catalog, filters,
Tyler Scotta1530052015-07-24 00:13:28 -0600189 function(interest, data){ //Response function
190 console.log("Query Response:", interest, data);
191
Tyler Scottd90f84e2015-07-27 12:43:04 -0600192 var parameters = JSON.stringify(filters);
193
194 var ack = data.getName();
195
Tyler Scottb6e329f2015-07-28 16:08:33 -0600196 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 -0600197
Tyler Scottc55879f2015-07-28 14:56:37 -0600198 scope.getResults(0);
Tyler Scotta1530052015-07-24 00:13:28 -0600199
200 }, function(interest){ //Timeout function
Tyler Scottb6e329f2015-07-28 16:08:33 -0600201 console.warn("Request failed! Timeout");
Tyler Scott4d951222015-07-31 14:38:27 -0600202 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600203 });
204
205 }
206
207 Atmos.prototype.autoComplete = function(field, callback){
Tyler Scotta1530052015-07-24 00:13:28 -0600208
Tyler Scottc55879f2015-07-28 14:56:37 -0600209 if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){
210 if (!this.searchBar.hasClass('has-error')){
211 this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>');
212 }
213 return;
214 } else {
215 this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()});
216 }
217
Tyler Scottb6e329f2015-07-28 16:08:33 -0600218 var scope = this;
219
Tyler Scott4d951222015-07-31 14:38:27 -0600220 this.query(this.catalog, {"?": field},
Tyler Scotta1530052015-07-24 00:13:28 -0600221 function(interest, data){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600222
223 var ack = data.getName();
224
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600225 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 -0600226
Tyler Scottab822fe2015-08-10 14:38:49 -0600227 var interest = new Interest(name);
228 interest.setInterestLifetimeMilliseconds(5000);
229 interest.setMustBeFresh(true);
230
231 scope.face.expressInterest(interest,
Tyler Scottb6e329f2015-07-28 16:08:33 -0600232 function(interest, data){
Tyler Scott4d951222015-07-31 14:38:27 -0600233
Tyler Scottfe8e4932015-07-28 17:45:45 -0600234 if (data.getContent().length !== 0){
235 var options = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")).next.map(function(element){
Tyler Scottfc990392015-08-10 13:48:03 -0600236 return field + element + "/";
Tyler Scottfe8e4932015-07-28 17:45:45 -0600237 });
238 callback(options);
239 }
240
Tyler Scottb6e329f2015-07-28 16:08:33 -0600241 }, function(interest){
242 console.warn("Interest timed out!", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600243 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottb6e329f2015-07-28 16:08:33 -0600244 });
245
Tyler Scotta1530052015-07-24 00:13:28 -0600246 }, function(interest){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600247 console.error("Request failed! Timeout", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600248 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600249 });
Tyler Scott93cae872015-07-21 14:58:23 -0600250
Tyler Scott424ee102015-07-14 16:50:41 -0600251 }
252
Tyler Scottc55879f2015-07-28 14:56:37 -0600253 Atmos.prototype.showResults = function(resultIndex) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600254
Tyler Scott4e4865a2015-08-06 14:23:21 -0600255 var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1));
256
257 var resultDOM = $(results.reduce(function(prev, current){
Tyler Scott696d38f2015-08-04 22:41:22 -0600258 prev.push('<tr><td><input type="checkbox"></td><td>');
259 prev.push(current);
260 prev.push('</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr>');
261 return prev;
262 }, []).join(''));
Tyler Scott7d076e22015-07-06 19:21:50 -0600263
Tyler Scott4e4865a2015-08-06 14:23:21 -0600264 this.resultTable.empty().append(resultDOM);
Tyler Scott7d076e22015-07-06 19:21:50 -0600265
Tyler Scott4e4865a2015-08-06 14:23:21 -0600266 this.pagers.find('.pageNumber').text(resultIndex + 1);
Tyler Scott7d076e22015-07-06 19:21:50 -0600267
Tyler Scott4e4865a2015-08-06 14:23:21 -0600268 if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) {
Tyler Scottc55879f2015-07-28 14:56:37 -0600269 this.pagers.find('.next').addClass('disabled');
Tyler Scott4e4865a2015-08-06 14:23:21 -0600270 } else if (resultIndex === 0){
271 this.pagers.find('.next').removeClass('disabled');
Tyler Scottc55879f2015-07-28 14:56:37 -0600272 }
Tyler Scott93cae872015-07-21 14:58:23 -0600273
Tyler Scottc55879f2015-07-28 14:56:37 -0600274 if (resultIndex === 0){
Tyler Scottc55879f2015-07-28 14:56:37 -0600275 this.pagers.find('.previous').addClass('disabled');
276 } else if (resultIndex === 1) {
277 this.pagers.find('.previous').removeClass('disabled');
278 }
Tyler Scott93cae872015-07-21 14:58:23 -0600279
Tyler Scottc55879f2015-07-28 14:56:37 -0600280 }
Tyler Scott93cae872015-07-21 14:58:23 -0600281
Tyler Scottc55879f2015-07-28 14:56:37 -0600282 Atmos.prototype.getResults = function(index){
Tyler Scott93cae872015-07-21 14:58:23 -0600283
Tyler Scott4e4865a2015-08-06 14:23:21 -0600284 if ((this.results.length === this.resultCount) || (this.resultsPerPage * (index + 1) < this.results.length)){
Tyler Scottc55879f2015-07-28 14:56:37 -0600285 //console.log("We already have index", index);
286 this.page = index;
287 this.showResults(index);
288 return;
289 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600290
Tyler Scottc55879f2015-07-28 14:56:37 -0600291 if (this.name === null) {
292 console.error("This shouldn't be reached! We are getting results before a search has occured!");
293 throw new Error("Illegal State");
294 }
Tyler Scott424ee102015-07-14 16:50:41 -0600295
Tyler Scott4e4865a2015-08-06 14:23:21 -0600296 var first = new Name(this.name).appendSegment(this.retrievedSegments++);
Tyler Scottc55879f2015-07-28 14:56:37 -0600297
Tyler Scott4e4865a2015-08-06 14:23:21 -0600298 console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri());
Tyler Scottc55879f2015-07-28 14:56:37 -0600299
300 var scope = this;
301
Tyler Scottab822fe2015-08-10 14:38:49 -0600302 var interest = new Interest(first)
303 interest.setInterestLifetimeMilliseconds(5000);
304 interest.setMustBeFresh(true);
305
306 this.face.expressInterest(interest,
Tyler Scottc55879f2015-07-28 14:56:37 -0600307 function(interest, data){ //Response
308
309 if (data.getContent().length === 0){
Tyler Scott03854852015-08-07 15:45:10 -0600310 scope.pagers.find('.totalResults').text(0);
311 scope.pagers.find('.pageNumber').text(0);
Tyler Scottc55879f2015-07-28 14:56:37 -0600312 console.log("Empty response.");
313 return;
314 }
315
Tyler Scottc55879f2015-07-28 14:56:37 -0600316 var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,""));
317
Tyler Scott4e4865a2015-08-06 14:23:21 -0600318 if (!content.results){
319 scope.pagers.find('.totalResults').text(0);
320 scope.pagers.find('.pageNumber').text(0);
Tyler Scottc55879f2015-07-28 14:56:37 -0600321 console.log("No results were found!");
322 return;
323 }
324
Tyler Scott4e4865a2015-08-06 14:23:21 -0600325 scope.results = scope.results.concat(content.results);
326
327 scope.resultCount = content.resultCount;
328
329 scope.pagers.find('.totalResults').text(scope.resultCount);
330
331 scope.page = index;
332
333 scope.getResults(index); //Keep calling this until we have enough data.
Tyler Scottc55879f2015-07-28 14:56:37 -0600334
335 },
336 function(interest){ //Timeout
Tyler Scott4d951222015-07-31 14:38:27 -0600337 console.error("Failed to retrieve results: timeout", interest);
338 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottc55879f2015-07-28 14:56:37 -0600339 }
340 );
341
342 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600343
Tyler Scotta1530052015-07-24 00:13:28 -0600344 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600345
Tyler Scott93cae872015-07-21 14:58:23 -0600346 var queryPrefix = new Name(prefix);
347 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600348
Tyler Scott93cae872015-07-21 14:58:23 -0600349 var jsonString = JSON.stringify(parameters);
350 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600351
Tyler Scott93cae872015-07-21 14:58:23 -0600352 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600353 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scottab822fe2015-08-10 14:38:49 -0600354 queryInterest.setMustBeFresh(true);
Tyler Scott7d076e22015-07-06 19:21:50 -0600355
Tyler Scotta1530052015-07-24 00:13:28 -0600356 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600357
Tyler Scott93cae872015-07-21 14:58:23 -0600358 }
359
Tyler Scott93cae872015-07-21 14:58:23 -0600360 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600361 * This function returns a map of all the categories active filters.
362 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600363 */
Tyler Scotta1530052015-07-24 00:13:28 -0600364 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600365 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600366 var data = $(current).text().split(/:/);
367 prev[data[0]] = data[1];
368 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600369 }, {}); //Collect a map<category, filter>.
370 //TODO Make the return value map<category, Array<filter>>
371 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600372 }
373
Tyler Scott4d951222015-07-31 14:38:27 -0600374 /**
375 * Creates a closable alert for the user.
376 *
377 * @param {string} message
378 * @param {string} type - Override the alert type.
379 */
380 Atmos.prototype.createAlert = function(message, type) {
381
382 var alert = $('<div class="alert"><div>');
383 alert.addClass(type?type:'alert-info');
384 alert.text(message);
385 alert.append(Atmos.closeButton);
386
387 this.alerts.append(alert);
388 }
389 Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
390
Tyler Scott93cae872015-07-21 14:58:23 -0600391 return Atmos;
392
393})();