Last active
August 29, 2015 14:05
-
-
Save mwiencek/ba058fe390c9b39f39f6 to your computer and use it in GitHub Desktop.
Bitbucket: Sortable pull requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Bitbucket: Sortable pull requests | |
// @version 2014-08-22 | |
// @author Michael Wiencek <mwtuea@gmail.com> | |
// @include https://bitbucket.org/*/*/pull-requests* | |
// @match https://bitbucket.org/*/*/pull-requests* | |
// ==/UserScript== | |
//**************************************************************************// | |
var script = document.createElement("script"); | |
script.innerHTML = "(" + sortablePullRequests + ")();"; | |
document.body.appendChild(script); | |
function sortablePullRequests() { | |
function init() { | |
var $table = $("table.pullrequest-list"); | |
if (!$table.length) { | |
return false; | |
} | |
var $thead = $table.find("thead"), | |
$tbody = $table.find("tbody"); | |
if (!$thead.length || !$tbody.length) { | |
return false; | |
} | |
var currentSortColumn, | |
sortIndicator = document.createTextNode(""), | |
ascending = !$.cookie("pr-sort-descending"), | |
prNumber = /^#(\d+)/; | |
$thead.find("th").css("cursor", "pointer"); | |
function sortColumnByHeader(th, direction) { | |
var $thRow = $(th).parent(), | |
index = $thRow.children("th").index(th), | |
rows = $tbody.children("tr"), | |
sortFunc; | |
if (index === currentSortColumn) { | |
ascending = !ascending; | |
} else { | |
ascending = direction === undefined ? true : direction; | |
currentSortColumn = index; | |
} | |
$.cookie("pr-sort-column", th.className, { expires: 1000 }); | |
$.cookie("pr-sort-descending", ascending ? "" : "1", { expires: 1000 }); | |
sortIndicator.textContent = ascending ? "\u25B2" : "\u25BC"; | |
th.appendChild(sortIndicator); | |
$table[0].removeChild($tbody[0]); | |
function rowText(row) { | |
return $.trim($(row).children("td:eq(" + index + ")").text()).toLowerCase(); | |
} | |
switch (th.className) { | |
case "title": | |
sortFunc = function (row) { return +rowText(row).match(prNumber)[1] }; | |
break; | |
case "date": | |
sortFunc = function (row) { return $(row).find("time").attr("datetime") }; | |
break; | |
default: | |
sortFunc = rowText; | |
break; | |
} | |
rows = _.sortBy(rows, sortFunc); | |
if (!ascending) { | |
rows.reverse(); | |
} | |
for (var i = 0, len = rows.length; i < len; i++) { | |
$tbody[0].appendChild(rows[i]); | |
} | |
$table[0].appendChild($tbody[0]); | |
} | |
$thead.on("click", "th", function (event) { | |
if (event.target.nodeName.toLowerCase() === "th") { | |
sortColumnByHeader(event.target); | |
} | |
}); | |
var savedSortColumn = $.cookie("pr-sort-column"); | |
if (savedSortColumn) { | |
var $savedSortHeader = $thead.find("th." + savedSortColumn); | |
if ($savedSortHeader.length) { | |
sortColumnByHeader($savedSortHeader[0], ascending); | |
} | |
} | |
} | |
function tryInit() { | |
var initAttempts = 0; | |
var interval = window.setInterval(function () { | |
++initAttempts; | |
if (init() !== false || initAttempts === 6) { | |
window.clearInterval(interval); | |
} | |
}, 500); | |
} | |
tryInit(); | |
$("body").on("click", "div.filter-container a", function () { | |
_.delay(tryInit, 1000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment