blob: 3de2220cee9ab9ba059c0eb537604d9dc4f714e7 [file] [log] [blame]
Tyler Scottf08ab962015-06-30 16:31:29 -06001//{@ @todo: this need to be configured before the document load
Tyler Scott3c17d5f2015-06-23 17:49:29 -06002var catalog = "/catalog/myUniqueName";
Tyler Scotta1ac69d2015-07-02 17:42:03 -06003var config = {
4 host: "atmos-csu.research-lan.colostate.edu",
Tyler Scott3c17d5f2015-06-23 17:49:29 -06005 port: 9696
Tyler Scotta1ac69d2015-07-02 17:42:03 -06006};
Tyler Scott3c17d5f2015-06-23 17:49:29 -06007
8// @}
9
Tyler Scotta1ac69d2015-07-02 17:42:03 -060010var atmos = {}; //Comment this out if you don't want debug access.
11
12//Run when the document loads.
13$(function () {
Tyler Scott7d076e22015-07-06 19:21:50 -060014
15 //remove "atmos =" if you don't want debug access
Tyler Scotta1ac69d2015-07-02 17:42:03 -060016 atmos = new Atmos(catalog, config);
Tyler Scottf65b7102015-06-30 18:40:14 -060017
Tyler Scott3c17d5f2015-06-23 17:49:29 -060018});
19
Tyler Scott7d076e22015-07-06 19:21:50 -060020/*
Tyler Scotte815d3e2015-07-09 16:56:17 -060021
Tyler Scott7d076e22015-07-06 19:21:50 -060022*/
23
Tyler Scotte815d3e2015-07-09 16:56:17 -060024/**
25 * Atmos
26 * @version 2.0
27 *
28 * Configures an Atmos object. This manages the atmos interface.
29 *
30 * @constructor
31 * @param {string} catalog - NDN path
32 * @param {Object} config - Object of configuration options for a Face.
33 */
Tyler Scott7d076e22015-07-06 19:21:50 -060034function Atmos(catalog, config){
35 "use strict";
36 //Internal variables.
37 this.results = []
38 this.resultCount = 0;
39 this.page = 1;
40 this.totalPages = 1;
41 this.selectedSearch = {};
42 this.dropdown = [];
43 this.state = {};
44 this.currentViewIndex = 0;
45
Tyler Scotte815d3e2015-07-09 16:56:17 -060046 this.catalog = catalog;
47
Tyler Scott7d076e22015-07-06 19:21:50 -060048 this.face = new Face(config);
49 this.categories = $('#side-menu');
50 this.resultTable = $('#resultTable');
Tyler Scotte815d3e2015-07-09 16:56:17 -060051 this.filters = $('#filters');
Tyler Scott7d076e22015-07-06 19:21:50 -060052
53 var scope = this;
54
55 this.resultTable.on('click', '.interest-button', function(){
56 var button = $(this);
57
58 var name = button.parent().prev().text();
59 var interest = new Interest(new Name('/retrieve' + name));
60 scope.face.expressInterest(interest, function(){
61 var message = $('<div class="success"><span class="glyphicon glyphicon-ok"></span> Success!</div>');
62 message.insertAfter(button);
63 message.fadeOut(5000);
64 }, function(){
65 var message = $('<div class="fail"><span class="glyphicon glyphicon-remove"></span> Failed!</div>');
66 message.insertAfter(button);
67 message.fadeOut(5000);
68 });
69
70 });
71
72 $.getJSON("search_catagories.json").done(function (data) {
73 $.each(data, function (pageSection, contents) {
74 if (pageSection == "SearchCatagories") {
75 $.each(contents, function (search, searchOptions) {
76 var e = $('<li><a href="#">' + search.replace(/\_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>');
77
78 var sub = e.find('ul.subnav');
79 $.each(searchOptions, function(index, name){
80 var item = $('<li><a href="#">' + name + '</a></li>');
81 sub.append(item);
82 item.click(function(){
Tyler Scotte815d3e2015-07-09 16:56:17 -060083 scope.addFilter(name);
Tyler Scott7d076e22015-07-06 19:21:50 -060084 });
85 });
86
87 //Toggle the menus.
88 e.click(function(){
89 scope.categories.find('.subnav').slideUp();
90 var t = $(this).find('.subnav');
91 if ( !t.is(':visible')){
92 t.slideDown().triggerHandler('focus'); //Cancel other animations and slide down.
93 }
94 });
95
96 scope.categories.append(e);
97 });
98 }
99 });
100 });
101
102 $('#searchBar').submit(function(e){
103 e.preventDefault();
Tyler Scotte815d3e2015-07-09 16:56:17 -0600104 console.log("Search started!", $('#search'));
Tyler Scott7d076e22015-07-06 19:21:50 -0600105 })
106
107}
108
109Atmos.prototype.onData = function(data) {
110 var payloadStr = data.content.toString().split("\n")[0];
111
112 var queryResults = JSON.parse(payloadStr);
113
114 var scope = this;
115
116 $.each(this.queryResults, function (queryResult, field) {
117
118 if (queryResult == "next") {
119 scope.populateAutocomplete(field);
120 }
121
122 $.each(field, function (entryCount, name) {
123 scope.results.push(name);
124 });
125 });
126
127 // Calculating the current page and the view
128 this.totalPages = Math.ceil(this.resultCount / 20);
129 this.populateResults(0);
130}
131
132Atmos.prototype.query = function(prefix, parameters, callback, pipeline) {
133 this.results = [];
134 this.dropdown = [];
135 this.resultTable.empty();
136 this.resultTable.append('<tr><th colspan="2">Results</th></tr>');
137
138 var queryPrefix = new Name(prefix);
139 queryPrefix.append("query");
140
141 var jsonString = JSON.stringify(parameters);
142 queryPrefix.append(jsonString);
143
144 this.state = {
145 prefix: new Name(prefix),
146 userOnData: callback,
147 outstanding: {},
148 nextSegment: 0,
149 };
150
151 /*if (state.hasOwnProperty("version")) {
152 console.log("state already has version");
153 }*/
154
155 var queryInterest = new Interest(queryPrefix);
156 queryInterest.setInterestLifetimeMilliseconds(10000);
157
158 var scope = this;
159
160 this.face.expressInterest(queryInterest,
Tyler Scotte815d3e2015-07-09 16:56:17 -0600161 function(interest, data){
162 scope.onQueryData(interest, data);
163 }, function(interest){
164 scope.onQueryTimeout(interest);
Tyler Scott7d076e22015-07-06 19:21:50 -0600165 }
166 );
167
168 this.state["outstanding"][queryInterest.getName().toUri()] = 0;
169}
170
Tyler Scotte815d3e2015-07-09 16:56:17 -0600171/**
172 * @deprecated
173 * Use applyFilters/addFilters as appropriate.
174 */
Tyler Scott7d076e22015-07-06 19:21:50 -0600175Atmos.prototype.submitCatalogSearch = function(field) {
Tyler Scotte815d3e2015-07-09 16:56:17 -0600176 console.warn("Use of deprecated function submitCatalogSearch! (Use applyFilters/addFilters as appropriate)");
Tyler Scott7d076e22015-07-06 19:21:50 -0600177}
178
179Atmos.prototype.expressNextInterest = function() {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600180 // @todo pipelines
Tyler Scotte815d3e2015-07-09 16:56:17 -0600181 var nextName = new Name(this.state["results"]);
182 nextName.appendSegment(this.state["nextSegment"]);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600183
184 var nextInterest = new Interest(nextName);
185 nextInterest.setInterestLifetimeMilliseconds(10000);
186
Tyler Scott7d076e22015-07-06 19:21:50 -0600187 var scope = this;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600188
Tyler Scott7d076e22015-07-06 19:21:50 -0600189 this.face.expressInterest(nextInterest,
Tyler Scotte815d3e2015-07-09 16:56:17 -0600190 function(interest, data){
191 scope.onQueryResultsData(interest, data);
Tyler Scott7d076e22015-07-06 19:21:50 -0600192 },
Tyler Scotte815d3e2015-07-09 16:56:17 -0600193 function(interest){
194 scope.onQueryResultsTimeout(interest);
Tyler Scott7d076e22015-07-06 19:21:50 -0600195 });
196
197 this.state["nextSegment"] ++;
198 this.state["outstanding"][nextName.toUri()] = 0;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600199}
200
Tyler Scott7d076e22015-07-06 19:21:50 -0600201Atmos.prototype.onQueryData = function(interest, data) {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600202 var name = data.getName();
203
Tyler Scott7d076e22015-07-06 19:21:50 -0600204 delete this.state["outstanding"][interest.getName().toUri()];
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600205
Tyler Scott7d076e22015-07-06 19:21:50 -0600206 this.state["version"] = name.get(this.state["prefix"].size() + 2).toVersion();
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600207
Tyler Scott7d076e22015-07-06 19:21:50 -0600208 this.state["results"] = new Name(this.state["prefix"]).append("query-results").appendVersion(this.state["version"]);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600209
Tyler Scott7d076e22015-07-06 19:21:50 -0600210 this.expressNextInterest();
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600211}
212
Tyler Scott7d076e22015-07-06 19:21:50 -0600213Atmos.prototype.onQueryResultsData = function(interest, data) {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600214 var name = data.getName();
Tyler Scott7d076e22015-07-06 19:21:50 -0600215 delete this.state["outstanding"][interest.getName().toUri()];
Tyler Scottf08ab962015-06-30 16:31:29 -0600216 if (!name.get(-1).equals(data.getMetaInfo().getFinalBlockId())) {
Tyler Scott7d076e22015-07-06 19:21:50 -0600217 this.expressNextInterest();
Tyler Scottf08ab962015-06-30 16:31:29 -0600218 } //else {
219 //alert("found final block");
220 //}
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600221
Tyler Scott7d076e22015-07-06 19:21:50 -0600222 this.state["userOnData"](data);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600223}
224
Tyler Scott7d076e22015-07-06 19:21:50 -0600225Atmos.prototype.onQueryTimeout = function(interest) {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600226 var uri = interest.getName().toUri();
Tyler Scott7d076e22015-07-06 19:21:50 -0600227 if (this.state["outstanding"][uri] < 1) {
228 this.state["outstanding"][uri] ++;
229 var scope = this;
230 this.face.expressInterest(interest,
231 function(){
232 scope.onQueryData.apply(scope, arguments);
233 },
234 function(){
235 scope.onQueryTimeout.apply(scope, arguments);
236 });
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600237 } else {
Tyler Scott7d076e22015-07-06 19:21:50 -0600238 delete this.state["outstanding"][uri];
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600239
240 // We modify the autocomplete box here because we need to know
241 // we have all of the entries first. Fairly hacky.
Tyler Scott7d076e22015-07-06 19:21:50 -0600242 /* TODO FIXME
243 var autocompleteFullName = this.autocompleteText.value;
244 for (var i = 0; i < dropdown.length; ++i) {
245 if (this.dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === this.autocompleteText.value.toUpperCase || dropdown.length == 1) {
246 autocompleteText.value = dropdown[i];
247 }
248 }
249 */
250 }
251}
252
253Atmos.prototype.onQueryResultsTimeout = function(interest) {
254 var uri = interest.getName().toUri();
255 if (this.state["outstanding"][uri] < 1) {
256 this.state["outstanding"][uri] ++;
257 var scope = this;
258 this.face.expressInterest(interest,
259 function(){
260 scope.onQueryResultsData.apply(scope, arguments);
261 },
262 function(){
263 scope.onQueryResultsTimeout.apply(scope, arguments);
264 });
265 } else {
266 delete this.state["outstanding"][uri];
267 // We modify the autocomplete box here because we need to know
268 // we have all of the entries first. Fairly hacky.
269 /* TODO FIXME
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600270 var autocompleteFullName = autocompleteText.value;
271 for (var i = 0; i < dropdown.length; ++i) {
272 if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) {
273 autocompleteText.value = dropdown[i];
274 }
275 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600276 */
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600277 }
278}
279
Tyler Scott7d076e22015-07-06 19:21:50 -0600280Atmos.prototype.populateResults = function(startIndex) {
281 this.resultTable.empty();
282 this.resultTable.append('<tr><th colspan="2">Results</th></tr>');
283
284
285 for (var i = startIndex; i < startIndex + 20 && i < this.results.length; ++i) {
286 resultTable.append('<tr><td>' + this.results[i]
287 + '</td><td><button class="interest-button btn btn-default btn-xs">Retrieve</button></td></tr>');
288 }
289
290 if (this.results.length <= 20) {
291 this.page = 1;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600292 } else {
Tyler Scott7d076e22015-07-06 19:21:50 -0600293 this.page = startIndex / 20 + 1;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600294 }
295
Tyler Scott7d076e22015-07-06 19:21:50 -0600296 this.totalPages = Math.ceil(this.results.length / 20);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600297
Tyler Scott7d076e22015-07-06 19:21:50 -0600298 //TODO Fix the page to fit the theme.
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600299 var currentPage = $(".page");
300 currentPage.empty();
Tyler Scott7d076e22015-07-06 19:21:50 -0600301 if (this.page != 1) {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600302 currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>');
303 }
304 // This section of code creates the paging for the results.
305 // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after
306 // the current page and the total pages (expect users not to really jump around a lot).
Tyler Scott7d076e22015-07-06 19:21:50 -0600307 for (var i = 1; i <= this.totalPages; ++i) {
308 if (i == 1 || i == this.totalPages // Min or max
309 || (i <= this.page && i + 5 >= this.page) // in our current page range
310 || (i >= this.page && i - 5 <= this.page)) { // in our current page range
311 if (i != this.page) {
Tyler Scottf08ab962015-06-30 16:31:29 -0600312 currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>')
Tyler Scott7d076e22015-07-06 19:21:50 -0600313 if (i == 1 && this.page > i + 5) {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600314 currentPage.append(' ... ');
315 }
316 } else {
317 currentPage.append(' ' + i);
318 }
319 } else { // Need to skip ahead
320 if (i == page + 6) {
321 currentPage.append(' ... ');
322
Tyler Scottf08ab962015-06-30 16:31:29 -0600323 currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>')
Tyler Scott7d076e22015-07-06 19:21:50 -0600324 i = this.totalPages - 1;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600325 }
326 }
327 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600328 currentPage.append(' ' + this.results.length + ' results');
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600329}
330
Tyler Scott7d076e22015-07-06 19:21:50 -0600331Atmos.prototype.getPage = function(clickedPage) {
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600332 console.log(clickedPage);
333
334 var nextPage = clickedPage;
335 if (clickedPage === "<") {
Tyler Scott7d076e22015-07-06 19:21:50 -0600336 nextPage = this.page - 5;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600337 } else if (clickedPage === ">") {
338 console.log("> enabled");
339
Tyler Scott7d076e22015-07-06 19:21:50 -0600340 nextPage = this.page + 5;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600341 }
342
343 nextPage--; // Need to adjust for starting at 0
344
345 if (nextPage < 0 ) {
346 nextPage = 0;
347 console.log("0 enabled");
Tyler Scott7d076e22015-07-06 19:21:50 -0600348 } else if (nextPage > this.totalPages - 1) {
349 nextPage = this.totalPages - 1;
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600350 console.log("total enabled");
351 }
352
Tyler Scott7d076e22015-07-06 19:21:50 -0600353 this.populateResults(nextPage * 20);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600354 return false;
355}
356
Tyler Scott7d076e22015-07-06 19:21:50 -0600357
358Atmos.prototype.submitAutoComplete = function() {
359 /* FIXME TODO
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600360 if (autocompleteText.value.length > 0) {
361 var selection = autocompleteText.value;
362 $.each(dropdown, function (i, dropdownEntry) {
363 if (dropdownEntry.substr(0, dropdownEntry.length - 1) == selection) {
364 selection = dropdownEntry;
365 }
366 });
367
368 selectedSearch["?"] = selection;
369 query(catalog, selectedSearch, onData, 1);
370 delete selectedSearch["?"];
371 }
Tyler Scott7d076e22015-07-06 19:21:50 -0600372 */
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600373}
374
Tyler Scott7d076e22015-07-06 19:21:50 -0600375Atmos.prototype.populateAutocomplete = function(fields) {
376 /* FIXME TODO
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600377 var isAutocompleteFullName = (autocompleteText.value.charAt(autocompleteText.value.length - 1) === "/");
378 var autocompleteFullName = autocompleteText.value;
379 for (var i = 0; i < fields.length; ++i) {
380 var fieldFullName = fields[i];
381 var entry = autocompleteFullName;
382 var skipahead = "";
383
384 if (isAutocompleteFullName) {
385 skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length);
386 } else {
387 if (fieldFullName.charAt(autocompleteText.value.length) === "/") {
388 entry += "/";
389 skipahead = fieldFullName.substr(autocompleteText.value.length + 1, fieldFullName.length);
390 } else {
391 skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length);
392 }
393 }
394 if (skipahead.indexOf("/") != -1) {
395 entry += skipahead.substr(0, skipahead.indexOf("/") + 1);
396 } else {
397 entry += skipahead;
398 }
399
400 var added = false;
401 for (var j = 0; j < dropdown.length && !added; ++j) {
402 if (dropdown[j] === entry) {
403 added = true;
404 } else if (dropdown[j] > entry) {
405 dropdown.splice(j, 0, entry);
406 added = true;
407 }
408 }
409 if (!added) {
410 dropdown.push(entry);
411 }
412
413 }
414 $("#autocompleteText").autocomplete({
415 source: dropdown
416 });
Tyler Scott7d076e22015-07-06 19:21:50 -0600417 */
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600418}
419
Tyler Scotte815d3e2015-07-09 16:56:17 -0600420/**
421 * Adds a filter to the list of filters.
422 * There is no need to remove the filter, it is done via ui clicks.
423 *
424 * @param {string} filter
425 */
426Atmos.prototype.addFilter = function(name){
427 var existing = this.filters.find('span:contains(' + name + ')');
428 if (existing.length){
429 //If the category is clicked twice, then we delete it.
430 existing.remove();
431 this.applyFilters();
432
433 } else {
434
435 var filter = $('<span class="label label-default"></span>');
436 filter.text(name);
437
438 this.filters.append(filter);
439
440 this.applyFilters();
441
442 var scope = this;
443 filter.click(function(){
444 $(this).remove();
445 scope.applyFilters();
446 });
447
448 }
449
450}
451
452Atmos.prototype.applyFilters = function(){
453 var filters = this.filters.children().toArray().map(function(obj, index){
454 return $(obj).text();
455 }, []);
456 console.log('Collected filters:', filters);
457 this.query(this.catalog, filters, function(data){
458 scope.onData(data);
459 }, 1);
460}
461
462/**
463 * @deprecated
464 * Use addFilter instead.
465 */
466Atmos.prototype.populateCurrentSelections = function() {
467 console.warn("Use of deprecated function populateCurrentSelections! (Use addFilter instead)");
468 /*
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600469 var currentSelection = $(".currentSelections");
470 currentSelection.empty();
471
472 currentSelection.append("<p>Filtering on:");
473
Tyler Scott7d076e22015-07-06 19:21:50 -0600474 var scope = this;
475
476 $.each(this.selectedSearch, function (searchMenuCatagory, selection) {
477 var e = $('<a href="#">[X] ' + searchMenuCatagory + ":" + selection + '</a>');
478 e.onclick(function(){
479 var searchFilter = $(this).text();
480
481 var search = "";
482 for (var j = 0; j < searchFilter.length; ++j) {
483 search += searchFilter[j] + " ";
484 }
485 console.log("Split values: '" + search + "'");
486
487 delete this.selectedSearch[searchFilter[0]];
488 this.query(catalog, selectedSearch, onData, 1);
489 populateCurrentSelections();
490 });
491 currentSelection.append(e);
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600492 });
493
494 currentSelection.append("</p>");
Tyler Scotte815d3e2015-07-09 16:56:17 -0600495 */
Tyler Scott3c17d5f2015-06-23 17:49:29 -0600496}