blob: f3431976d51fe5296c5a04ef86c2c64cbff68856 [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 Scott93cae872015-07-21 14:58:23 -060029 this.page = 1;
Tyler Scotta1530052015-07-24 00:13:28 -060030 this.itemsPerPage = 25;
Tyler Scotte815d3e2015-07-09 16:56:17 -060031
Tyler Scott93cae872015-07-21 14:58:23 -060032 this.catalog = catalog;
Tyler Scott7d076e22015-07-06 19:21:50 -060033
Tyler Scott93cae872015-07-21 14:58:23 -060034 this.face = new Face(config);
35 this.categories = $('#side-menu');
36 this.resultTable = $('#resultTable');
37 this.filters = $('#filters');
Tyler Scotta1530052015-07-24 00:13:28 -060038 this.searchInput = $('#search');
39 this.searchBar = $('#searchBar');
Tyler Scott7d076e22015-07-06 19:21:50 -060040
Tyler Scott93cae872015-07-21 14:58:23 -060041 var scope = this;
Tyler Scott7d076e22015-07-06 19:21:50 -060042
Tyler Scott93cae872015-07-21 14:58:23 -060043 this.resultTable.on('click', '.interest-button', function(){
44 var button = $(this);
Tyler Scott087aef72015-07-14 14:11:59 -060045
Tyler Scott93cae872015-07-21 14:58:23 -060046 if (button.is(':disabled')){
47 console.warn("Attempt to request again!");
Tyler Scott7d076e22015-07-06 19:21:50 -060048 }
Tyler Scott93cae872015-07-21 14:58:23 -060049
50 var name = button.parent().prev().text();
51 var interest = new Interest(new Name('/retrieve' + name));
52 scope.face.expressInterest(interest, function(){}, function(){});
53
54 button.text("Requested!")
55 .removeClass('btn-primary')
56 .addClass('btn-default')
57 .addClass('disabled')
58 .prop('disabled', true);
Tyler Scott7d076e22015-07-06 19:21:50 -060059 });
Tyler Scott7d076e22015-07-06 19:21:50 -060060
Tyler Scotta1530052015-07-24 00:13:28 -060061 //Filter setup
Tyler Scott93cae872015-07-21 14:58:23 -060062 $.getJSON("search_catagories.json").done(function (data) {
63 $.each(data, function (pageSection, contents) {
64 if (pageSection == "SearchCatagories") {
Tyler Scotta1530052015-07-24 00:13:28 -060065 $.each(contents, function (category, searchOptions) {
66 //Create the category
67 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 -060068
Tyler Scott93cae872015-07-21 14:58:23 -060069 var sub = e.find('ul.subnav');
70 $.each(searchOptions, function(index, name){
Tyler Scotta1530052015-07-24 00:13:28 -060071 //Create the filter list inside the category
Tyler Scott93cae872015-07-21 14:58:23 -060072 var item = $('<li><a href="#">' + name + '</a></li>');
73 sub.append(item);
Tyler Scotta1530052015-07-24 00:13:28 -060074 item.click(function(){ //Click on the side menu filters
75 if (item.hasClass('active')){ //Does the filter already exist?
76 item.removeClass('active');
77 scope.filters.find(':contains(' + category + ':' + name + ')').remove();
78 } else { //Add a filter
79 item.addClass('active');
80 var filter = $('<span class="label label-default"></span>');
81 filter.text(category + ':' + name);
82
83 scope.filters.append(filter);
84
85 filter.click(function(){ //Click on a filter
86 filter.remove();
87 item.removeClass('active');
88 });
89 }
90
Tyler Scott93cae872015-07-21 14:58:23 -060091 });
92 });
Tyler Scott7d076e22015-07-06 19:21:50 -060093
Tyler Scott93cae872015-07-21 14:58:23 -060094 //Toggle the menus. (Only respond when the immediate tab is clicked.)
95 e.find('> a').click(function(){
96 scope.categories.find('.subnav').slideUp();
97 var t = $(this).siblings('.subnav');
Tyler Scotta1530052015-07-24 00:13:28 -060098 if ( !t.is(':visible') ){ //If the sub menu is not visible
99 t.slideDown(function(){
100 t.triggerHandler('focus');
101 }); //Make it visible and look at it.
Tyler Scott93cae872015-07-21 14:58:23 -0600102 }
103 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600104
Tyler Scott93cae872015-07-21 14:58:23 -0600105 scope.categories.append(e);
Tyler Scotta1530052015-07-24 00:13:28 -0600106
Tyler Scott93cae872015-07-21 14:58:23 -0600107 });
108 }
109 });
110 });
111
Tyler Scotta1530052015-07-24 00:13:28 -0600112 this.searchBar.submit(function(e){
Tyler Scott93cae872015-07-21 14:58:23 -0600113 e.preventDefault();
Tyler Scotta1530052015-07-24 00:13:28 -0600114 scope.search();
115 });
116
117 this.searchInput.autoComplete(function(field, callback){
118 scope.autoComplete(field, callback);
119 });
120
121 }
122
123 Atmos.prototype.search = function(){
124 var filters = this.getFilters();
125
126 if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){
127 if (!this.searchBar.hasClass('has-error')){
128 this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>');
129 }
130 return;
131 } else {
132 this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()});
133 }
134
135 console.log("Search started!", this.searchInput.val(), filters);
136
137 console.log("Initiating query");
138
139 this.query(this.catalog, filters,
140 function(interest, data){ //Response function
141 console.log("Query Response:", interest, data);
142
143
144
145 }, function(interest){ //Timeout function
146 console.error("Request failed! Timeout");
147 });
148
149 }
150
151 Atmos.prototype.autoComplete = function(field, callback){
152 console.log("Autocomplete triggered");
153
154 var filters = this.getFilters();
155
156 filters["?"] = this.searchInput.val();
157
158 this.query(this.catalog, filters,
159 function(interest, data){
160 console.log(interest, data);
161 }, function(interest){
162 console.error("Request failed! Timeout");
163 });
Tyler Scott93cae872015-07-21 14:58:23 -0600164
Tyler Scott424ee102015-07-14 16:50:41 -0600165 }
166
Tyler Scott93cae872015-07-21 14:58:23 -0600167 Atmos.prototype.onData = function(data) {
168 console.log("Recieved data", data);
Tyler Scott7d076e22015-07-06 19:21:50 -0600169
Tyler Scott93cae872015-07-21 14:58:23 -0600170 var payloadStr = data.content.toString().split("\n")[0];
Tyler Scott7d076e22015-07-06 19:21:50 -0600171
Tyler Scott93cae872015-07-21 14:58:23 -0600172 if (!payloadStr || payloadStr.length === 0){
173 this.populateResults();
174 return; //No results were returned.
Tyler Scottbb013562015-07-16 15:52:40 -0600175 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600176
Tyler Scott93cae872015-07-21 14:58:23 -0600177 var queryResults = JSON.parse(payloadStr);
Tyler Scott7d076e22015-07-06 19:21:50 -0600178
Tyler Scott93cae872015-07-21 14:58:23 -0600179 var scope = this;
180
181 $.each(queryResults, function (queryResult, field) {
182
183 if (queryResult == "next") {
184 scope.populateAutocomplete(field);
185 }
186
187 if (queryResult == "results" && field == null){
188 return; //Sometimes the results are null. (We should skip this.)
189 }
190
191 $.each(field, function (entryCount, name) {
192 scope.results.push(name);
193 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600194 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600195
Tyler Scott93cae872015-07-21 14:58:23 -0600196 // Calculating the current page and the view
197 this.populateResults();
Tyler Scott424ee102015-07-14 16:50:41 -0600198
Tyler Scott93cae872015-07-21 14:58:23 -0600199 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600200
Tyler Scotta1530052015-07-24 00:13:28 -0600201 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600202
Tyler Scott93cae872015-07-21 14:58:23 -0600203 var queryPrefix = new Name(prefix);
204 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600205
Tyler Scott93cae872015-07-21 14:58:23 -0600206 var jsonString = JSON.stringify(parameters);
207 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600208
Tyler Scott93cae872015-07-21 14:58:23 -0600209 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600210 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scott7d076e22015-07-06 19:21:50 -0600211
Tyler Scotta1530052015-07-24 00:13:28 -0600212 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600213
Tyler Scott93cae872015-07-21 14:58:23 -0600214 }
215
216 Atmos.prototype.expressNextInterest = function() {
217 // @todo pipelines
218 var nextName = new Name(this.state["results"]);
219 nextName.appendSegment(this.state["nextSegment"]);
220
221 var nextInterest = new Interest(nextName);
222 nextInterest.setInterestLifetimeMilliseconds(10000);
223
224 var scope = this;
225
226 this.face.expressInterest(nextInterest,
227 function(interest, data){
228 scope.onQueryResultsData(interest, data);
Tyler Scott7d076e22015-07-06 19:21:50 -0600229 },
Tyler Scott575c61b2015-07-13 13:42:16 -0600230 function(interest){
Tyler Scott93cae872015-07-21 14:58:23 -0600231 scope.onQueryResultsTimeout(interest);
Tyler Scott7d076e22015-07-06 19:21:50 -0600232 });
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600233
Tyler Scott93cae872015-07-21 14:58:23 -0600234 this.state["nextSegment"] ++;
235 this.state["outstanding"][nextName.toUri()] = 0;
236 }
237
238 Atmos.prototype.onQueryData = function(interest, data) {
239 var name = data.getName();
240
241 delete this.state["outstanding"][interest.getName().toUri()];
242
243 this.state["version"] = name.get(this.state["prefix"].size() + 2).toVersion();
244
245 this.state["results"] = new Name(this.state["prefix"]).append("query-results").append(this.state['parameters'])
246 .appendVersion(this.state["version"]).append(name.getComponent(name.getComponentCount() - 2));
247
248 console.log("Requested URI", this.state.results.toUri());
249
250 this.expressNextInterest();
251 }
252
253 Atmos.prototype.onQueryResultsData = function(interest, data) {
254 var name = data.getName();
255 delete this.state["outstanding"][interest.getName().toUri()];
256 if (!name.get(-1).equals(data.getMetaInfo().getFinalBlockId())) {
257 this.expressNextInterest();
258 } //else {
259 //alert("found final block");
260 //}
261
262 this.state["userOnData"](data);
263 }
264
265 Atmos.prototype.onQueryTimeout = function(interest) {
266 var uri = interest.getName().toUri();
267 if (this.state["outstanding"][uri] < 1) {
268 this.state["outstanding"][uri] ++;
269 var scope = this;
270 this.face.expressInterest(interest,
271 function(interest, data){
272 scope.onQueryData(interest, data);
273 },
274 function(interest){
275 scope.onQueryTimeout(interest);
276 });
277 } else {
278 delete this.state["outstanding"][uri];
279
280 // We modify the autocomplete box here because we need to know
281 // we have all of the entries first. Fairly hacky.
282 /* TODO FIXME
283 var autocompleteFullName = this.autocompleteText.value;
284 for (var i = 0; i < dropdown.length; ++i) {
285 if (this.dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === this.autocompleteText.value.toUpperCase || dropdown.length == 1) {
286 autocompleteText.value = dropdown[i];
287 }
288 }
289 */
Tyler Scott7d076e22015-07-06 19:21:50 -0600290 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600291 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600292
Tyler Scott93cae872015-07-21 14:58:23 -0600293 Atmos.prototype.onQueryResultsTimeout = function(interest) {
294 var uri = interest.getName().toUri();
295 if (this.state["outstanding"][uri] < 1) {
296 this.state["outstanding"][uri] ++;
297 var scope = this;
298 this.face.expressInterest(interest,
299 function(){
300 scope.onQueryResultsData.apply(scope, arguments);
301 },
302 function(){
303 scope.onQueryResultsTimeout.apply(scope, arguments);
304 });
305 } else {
306 delete this.state["outstanding"][uri];
307 // We modify the autocomplete box here because we need to know
308 // we have all of the entries first. Fairly hacky.
309 /* TODO FIXME
310 var autocompleteFullName = autocompleteText.value;
311 for (var i = 0; i < dropdown.length; ++i) {
312 if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) {
313 autocompleteText.value = dropdown[i];
314 }
315 }
316 */
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600317 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600318 }
319
Tyler Scott93cae872015-07-21 14:58:23 -0600320 Atmos.prototype.populateResults = function() {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600321
Tyler Scott93cae872015-07-21 14:58:23 -0600322 //TODO Check only for page changes and result length
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600323
Tyler Scott93cae872015-07-21 14:58:23 -0600324 this.resultTable.empty();
325
326 for (var i = startIndex; i < startIndex + 20 && i < this.results.length; ++i) {
327 this.resultTable.append('<tr><td>' + this.results[i]
328 + '</td><td><button class="interest-button btn btn-primary btn-xs">Retrieve</button></td></tr>');
329 }
330
331 if (this.results.length <= 20) {
332 this.page = 1;
333 } else {
334 this.page = startIndex / 20 + 1;
335 }
336
337 this.totalPages = Math.ceil(this.results.length / 20);
338
339 //TODO Fix the page to fit the theme.
340 var currentPage = $(".page");
341 currentPage.empty();
342 if (this.page != 1) {
343 currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>');
344 }
345 // This section of code creates the paging for the results.
346 // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after
347 // the current page and the total pages (expect users not to really jump around a lot).
348 for (var i = 1; i <= this.totalPages; ++i) {
349 if (i == 1 || i == this.totalPages // Min or max
350 || (i <= this.page && i + 5 >= this.page) // in our current page range
351 || (i >= this.page && i - 5 <= this.page)) { // in our current page range
352 if (i != this.page) {
Tyler Scotta1530052015-07-24 00:13:28 -0600353 currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>');
Tyler Scott93cae872015-07-21 14:58:23 -0600354 if (i == 1 && this.page > i + 5) {
355 currentPage.append(' ... ');
356 }
357 } else {
358 currentPage.append(' ' + i);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600359 }
Tyler Scott93cae872015-07-21 14:58:23 -0600360 } else { // Need to skip ahead
361 if (i == this.page + 6) {
362 currentPage.append(' ... ');
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600363
Tyler Scott93cae872015-07-21 14:58:23 -0600364 currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>')
365 i = this.totalPages - 1;
366 }
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600367 }
368 }
Tyler Scott93cae872015-07-21 14:58:23 -0600369 currentPage.append(' ' + this.results.length + ' results');
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600370 }
371
Tyler Scott93cae872015-07-21 14:58:23 -0600372 Atmos.prototype.getPage = function(clickedPage) {
373 console.log(clickedPage);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600374
Tyler Scott93cae872015-07-21 14:58:23 -0600375 var nextPage = clickedPage;
376 if (clickedPage === "<") {
377 nextPage = this.page - 5;
378 } else if (clickedPage === ">") {
379 console.log("> enabled");
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600380
Tyler Scott93cae872015-07-21 14:58:23 -0600381 nextPage = this.page + 5;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600382 }
383
Tyler Scott93cae872015-07-21 14:58:23 -0600384 nextPage--; // Need to adjust for starting at 0
385
386 if (nextPage < 0 ) {
387 nextPage = 0;
388 console.log("0 enabled");
389 } else if (nextPage > this.totalPages - 1) {
390 nextPage = this.totalPages - 1;
391 console.log("total enabled");
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600392 }
393
Tyler Scott93cae872015-07-21 14:58:23 -0600394 this.populateResults(nextPage * 20);
395 return false;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600396 }
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600397
Tyler Scott93cae872015-07-21 14:58:23 -0600398 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600399 * This function returns a map of all the categories active filters.
400 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600401 */
Tyler Scotta1530052015-07-24 00:13:28 -0600402 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600403 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600404 var data = $(current).text().split(/:/);
405 prev[data[0]] = data[1];
406 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600407 }, {}); //Collect a map<category, filter>.
408 //TODO Make the return value map<category, Array<filter>>
409 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600410 }
411
412 return Atmos;
413
414})();
415
416