Skip to content

Commit

Permalink
Merge pull request #37 from Yatser/fix-diff-auto-collapsing
Browse files Browse the repository at this point in the history
Make diff ids more unique when saving them in Chrome local storage
  • Loading branch information
Brent Yates committed Mar 27, 2016
2 parents 89ee979 + e300819 commit c34e9c2
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions pullrequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var isGitHub = $("meta[property='og:site_name']").attr('content') === 'GitHub';
var useLocalStorage = true;
var lsNamespace = 'ppr'; // Prepend to entries in localStorage for some namespacing to make deletion easier.
var pullRequestNumber;
var commitHash;
var repositoryName;
var repositoryAuthor;

function htmlIsInjected() {
return $('.pretty-pull-requests').length !== 0;
Expand Down Expand Up @@ -54,6 +59,19 @@ function getId(path) {
return id;
}

function uniquify(diffId) {
var diffViewId = pullRequestNumber || commitHash;

return lsNamespace + '|' + repositoryAuthor + '|' + repositoryName + '|' + diffViewId + '|' + diffId;
}

function collectUniquePageInfo() {
repositoryAuthor = $('[itemprop="author"]').find('a').text();
repositoryName = $('strong[itemprop="name"]').find('a').text();
pullRequestNumber = $('.gh-header-number').text();
commitHash = $('.sha.user-select-contain').text();
}

function toggleDiff(id, duration, display) {
var $a = $('a[name^=' + id + ']');

Expand All @@ -63,7 +81,7 @@ function toggleDiff(id, duration, display) {
if (!useLocalStorage) {
display = 'toggle';
} else {
display = (localStorage.getItem(id) === 'collapse') ? 'expand' : 'collapse';
display = (localStorage.getItem(uniquify(id)) === 'collapse') ? 'expand' : 'collapse';
}
}

Expand All @@ -80,11 +98,11 @@ function toggleDiff(id, duration, display) {
case 'expand':
$data.slideDown(duration);
$bottom.show();
return useLocalStorage ? localStorage.removeItem(id) : true;
return useLocalStorage ? localStorage.removeItem(uniquify(id)) : true;
default:
$data.slideUp(duration);
$bottom.hide();
return useLocalStorage ? localStorage.setItem(id, display) : true;
return useLocalStorage ? localStorage.setItem(uniquify(id), display) : true;
}
}
return false;
Expand Down Expand Up @@ -119,7 +137,7 @@ function initDiffs() {
$('a[name^=diff-]').each(function(index, item) {
var id = $(item).attr('name');

if (localStorage.getItem(id) === 'collapse') {
if (localStorage.getItem(uniquify(id)) === 'collapse') {
toggleDiff(id, 0, 'collapse');
}
});
Expand Down Expand Up @@ -147,6 +165,7 @@ chrome.storage.sync.get({url: '', saveCollapsedDiffs: true, tabSwitchingEnabled:

var injectHtmlIfNecessary = function () {
if (!htmlIsInjected()) {
collectUniquePageInfo();
injectHtml();
initDiffs();
}
Expand Down

0 comments on commit c34e9c2

Please sign in to comment.