blob: 0546cd753f22accab5334409adaf74c944f2a60a [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
17 *
18 * Configures an Atmos object. This manages the atmos interface.
19 *
20 * @constructor
21 * @param {string} catalog - NDN path
22 * @param {Object} config - Object of configuration options for a Face.
23 */
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 Scott7d076e22015-07-06 19:21:50 -060044
Tyler Scott93cae872015-07-21 14:58:23 -060045 var scope = this;
Tyler Scott7d076e22015-07-06 19:21:50 -060046
Tyler Scott93cae872015-07-21 14:58:23 -060047 this.resultTable.on('click', '.interest-button', function(){
48 var button = $(this);
Tyler Scott087aef72015-07-14 14:11:59 -060049
Tyler Scott93cae872015-07-21 14:58:23 -060050 if (button.is(':disabled')){
51 console.warn("Attempt to request again!");
Tyler Scott7d076e22015-07-06 19:21:50 -060052 }
Tyler Scott93cae872015-07-21 14:58:23 -060053
54 var name = button.parent().prev().text();
55 var interest = new Interest(new Name('/retrieve' + name));
56 scope.face.expressInterest(interest, function(){}, function(){});
57
58 button.text("Requested!")
59 .removeClass('btn-primary')
60 .addClass('btn-default')
61 .addClass('disabled')
62 .prop('disabled', true);
Tyler Scott7d076e22015-07-06 19:21:50 -060063 });
Tyler Scott7d076e22015-07-06 19:21:50 -060064
Tyler Scotta1530052015-07-24 00:13:28 -060065 //Filter setup
Tyler Scott93cae872015-07-21 14:58:23 -060066 $.getJSON("search_catagories.json").done(function (data) {
67 $.each(data, function (pageSection, contents) {
68 if (pageSection == "SearchCatagories") {
Tyler Scotta1530052015-07-24 00:13:28 -060069 $.each(contents, function (category, searchOptions) {
70 //Create the category
71 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 -060072
Tyler Scott93cae872015-07-21 14:58:23 -060073 var sub = e.find('ul.subnav');
74 $.each(searchOptions, function(index, name){
Tyler Scotta1530052015-07-24 00:13:28 -060075 //Create the filter list inside the category
Tyler Scott93cae872015-07-21 14:58:23 -060076 var item = $('<li><a href="#">' + name + '</a></li>');
77 sub.append(item);
Tyler Scotta1530052015-07-24 00:13:28 -060078 item.click(function(){ //Click on the side menu filters
79 if (item.hasClass('active')){ //Does the filter already exist?
80 item.removeClass('active');
81 scope.filters.find(':contains(' + category + ':' + name + ')').remove();
82 } else { //Add a filter
83 item.addClass('active');
84 var filter = $('<span class="label label-default"></span>');
85 filter.text(category + ':' + name);
86
87 scope.filters.append(filter);
88
89 filter.click(function(){ //Click on a filter
90 filter.remove();
91 item.removeClass('active');
92 });
93 }
94
Tyler Scott93cae872015-07-21 14:58:23 -060095 });
96 });
Tyler Scott7d076e22015-07-06 19:21:50 -060097
Tyler Scott93cae872015-07-21 14:58:23 -060098 //Toggle the menus. (Only respond when the immediate tab is clicked.)
99 e.find('> a').click(function(){
100 scope.categories.find('.subnav').slideUp();
101 var t = $(this).siblings('.subnav');
Tyler Scotta1530052015-07-24 00:13:28 -0600102 if ( !t.is(':visible') ){ //If the sub menu is not visible
103 t.slideDown(function(){
104 t.triggerHandler('focus');
105 }); //Make it visible and look at it.
Tyler Scott93cae872015-07-21 14:58:23 -0600106 }
107 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600108
Tyler Scott93cae872015-07-21 14:58:23 -0600109 scope.categories.append(e);
Tyler Scotta1530052015-07-24 00:13:28 -0600110
Tyler Scott93cae872015-07-21 14:58:23 -0600111 });
112 }
113 });
114 });
115
Tyler Scottc55879f2015-07-28 14:56:37 -0600116 this.searchInput.autoComplete(function(field, callback){
117 scope.autoComplete(field, callback);
118 });
119
Tyler Scotta1530052015-07-24 00:13:28 -0600120 this.searchBar.submit(function(e){
Tyler Scott93cae872015-07-21 14:58:23 -0600121 e.preventDefault();
Tyler Scottc55879f2015-07-28 14:56:37 -0600122 scope.searchInput.trigger('autoComplete');
123 });
124
125 this.searchButton.click(function(){
126 console.log("Search Button Pressed");
Tyler Scotta1530052015-07-24 00:13:28 -0600127 scope.search();
128 });
129
Tyler Scottc55879f2015-07-28 14:56:37 -0600130 this.pagers.find('.next').click(function(){
131 if (!$(this).hasClass('disabled')){
132 scope.getResults(scope.page + 1);
133 }
134 });
135 this.pagers.find('.previous').click(function(){
136 if (!$(this).hasClass('disabled')){
137 scope.getResults(scope.page - 1);
138 }
Tyler Scotta1530052015-07-24 00:13:28 -0600139 });
140
141 }
142
143 Atmos.prototype.search = function(){
Tyler Scotta1530052015-07-24 00:13:28 -0600144
Tyler Scottc55879f2015-07-28 14:56:37 -0600145 var filters = this.getFilters();
Tyler Scotta1530052015-07-24 00:13:28 -0600146
147 console.log("Search started!", this.searchInput.val(), filters);
148
149 console.log("Initiating query");
150
Tyler Scottc55879f2015-07-28 14:56:37 -0600151 this.results = []; //Drop any old results.
152 this.resultTable.empty();
153
Tyler Scottd90f84e2015-07-27 12:43:04 -0600154 var scope = this;
155
Tyler Scotta1530052015-07-24 00:13:28 -0600156 this.query(this.catalog, filters,
157 function(interest, data){ //Response function
158 console.log("Query Response:", interest, data);
159
Tyler Scottd90f84e2015-07-27 12:43:04 -0600160 var parameters = JSON.stringify(filters);
161
162 var ack = data.getName();
163
Tyler Scottc55879f2015-07-28 14:56:37 -0600164 scope.name = new Name(scope.catalog).append("query-results").append(parameters)
165 .append(ack.get(ack.getComponentCount() - 3)).append(ack.getComponent(ack.getComponentCount() - 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
170 console.error("Request failed! Timeout");
171 });
172
173 }
174
175 Atmos.prototype.autoComplete = function(field, callback){
176 console.log("Autocomplete triggered");
177
Tyler Scottc55879f2015-07-28 14:56:37 -0600178 if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){
179 if (!this.searchBar.hasClass('has-error')){
180 this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>');
181 }
182 return;
183 } else {
184 this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()});
185 }
186
Tyler Scotta1530052015-07-24 00:13:28 -0600187 var filters = this.getFilters();
188
189 filters["?"] = this.searchInput.val();
190
191 this.query(this.catalog, filters,
192 function(interest, data){
193 console.log(interest, data);
194 }, function(interest){
195 console.error("Request failed! Timeout");
196 });
Tyler Scott93cae872015-07-21 14:58:23 -0600197
Tyler Scott424ee102015-07-14 16:50:41 -0600198 }
199
Tyler Scottc55879f2015-07-28 14:56:37 -0600200 Atmos.prototype.showResults = function(resultIndex) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600201
Tyler Scottc55879f2015-07-28 14:56:37 -0600202 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>') +
203 '</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 -0600204
Tyler Scottc55879f2015-07-28 14:56:37 -0600205 this.resultTable.empty().append(results);
Tyler Scott7d076e22015-07-06 19:21:50 -0600206
Tyler Scottc55879f2015-07-28 14:56:37 -0600207 this.pagers.find('.totalResults').text(this.results[resultIndex].length + " of " + this.resultCount + " results");
Tyler Scott7d076e22015-07-06 19:21:50 -0600208
Tyler Scottc55879f2015-07-28 14:56:37 -0600209 if (resultIndex === this.lastPage) {
210 this.pagers.find('.next').addClass('disabled');
211 }
Tyler Scott93cae872015-07-21 14:58:23 -0600212
Tyler Scottc55879f2015-07-28 14:56:37 -0600213 if (resultIndex === 0){
214 this.pagers.find('.next').removeClass('disabled');
215 this.pagers.find('.previous').addClass('disabled');
216 } else if (resultIndex === 1) {
217 this.pagers.find('.previous').removeClass('disabled');
218 }
Tyler Scott93cae872015-07-21 14:58:23 -0600219
Tyler Scottc55879f2015-07-28 14:56:37 -0600220 }
Tyler Scott93cae872015-07-21 14:58:23 -0600221
Tyler Scottc55879f2015-07-28 14:56:37 -0600222 Atmos.prototype.getResults = function(index){
Tyler Scott93cae872015-07-21 14:58:23 -0600223
Tyler Scottc55879f2015-07-28 14:56:37 -0600224 if (this.results[index]){
225 //console.log("We already have index", index);
226 this.page = index;
227 this.showResults(index);
228 return;
229 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600230
Tyler Scottc55879f2015-07-28 14:56:37 -0600231 if (this.name === null) {
232 console.error("This shouldn't be reached! We are getting results before a search has occured!");
233 throw new Error("Illegal State");
234 }
Tyler Scott424ee102015-07-14 16:50:41 -0600235
Tyler Scottc55879f2015-07-28 14:56:37 -0600236 var first = new Name(this.name).appendSegment(index);
237
238 console.log("Requesting data index: (", index, ") at ", first);
239
240 var scope = this;
241
242 this.face.expressInterest(new Interest(first).setInterestLifetimeMilliseconds(5000),
243 function(interest, data){ //Response
244
245 if (data.getContent().length === 0){
246 console.log("Empty response.");
247 return;
248 }
249
250 if (data.getName().get(-1).equals(data.getMetaInfo().getFinalBlockId())) { //Final page.
251 scope.lastPage = index;
252 //The next buttons will be disabled by showResults.
253 }
254
255 var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,""));
256
257 var results = scope.results[index] = content.results;
258
259 scope.resultCount = content.resultCount;
260
261 scope.pagers.find('.totalResults').text(scope.resultCount + " Results");
262
263 //console.log("Got results:", results);
264
265 scope.page = index;
266
267 if (!results){
268 console.log("No results were found!");
269 return;
270 }
271
272 scope.showResults(index);
273
274 },
275 function(interest){ //Timeout
276 console.error("Failed to retrieve results: timeout");
277 }
278 );
279
280 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600281
Tyler Scotta1530052015-07-24 00:13:28 -0600282 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600283
Tyler Scott93cae872015-07-21 14:58:23 -0600284 var queryPrefix = new Name(prefix);
285 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600286
Tyler Scott93cae872015-07-21 14:58:23 -0600287 var jsonString = JSON.stringify(parameters);
288 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600289
Tyler Scott93cae872015-07-21 14:58:23 -0600290 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600291 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scott7d076e22015-07-06 19:21:50 -0600292
Tyler Scotta1530052015-07-24 00:13:28 -0600293 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600294
Tyler Scott93cae872015-07-21 14:58:23 -0600295 }
296
Tyler Scott93cae872015-07-21 14:58:23 -0600297 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600298 * This function returns a map of all the categories active filters.
299 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600300 */
Tyler Scotta1530052015-07-24 00:13:28 -0600301 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600302 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600303 var data = $(current).text().split(/:/);
304 prev[data[0]] = data[1];
305 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600306 }, {}); //Collect a map<category, filter>.
307 //TODO Make the return value map<category, Array<filter>>
308 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600309 }
310
311 return Atmos;
312
313})();
314
315