blob: b84ed46cc27087dac7c9a8ee2ae61cb242b85708 [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 Scott4d951222015-07-31 14:38:27 -0600145 console.warn("This feature is 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
227 scope.face.expressInterest(new Interest(name).setInterestLifetimeMilliseconds(5000),
228 function(interest, data){
Tyler Scott4d951222015-07-31 14:38:27 -0600229
Tyler Scottfe8e4932015-07-28 17:45:45 -0600230 if (data.getContent().length !== 0){
231 var options = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")).next.map(function(element){
232 return field + element;
233 });
234 callback(options);
235 }
236
Tyler Scottb6e329f2015-07-28 16:08:33 -0600237 }, function(interest){
238 console.warn("Interest timed out!", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600239 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottb6e329f2015-07-28 16:08:33 -0600240 });
241
Tyler Scotta1530052015-07-24 00:13:28 -0600242 }, function(interest){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600243 console.error("Request failed! Timeout", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600244 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600245 });
Tyler Scott93cae872015-07-21 14:58:23 -0600246
Tyler Scott424ee102015-07-14 16:50:41 -0600247 }
248
Tyler Scottc55879f2015-07-28 14:56:37 -0600249 Atmos.prototype.showResults = function(resultIndex) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600250
Tyler Scott4e4865a2015-08-06 14:23:21 -0600251 var results = this.results.slice(this.resultsPerPage * resultIndex, this.resultsPerPage * (resultIndex + 1));
252
253 var resultDOM = $(results.reduce(function(prev, current){
Tyler Scott696d38f2015-08-04 22:41:22 -0600254 prev.push('<tr><td><input type="checkbox"></td><td>');
255 prev.push(current);
256 prev.push('</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr>');
257 return prev;
258 }, []).join(''));
Tyler Scott7d076e22015-07-06 19:21:50 -0600259
Tyler Scott4e4865a2015-08-06 14:23:21 -0600260 this.resultTable.empty().append(resultDOM);
Tyler Scott7d076e22015-07-06 19:21:50 -0600261
Tyler Scott4e4865a2015-08-06 14:23:21 -0600262 this.pagers.find('.pageNumber').text(resultIndex + 1);
Tyler Scott7d076e22015-07-06 19:21:50 -0600263
Tyler Scott4e4865a2015-08-06 14:23:21 -0600264 if (this.resultsPerPage * (resultIndex + 1) >= this.resultCount) {
Tyler Scottc55879f2015-07-28 14:56:37 -0600265 this.pagers.find('.next').addClass('disabled');
Tyler Scott4e4865a2015-08-06 14:23:21 -0600266 } else if (resultIndex === 0){
267 this.pagers.find('.next').removeClass('disabled');
Tyler Scottc55879f2015-07-28 14:56:37 -0600268 }
Tyler Scott93cae872015-07-21 14:58:23 -0600269
Tyler Scottc55879f2015-07-28 14:56:37 -0600270 if (resultIndex === 0){
Tyler Scottc55879f2015-07-28 14:56:37 -0600271 this.pagers.find('.previous').addClass('disabled');
272 } else if (resultIndex === 1) {
273 this.pagers.find('.previous').removeClass('disabled');
274 }
Tyler Scott93cae872015-07-21 14:58:23 -0600275
Tyler Scottc55879f2015-07-28 14:56:37 -0600276 }
Tyler Scott93cae872015-07-21 14:58:23 -0600277
Tyler Scottc55879f2015-07-28 14:56:37 -0600278 Atmos.prototype.getResults = function(index){
Tyler Scott93cae872015-07-21 14:58:23 -0600279
Tyler Scott4e4865a2015-08-06 14:23:21 -0600280 if ((this.results.length === this.resultCount) || (this.resultsPerPage * (index + 1) < this.results.length)){
Tyler Scottc55879f2015-07-28 14:56:37 -0600281 //console.log("We already have index", index);
282 this.page = index;
283 this.showResults(index);
284 return;
285 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600286
Tyler Scottc55879f2015-07-28 14:56:37 -0600287 if (this.name === null) {
288 console.error("This shouldn't be reached! We are getting results before a search has occured!");
289 throw new Error("Illegal State");
290 }
Tyler Scott424ee102015-07-14 16:50:41 -0600291
Tyler Scott4e4865a2015-08-06 14:23:21 -0600292 var first = new Name(this.name).appendSegment(this.retrievedSegments++);
Tyler Scottc55879f2015-07-28 14:56:37 -0600293
Tyler Scott4e4865a2015-08-06 14:23:21 -0600294 console.log("Requesting data index: (", this.retrievedSegments - 1, ") at ", first.toUri());
Tyler Scottc55879f2015-07-28 14:56:37 -0600295
296 var scope = this;
297
298 this.face.expressInterest(new Interest(first).setInterestLifetimeMilliseconds(5000),
299 function(interest, data){ //Response
300
301 if (data.getContent().length === 0){
302 console.log("Empty response.");
303 return;
304 }
305
Tyler Scottc55879f2015-07-28 14:56:37 -0600306 var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,""));
307
Tyler Scott4e4865a2015-08-06 14:23:21 -0600308 if (!content.results){
309 scope.pagers.find('.totalResults').text(0);
310 scope.pagers.find('.pageNumber').text(0);
Tyler Scottc55879f2015-07-28 14:56:37 -0600311 console.log("No results were found!");
312 return;
313 }
314
Tyler Scott4e4865a2015-08-06 14:23:21 -0600315 scope.results = scope.results.concat(content.results);
316
317 scope.resultCount = content.resultCount;
318
319 scope.pagers.find('.totalResults').text(scope.resultCount);
320
321 scope.page = index;
322
323 scope.getResults(index); //Keep calling this until we have enough data.
Tyler Scottc55879f2015-07-28 14:56:37 -0600324
325 },
326 function(interest){ //Timeout
Tyler Scott4d951222015-07-31 14:38:27 -0600327 console.error("Failed to retrieve results: timeout", interest);
328 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottc55879f2015-07-28 14:56:37 -0600329 }
330 );
331
332 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600333
Tyler Scotta1530052015-07-24 00:13:28 -0600334 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600335
Tyler Scott93cae872015-07-21 14:58:23 -0600336 var queryPrefix = new Name(prefix);
337 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600338
Tyler Scott93cae872015-07-21 14:58:23 -0600339 var jsonString = JSON.stringify(parameters);
340 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600341
Tyler Scott93cae872015-07-21 14:58:23 -0600342 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600343 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scott7d076e22015-07-06 19:21:50 -0600344
Tyler Scotta1530052015-07-24 00:13:28 -0600345 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600346
Tyler Scott93cae872015-07-21 14:58:23 -0600347 }
348
Tyler Scott93cae872015-07-21 14:58:23 -0600349 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600350 * This function returns a map of all the categories active filters.
351 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600352 */
Tyler Scotta1530052015-07-24 00:13:28 -0600353 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600354 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600355 var data = $(current).text().split(/:/);
356 prev[data[0]] = data[1];
357 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600358 }, {}); //Collect a map<category, filter>.
359 //TODO Make the return value map<category, Array<filter>>
360 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600361 }
362
Tyler Scott4d951222015-07-31 14:38:27 -0600363 /**
364 * Creates a closable alert for the user.
365 *
366 * @param {string} message
367 * @param {string} type - Override the alert type.
368 */
369 Atmos.prototype.createAlert = function(message, type) {
370
371 var alert = $('<div class="alert"><div>');
372 alert.addClass(type?type:'alert-info');
373 alert.text(message);
374 alert.append(Atmos.closeButton);
375
376 this.alerts.append(alert);
377 }
378 Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
379
Tyler Scott93cae872015-07-21 14:58:23 -0600380 return Atmos;
381
382})();
383
384