sidebysidecommitdiff.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * GitPHP javascript commitdiff
  3. *
  4. * Javascript enhancements to make side-by-side
  5. * commitdiff more usable
  6. *
  7. * @author Christopher Han <xiphux@gmail.com>
  8. * @copyright Copyright (c) 2011 Christopher Han
  9. * @package GitPHP
  10. * @subpackage Javascript
  11. */
  12. define(["jquery"],
  13. function($) {
  14. var toc = null;
  15. var blobContent = null;
  16. var tocYLoc = null;
  17. var tocPosition = null;
  18. var tocTop = null;
  19. var pinned = false;
  20. function scrollToTop() {
  21. var contentTop = blobContent.offset().top;
  22. if ($(document).scrollTop() > contentTop) {
  23. $('html, body').animate({
  24. scrollTop: contentTop
  25. }, 200);
  26. }
  27. }
  28. function markTOCItemActive(item) {
  29. toc.find('a.SBSTOCItem').each(function(index, value) {
  30. if (item == value) {
  31. $(this).parent().addClass('activeItem');
  32. } else {
  33. $(this).parent().removeClass('activeItem');
  34. }
  35. });
  36. }
  37. function showBlob(id) {
  38. blobContent.find('div.diffBlob').each(function() {
  39. var jThis = $(this);
  40. if (jThis.attr('id') == id) {
  41. jThis.slideDown('fast');
  42. } else {
  43. jThis.slideUp('fast');
  44. }
  45. });
  46. }
  47. var windowScroll = function() {
  48. var windowYLoc = $(document).scrollTop();
  49. if (windowYLoc > tocYLoc) {
  50. if (!pinned) {
  51. toc.css('position', 'fixed');
  52. toc.css('top', '0px');
  53. pinned = true;
  54. }
  55. } else {
  56. if (pinned) {
  57. toc.css('position', tocPosition);
  58. toc.css('top', tocTop);
  59. pinned = false;
  60. }
  61. }
  62. };
  63. var tocClick = function() {
  64. var jThis = $(this);
  65. markTOCItemActive(jThis.get(0));
  66. showBlob(jThis.attr('href').substring(1));
  67. toc.find('a.showAll').show();
  68. scrollToTop();
  69. return false;
  70. };
  71. var showAllClick = function() {
  72. toc.find('a.SBSTOCItem').parent().removeClass('activeItem');
  73. blobContent.find('div.diffBlob').slideDown('fast');
  74. $(this).hide();
  75. scrollToTop();
  76. return false;
  77. };
  78. var init = function(tocElem, blobContentElem) {
  79. toc = tocElem;
  80. blobContent = blobContentElem;
  81. tocYLoc = toc.position().top;
  82. tocPosition = toc.css('position');
  83. tocTop = toc.css('top');
  84. $(window).scroll(windowScroll);
  85. toc.find('a.SBSTOCItem').click(tocClick);
  86. toc.find('a.showAll').click(showAllClick);
  87. };
  88. return {
  89. init: init
  90. }
  91. }
  92. );