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 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 89 | //Toggle the menus. (Only respond when the immediate tab is clicked.) |
| 90 | e.find('> a').click(function(){ |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 91 | scope.categories.find('.subnav').slideUp(); |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 92 | var t = $(this).siblings('.subnav'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 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 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 114 | if (!payloadStr || payloadStr.length === 0){ |
| 115 | this.populateResults(0); |
| 116 | return; //No results were returned. |
| 117 | } |
| 118 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 119 | var queryResults = JSON.parse(payloadStr); |
| 120 | |
| 121 | var scope = this; |
| 122 | |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 123 | //TODO Fix paging. |
| 124 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 125 | try { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 126 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 127 | $.each(queryResults, function (queryResult, field) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 128 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 129 | if (queryResult == "next") { |
| 130 | scope.populateAutocomplete(field); |
| 131 | } |
| 132 | |
| 133 | if (queryResult == "results" && field == null){ |
| 134 | return; //Sometimes the results are null. (We should skip this.) |
| 135 | } |
| 136 | |
| 137 | $.each(field, function (entryCount, name) { |
| 138 | scope.results.push(name); |
| 139 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 140 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 141 | |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 142 | // Calculating the current page and the view |
| 143 | this.totalPages = Math.ceil(this.resultCount / 20); |
| 144 | this.populateResults(0); |
| 145 | |
| 146 | } catch (e) { |
| 147 | console.error(e.message, e.stack); |
| 148 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | Atmos.prototype.query = function(prefix, parameters, callback, pipeline) { |
| 152 | this.results = []; |
| 153 | this.dropdown = []; |
| 154 | this.resultTable.empty(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 155 | |
| 156 | var queryPrefix = new Name(prefix); |
| 157 | queryPrefix.append("query"); |
| 158 | |
| 159 | var jsonString = JSON.stringify(parameters); |
| 160 | queryPrefix.append(jsonString); |
| 161 | |
| 162 | this.state = { |
| 163 | prefix: new Name(prefix), |
| 164 | userOnData: callback, |
| 165 | outstanding: {}, |
| 166 | nextSegment: 0, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 167 | parameters: jsonString |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /*if (state.hasOwnProperty("version")) { |
| 171 | console.log("state already has version"); |
| 172 | }*/ |
| 173 | |
| 174 | var queryInterest = new Interest(queryPrefix); |
| 175 | queryInterest.setInterestLifetimeMilliseconds(10000); |
| 176 | |
| 177 | var scope = this; |
| 178 | |
| 179 | this.face.expressInterest(queryInterest, |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 180 | function(interest, data){ |
| 181 | scope.onQueryData(interest, data); |
| 182 | }, function(interest){ |
| 183 | scope.onQueryTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 184 | } |
| 185 | ); |
| 186 | |
| 187 | this.state["outstanding"][queryInterest.getName().toUri()] = 0; |
| 188 | } |
| 189 | |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 190 | /** |
| 191 | * @deprecated |
| 192 | * Use applyFilters/addFilters as appropriate. |
| 193 | */ |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 194 | Atmos.prototype.submitCatalogSearch = function(field) { |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 195 | console.warn("Use of deprecated function submitCatalogSearch! (Use applyFilters/addFilters as appropriate)"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | Atmos.prototype.expressNextInterest = function() { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 199 | // @todo pipelines |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 200 | var nextName = new Name(this.state["results"]); |
| 201 | nextName.appendSegment(this.state["nextSegment"]); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 202 | |
| 203 | var nextInterest = new Interest(nextName); |
| 204 | nextInterest.setInterestLifetimeMilliseconds(10000); |
| 205 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 206 | var scope = this; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 207 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 208 | this.face.expressInterest(nextInterest, |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 209 | function(interest, data){ |
| 210 | scope.onQueryResultsData(interest, data); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 211 | }, |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 212 | function(interest){ |
| 213 | scope.onQueryResultsTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 214 | }); |
| 215 | |
| 216 | this.state["nextSegment"] ++; |
| 217 | this.state["outstanding"][nextName.toUri()] = 0; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 218 | } |
| 219 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 220 | Atmos.prototype.onQueryData = function(interest, data) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 221 | var name = data.getName(); |
| 222 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 223 | delete this.state["outstanding"][interest.getName().toUri()]; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 224 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 225 | this.state["version"] = name.get(this.state["prefix"].size() + 2).toVersion(); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 226 | |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 227 | 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] | 228 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 229 | this.expressNextInterest(); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 230 | } |
| 231 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 232 | Atmos.prototype.onQueryResultsData = function(interest, data) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 233 | var name = data.getName(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 234 | delete this.state["outstanding"][interest.getName().toUri()]; |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 235 | if (!name.get(-1).equals(data.getMetaInfo().getFinalBlockId())) { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 236 | this.expressNextInterest(); |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 237 | } //else { |
| 238 | //alert("found final block"); |
| 239 | //} |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 240 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 241 | this.state["userOnData"](data); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 242 | } |
| 243 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 244 | Atmos.prototype.onQueryTimeout = function(interest) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 245 | var uri = interest.getName().toUri(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 246 | if (this.state["outstanding"][uri] < 1) { |
| 247 | this.state["outstanding"][uri] ++; |
| 248 | var scope = this; |
| 249 | this.face.expressInterest(interest, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 250 | function(interest, data){ |
| 251 | scope.onQueryData(interest, data); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 252 | }, |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 253 | function(interest){ |
| 254 | scope.onQueryTimeout(interest); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 255 | }); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 256 | } else { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 257 | delete this.state["outstanding"][uri]; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 258 | |
| 259 | // We modify the autocomplete box here because we need to know |
| 260 | // we have all of the entries first. Fairly hacky. |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 261 | /* TODO FIXME |
| 262 | var autocompleteFullName = this.autocompleteText.value; |
| 263 | for (var i = 0; i < dropdown.length; ++i) { |
| 264 | if (this.dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === this.autocompleteText.value.toUpperCase || dropdown.length == 1) { |
| 265 | autocompleteText.value = dropdown[i]; |
| 266 | } |
| 267 | } |
| 268 | */ |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | Atmos.prototype.onQueryResultsTimeout = function(interest) { |
| 273 | var uri = interest.getName().toUri(); |
| 274 | if (this.state["outstanding"][uri] < 1) { |
| 275 | this.state["outstanding"][uri] ++; |
| 276 | var scope = this; |
| 277 | this.face.expressInterest(interest, |
| 278 | function(){ |
| 279 | scope.onQueryResultsData.apply(scope, arguments); |
| 280 | }, |
| 281 | function(){ |
| 282 | scope.onQueryResultsTimeout.apply(scope, arguments); |
| 283 | }); |
| 284 | } else { |
| 285 | delete this.state["outstanding"][uri]; |
| 286 | // We modify the autocomplete box here because we need to know |
| 287 | // we have all of the entries first. Fairly hacky. |
| 288 | /* TODO FIXME |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 289 | var autocompleteFullName = autocompleteText.value; |
| 290 | for (var i = 0; i < dropdown.length; ++i) { |
| 291 | if (dropdown[i].substr(0, dropdown[i].length - 1).toUpperCase === autocompleteText.value.toUpperCase || dropdown.length == 1) { |
| 292 | autocompleteText.value = dropdown[i]; |
| 293 | } |
| 294 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 295 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 299 | Atmos.prototype.populateResults = function(startIndex) { |
| 300 | this.resultTable.empty(); |
Tyler Scott | 424ee10 | 2015-07-14 16:50:41 -0600 | [diff] [blame^] | 301 | //this.resultTable.append('<tr><th colspan="2">Results</th></tr>'); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 302 | |
| 303 | for (var i = startIndex; i < startIndex + 20 && i < this.results.length; ++i) { |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 304 | this.resultTable.append('<tr><td>' + this.results[i] |
| 305 | + '</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] | 306 | } |
| 307 | |
| 308 | if (this.results.length <= 20) { |
| 309 | this.page = 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 310 | } else { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 311 | this.page = startIndex / 20 + 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 312 | } |
| 313 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 314 | this.totalPages = Math.ceil(this.results.length / 20); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 315 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 316 | //TODO Fix the page to fit the theme. |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 317 | var currentPage = $(".page"); |
| 318 | currentPage.empty(); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 319 | if (this.page != 1) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 320 | currentPage.append('<a href="#" onclick="getPage(this.id);" id="<"><</a>'); |
| 321 | } |
| 322 | // This section of code creates the paging for the results. |
| 323 | // To prevent it from having a 1000+ pages, it will only show the 5 pages before/after |
| 324 | // 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] | 325 | for (var i = 1; i <= this.totalPages; ++i) { |
| 326 | if (i == 1 || i == this.totalPages // Min or max |
| 327 | || (i <= this.page && i + 5 >= this.page) // in our current page range |
| 328 | || (i >= this.page && i - 5 <= this.page)) { // in our current page range |
| 329 | if (i != this.page) { |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 330 | currentPage.append(' <a href="#" onclick="getPage(' + i + ');">' + i + '</a>') |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 331 | if (i == 1 && this.page > i + 5) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 332 | currentPage.append(' ... '); |
| 333 | } |
| 334 | } else { |
| 335 | currentPage.append(' ' + i); |
| 336 | } |
| 337 | } else { // Need to skip ahead |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 338 | if (i == this.page + 6) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 339 | currentPage.append(' ... '); |
| 340 | |
Tyler Scott | f08ab96 | 2015-06-30 16:31:29 -0600 | [diff] [blame] | 341 | currentPage.append(' <a href="#" onclick="getPage(this.id);" id=">">></a>') |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 342 | i = this.totalPages - 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 346 | currentPage.append(' ' + this.results.length + ' results'); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 347 | } |
| 348 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 349 | Atmos.prototype.getPage = function(clickedPage) { |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 350 | console.log(clickedPage); |
| 351 | |
| 352 | var nextPage = clickedPage; |
| 353 | if (clickedPage === "<") { |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 354 | nextPage = this.page - 5; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 355 | } else if (clickedPage === ">") { |
| 356 | console.log("> enabled"); |
| 357 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 358 | nextPage = this.page + 5; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | nextPage--; // Need to adjust for starting at 0 |
| 362 | |
| 363 | if (nextPage < 0 ) { |
| 364 | nextPage = 0; |
| 365 | console.log("0 enabled"); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 366 | } else if (nextPage > this.totalPages - 1) { |
| 367 | nextPage = this.totalPages - 1; |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 368 | console.log("total enabled"); |
| 369 | } |
| 370 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 371 | this.populateResults(nextPage * 20); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 375 | |
| 376 | Atmos.prototype.submitAutoComplete = function() { |
| 377 | /* FIXME TODO |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 378 | if (autocompleteText.value.length > 0) { |
| 379 | var selection = autocompleteText.value; |
| 380 | $.each(dropdown, function (i, dropdownEntry) { |
| 381 | if (dropdownEntry.substr(0, dropdownEntry.length - 1) == selection) { |
| 382 | selection = dropdownEntry; |
| 383 | } |
| 384 | }); |
| 385 | |
| 386 | selectedSearch["?"] = selection; |
| 387 | query(catalog, selectedSearch, onData, 1); |
| 388 | delete selectedSearch["?"]; |
| 389 | } |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 390 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 391 | } |
| 392 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 393 | Atmos.prototype.populateAutocomplete = function(fields) { |
| 394 | /* FIXME TODO |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 395 | var isAutocompleteFullName = (autocompleteText.value.charAt(autocompleteText.value.length - 1) === "/"); |
| 396 | var autocompleteFullName = autocompleteText.value; |
| 397 | for (var i = 0; i < fields.length; ++i) { |
| 398 | var fieldFullName = fields[i]; |
| 399 | var entry = autocompleteFullName; |
| 400 | var skipahead = ""; |
| 401 | |
| 402 | if (isAutocompleteFullName) { |
| 403 | skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length); |
| 404 | } else { |
| 405 | if (fieldFullName.charAt(autocompleteText.value.length) === "/") { |
| 406 | entry += "/"; |
| 407 | skipahead = fieldFullName.substr(autocompleteText.value.length + 1, fieldFullName.length); |
| 408 | } else { |
| 409 | skipahead = fieldFullName.substr(autocompleteText.value.length, fieldFullName.length); |
| 410 | } |
| 411 | } |
| 412 | if (skipahead.indexOf("/") != -1) { |
| 413 | entry += skipahead.substr(0, skipahead.indexOf("/") + 1); |
| 414 | } else { |
| 415 | entry += skipahead; |
| 416 | } |
| 417 | |
| 418 | var added = false; |
| 419 | for (var j = 0; j < dropdown.length && !added; ++j) { |
| 420 | if (dropdown[j] === entry) { |
| 421 | added = true; |
| 422 | } else if (dropdown[j] > entry) { |
| 423 | dropdown.splice(j, 0, entry); |
| 424 | added = true; |
| 425 | } |
| 426 | } |
| 427 | if (!added) { |
| 428 | dropdown.push(entry); |
| 429 | } |
| 430 | |
| 431 | } |
| 432 | $("#autocompleteText").autocomplete({ |
| 433 | source: dropdown |
| 434 | }); |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 435 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 436 | } |
| 437 | |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 438 | /** |
| 439 | * Adds a filter to the list of filters. |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 440 | * If a filter is already added, it will be removed instead. |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 441 | * |
| 442 | * @param {string} filter |
| 443 | */ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 444 | Atmos.prototype.addFilter = function(name, category){ |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 445 | var existing = this.filters.find('span:contains(' + name + ')'); |
| 446 | if (existing.length){ |
| 447 | //If the category is clicked twice, then we delete it. |
| 448 | existing.remove(); |
| 449 | this.applyFilters(); |
| 450 | |
| 451 | } else { |
| 452 | |
| 453 | var filter = $('<span class="label label-default"></span>'); |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 454 | filter.text(category + ':' + name); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 455 | |
| 456 | this.filters.append(filter); |
| 457 | |
| 458 | this.applyFilters(); |
| 459 | |
| 460 | var scope = this; |
| 461 | filter.click(function(){ |
| 462 | $(this).remove(); |
| 463 | scope.applyFilters(); |
| 464 | }); |
| 465 | |
| 466 | } |
| 467 | |
| 468 | } |
| 469 | |
| 470 | Atmos.prototype.applyFilters = function(){ |
Tyler Scott | 575c61b | 2015-07-13 13:42:16 -0600 | [diff] [blame] | 471 | var filters = this.filters.children().toArray().reduce(function(prev, current){ |
| 472 | var data = $(current).text().split(/:/); |
| 473 | prev[data[0]] = data[1]; |
| 474 | return prev; |
| 475 | }, {}); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 476 | console.log('Collected filters:', filters); |
Tyler Scott | 087aef7 | 2015-07-14 14:11:59 -0600 | [diff] [blame] | 477 | var scope = this; |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 478 | this.query(this.catalog, filters, function(data){ |
| 479 | scope.onData(data); |
| 480 | }, 1); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * @deprecated |
| 485 | * Use addFilter instead. |
| 486 | */ |
| 487 | Atmos.prototype.populateCurrentSelections = function() { |
| 488 | console.warn("Use of deprecated function populateCurrentSelections! (Use addFilter instead)"); |
| 489 | /* |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 490 | var currentSelection = $(".currentSelections"); |
| 491 | currentSelection.empty(); |
| 492 | |
| 493 | currentSelection.append("<p>Filtering on:"); |
| 494 | |
Tyler Scott | 7d076e2 | 2015-07-06 19:21:50 -0600 | [diff] [blame] | 495 | var scope = this; |
| 496 | |
| 497 | $.each(this.selectedSearch, function (searchMenuCatagory, selection) { |
| 498 | var e = $('<a href="#">[X] ' + searchMenuCatagory + ":" + selection + '</a>'); |
| 499 | e.onclick(function(){ |
| 500 | var searchFilter = $(this).text(); |
| 501 | |
| 502 | var search = ""; |
| 503 | for (var j = 0; j < searchFilter.length; ++j) { |
| 504 | search += searchFilter[j] + " "; |
| 505 | } |
| 506 | console.log("Split values: '" + search + "'"); |
| 507 | |
| 508 | delete this.selectedSearch[searchFilter[0]]; |
| 509 | this.query(catalog, selectedSearch, onData, 1); |
| 510 | populateCurrentSelections(); |
| 511 | }); |
| 512 | currentSelection.append(e); |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 513 | }); |
| 514 | |
| 515 | currentSelection.append("</p>"); |
Tyler Scott | e815d3e | 2015-07-09 16:56:17 -0600 | [diff] [blame] | 516 | */ |
Tyler Scott | 3c17d5f | 2015-06-23 17:49:29 -0600 | [diff] [blame] | 517 | } |