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