Collapsable Diffs and Linked Branches (GitHub)

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

Versione datata 11/02/2017. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Collapsable Diffs and Linked Branches (GitHub)
// @namespace   chriskim06
// @description Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces
// @include     https://github.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @require     https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=19641
// @version     1.5.4
// @grant       none
// @locale      en
// ==/UserScript==

this.$ = this.jQuery = jQuery.noConflict(true);

$(function() {

  waitForKeyElements('#files div[id^="diff-"]', addDiffCollapseButtons);
  waitForKeyElements('.pr-review-tools > .diffbar-item:nth-child(1)', addCollapseAllButton);
  waitForKeyElements('#partial-discussion-header .commit-ref', makeLinks);

  function addDiffCollapseButtons(jNode) {
    var expanded = 'M0 5l6 6 6-6H0z';
    var collapsed = 'M0 14l6-6L0 2v12z';

    // Add buttons in each header to allow folding the diff
    var info = jNode.find('.file-info');
    if (!info.children().first().is('a')) {
      // Add the button if it isn't there
      info.prepend(
        '<a class="octicon-btn custom-collapsable" href="javascript:void(0)">' +
          '<svg height="16" width="12" xmlns="http://www.w3.org/2000/svg">' +
            '<path d="' + expanded + '" />' +
          '</svg>' +
        '</a>'
      );
    }

    // Add the event handler if its not already bound to the arrow
    var btn = jNode.find('.octicon-btn.custom-collapsable');
    if (btn.length) {
      var events = $.data(btn.get(0), 'events');
      if (!events || !events.click) {
        btn.on('click', function() {
          // Toggle the visibility of the diff and the direction of the arrow
          var icon = $(this).find('path');
          jNode.children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content').toggle();
          icon.attr('d', (icon.attr('d') === collapsed) ? expanded : collapsed);
          var collapseAllButton = $('#diff-collapse-button');
          if (collapseAllButton.length) {
            var state = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper').is(':visible');
            collapseAllButton.attr('data-toggle-state', state ? 'expanded' : 'collapsed');
            collapseAllButton.text(state ? 'Fold All' : 'Show All');
          }
        });
      }
    }
  }

  function addCollapseAllButton(jNode) {
    // Add a Show/Fold All button if its not there
    if (!$('#diff-collapse-button').length) {
      var blobs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content');
      jNode.after(
        '<div class="diffbar-item">' +
          '<button id="diff-collapse-button"' +
            'class="btn btn-sm btn-outline BtnGroup-item" style="width: 75px"' +
            'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
            (blobs.is(':visible') ? 'Fold All' : 'Show All') +
          '</button>' +
        '</div>'
      );
      $('#diff-collapse-button').on('click', function() {
        // Toggle the visibility of all diffs, directions of arrows, and the button
        var diffs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content');
        var state = ($(this).attr('data-toggle-state') === 'expanded');
        if (state) {
          diffs.hide();
        } else {
          diffs.show();
        }
        var expanded = 'M0 5l6 6 6-6H0z';
        var collapsed = 'M0 14l6-6L0 2v12z';
        $('.octicon-btn.custom-collapsable').find('path').attr('d', state ? collapsed : expanded);
        $(this).attr('data-toggle-state', state ? 'collapsed' : 'expanded');
        $(this).text(state ? 'Show All' : 'Fold All');
      });
    }
  }

  function makeLinks(jNode) {
    if (!jNode.children(':first-child').is('a')) {
      // Turn the branches being compared into links if they aren't already
      var url = window.location.href.match(/(https:\/\/github\.com\/)[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)/);
      if (url) {
        var branch = jNode.contents().text();
        if (!branch.includes(':')) {
          jNode.contents().wrapAll('<a href="' + url[0] + '/tree/' + branch + '"></a>');
        } else {
          jNode.contents().wrapAll('<a href="' + url[1] + branch.replace(':', url[2] + '/tree/') + '"></a>');
        }
      }
    }
  }

});