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