blob: faac6858debf14d367e6e8adca15756dc51059e6 [file] [log] [blame]
Tyler Scott3c17d5f2015-06-23 17:49:29 -06001var catalog = "/catalog/myUniqueName";
Tyler Scotta1ac69d2015-07-02 17:42:03 -06002var config = {
Tyler Scott087aef72015-07-14 14:11:59 -06003 host: "atmos-csu.research-lan.colostate.edu",
Tyler Scott3c17d5f2015-06-23 17:49:29 -06004 port: 9696
Tyler Scotta1ac69d2015-07-02 17:42:03 -06005};
Tyler Scott3c17d5f2015-06-23 17:49:29 -06006
Tyler Scotta1ac69d2015-07-02 17:42:03 -06007//Run when the document loads.
8$(function () {
Tyler Scottbb013562015-07-16 15:52:40 -06009 new Atmos(catalog, config);
Tyler Scott3c17d5f2015-06-23 17:49:29 -060010});
11
Tyler Scott93cae872015-07-21 14:58:23 -060012var Atmos = (function(){
Tyler Scott7d076e22015-07-06 19:21:50 -060013 "use strict";
Tyler Scott93cae872015-07-21 14:58:23 -060014 /**
15 * Atmos
16 * @version 2.0
Tyler Scott4d951222015-07-31 14:38:27 -060017 *
Tyler Scott93cae872015-07-21 14:58:23 -060018 * Configures an Atmos object. This manages the atmos interface.
Tyler Scott4d951222015-07-31 14:38:27 -060019 *
20 * @constructor
Tyler Scott93cae872015-07-21 14:58:23 -060021 * @param {string} catalog - NDN path
Tyler Scott4d951222015-07-31 14:38:27 -060022 * @param {Object} config - Object of configuration options for a Face.
Tyler Scott93cae872015-07-21 14:58:23 -060023 */
24 function Atmos(catalog, config){
Tyler Scott7d076e22015-07-06 19:21:50 -060025
Tyler Scott93cae872015-07-21 14:58:23 -060026 //Internal variables.
27 this.results = [];
Tyler Scotta1530052015-07-24 00:13:28 -060028 this.resultCount = 0;
Tyler Scottc55879f2015-07-28 14:56:37 -060029 this.name = null;
30 this.page = 0;
31 this.lastPage = -1;
32 //this.itemsPerPage = 25; //TODO
Tyler Scotte815d3e2015-07-09 16:56:17 -060033
Tyler Scott93cae872015-07-21 14:58:23 -060034 this.catalog = catalog;
Tyler Scott7d076e22015-07-06 19:21:50 -060035
Tyler Scott93cae872015-07-21 14:58:23 -060036 this.face = new Face(config);
37 this.categories = $('#side-menu');
38 this.resultTable = $('#resultTable');
39 this.filters = $('#filters');
Tyler Scotta1530052015-07-24 00:13:28 -060040 this.searchInput = $('#search');
41 this.searchBar = $('#searchBar');
Tyler Scottc55879f2015-07-28 14:56:37 -060042 this.searchButton = $('#searchButton');
43 this.pagers = $('.pager');
Tyler Scott4d951222015-07-31 14:38:27 -060044 this.alerts = $('#alerts');
Tyler Scott7d076e22015-07-06 19:21:50 -060045
Tyler Scott93cae872015-07-21 14:58:23 -060046 var scope = this;
Tyler Scott7d076e22015-07-06 19:21:50 -060047
Tyler Scott93cae872015-07-21 14:58:23 -060048 this.resultTable.on('click', '.interest-button', function(){
49 var button = $(this);
Tyler Scott087aef72015-07-14 14:11:59 -060050
Tyler Scott93cae872015-07-21 14:58:23 -060051 if (button.is(':disabled')){
52 console.warn("Attempt to request again!");
Tyler Scott7d076e22015-07-06 19:21:50 -060053 }
Tyler Scott93cae872015-07-21 14:58:23 -060054
55 var name = button.parent().prev().text();
56 var interest = new Interest(new Name('/retrieve' + name));
57 scope.face.expressInterest(interest, function(){}, function(){});
58
59 button.text("Requested!")
60 .removeClass('btn-primary')
61 .addClass('btn-default')
62 .addClass('disabled')
63 .prop('disabled', true);
Tyler Scott7d076e22015-07-06 19:21:50 -060064 });
Tyler Scott7d076e22015-07-06 19:21:50 -060065
Tyler Scotta1530052015-07-24 00:13:28 -060066 //Filter setup
Tyler Scott93cae872015-07-21 14:58:23 -060067 $.getJSON("search_catagories.json").done(function (data) {
68 $.each(data, function (pageSection, contents) {
69 if (pageSection == "SearchCatagories") {
Tyler Scotta1530052015-07-24 00:13:28 -060070 $.each(contents, function (category, searchOptions) {
71 //Create the category
72 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 -060073
Tyler Scott93cae872015-07-21 14:58:23 -060074 var sub = e.find('ul.subnav');
75 $.each(searchOptions, function(index, name){
Tyler Scotta1530052015-07-24 00:13:28 -060076 //Create the filter list inside the category
Tyler Scott93cae872015-07-21 14:58:23 -060077 var item = $('<li><a href="#">' + name + '</a></li>');
78 sub.append(item);
Tyler Scotta1530052015-07-24 00:13:28 -060079 item.click(function(){ //Click on the side menu filters
80 if (item.hasClass('active')){ //Does the filter already exist?
81 item.removeClass('active');
82 scope.filters.find(':contains(' + category + ':' + name + ')').remove();
83 } else { //Add a filter
84 item.addClass('active');
85 var filter = $('<span class="label label-default"></span>');
86 filter.text(category + ':' + name);
87
88 scope.filters.append(filter);
89
90 filter.click(function(){ //Click on a filter
91 filter.remove();
92 item.removeClass('active');
93 });
94 }
Tyler Scott4d951222015-07-31 14:38:27 -060095
Tyler Scott93cae872015-07-21 14:58:23 -060096 });
97 });
Tyler Scott7d076e22015-07-06 19:21:50 -060098
Tyler Scott93cae872015-07-21 14:58:23 -060099 //Toggle the menus. (Only respond when the immediate tab is clicked.)
100 e.find('> a').click(function(){
101 scope.categories.find('.subnav').slideUp();
102 var t = $(this).siblings('.subnav');
Tyler Scotta1530052015-07-24 00:13:28 -0600103 if ( !t.is(':visible') ){ //If the sub menu is not visible
104 t.slideDown(function(){
105 t.triggerHandler('focus');
106 }); //Make it visible and look at it.
Tyler Scott93cae872015-07-21 14:58:23 -0600107 }
108 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600109
Tyler Scott93cae872015-07-21 14:58:23 -0600110 scope.categories.append(e);
Tyler Scotta1530052015-07-24 00:13:28 -0600111
Tyler Scott93cae872015-07-21 14:58:23 -0600112 });
113 }
114 });
115 });
116
Tyler Scottc55879f2015-07-28 14:56:37 -0600117 this.searchInput.autoComplete(function(field, callback){
118 scope.autoComplete(field, callback);
119 });
120
Tyler Scotta1530052015-07-24 00:13:28 -0600121 this.searchBar.submit(function(e){
Tyler Scott93cae872015-07-21 14:58:23 -0600122 e.preventDefault();
Tyler Scott4d951222015-07-31 14:38:27 -0600123 console.warn("This feature is incomplete.");
Tyler Scottc55879f2015-07-28 14:56:37 -0600124 });
125
126 this.searchButton.click(function(){
127 console.log("Search Button Pressed");
Tyler Scotta1530052015-07-24 00:13:28 -0600128 scope.search();
129 });
130
Tyler Scottc55879f2015-07-28 14:56:37 -0600131 this.pagers.find('.next').click(function(){
132 if (!$(this).hasClass('disabled')){
133 scope.getResults(scope.page + 1);
134 }
135 });
136 this.pagers.find('.previous').click(function(){
137 if (!$(this).hasClass('disabled')){
138 scope.getResults(scope.page - 1);
139 }
Tyler Scotta1530052015-07-24 00:13:28 -0600140 });
141
142 }
143
144 Atmos.prototype.search = function(){
Tyler Scotta1530052015-07-24 00:13:28 -0600145
Tyler Scottc55879f2015-07-28 14:56:37 -0600146 var filters = this.getFilters();
Tyler Scotta1530052015-07-24 00:13:28 -0600147
148 console.log("Search started!", this.searchInput.val(), filters);
149
Tyler Scott4d951222015-07-31 14:38:27 -0600150 console.log("Initiating query");
Tyler Scotta1530052015-07-24 00:13:28 -0600151
Tyler Scottc55879f2015-07-28 14:56:37 -0600152 this.results = []; //Drop any old results.
153 this.resultTable.empty();
154
Tyler Scottd90f84e2015-07-27 12:43:04 -0600155 var scope = this;
156
Tyler Scott4d951222015-07-31 14:38:27 -0600157 this.query(this.catalog, filters,
Tyler Scotta1530052015-07-24 00:13:28 -0600158 function(interest, data){ //Response function
159 console.log("Query Response:", interest, data);
160
Tyler Scottd90f84e2015-07-27 12:43:04 -0600161 var parameters = JSON.stringify(filters);
162
163 var ack = data.getName();
164
Tyler Scottb6e329f2015-07-28 16:08:33 -0600165 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 -0600166
Tyler Scottc55879f2015-07-28 14:56:37 -0600167 scope.getResults(0);
Tyler Scotta1530052015-07-24 00:13:28 -0600168
169 }, function(interest){ //Timeout function
Tyler Scottb6e329f2015-07-28 16:08:33 -0600170 console.warn("Request failed! Timeout");
Tyler Scott4d951222015-07-31 14:38:27 -0600171 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600172 });
173
174 }
175
176 Atmos.prototype.autoComplete = function(field, callback){
177 console.log("Autocomplete triggered");
178
Tyler Scottc55879f2015-07-28 14:56:37 -0600179 if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){
180 if (!this.searchBar.hasClass('has-error')){
181 this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>');
182 }
183 return;
184 } else {
185 this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()});
186 }
187
Tyler Scottb6e329f2015-07-28 16:08:33 -0600188 var scope = this;
189
Tyler Scott4d951222015-07-31 14:38:27 -0600190 this.query(this.catalog, {"?": field},
Tyler Scotta1530052015-07-24 00:13:28 -0600191 function(interest, data){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600192
193 var ack = data.getName();
194
195 var name = new Name(scope.catalog).append('query-results').append(JSON.stringify(filters)).append(ack.get(-3)).append(ack.get(-2));
196
197 console.log(name.toUri(), filters);
198
199 scope.face.expressInterest(new Interest(name).setInterestLifetimeMilliseconds(5000),
200 function(interest, data){
201 console.log("Autocomplete query return: ", data.getContent().toString());
Tyler Scott4d951222015-07-31 14:38:27 -0600202
Tyler Scottfe8e4932015-07-28 17:45:45 -0600203 if (data.getContent().length !== 0){
204 var options = JSON.parse(data.getContent().toString().replace(/[\n\0]/g, "")).next.map(function(element){
205 return field + element;
206 });
207 callback(options);
208 }
209
Tyler Scottb6e329f2015-07-28 16:08:33 -0600210 }, function(interest){
211 console.warn("Interest timed out!", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600212 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottb6e329f2015-07-28 16:08:33 -0600213 });
214
Tyler Scotta1530052015-07-24 00:13:28 -0600215 }, function(interest){
Tyler Scottb6e329f2015-07-28 16:08:33 -0600216 console.error("Request failed! Timeout", interest);
Tyler Scott4d951222015-07-31 14:38:27 -0600217 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scotta1530052015-07-24 00:13:28 -0600218 });
Tyler Scott93cae872015-07-21 14:58:23 -0600219
Tyler Scott424ee102015-07-14 16:50:41 -0600220 }
221
Tyler Scottc55879f2015-07-28 14:56:37 -0600222 Atmos.prototype.showResults = function(resultIndex) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600223
Tyler Scottc55879f2015-07-28 14:56:37 -0600224 var results = $('<tr><td>' + this.results[resultIndex].join('</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr><tr><td>') +
225 '</td><td><button class="interest-button btn btn-primary btn-sm">Retrieve</button></td></tr>'); //Fastest way to generate the table.
Tyler Scott7d076e22015-07-06 19:21:50 -0600226
Tyler Scottc55879f2015-07-28 14:56:37 -0600227 this.resultTable.empty().append(results);
Tyler Scott7d076e22015-07-06 19:21:50 -0600228
Tyler Scottb6e329f2015-07-28 16:08:33 -0600229 this.pagers.find('.totalResults').text('(Page' + (resultIndex + 1) + ') Showing ' + this.results[resultIndex].length + ' of ' + this.resultCount + ' results');
Tyler Scott7d076e22015-07-06 19:21:50 -0600230
Tyler Scottc55879f2015-07-28 14:56:37 -0600231 if (resultIndex === this.lastPage) {
232 this.pagers.find('.next').addClass('disabled');
233 }
Tyler Scott93cae872015-07-21 14:58:23 -0600234
Tyler Scottc55879f2015-07-28 14:56:37 -0600235 if (resultIndex === 0){
236 this.pagers.find('.next').removeClass('disabled');
237 this.pagers.find('.previous').addClass('disabled');
238 } else if (resultIndex === 1) {
239 this.pagers.find('.previous').removeClass('disabled');
240 }
Tyler Scott93cae872015-07-21 14:58:23 -0600241
Tyler Scottc55879f2015-07-28 14:56:37 -0600242 }
Tyler Scott93cae872015-07-21 14:58:23 -0600243
Tyler Scottc55879f2015-07-28 14:56:37 -0600244 Atmos.prototype.getResults = function(index){
Tyler Scott93cae872015-07-21 14:58:23 -0600245
Tyler Scottc55879f2015-07-28 14:56:37 -0600246 if (this.results[index]){
247 //console.log("We already have index", index);
248 this.page = index;
249 this.showResults(index);
250 return;
251 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600252
Tyler Scottc55879f2015-07-28 14:56:37 -0600253 if (this.name === null) {
254 console.error("This shouldn't be reached! We are getting results before a search has occured!");
255 throw new Error("Illegal State");
256 }
Tyler Scott424ee102015-07-14 16:50:41 -0600257
Tyler Scottc55879f2015-07-28 14:56:37 -0600258 var first = new Name(this.name).appendSegment(index);
259
Tyler Scottb6e329f2015-07-28 16:08:33 -0600260 console.log("Requesting data index: (", index, ") at ", first.toUri());
Tyler Scottc55879f2015-07-28 14:56:37 -0600261
262 var scope = this;
263
264 this.face.expressInterest(new Interest(first).setInterestLifetimeMilliseconds(5000),
265 function(interest, data){ //Response
266
267 if (data.getContent().length === 0){
268 console.log("Empty response.");
269 return;
270 }
271
272 if (data.getName().get(-1).equals(data.getMetaInfo().getFinalBlockId())) { //Final page.
273 scope.lastPage = index;
274 //The next buttons will be disabled by showResults.
275 }
276
277 var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,""));
278
279 var results = scope.results[index] = content.results;
280
281 scope.resultCount = content.resultCount;
282
283 scope.pagers.find('.totalResults').text(scope.resultCount + " Results");
284
285 //console.log("Got results:", results);
286
287 scope.page = index;
288
289 if (!results){
290 console.log("No results were found!");
291 return;
292 }
293
294 scope.showResults(index);
295
296 },
297 function(interest){ //Timeout
Tyler Scott4d951222015-07-31 14:38:27 -0600298 console.error("Failed to retrieve results: timeout", interest);
299 scope.createAlert("Request timed out. \"" + interest.getName().toUri() + "\" See console for details.");
Tyler Scottc55879f2015-07-28 14:56:37 -0600300 }
301 );
302
303 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600304
Tyler Scotta1530052015-07-24 00:13:28 -0600305 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600306
Tyler Scott93cae872015-07-21 14:58:23 -0600307 var queryPrefix = new Name(prefix);
308 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600309
Tyler Scott93cae872015-07-21 14:58:23 -0600310 var jsonString = JSON.stringify(parameters);
311 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600312
Tyler Scott93cae872015-07-21 14:58:23 -0600313 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600314 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scott7d076e22015-07-06 19:21:50 -0600315
Tyler Scotta1530052015-07-24 00:13:28 -0600316 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600317
Tyler Scott93cae872015-07-21 14:58:23 -0600318 }
319
Tyler Scott93cae872015-07-21 14:58:23 -0600320 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600321 * This function returns a map of all the categories active filters.
322 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600323 */
Tyler Scotta1530052015-07-24 00:13:28 -0600324 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600325 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600326 var data = $(current).text().split(/:/);
327 prev[data[0]] = data[1];
328 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600329 }, {}); //Collect a map<category, filter>.
330 //TODO Make the return value map<category, Array<filter>>
331 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600332 }
333
Tyler Scott4d951222015-07-31 14:38:27 -0600334 /**
335 * Creates a closable alert for the user.
336 *
337 * @param {string} message
338 * @param {string} type - Override the alert type.
339 */
340 Atmos.prototype.createAlert = function(message, type) {
341
342 var alert = $('<div class="alert"><div>');
343 alert.addClass(type?type:'alert-info');
344 alert.text(message);
345 alert.append(Atmos.closeButton);
346
347 this.alerts.append(alert);
348 }
349 Atmos.closeButton = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
350
Tyler Scott93cae872015-07-21 14:58:23 -0600351 return Atmos;
352
353})();
354
355