A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.4 KiB

  1. extends layout
  2. block content
  3. div.uk-grid
  4. br
  5. br
  6. div.uk-width-1-1
  7. div.uk-panel.uk-panel-box
  8. div.uk-badge.uk-float-right
  9. form.uk-search
  10. input(class='uk-search-field', type='search', placeholder='Search...')
  11. button(class='uk-close', type='reset')
  12. table.uk-table.uk-table-hover.uk-table-striped
  13. caption #{ucfirst(currency)} Farmer Ladder Rankings (<span id="total_farmers"></span> farmers total)
  14. thead
  15. tr
  16. th Rank
  17. th Viewer
  18. th #{ucfirst(currency)}
  19. tbody
  20. ul.uk-pagination
  21. block postscript
  22. script.
  23. var perPage = 20,
  24. data = !{JSON.stringify(rows)},
  25. lastPage = Math.ceil(data.length/perPage);
  26. function minifyPagination() {
  27. var current = parseInt($('li.uk-active').attr('page')),
  28. min = Math.max(current-3, 1),
  29. max = current+3;
  30. $('#page_first, #page_last').remove();
  31. $('ul.uk-pagination > li').addClass('uk-hidden');
  32. for(var x = min; x <= max; x++) {
  33. $('li#'+x).removeClass('uk-hidden');
  34. }
  35. if (min > 1)
  36. $('li#'+min).before("<button id='page_first' class='uk-button'>&lt;&lt;</button>");
  37. if (max < lastPage)
  38. $('li#'+max).after("<button id='page_last' class='uk-button'>&gt;&gt;</button>");
  39. $('#page_first').click(function(){
  40. $('li#'+1).click();
  41. });
  42. $('#page_last').click(function(){
  43. $('li#'+lastPage).click();
  44. });
  45. };
  46. $(document).ready(function(){
  47. // Live search of the ladder listings
  48. $('.uk-search-field').keyup(function() {
  49. var query = $(this).val();
  50. if(query.length > 2) {
  51. var temp = $('.uk-active').attr('page');
  52. $('.uk-active').removeClass('uk-active').empty().append('<a>'+temp+'</a>');
  53. $('table > tbody').empty();
  54. data.forEach(function(element, index, array) {
  55. if(element.user.search(new RegExp(query, 'i')) != -1) {
  56. $('table > tbody').append($('<tr></tr>').append('<td>'+index+'</td><td>'+element.user+'</td><td>'+element.points+'</td>'));
  57. }
  58. });
  59. }
  60. });
  61. // Create pagination buttons
  62. for(var i = 1; i <= lastPage; i++) {
  63. var button = $('<li page='+i+' id='+i+'></li>').append('<a>'+i+'</a>');
  64. button.on('click', function(){
  65. //get page
  66. var z = $(this).attr('page');
  67. // cleanup
  68. var temp = $('.uk-active').attr('page');
  69. $('.uk-active').removeClass('uk-active').empty().append('<a>'+temp+'</a>');
  70. $('table > tbody').empty();
  71. $(this).addClass('uk-active').empty().append('<span>'+z+'</span>');
  72. // slice(a, b): a = (n*(x-1))+1, b = n*x where n = perPage and x=curerntPage (skip +1 at end of a for splice)
  73. var a = (perPage*(z-1)),
  74. b = perPage*z;
  75. data.slice(a, b).forEach(function(element, index, array){
  76. $('table > tbody').append($('<tr></tr>').append('<td>'+(Number(a+index+1).toLocaleString('en'))+'</td><td>'+element.user+'</td><td>'+element.points+'</td>'));
  77. });
  78. });
  79. $('.uk-pagination').append(button);
  80. }
  81. // When search input is cleared, go back to first page
  82. $('.uk-close').on('click', function(){
  83. $('ul.uk-pagination > li').first().click();
  84. });
  85. // Show the first page when we load up
  86. $('ul.uk-pagination > li').first().click();
  87. minifyPagination();
  88. $('ul.uk-pagination > li').on('click', function(){
  89. minifyPagination();
  90. });
  91. $('#total_farmers').append(Number(data.length).toLocaleString('en'));
  92. });