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 = []; |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 28 | this.resultCount = 0; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 29 | this.page = 1; |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 30 | this.itemsPerPage = 25; |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 31 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 32 | this.catalog = catalog; |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 33 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 34 | this.face = new Face(config); |
| 35 | this.categories = $('#side-menu'); |
| 36 | this.resultTable = $('#resultTable'); |
| 37 | this.filters = $('#filters'); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 38 | this.searchInput = $('#search'); |
| 39 | this.searchBar = $('#searchBar'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 40 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 41 | var scope = this; |
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 | this.resultTable.on('click', '.interest-button', function(){ |
| 44 | var button = $(this); |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 45 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 46 | if (button.is(':disabled')){ |
| 47 | console.warn("Attempt to request again!"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 48 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 49 | |
| 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 Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 59 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 60 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 61 | //Filter setup |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 62 | $.getJSON("search_catagories.json").done(function (data) { |
| 63 | $.each(data, function (pageSection, contents) { |
| 64 | if (pageSection == "SearchCatagories") { |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 65 | $.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 Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 68 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 69 | var sub = e.find('ul.subnav'); |
| 70 | $.each(searchOptions, function(index, name){ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 71 | //Create the filter list inside the category |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 72 | var item = $('<li><a href="#">' + name + '</a></li>'); |
| 73 | sub.append(item); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 74 | 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 Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 91 | }); |
| 92 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 93 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 94 | //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 Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 98 | 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 Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 102 | } |
| 103 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 104 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 105 | scope.categories.append(e); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 106 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 107 | }); |
| 108 | } |
| 109 | }); |
| 110 | }); |
| 111 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 112 | this.searchBar.submit(function(e){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 113 | e.preventDefault(); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 114 | scope.search(); |
| 115 | }); |
| 116 | |
| 117 | this.searchInput.autoComplete(function(field, callback){ |
| 118 | scope.autoComplete(field, callback); |
| 119 | }); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | Atmos.prototype.search = function(){ |
| 124 | var filters = this.getFilters(); |
| 125 | |
| 126 | if (this.searchInput.val().length === 0 && !filters.hasOwnProperty()){ |
| 127 | if (!this.searchBar.hasClass('has-error')){ |
| 128 | this.searchBar.addClass('has-error').append('<span class="help-block">A filter or search value is required!</span>'); |
| 129 | } |
| 130 | return; |
| 131 | } else { |
| 132 | this.searchBar.removeClass('has-error').find('.help-block').fadeOut(function(){$(this).remove()}); |
| 133 | } |
| 134 | |
| 135 | console.log("Search started!", this.searchInput.val(), filters); |
| 136 | |
| 137 | console.log("Initiating query"); |
| 138 | |
| 139 | this.query(this.catalog, filters, |
| 140 | function(interest, data){ //Response function |
| 141 | console.log("Query Response:", interest, data); |
| 142 | |
| 143 | |
| 144 | |
| 145 | }, function(interest){ //Timeout function |
| 146 | console.error("Request failed! Timeout"); |
| 147 | }); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | Atmos.prototype.autoComplete = function(field, callback){ |
| 152 | console.log("Autocomplete triggered"); |
| 153 | |
| 154 | var filters = this.getFilters(); |
| 155 | |
| 156 | filters["?"] = this.searchInput.val(); |
| 157 | |
| 158 | this.query(this.catalog, filters, |
| 159 | function(interest, data){ |
| 160 | console.log(interest, data); |
| 161 | }, function(interest){ |
| 162 | console.error("Request failed! Timeout"); |
| 163 | }); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 164 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 165 | } |
| 166 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 167 | Atmos.prototype.onData = function(data) { |
| 168 | console.log("Recieved data", data); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 169 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 170 | var payloadStr = data.content.toString().split("\n")[0]; |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 171 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 172 | if (!payloadStr || payloadStr.length === 0){ |
| 173 | this.populateResults(); |
| 174 | return; //No results were returned. |
Tyler Scott | bb01356 | 2015-07-16 15:52:40 -0600 | [diff] [blame] | 175 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 176 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 177 | var queryResults = JSON.parse(payloadStr); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 178 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 179 | var scope = this; |
| 180 | |
| 181 | $.each(queryResults, function (queryResult, field) { |
| 182 | |
| 183 | if (queryResult == "next") { |
| 184 | scope.populateAutocomplete(field); |
| 185 | } |
| 186 | |
| 187 | if (queryResult == "results" && field == null){ |
| 188 | return; //Sometimes the results are null. (We should skip this.) |
| 189 | } |
| 190 | |
| 191 | $.each(field, function (entryCount, name) { |
| 192 | scope.results.push(name); |
| 193 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 194 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 195 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 196 | // Calculating the current page and the view |
| 197 | this.populateResults(); |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame] | 198 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 199 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 200 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 201 | Atmos.prototype.query = function(prefix, parameters, callback, timeout) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 202 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 203 | var queryPrefix = new Name(prefix); |
| 204 | queryPrefix.append("query"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 205 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 206 | var jsonString = JSON.stringify(parameters); |
| 207 | queryPrefix.append(jsonString); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 208 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 209 | var queryInterest = new Interest(queryPrefix); |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 210 | queryInterest.setInterestLifetimeMilliseconds(4000); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 211 | |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 212 | this.face.expressInterest(queryInterest, callback, timeout); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 213 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | Atmos.prototype.expressNextInterest = function() { |
| 217 | // @todo pipelines |
| 218 | var nextName = new Name(this.state["results"]); |
| 219 | nextName.appendSegment(this.state["nextSegment"]); |
| 220 | |
| 221 | var nextInterest = new Interest(nextName); |
| 222 | nextInterest.setInterestLifetimeMilliseconds(10000); |
| 223 | |
| 224 | var scope = this; |
| 225 | |
| 226 | this.face.expressInterest(nextInterest, |
| 227 | function(interest, data){ |
| 228 | scope.onQueryResultsData(interest, data); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 229 | }, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 230 | function(interest){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 231 | scope.onQueryResultsTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 232 | }); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 233 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 234 | this.state["nextSegment"] ++; |
| 235 | this.state["outstanding"][nextName.toUri()] = 0; |
| 236 | } |
| 237 | |
| 238 | Atmos.prototype.onQueryData = function(interest, data) { |
| 239 | var name = data.getName(); |
| 240 | |
| 241 | delete this.state["outstanding"][interest.getName().toUri()]; |
| 242 | |
| 243 | this.state["version"] = name.get(this.state["prefix"].size() + 2).toVersion(); |
| 244 | |
| 245 | this.state["results"] = new Name(this.state["prefix"]).append("query-results").append(this.state['parameters']) |
| 246 | .appendVersion(this.state["version"]).append(name.getComponent(name.getComponentCount() - 2)); |
| 247 | |
| 248 | console.log("Requested URI", this.state.results.toUri()); |
| 249 | |
| 250 | this.expressNextInterest(); |
| 251 | } |
| 252 | |
| 253 | Atmos.prototype.onQueryResultsData = function(interest, data) { |
| 254 | var name = data.getName(); |
| 255 | delete this.state["outstanding"][interest.getName().toUri()]; |
| 256 | if (!name.get(-1).equals(data.getMetaInfo().getFinalBlockId())) { |
| 257 | this.expressNextInterest(); |
| 258 | } //else { |
| 259 | //alert("found final block"); |
| 260 | //} |
| 261 | |
| 262 | this.state["userOnData"](data); |
| 263 | } |
| 264 | |
| 265 | Atmos.prototype.onQueryTimeout = function(interest) { |
| 266 | var uri = interest.getName().toUri(); |
| 267 | if (this.state["outstanding"][uri] < 1) { |
| 268 | this.state["outstanding"][uri] ++; |
| 269 | var scope = this; |
| 270 | this.face.expressInterest(interest, |
| 271 | function(interest, data){ |
| 272 | scope.onQueryData(interest, data); |
| 273 | }, |
| 274 | function(interest){ |
| 275 | scope.onQueryTimeout(interest); |
| 276 | }); |
| 277 | } else { |
| 278 | delete this.state["outstanding"][uri]; |
| 279 | |
| 280 | // We modify the autocomplete box here because we need to know |
| 281 | // we have all of the entries first. Fairly hacky. |
| 282 | /* TODO FIXME |
| 283 | var autocompleteFullName = this.autocompleteText.value; |
| 284 | for (var i = 0; i < dropdown.length; ++i) { |
| 285 | if (this.dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === this.autocompleteText.value.toUpperCase || dropdown.length == 1) { |
| 286 | autocompleteText.value = dropdown[i]; |
| 287 | } |
| 288 | } |
| 289 | */ |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 290 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 291 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 292 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 293 | Atmos.prototype.onQueryResultsTimeout = function(interest) { |
| 294 | var uri = interest.getName().toUri(); |
| 295 | if (this.state["outstanding"][uri] < 1) { |
| 296 | this.state["outstanding"][uri] ++; |
| 297 | var scope = this; |
| 298 | this.face.expressInterest(interest, |
| 299 | function(){ |
| 300 | scope.onQueryResultsData.apply(scope, arguments); |
| 301 | }, |
| 302 | function(){ |
| 303 | scope.onQueryResultsTimeout.apply(scope, arguments); |
| 304 | }); |
| 305 | } else { |
| 306 | delete this.state["outstanding"][uri]; |
| 307 | // We modify the autocomplete box here because we need to know |
| 308 | // we have all of the entries first. Fairly hacky. |
| 309 | /* TODO FIXME |
| 310 | var autocompleteFullName = autocompleteText.value; |
| 311 | for (var i = 0; i < dropdown.length; ++i) { |
| 312 | if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) { |
| 313 | autocompleteText.value = dropdown[i]; |
| 314 | } |
| 315 | } |
| 316 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 317 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 318 | } |
| 319 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 320 | Atmos.prototype.populateResults = function() { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 321 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 322 | //TODO Check only for page changes and result length |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 323 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 324 | this.resultTable.empty(); |
| 325 | |
| 326 | for (var i = startIndex; i < startIndex + 20 && i < this.results.length; ++i) { |
| 327 | this.resultTable.append('<tr><td>' + this.results[i] |
| 328 | + '</td><td><button class="interest-button btn btn-primary btn-xs">Retrieve</button></td></tr>'); |
| 329 | } |
| 330 | |
| 331 | if (this.results.length <= 20) { |
| 332 | this.page = 1; |
| 333 | } else { |
| 334 | this.page = startIndex / 20 + 1; |
| 335 | } |
| 336 | |
| 337 | this.totalPages = Math.ceil(this.results.length / 20); |
| 338 | |
| 339 | //TODO Fix the page to fit the theme. |
| 340 | var currentPage = $(".page"); |
| 341 | currentPage.empty(); |
| 342 | if (this.page != 1) { |
| 343 | currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>'); |
| 344 | } |
| 345 | // This section of code creates the paging for the results. |
| 346 | // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after |
| 347 | // the current page and the total pages (expect users not to really jump around a lot). |
| 348 | for (var i = 1; i <= this.totalPages; ++i) { |
| 349 | if (i == 1 || i == this.totalPages // Min or max |
| 350 | || (i <= this.page && i + 5 >= this.page) // in our current page range |
| 351 | || (i >= this.page && i - 5 <= this.page)) { // in our current page range |
| 352 | if (i != this.page) { |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 353 | currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>'); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 354 | if (i == 1 && this.page > i + 5) { |
| 355 | currentPage.append(' ... '); |
| 356 | } |
| 357 | } else { |
| 358 | currentPage.append(' ' + i); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 359 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 360 | } else { // Need to skip ahead |
| 361 | if (i == this.page + 6) { |
| 362 | currentPage.append(' ... '); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 363 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 364 | currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>') |
| 365 | i = this.totalPages - 1; |
| 366 | } |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 367 | } |
| 368 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 369 | currentPage.append(' ' + this.results.length + ' results'); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 370 | } |
| 371 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 372 | Atmos.prototype.getPage = function(clickedPage) { |
| 373 | console.log(clickedPage); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 374 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 375 | var nextPage = clickedPage; |
| 376 | if (clickedPage === "<") { |
| 377 | nextPage = this.page - 5; |
| 378 | } else if (clickedPage === ">") { |
| 379 | console.log("> enabled"); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 380 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 381 | nextPage = this.page + 5; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 382 | } |
| 383 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 384 | nextPage--; // Need to adjust for starting at 0 |
| 385 | |
| 386 | if (nextPage < 0 ) { |
| 387 | nextPage = 0; |
| 388 | console.log("0 enabled"); |
| 389 | } else if (nextPage > this.totalPages - 1) { |
| 390 | nextPage = this.totalPages - 1; |
| 391 | console.log("total enabled"); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 392 | } |
| 393 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 394 | this.populateResults(nextPage * 20); |
| 395 | return false; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 396 | } |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 397 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 398 | /** |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 399 | * This function returns a map of all the categories active filters. |
| 400 | * @return {Object<string, string>} |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 401 | */ |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 402 | Atmos.prototype.getFilters = function(){ |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 403 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 404 | var data = $(current).text().split(/:/); |
| 405 | prev[data[0]] = data[1]; |
| 406 | return prev; |
Tyler Scott | a153005 | 2015-07-24 00:13:28 -0600 | [diff] [blame^] | 407 | }, {}); //Collect a map<category, filter>. |
| 408 | //TODO Make the return value map<category, Array<filter>> |
| 409 | return filters; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return Atmos; |
| 413 | |
| 414 | })(); |
| 415 | |
| 416 | |