Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 1 | //{@ @todo: this need to be configured before the document load |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 2 | var catalog = "/catalog/myUniqueName"; |
Tyler Scott | a1ac69d | 2015-07-02 17:42:03 -0600 | [diff] [blame] | 3 | var config = { |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 4 | host: "atmos-csu.research-lan.colostate.edu", |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 5 | port: 9696 |
Tyler Scott | a1ac69d | 2015-07-02 17:42:03 -0600 | [diff] [blame] | 6 | }; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 7 | |
| 8 | // @} |
| 9 | |
Tyler Scott | a1ac69d | 2015-07-02 17:42:03 -0600 | [diff] [blame] | 10 | var atmos = {}; //Comment this out if you don't want debug access. |
| 11 | |
| 12 | //Run when the document loads. |
| 13 | $(function () { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 14 | |
| 15 | //remove "atmos =" if you don't want debug access |
Tyler Scott | a1ac69d | 2015-07-02 17:42:03 -0600 | [diff] [blame] | 16 | atmos = new Atmos(catalog, config); |
Tyler Scott | f65b710 | 2015-06-30 18:40:14 -0600 | [diff] [blame] | 17 | |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 18 | }); |
| 19 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 20 | /* |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 21 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 22 | */ |
| 23 | |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 24 | /** |
| 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 Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 34 | function 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 Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 46 | this.catalog = catalog; |
| 47 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 48 | this.face = new Face(config); |
| 49 | this.categories = $('#side-menu'); |
| 50 | this.resultTable = $('#resultTable'); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 51 | this.filters = $('#filters'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 52 | |
| 53 | var scope = this; |
| 54 | |
| 55 | this.resultTable.on('click', '.interest-button', function(){ |
| 56 | var button = $(this); |
| 57 | |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 58 | if (button.is(':disabled')){ |
| 59 | console.warn("Attempt to request again!"); |
| 60 | } |
| 61 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 62 | var name = button.parent().prev().text(); |
| 63 | var interest = new Interest(new Name('/retrieve' + name)); |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 64 | scope.face.expressInterest(interest, function(){}, function(){}); |
| 65 | |
| 66 | button.text("Requested!") |
| 67 | .removeClass('btn-primary') |
| 68 | .addClass('btn-default') |
| 69 | .addClass('disabled') |
| 70 | .prop('disabled', true); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 71 | |
| 72 | }); |
| 73 | |
| 74 | $.getJSON("search_catagories.json").done(function (data) { |
| 75 | $.each(data, function (pageSection, contents) { |
| 76 | if (pageSection == "SearchCatagories") { |
| 77 | $.each(contents, function (search, searchOptions) { |
| 78 | var e = $('<li><a href="#">' + search.replace(/\_/g, " ") + '</a><ul class="subnav nav nav-pills nav-stacked"></ul></li>'); |
| 79 | |
| 80 | var sub = e.find('ul.subnav'); |
| 81 | $.each(searchOptions, function(index, name){ |
| 82 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 83 | sub.append(item); |
| 84 | item.click(function(){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 85 | scope.addFilter(name, search); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 86 | }); |
| 87 | }); |
| 88 | |
| 89 | //Toggle the menus. |
| 90 | e.click(function(){ |
| 91 | scope.categories.find('.subnav').slideUp(); |
| 92 | var t = $(this).find('.subnav'); |
| 93 | if ( !t.is(':visible')){ |
| 94 | t.slideDown().triggerHandler('focus'); //Cancel other animations and slide down. |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | scope.categories.append(e); |
| 99 | }); |
| 100 | } |
| 101 | }); |
| 102 | }); |
| 103 | |
| 104 | $('#searchBar').submit(function(e){ |
| 105 | e.preventDefault(); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 106 | console.log("Search started!", $('#search')); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 107 | }) |
| 108 | |
| 109 | } |
| 110 | |
| 111 | Atmos.prototype.onData = function(data) { |
| 112 | var payloadStr = data.content.toString().split("\n")[0]; |
| 113 | |
| 114 | var queryResults = JSON.parse(payloadStr); |
| 115 | |
| 116 | var scope = this; |
| 117 | |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 118 | //TODO Fix paging. |
| 119 | |
| 120 | $.each(queryResults, function (queryResult, field) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 121 | |
| 122 | if (queryResult == "next") { |
| 123 | scope.populateAutocomplete(field); |
| 124 | } |
| 125 | |
| 126 | $.each(field, function (entryCount, name) { |
| 127 | scope.results.push(name); |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | // Calculating the current page and the view |
| 132 | this.totalPages = Math.ceil(this.resultCount / 20); |
| 133 | this.populateResults(0); |
| 134 | } |
| 135 | |
| 136 | Atmos.prototype.query = function(prefix, parameters, callback, pipeline) { |
| 137 | this.results = []; |
| 138 | this.dropdown = []; |
| 139 | this.resultTable.empty(); |
| 140 | this.resultTable.append('<tr><th colspan="2">Results</th></tr>'); |
| 141 | |
| 142 | var queryPrefix = new Name(prefix); |
| 143 | queryPrefix.append("query"); |
| 144 | |
| 145 | var jsonString = JSON.stringify(parameters); |
| 146 | queryPrefix.append(jsonString); |
| 147 | |
| 148 | this.state = { |
| 149 | prefix: new Name(prefix), |
| 150 | userOnData: callback, |
| 151 | outstanding: {}, |
| 152 | nextSegment: 0, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 153 | parameters: jsonString |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /*if (state.hasOwnProperty("version")) { |
| 157 | console.log("state already has version"); |
| 158 | }*/ |
| 159 | |
| 160 | var queryInterest = new Interest(queryPrefix); |
| 161 | queryInterest.setInterestLifetimeMilliseconds(10000); |
| 162 | |
| 163 | var scope = this; |
| 164 | |
| 165 | this.face.expressInterest(queryInterest, |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 166 | function(interest, data){ |
| 167 | scope.onQueryData(interest, data); |
| 168 | }, function(interest){ |
| 169 | scope.onQueryTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 170 | } |
| 171 | ); |
| 172 | |
| 173 | this.state["outstanding"][queryInterest.getName().toUri()] = 0; |
| 174 | } |
| 175 | |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 176 | /** |
| 177 | * @deprecated |
| 178 | * Use applyFilters/addFilters as appropriate. |
| 179 | */ |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 180 | Atmos.prototype.submitCatalogSearch = function(field) { |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 181 | console.warn("Use of deprecated function submitCatalogSearch! (Use applyFilters/addFilters as appropriate)"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | Atmos.prototype.expressNextInterest = function() { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 185 | // @todo pipelines |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 186 | var nextName = new Name(this.state["results"]); |
| 187 | nextName.appendSegment(this.state["nextSegment"]); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 188 | |
| 189 | var nextInterest = new Interest(nextName); |
| 190 | nextInterest.setInterestLifetimeMilliseconds(10000); |
| 191 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 192 | var scope = this; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 193 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 194 | this.face.expressInterest(nextInterest, |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 195 | function(interest, data){ |
| 196 | scope.onQueryResultsData(interest, data); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 197 | }, |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 198 | function(interest){ |
| 199 | scope.onQueryResultsTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 200 | }); |
| 201 | |
| 202 | this.state["nextSegment"] ++; |
| 203 | this.state["outstanding"][nextName.toUri()] = 0; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 204 | } |
| 205 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 206 | Atmos.prototype.onQueryData = function(interest, data) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 207 | var name = data.getName(); |
| 208 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 209 | delete this.state["outstanding"][interest.getName().toUri()]; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 210 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 211 | this.state["version"] = name.get(this.state["prefix"].size() + 2).toVersion(); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 212 | |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 213 | this.state["results"] = new Name(this.state["prefix"]).append("query-results").append(this.state['parameters']).appendVersion(this.state["version"]); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 214 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 215 | this.expressNextInterest(); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 216 | } |
| 217 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 218 | Atmos.prototype.onQueryResultsData = function(interest, data) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 219 | var name = data.getName(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 220 | delete this.state["outstanding"][interest.getName().toUri()]; |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 221 | if (!name.get(-1).equals(data.getMetaInfo().getFinalBlockId())) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 222 | this.expressNextInterest(); |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 223 | } //else { |
| 224 | //alert("found final block"); |
| 225 | //} |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 226 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 227 | this.state["userOnData"](data); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 228 | } |
| 229 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 230 | Atmos.prototype.onQueryTimeout = function(interest) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 231 | var uri = interest.getName().toUri(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 232 | if (this.state["outstanding"][uri] < 1) { |
| 233 | this.state["outstanding"][uri] ++; |
| 234 | var scope = this; |
| 235 | this.face.expressInterest(interest, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 236 | function(interest, data){ |
| 237 | scope.onQueryData(interest, data); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 238 | }, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 239 | function(interest){ |
| 240 | scope.onQueryTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 241 | }); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 242 | } else { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 243 | delete this.state["outstanding"][uri]; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 244 | |
| 245 | // We modify the autocomplete box here because we need to know |
| 246 | // we have all of the entries first. Fairly hacky. |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 247 | /* TODO FIXME |
| 248 | var autocompleteFullName = this.autocompleteText.value; |
| 249 | for (var i = 0; i < dropdown.length; ++i) { |
| 250 | if (this.dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === this.autocompleteText.value.toUpperCase || dropdown.length == 1) { |
| 251 | autocompleteText.value = dropdown[i]; |
| 252 | } |
| 253 | } |
| 254 | */ |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | Atmos.prototype.onQueryResultsTimeout = function(interest) { |
| 259 | var uri = interest.getName().toUri(); |
| 260 | if (this.state["outstanding"][uri] < 1) { |
| 261 | this.state["outstanding"][uri] ++; |
| 262 | var scope = this; |
| 263 | this.face.expressInterest(interest, |
| 264 | function(){ |
| 265 | scope.onQueryResultsData.apply(scope, arguments); |
| 266 | }, |
| 267 | function(){ |
| 268 | scope.onQueryResultsTimeout.apply(scope, arguments); |
| 269 | }); |
| 270 | } else { |
| 271 | delete this.state["outstanding"][uri]; |
| 272 | // We modify the autocomplete box here because we need to know |
| 273 | // we have all of the entries first. Fairly hacky. |
| 274 | /* TODO FIXME |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 275 | var autocompleteFullName = autocompleteText.value; |
| 276 | for (var i = 0; i < dropdown.length; ++i) { |
| 277 | if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) { |
| 278 | autocompleteText.value = dropdown[i]; |
| 279 | } |
| 280 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 281 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 285 | Atmos.prototype.populateResults = function(startIndex) { |
| 286 | this.resultTable.empty(); |
| 287 | this.resultTable.append('<tr><th colspan="2">Results</th></tr>'); |
| 288 | |
| 289 | |
| 290 | for (var i = startIndex; i < startIndex + 20 && i < this.results.length; ++i) { |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 291 | this.resultTable.append('<tr><td>' + this.results[i] |
| 292 | + '</td><td><button class="interest-button btn btn-primary btn-xs">Retrieve</button></td></tr>'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if (this.results.length <= 20) { |
| 296 | this.page = 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 297 | } else { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 298 | this.page = startIndex / 20 + 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 299 | } |
| 300 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 301 | this.totalPages = Math.ceil(this.results.length / 20); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 302 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 303 | //TODO Fix the page to fit the theme. |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 304 | var currentPage = $(".page"); |
| 305 | currentPage.empty(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 306 | if (this.page != 1) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 307 | currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>'); |
| 308 | } |
| 309 | // This section of code creates the paging for the results. |
| 310 | // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after |
| 311 | // the current page and the total pages (expect users not to really jump around a lot). |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 312 | for (var i = 1; i <= this.totalPages; ++i) { |
| 313 | if (i == 1 || i == this.totalPages // Min or max |
| 314 | || (i <= this.page && i + 5 >= this.page) // in our current page range |
| 315 | || (i >= this.page && i - 5 <= this.page)) { // in our current page range |
| 316 | if (i != this.page) { |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 317 | currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>') |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 318 | if (i == 1 && this.page > i + 5) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 319 | currentPage.append(' ... '); |
| 320 | } |
| 321 | } else { |
| 322 | currentPage.append(' ' + i); |
| 323 | } |
| 324 | } else { // Need to skip ahead |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 325 | if (i == this.page + 6) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 326 | currentPage.append(' ... '); |
| 327 | |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 328 | currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>') |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 329 | i = this.totalPages - 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 333 | currentPage.append(' ' + this.results.length + ' results'); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 334 | } |
| 335 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 336 | Atmos.prototype.getPage = function(clickedPage) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 337 | console.log(clickedPage); |
| 338 | |
| 339 | var nextPage = clickedPage; |
| 340 | if (clickedPage === "<") { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 341 | nextPage = this.page - 5; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 342 | } else if (clickedPage === ">") { |
| 343 | console.log("> enabled"); |
| 344 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 345 | nextPage = this.page + 5; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | nextPage--; // Need to adjust for starting at 0 |
| 349 | |
| 350 | if (nextPage < 0 ) { |
| 351 | nextPage = 0; |
| 352 | console.log("0 enabled"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 353 | } else if (nextPage > this.totalPages - 1) { |
| 354 | nextPage = this.totalPages - 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 355 | console.log("total enabled"); |
| 356 | } |
| 357 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 358 | this.populateResults(nextPage * 20); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 362 | |
| 363 | Atmos.prototype.submitAutoComplete = function() { |
| 364 | /* FIXME TODO |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 365 | if (autocompleteText.value.length > 0) { |
| 366 | var selection = autocompleteText.value; |
| 367 | $.each(dropdown, function (i, dropdownEntry) { |
| 368 | if (dropdownEntry.substr(0, dropdownEntry.length - 1) == selection) { |
| 369 | selection = dropdownEntry; |
| 370 | } |
| 371 | }); |
| 372 | |
| 373 | selectedSearch["?"] = selection; |
| 374 | query(catalog, selectedSearch, onData, 1); |
| 375 | delete selectedSearch["?"]; |
| 376 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 377 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 378 | } |
| 379 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 380 | Atmos.prototype.populateAutocomplete = function(fields) { |
| 381 | /* FIXME TODO |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 382 | var isAutocompleteFullName = (autocompleteText.value.charAt(autocompleteText.value.length - 1) === "/"); |
| 383 | var autocompleteFullName = autocompleteText.value; |
| 384 | for (var i = 0; i < fields.length; ++i) { |
| 385 | var fieldFullName = fields[i]; |
| 386 | var entry = autocompleteFullName; |
| 387 | var skipahead = ""; |
| 388 | |
| 389 | if (isAutocompleteFullName) { |
| 390 | skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length); |
| 391 | } else { |
| 392 | if (fieldFullName.charAt(autocompleteText.value.length) === "/") { |
| 393 | entry += "/"; |
| 394 | skipahead = fieldFullName.substr(autocompleteText.value.length + 1, fieldFullName.length); |
| 395 | } else { |
| 396 | skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length); |
| 397 | } |
| 398 | } |
| 399 | if (skipahead.indexOf("/") != -1) { |
| 400 | entry += skipahead.substr(0, skipahead.indexOf("/") + 1); |
| 401 | } else { |
| 402 | entry += skipahead; |
| 403 | } |
| 404 | |
| 405 | var added = false; |
| 406 | for (var j = 0; j < dropdown.length && !added; ++j) { |
| 407 | if (dropdown[j] === entry) { |
| 408 | added = true; |
| 409 | } else if (dropdown[j] > entry) { |
| 410 | dropdown.splice(j, 0, entry); |
| 411 | added = true; |
| 412 | } |
| 413 | } |
| 414 | if (!added) { |
| 415 | dropdown.push(entry); |
| 416 | } |
| 417 | |
| 418 | } |
| 419 | $("#autocompleteText").autocomplete({ |
| 420 | source: dropdown |
| 421 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 422 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 423 | } |
| 424 | |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 425 | /** |
| 426 | * Adds a filter to the list of filters. |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 427 | * If a filter is already added, it will be removed instead. |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 428 | * |
| 429 | * @param {string} filter |
| 430 | */ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 431 | Atmos.prototype.addFilter = function(name, category){ |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 432 | var existing = this.filters.find('span:contains(' + name + ')'); |
| 433 | if (existing.length){ |
| 434 | //If the category is clicked twice, then we delete it. |
| 435 | existing.remove(); |
| 436 | this.applyFilters(); |
| 437 | |
| 438 | } else { |
| 439 | |
| 440 | var filter = $('<span class="label label-default"></span>'); |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 441 | filter.text(category + ':' + name); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 442 | |
| 443 | this.filters.append(filter); |
| 444 | |
| 445 | this.applyFilters(); |
| 446 | |
| 447 | var scope = this; |
| 448 | filter.click(function(){ |
| 449 | $(this).remove(); |
| 450 | scope.applyFilters(); |
| 451 | }); |
| 452 | |
| 453 | } |
| 454 | |
| 455 | } |
| 456 | |
| 457 | Atmos.prototype.applyFilters = function(){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 458 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
| 459 | var data = $(current).text().split(/:/); |
| 460 | prev[data[0]] = data[1]; |
| 461 | return prev; |
| 462 | }, {}); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 463 | console.log('Collected filters:', filters); |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame^] | 464 | var scope = this; |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 465 | this.query(this.catalog, filters, function(data){ |
| 466 | scope.onData(data); |
| 467 | }, 1); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * @deprecated |
| 472 | * Use addFilter instead. |
| 473 | */ |
| 474 | Atmos.prototype.populateCurrentSelections = function() { |
| 475 | console.warn("Use of deprecated function populateCurrentSelections! (Use addFilter instead)"); |
| 476 | /* |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 477 | var currentSelection = $(".currentSelections"); |
| 478 | currentSelection.empty(); |
| 479 | |
| 480 | currentSelection.append("<p>Filtering on:"); |
| 481 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 482 | var scope = this; |
| 483 | |
| 484 | $.each(this.selectedSearch, function (searchMenuCatagory, selection) { |
| 485 | var e = $('<a href="#">[X] ' + searchMenuCatagory + ":" + selection + '</a>'); |
| 486 | e.onclick(function(){ |
| 487 | var searchFilter = $(this).text(); |
| 488 | |
| 489 | var search = ""; |
| 490 | for (var j = 0; j < searchFilter.length; ++j) { |
| 491 | search += searchFilter[j] + " "; |
| 492 | } |
| 493 | console.log("Split values: '" + search + "'"); |
| 494 | |
| 495 | delete this.selectedSearch[searchFilter[0]]; |
| 496 | this.query(catalog, selectedSearch, onData, 1); |
| 497 | populateCurrentSelections(); |
| 498 | }); |
| 499 | currentSelection.append(e); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 500 | }); |
| 501 | |
| 502 | currentSelection.append("</p>"); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 503 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 504 | } |