Collapsable Diffs and Linked Branches (GitHub)

Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces

As of 2016-03-16. See the latest version.

  1. // ==UserScript==
  2. // @name Collapsable Diffs and Linked Branches (GitHub)
  3. // @namespace chriskim06
  4. // @description Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces
  5. // @include https://github.com/*
  6. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  7. // @version 1.4.2
  8. // @grant none
  9. // @locale en
  10. // ==/UserScript==
  11.  
  12. this.$ = this.jQuery = jQuery.noConflict(true);
  13.  
  14. $(function() {
  15.  
  16. function collapsable() {
  17. var files = $('#files');
  18. if (files.length) {
  19. // Add buttons in each header to allow folding the diff
  20. files.find('div[id^="diff-"]').each(function() {
  21. var diff = $(this);
  22. var info = diff.find('.file-info');
  23. if (!info.children().first().is('a')) {
  24. // If the button isn't there add it and add the event handler
  25. info.prepend(
  26. '<a class="octicon-btn custom-collapsable" href="javascript:void(0)">' +
  27. '<svg height="16" width="12" xmlns="http://www.w3.org/2000/svg">' +
  28. '<path d="M0 5l6 6 6-6H0z" />' +
  29. '</svg>' +
  30. '</a>'
  31. );
  32. diff.find('.octicon-btn.custom-collapsable').on('click', function() {
  33. // Toggle the visibility of the diff and the direction of the arrow
  34. var icon = $(this).find('path');
  35. var arrow = (icon.attr('d') === 'M0 14l6-6L0 2v12z') ? 'M0 5l6 6 6-6H0z' : 'M0 14l6-6L0 2v12z';
  36. diff.children('.data.highlight.blob-wrapper').toggle();
  37. icon.attr('d', arrow);
  38. });
  39. }
  40. });
  41.  
  42. var diffOptions = $('.diffbar-item.dropdown.js-menu-container');
  43. if (diffOptions.length && diffOptions.find('a').length === 2) {
  44. // Add an Expand/Collapse All button if its not there
  45. var blobs = files.find('div[id^="diff-"]').children('.data.highlight.blob-wrapper');
  46. $('.diffbar-item.dropdown.js-menu-container').find('ul').append(
  47. '<a id="diff-collapse-button" ' +
  48. 'href="javascript:void(0)" ' +
  49. 'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
  50. (blobs.is(':visible') ? 'Collapse' : 'Expand') + ' All'
  51. '</a>'
  52. );
  53. $('#diff-collapse-button').on('click', function() {
  54. // Toggle the visibility of all diffs, directions of arrows, and the button
  55. var allButton = $(this);
  56. var icons = $('.octicon-btn.custom-collapsable').find('path');
  57. var state = (allButton.attr('data-toggle-state') === 'expanded');
  58. blobs.toggle();
  59. icons.attr('d', state ? 'M0 14l6-6L0 2v12z' : 'M0 5l6 6 6-6H0z');
  60. allButton.attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  61. allButton.text(state ? 'Expand All' : 'Collapse All');
  62. });
  63. }
  64. }
  65. }
  66.  
  67. function makeLinks() {
  68. var head = $('#partial-discussion-header');
  69. if (head.length && head.find('.flex-table-item.flex-table-item-primary > a').length === 1) {
  70. // Turn the branches being compared into links if they aren't already
  71. $('span.commit-ref.current-branch').each(function() {
  72. var elem = $(this);
  73. var repo = $('.entry-title').find('a[data-pjax]');
  74. var url = 'https://github.com';
  75. var branch = elem.text();
  76. if (branch.indexOf(':') === -1) {
  77. url += repo.attr('href') + '/tree/' + branch;
  78. } else {
  79. var fork = branch.split(':');
  80. url += '/' + fork[0] + '/' + repo.text() + '/tree/' + fork[1];
  81. }
  82. elem.wrap('<a href="' + url + '"></a>');
  83. });
  84. }
  85. }
  86. makeLinks();
  87. collapsable();
  88.  
  89. window.$(document).on('pjax:end pjax:complete', function() {
  90. makeLinks();
  91. collapsable();
  92. });
  93. });