blob: 258afd8d2ddcb755b7381446d31714f5ab0c2d81 [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
Tyler Scottd90f84e2015-07-27 12:43:04 -0600139 var scope = this;
140
Tyler Scotta1530052015-07-24 00:13:28 -0600141 this.query(this.catalog, filters,
142 function(interest, data){ //Response function
143 console.log("Query Response:", interest, data);
144
Tyler Scottd90f84e2015-07-27 12:43:04 -0600145 var parameters = JSON.stringify(filters);
146
147 var ack = data.getName();
148
149 var name = new Name(scope.catalog).append("query-results").append(parameters)
150 .append(ack.get(ack.getComponentCount() - 3)).append(ack.getComponent(ack.getComponentCount() - 2))
151
152 var first = new Name(name).appendSegment(0);
153
154 console.log("Next request:", first.toUri());
155
156 scope.face.expressInterest(new Interest(first).setInterestLifetimeMilliseconds(10000),
157 function(interest, data){ //Response
158
159 var content = JSON.parse(data.getContent().toString().replace(/[\n\0]/g,""));
160
161 var results = scope.results[0] = content.results;
162 scope.resultCount = content.resultCount;
163
164 console.log("Got results:", results);
165
166 },
167 function(interest){ //Timeout
168 console.error("Failed to retrieve results: timeout");
169 }
170 );
Tyler Scotta1530052015-07-24 00:13:28 -0600171
172
173 }, function(interest){ //Timeout function
174 console.error("Request failed! Timeout");
175 });
176
177 }
178
179 Atmos.prototype.autoComplete = function(field, callback){
180 console.log("Autocomplete triggered");
181
182 var filters = this.getFilters();
183
184 filters["?"] = this.searchInput.val();
185
186 this.query(this.catalog, filters,
187 function(interest, data){
188 console.log(interest, data);
189 }, function(interest){
190 console.error("Request failed! Timeout");
191 });
Tyler Scott93cae872015-07-21 14:58:23 -0600192
Tyler Scott424ee102015-07-14 16:50:41 -0600193 }
194
Tyler Scottd90f84e2015-07-27 12:43:04 -0600195// Atmos.prototype.onData = function(data) {
196// console.log("Recieved data", data);
Tyler Scott7d076e22015-07-06 19:21:50 -0600197
Tyler Scottd90f84e2015-07-27 12:43:04 -0600198// var payloadStr = data.content.toString().split("\n")[0];
Tyler Scott7d076e22015-07-06 19:21:50 -0600199
Tyler Scottd90f84e2015-07-27 12:43:04 -0600200// if (!payloadStr || payloadStr.length === 0){
201// this.populateResults();
202// return; //No results were returned.
203// }
Tyler Scott7d076e22015-07-06 19:21:50 -0600204
Tyler Scottd90f84e2015-07-27 12:43:04 -0600205// var queryResults = JSON.parse(payloadStr);
Tyler Scott7d076e22015-07-06 19:21:50 -0600206
Tyler Scottd90f84e2015-07-27 12:43:04 -0600207// var scope = this;
Tyler Scott93cae872015-07-21 14:58:23 -0600208
Tyler Scottd90f84e2015-07-27 12:43:04 -0600209// $.each(queryResults, function (queryResult, field) {
Tyler Scott93cae872015-07-21 14:58:23 -0600210
Tyler Scottd90f84e2015-07-27 12:43:04 -0600211// if (queryResult == "next") {
212// scope.populateAutocomplete(field);
213// }
Tyler Scott93cae872015-07-21 14:58:23 -0600214
Tyler Scottd90f84e2015-07-27 12:43:04 -0600215// if (queryResult == "results" && field == null){
216// return; //Sometimes the results are null. (We should skip this.)
217// }
Tyler Scott93cae872015-07-21 14:58:23 -0600218
Tyler Scottd90f84e2015-07-27 12:43:04 -0600219// $.each(field, function (entryCount, name) {
220// scope.results.push(name);
221// });
222// });http://stackoverflow.com/questions/26334964/json-parse-unexpected-token-error
Tyler Scott7d076e22015-07-06 19:21:50 -0600223
Tyler Scottd90f84e2015-07-27 12:43:04 -0600224// // Calculating the current page and the view
225// this.populateResults();
Tyler Scott424ee102015-07-14 16:50:41 -0600226
Tyler Scottd90f84e2015-07-27 12:43:04 -0600227// }
Tyler Scott7d076e22015-07-06 19:21:50 -0600228
Tyler Scotta1530052015-07-24 00:13:28 -0600229 Atmos.prototype.query = function(prefix, parameters, callback, timeout) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600230
Tyler Scott93cae872015-07-21 14:58:23 -0600231 var queryPrefix = new Name(prefix);
232 queryPrefix.append("query");
Tyler Scott7d076e22015-07-06 19:21:50 -0600233
Tyler Scott93cae872015-07-21 14:58:23 -0600234 var jsonString = JSON.stringify(parameters);
235 queryPrefix.append(jsonString);
Tyler Scott7d076e22015-07-06 19:21:50 -0600236
Tyler Scott93cae872015-07-21 14:58:23 -0600237 var queryInterest = new Interest(queryPrefix);
Tyler Scotta1530052015-07-24 00:13:28 -0600238 queryInterest.setInterestLifetimeMilliseconds(4000);
Tyler Scott7d076e22015-07-06 19:21:50 -0600239
Tyler Scotta1530052015-07-24 00:13:28 -0600240 this.face.expressInterest(queryInterest, callback, timeout);
Tyler Scott93cae872015-07-21 14:58:23 -0600241
Tyler Scott93cae872015-07-21 14:58:23 -0600242 }
243
Tyler Scottd90f84e2015-07-27 12:43:04 -0600244// Atmos.prototype.expressNextInterest = function() {
245// // @todo pipelines
246// var nextName = new Name(this.state["results"]);
247// nextName.appendSegment(this.state["nextSegment"]);
Tyler Scott93cae872015-07-21 14:58:23 -0600248
Tyler Scottd90f84e2015-07-27 12:43:04 -0600249// var nextInterest = new Interest(nextName);
250// nextInterest.setInterestLifetimeMilliseconds(10000);
Tyler Scott93cae872015-07-21 14:58:23 -0600251
Tyler Scottd90f84e2015-07-27 12:43:04 -0600252// var scope = this;
Tyler Scott93cae872015-07-21 14:58:23 -0600253
Tyler Scottd90f84e2015-07-27 12:43:04 -0600254// this.face.expressInterest(nextInterest,
255// function(interest, data){
256// scope.onQueryResultsData(interest, data);
257// },
258// function(interest){
259// scope.onQueryResultsTimeout(interest);
260// });
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600261
Tyler Scottd90f84e2015-07-27 12:43:04 -0600262// this.state["nextSegment"] ++;
263// this.state["outstanding"][nextName.toUri()] = 0;
264// }
Tyler Scott93cae872015-07-21 14:58:23 -0600265
Tyler Scottd90f84e2015-07-27 12:43:04 -0600266// Atmos.prototype.onQueryData = function(interest, data) {
267// var name = data.getName();
Tyler Scott93cae872015-07-21 14:58:23 -0600268
Tyler Scottd90f84e2015-07-27 12:43:04 -0600269// delete this.state["outstanding"][interest.getName().toUri()];
Tyler Scott93cae872015-07-21 14:58:23 -0600270
Tyler Scottd90f84e2015-07-27 12:43:04 -0600271// this.state["version"] = name.get(this.state["prefix"].size() + 2).toVersion();
Tyler Scott93cae872015-07-21 14:58:23 -0600272
Tyler Scottd90f84e2015-07-27 12:43:04 -0600273// this.state["results"] = new Name(this.state["prefix"]).append("query-results").append(this.state['parameters'])
274// .appendVersion(this.state["version"]).append(name.getComponent(name.getComponentCount() - 2));
Tyler Scott93cae872015-07-21 14:58:23 -0600275
Tyler Scottd90f84e2015-07-27 12:43:04 -0600276// console.log("Requested URI", this.state.results.toUri());
Tyler Scott93cae872015-07-21 14:58:23 -0600277
Tyler Scottd90f84e2015-07-27 12:43:04 -0600278// this.expressNextInterest();
279// }
Tyler Scott93cae872015-07-21 14:58:23 -0600280
Tyler Scottd90f84e2015-07-27 12:43:04 -0600281// Atmos.prototype.onQueryResultsData = function(interest, data) {
282// var name = data.getName();
283// delete this.state["outstanding"][interest.getName().toUri()];
284// if (!name.get(-1).equals(data.getMetaInfo().getFinalBlockId())) {
285// this.expressNextInterest();
286// } //else {
287// //alert("found final block");
288// //}
Tyler Scott93cae872015-07-21 14:58:23 -0600289
Tyler Scottd90f84e2015-07-27 12:43:04 -0600290// this.state["userOnData"](data);
291// }
Tyler Scott93cae872015-07-21 14:58:23 -0600292
Tyler Scottd90f84e2015-07-27 12:43:04 -0600293// Atmos.prototype.onQueryTimeout = 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(interest, data){
300// scope.onQueryData(interest, data);
301// },
302// function(interest){
303// scope.onQueryTimeout(interest);
304// });
305// } else {
306// delete this.state["outstanding"][uri];
Tyler Scott93cae872015-07-21 14:58:23 -0600307
Tyler Scottd90f84e2015-07-27 12:43:04 -0600308// // We modify the autocomplete box here because we need to know
309// // we have all of the entries first. Fairly hacky.
310// /* TODO FIXME
311// var autocompleteFullName = this.autocompleteText.value;
312// for (var i = 0; i < dropdown.length; ++i) {
313// if (this.dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === this.autocompleteText.value.toUpperCase || dropdown.length == 1) {
314// autocompleteText.value = dropdown[i];
315// }
316// }
317// */
318// }
319// }
Tyler Scott7d076e22015-07-06 19:21:50 -0600320
Tyler Scottd90f84e2015-07-27 12:43:04 -0600321// Atmos.prototype.onQueryResultsTimeout = function(interest) {
322// var uri = interest.getName().toUri();
323// if (this.state["outstanding"][uri] < 1) {
324// this.state["outstanding"][uri] ++;
325// var scope = this;
326// this.face.expressInterest(interest,
327// function(){
328// scope.onQueryResultsData.apply(scope, arguments);
329// },
330// function(){
331// scope.onQueryResultsTimeout.apply(scope, arguments);
332// });
333// } else {
334// delete this.state["outstanding"][uri];
335// // We modify the autocomplete box here because we need to know
336// // we have all of the entries first. Fairly hacky.
337// /* TODO FIXME
338// var autocompleteFullName = autocompleteText.value;
339// for (var i = 0; i < dropdown.length; ++i) {
340// if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) {
341// autocompleteText.value = dropdown[i];
342// }
343// }
344// */
345// }
346// }
347// **/
Tyler Scott7d076e22015-07-06 19:21:50 -0600348
Tyler Scottd90f84e2015-07-27 12:43:04 -0600349// Atmos.prototype.populateResults = function() {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600350
Tyler Scottd90f84e2015-07-27 12:43:04 -0600351// //TODO Check only for page changes and result length
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600352
Tyler Scottd90f84e2015-07-27 12:43:04 -0600353// this.resultTable.empty();
Tyler Scott93cae872015-07-21 14:58:23 -0600354
Tyler Scottd90f84e2015-07-27 12:43:04 -0600355// for (var i = startIndex; i < startIndex + 20 && i < this.results.length; ++i) {
356// this.resultTable.append('<tr><td>' + this.results[i]
357// + '</td><td><button class="interest-button btn btn-primary btn-xs">Retrieve</button></td></tr>');
358// }
Tyler Scott93cae872015-07-21 14:58:23 -0600359
Tyler Scottd90f84e2015-07-27 12:43:04 -0600360// if (this.results.length <= 20) {
361// this.page = 1;
362// } else {
363// this.page = startIndex / 20 + 1;
364// }
Tyler Scott93cae872015-07-21 14:58:23 -0600365
Tyler Scottd90f84e2015-07-27 12:43:04 -0600366// this.totalPages = Math.ceil(this.results.length / 20);
Tyler Scott93cae872015-07-21 14:58:23 -0600367
Tyler Scottd90f84e2015-07-27 12:43:04 -0600368// //TODO Fix the page to fit the theme.
369// var currentPage = $(".page");
370// currentPage.empty();
371// if (this.page != 1) {
372// currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>');
373// }
374// // This section of code creates the paging for the results.
375// // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after
376// // the current page and the total pages (expect users not to really jump around a lot).
377// for (var i = 1; i <= this.totalPages; ++i) {
378// if (i == 1 || i == this.totalPages // Min or max
379// || (i <= this.page && i + 5 >= this.page) // in our current page range
380// || (i >= this.page && i - 5 <= this.page)) { // in our current page range
381// if (i != this.page) {
382// currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>');
383// if (i == 1 && this.page > i + 5) {
384// currentPage.append(' ... ');
385// }
386// } else {
387// currentPage.append(' ' + i);
388// }
389// } else { // Need to skip ahead
390// if (i == this.page + 6) {
391// currentPage.append(' ... ');
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600392
Tyler Scottd90f84e2015-07-27 12:43:04 -0600393// currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>')
394// i = this.totalPages - 1;
395// }
396// }
397// }
398// currentPage.append(' ' + this.results.length + ' results');
399// }
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600400
Tyler Scottd90f84e2015-07-27 12:43:04 -0600401// Atmos.prototype.getPage = function(clickedPage) {
402// console.log(clickedPage);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600403
Tyler Scottd90f84e2015-07-27 12:43:04 -0600404// var nextPage = clickedPage;
405// if (clickedPage === "<") {
406// nextPage = this.page - 5;
407// } else if (clickedPage === ">") {
408// console.log("> enabled");
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600409
Tyler Scottd90f84e2015-07-27 12:43:04 -0600410// nextPage = this.page + 5;
411// }
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600412
Tyler Scottd90f84e2015-07-27 12:43:04 -0600413// nextPage--; // Need to adjust for starting at 0
Tyler Scott93cae872015-07-21 14:58:23 -0600414
Tyler Scottd90f84e2015-07-27 12:43:04 -0600415// if (nextPage < 0 ) {
416// nextPage = 0;
417// console.log("0 enabled");
418// } else if (nextPage > this.totalPages - 1) {
419// nextPage = this.totalPages - 1;
420// console.log("total enabled");
421// }
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600422
Tyler Scottd90f84e2015-07-27 12:43:04 -0600423// this.populateResults(nextPage * 20);
424// return false;
425// }
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600426
Tyler Scott93cae872015-07-21 14:58:23 -0600427 /**
Tyler Scotta1530052015-07-24 00:13:28 -0600428 * This function returns a map of all the categories active filters.
429 * @return {Object<string, string>}
Tyler Scott93cae872015-07-21 14:58:23 -0600430 */
Tyler Scotta1530052015-07-24 00:13:28 -0600431 Atmos.prototype.getFilters = function(){
Tyler Scott93cae872015-07-21 14:58:23 -0600432 var filters = this.filters.children().toArray().reduce(function(prev, current){
Tyler Scott575c61b2015-07-13 13:42:16 -0600433 var data = $(current).text().split(/:/);
434 prev[data[0]] = data[1];
435 return prev;
Tyler Scotta1530052015-07-24 00:13:28 -0600436 }, {}); //Collect a map<category, filter>.
437 //TODO Make the return value map<category, Array<filter>>
438 return filters;
Tyler Scott93cae872015-07-21 14:58:23 -0600439 }
440
441 return Atmos;
442
443})();
444
445