|
3 | 3 | * Copyright © 2012-2014 Luca Wehrstedt <luca.wehrstedt@gmail.com> |
4 | 4 | * Copyright © 2013 Fabian Gundlach <320pointsguy@gmail.com> |
5 | 5 | * Copyright © 2014 Artem Iglikov <artem.iglikov@gmail.com> |
| 6 | + * Copyright © 2018 Gregor Eesmaa <gregoreesmaa1@gmail.com> |
6 | 7 | * |
7 | 8 | * This program is free software: you can redistribute it and/or modify |
8 | 9 | * it under the terms of the GNU Affero General Public License as |
@@ -52,7 +53,7 @@ CMS.AWSUtils = function(url_root, timestamp, |
52 | 53 | CMS.AWSUtils.create_url_builder = function(url_root) { |
53 | 54 | return function() { |
54 | 55 | var url = url_root; |
55 | | - for (let component of arguments) { |
| 56 | + for (var component in arguments) { |
56 | 57 | if (url.substr(-1) != "/") { |
57 | 58 | url += "/"; |
58 | 59 | } |
@@ -311,6 +312,121 @@ CMS.AWSUtils.prototype.close_notification = function(item) { |
311 | 312 | }; |
312 | 313 |
|
313 | 314 |
|
| 315 | +/** |
| 316 | + * Provides table row comparator for specified column and order. |
| 317 | + */ |
| 318 | +function get_table_row_comparator(column_idx, numeric, ascending) { |
| 319 | + return function(a, b) { |
| 320 | + var valA = $(a).children("td").eq(column_idx).text(); |
| 321 | + var valB = $(b).children("td").eq(column_idx).text(); |
| 322 | + var result = numeric |
| 323 | + ? Number(valA) - Number(valB) |
| 324 | + : valA.localeCompare(valB); |
| 325 | + return ascending ? -result : result; |
| 326 | + } |
| 327 | +} |
| 328 | + |
| 329 | + |
| 330 | +/** |
| 331 | + * Sorts specified table by specified column in specified order. |
| 332 | + */ |
| 333 | +CMS.AWSUtils.sort_table = function(table, column_idx, ascending) { |
| 334 | + var initial_column_idx = table.data("initial_sort_column_idx"); |
| 335 | + var ranks_column = table.data("ranks_column"); |
| 336 | + column_idx += ranks_column ? 1 : 0; |
| 337 | + var table_rows = table |
| 338 | + .children("tbody") |
| 339 | + .children("tr"); |
| 340 | + var column_header = table |
| 341 | + .children("thead") |
| 342 | + .children("tr") |
| 343 | + .children("th") |
| 344 | + .eq(column_idx); |
| 345 | + var settings = (column_header.attr("data-sort-settings") || "").split(" "); |
| 346 | + |
| 347 | + var numeric = settings.indexOf("numeric") >= 0; |
| 348 | + |
| 349 | + // If specified, flip column's natural order, e.g. due to meaning of values. |
| 350 | + if (settings.indexOf("reversed") >= 0) { |
| 351 | + ascending = !ascending; |
| 352 | + } |
| 353 | + |
| 354 | + // Normalize column index, converting negative to positive from the end. |
| 355 | + column_idx = column_header.index(); |
| 356 | + |
| 357 | + // Reassign arrows to headers |
| 358 | + table.find(".column-sort").html("↕"); |
| 359 | + column_header.find(".column-sort").html(ascending ? "↑" : "↓"); |
| 360 | + |
| 361 | + // Do the sorting, by initial column and then by selected column. |
| 362 | + table_rows |
| 363 | + .sort(get_table_row_comparator(initial_column_idx, numeric, ascending)) |
| 364 | + .sort(get_table_row_comparator(column_idx, numeric, ascending)) |
| 365 | + .each(function(idx, row) { |
| 366 | + table.children("tbody").append(row) |
| 367 | + }); |
| 368 | + |
| 369 | + if (ranks_column) { |
| 370 | + table_rows.each(function(idx, row) { |
| 371 | + $(row).children("td").first().text(idx + 1) |
| 372 | + }); |
| 373 | + } |
| 374 | +}; |
| 375 | + |
| 376 | + |
| 377 | +/** |
| 378 | + * Makes table sortable, adding ranks column and sorting buttons in header. |
| 379 | + */ |
| 380 | +CMS.AWSUtils.init_table_sort = function(table, ranks_column, |
| 381 | + initial_column_idx, |
| 382 | + initial_ascending) { |
| 383 | + table.addClass("sortable"); |
| 384 | + var table_column_headers = table |
| 385 | + .children("thead") |
| 386 | + .children("tr"); |
| 387 | + var table_rows = table |
| 388 | + .children("tbody") |
| 389 | + .children("tr"); |
| 390 | + |
| 391 | + // Normalize column index, converting negative to positive from the end. |
| 392 | + initial_column_idx = table_column_headers |
| 393 | + .children("th") |
| 394 | + .eq(initial_column_idx) |
| 395 | + .index(); |
| 396 | + |
| 397 | + table.data("ranks_column", ranks_column); |
| 398 | + table.data("initial_sort_column_idx", initial_column_idx); |
| 399 | + |
| 400 | + // Declaring sort settings. |
| 401 | + var previous_column_idx = initial_column_idx; |
| 402 | + var ascending = initial_ascending; |
| 403 | + |
| 404 | + // Add sorting indicators to column headers |
| 405 | + table_column_headers |
| 406 | + .children("th") |
| 407 | + .each(function(column_idx, header) { |
| 408 | + $("<a/>", { |
| 409 | + href: "#", |
| 410 | + class: "column-sort", |
| 411 | + click: function() { |
| 412 | + ascending = !ascending && previous_column_idx == column_idx; |
| 413 | + previous_column_idx = column_idx; |
| 414 | + CMS.AWSUtils.sort_table(table, column_idx, ascending); |
| 415 | + } |
| 416 | + }).appendTo(header); |
| 417 | + }); |
| 418 | + |
| 419 | + // Add ranks column |
| 420 | + if (ranks_column) { |
| 421 | + table_column_headers.prepend("<th>#</th>"); |
| 422 | + table_rows.prepend("<td></td>"); |
| 423 | + } |
| 424 | + |
| 425 | + // Do initial sorting |
| 426 | + CMS.AWSUtils.sort_table(table, initial_column_idx, initial_ascending); |
| 427 | +}; |
| 428 | + |
| 429 | + |
314 | 430 | /** |
315 | 431 | * Return a string representation of the number with two digits. |
316 | 432 | * |
|
0 commit comments