Advertisement
VinDuv

What The Daily WTF Paginator

May 24th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        What The Daily WTF Paginator
  3. // @namespace   http://example.org/userscripts/tdwtfpaginator
  4. // @include     http://what.thedailywtf.com/t/*
  5. // @version     1
  6. // @grant       none
  7. // ==/UserScript==
  8. jQuery(function () {
  9.   "use strict";
  10.  
  11.   // Configuration variables
  12.   var postsPerPage = 50;
  13.   var spacerSize = 2;
  14.   var topSpacing = function () { return $('header').height() + 5 };
  15.   var rightSpacing = function () { return 10; };
  16.  
  17.   // Discourse items that we will use
  18.   var keys = Em.keys(Ember.View.views);
  19.   var views = keys.map(function (key) {
  20.     return Ember.View.views[key]
  21.   });
  22.   var topic = views.filterProperty('renderedName','topic')[0];
  23.   var controller = topic.controller;
  24.  
  25.   var getUrlForPost = topic.controller.get('urlForPostNumber').bind(topic.controller);
  26.  
  27.   // Sausage bar loading
  28.   $("head").append("<link rel='stylesheet' type='text/css' href='//christophercliff.com/sausage/lib/sausage.css' />");
  29.  
  30.   var sausageBar = $('<div />').addClass('sausage-set sausage-set-init');
  31.   $('body').append(sausageBar);
  32.  
  33.   // Display functions
  34.   var barHeight = 0;
  35.   var sausageHeight = 0;
  36.   var currentPageItem = null;
  37.  
  38.   function getPageJumper(page) {
  39.     var postId = (page - 1) * postsPerPage + 1;
  40.     return function () {
  41.       console.log(postId);
  42.       Discourse.TopicView.jumpToPost(postId);
  43.     }
  44.   }
  45.  
  46.   function getUrlForPage(page) {
  47.     var postId = (page - 1) * postsPerPage + 1;
  48.     return getUrlForPost(postId);
  49.   }
  50.  
  51.   function updateBar(pageCount, currentPage) {
  52.     var currentCount = sausageBar.children().length;
  53.     var heightUpdateNeeded = sausageBar.height() != barHeight;
  54.     barHeight = sausageBar.height();
  55.    
  56.     if (barHeight == 0) {
  57.       // We are not ready (the CSS is not loaded yet)
  58.     }
  59.  
  60.     if (currentCount > pageCount) {
  61.       heightUpdateNeeded = true;
  62.       sausageBar.children().slice(pageCount).detach();
  63.  
  64.     } else if (currentCount < pageCount) {
  65.       heightUpdateNeeded = true;
  66.       for (var i = currentCount + 1 ; i <= pageCount ; i++) {
  67.         $('<a />')
  68.           .addClass('sausage')
  69.           .append($('<div />')
  70.             .addClass('sausage-span')
  71.             .text("Page " + i)
  72.             .width(50)
  73.             .css('left', '-50px')
  74.           )
  75.           .mouseover(function () {
  76.             $(this).addClass('sausage-hover');
  77.           })
  78.           .mouseout(function () {
  79.             $(this).removeClass('sausage-hover');
  80.           })
  81.           .attr('href', getUrlForPage(i))
  82. //          .click(getPageJumper(i)) // Does not work right
  83.           .appendTo(sausageBar);
  84.       }
  85.     }
  86.  
  87.     if (heightUpdateNeeded) {
  88.       var usedSpacerSize = spacerSize;
  89.       sausageHeight = (barHeight - topSpacing() - spacerSize * (pageCount + 1)) / pageCount;
  90.  
  91.       if (sausageHeight <= 0) {
  92.         usedSpacerSize = 0;
  93.         sausageHeight = barHeight / pageCount;
  94.       }
  95.  
  96.       sausageBar.css({
  97.         'top': topSpacing() + 'px',
  98.         'right': rightSpacing() + 'px'
  99.       });
  100.      
  101.       var position = usedSpacerSize;
  102.       sausageBar.children().each(function () {
  103.         $(this).css({
  104.           'height': sausageHeight + 'px',
  105.           'top': position + 'px',
  106.         });
  107.         position += sausageHeight + usedSpacerSize;
  108.       });
  109.     }
  110.  
  111.     if (currentPageItem) {
  112.       currentPageItem.removeClass('sausage-current');
  113.     }
  114.  
  115.     currentPageItem = sausageBar.children(':eq(' + (currentPage - 1) + ')');
  116.     currentPageItem.addClass('sausage-current');
  117.   }
  118.  
  119.   // Pagination
  120.   var currentPage = -1;
  121.   function updatePagination(force) {
  122.     var currentPost = controller.get('currentPost') - 1;
  123.     var lastPost = controller.get('highest_post_number');
  124.     var newPage = Math.floor(currentPost / postsPerPage) + 1;
  125.     var pageCount = Math.ceil(lastPost / postsPerPage);
  126.    
  127.     if (newPage == currentPage && !force) {
  128.       return;
  129.     }
  130.     currentPage = newPage;
  131.    
  132.     updateBar(pageCount, currentPage);
  133.   }
  134.  
  135.   // Hooking into the Discourse "progress bar"
  136.   var oldUpdateProgressBar = topic._updateProgressBar;
  137.  
  138.   topic._updateProgressBar = function () {
  139.     oldUpdateProgressBar.apply(this, arguments);
  140.     updatePagination();
  141.   };
  142.    
  143.   // Wait for CSS to be fully loaded before doing the first update
  144.   var waitTimer = window.setInterval(function () {
  145.     if (sausageBar.height() > 0) {
  146.       window.clearInterval(waitTimer);
  147.       updatePagination();
  148.       $(window).resize(function () { updatePagination(true); });
  149.     }
  150.   }, 50);
  151. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement