Last active
August 29, 2015 14:26
-
-
Save dccampbell/350b27eff01b7488864b to your computer and use it in GitHub Desktop.
Tampermonkey-friendly JS for modifying a GitPHP repo list page. Fixes age sorting to work across all repos by removing categories first.
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 GitPHP - Age Sort w/o Categories | |
// @version 1.0.0 | |
// @author dccampbell | |
// @run-at document-end | |
// @match http*://*/?sort=age | |
// @require http://code.jquery.com/jquery-latest.js | |
// @description GitPHP Repo List - Remove categories and sort all repos by last update. | |
// ==/UserScript== | |
(function($){ | |
// Safety Check | |
if($('table.projectList').length < 1) return; | |
// tablesort v3.1.0 (2015-07-03) - http://tristen.ca/tablesort/demo/ | |
!function(){function a(b,c){if(!(this instanceof a))return new a(b,c);if(!b||"TABLE"!==b.tagName)throw new Error("Element must be a table");this.init(b,c||{})}var b=[],c=function(a){var b;return window.CustomEvent&&"function"==typeof window.CustomEvent?b=new CustomEvent(a):(b=document.createEvent("CustomEvent"),b.initCustomEvent(a,!1,!1,void 0)),b},d=function(a){return a.getAttribute("data-sort")||a.textContent||a.innerText||""},e=function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a===b?0:b>a?1:-1},f=function(a,b){return function(c,d){var e=a(c.td,d.td);return 0===e?b?d.index-c.index:c.index-d.index:e}};a.extend=function(a,c,d){if("function"!=typeof c||"function"!=typeof d)throw new Error("Pattern and sort must be a function");b.push({name:a,pattern:c,sort:d})},a.prototype={init:function(a,b){var c,d,e,f,g=this;if(g.table=a,g.thead=!1,g.options=b,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(c=a.tHead.rows[a.tHead.rows.length-1],g.thead=!0):c=a.rows[0]),c){var h=function(){g.current&&g.current!==this&&(g.current.classList.remove("sort-up"),g.current.classList.remove("sort-down")),g.current=this,g.sortTable(this)};for(e=0;e<c.cells.length;e++)f=c.cells[e],f.classList.contains("no-sort")||(f.classList.add("sort-header"),f.tabindex=0,f.addEventListener("click",h,!1),f.classList.contains("sort-default")&&(d=f));d&&(g.current=d,g.sortTable(d))}},sortTable:function(a,g){var h,i=this,j=a.cellIndex,k=e,l="",m=[],n=i.thead?0:1,o=a.getAttribute("data-sort-method");if(i.table.dispatchEvent(c("beforeSort")),g?h=a.classList.contains("sort-up")?"sort-up":"sort-down":(h=a.classList.contains("sort-up")?"sort-down":a.classList.contains("sort-down")?"sort-up":i.options.descending?"sort-up":"sort-down",a.classList.remove("sort-down"===h?"sort-up":"sort-down"),a.classList.add(h)),!(i.table.rows.length<2)){if(!o){for(;m.length<3&&n<i.table.tBodies[0].rows.length;)l=d(i.table.tBodies[0].rows[n].cells[j]),l=l.trim(),l.length>0&&m.push(l),n++;if(!m)return}for(n=0;n<b.length;n++)if(l=b[n],o){if(l.name===o){k=l.sort;break}}else if(m.every(l.pattern)){k=l.sort;break}i.col=j;var p,q=[],r={},s=0,t=0;for(n=0;n<i.table.tBodies.length;n++)for(p=0;p<i.table.tBodies[n].rows.length;p++)l=i.table.tBodies[n].rows[p],l.classList.contains("no-sort")?r[s]=l:q.push({tr:l,td:d(l.cells[i.col]),index:s}),s++;for("sort-down"===h?(q.sort(f(k,!0)),q.reverse()):q.sort(f(k,!1)),n=0;s>n;n++)r[n]?(l=r[n],t++):l=q[n-t].tr,i.table.tBodies[0].appendChild(l);i.table.dispatchEvent(c("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}(); | |
// tablesort date - https://github.com/tristen/tablesort/blob/gh-pages/src/sorts/tablesort.date.js | |
!function(){var e=function(e){return e=e.replace(/\-/g,"/"),e=e.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),new Date(e).getTime()||-1};Tablesort.extend("date",function(r){return(-1!==r.search(/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i)||-1!==r.search(/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/)||-1!==r.search(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i))&&!isNaN(e(r))},function(r,n){return r=r.toLowerCase(),n=n.toLowerCase(),e(n)-e(r)})}(); | |
// Remove Categories | |
$('tr.categoryRow').remove(); | |
// Fix Headers | |
$('table.projectList').prepend($('<thead>')); | |
$('tr.projectHeader').detach().appendTo('thead'); | |
$('tr.projectHeader').find('th').eq(3).addClass('sort-default'); | |
// Prepare Rows | |
$('tr.projectRow').each(function(i) { | |
$(this).removeClass('light').removeClass('dark').find('.indent').removeClass('indent'); | |
$(this).find('td').addClass('no-sort'); | |
$(this).find('td.projectAge').removeClass('no-sort'); | |
$(this).find('td.projectAge').attr('data-sort-method', 'date'); | |
$(this).find('td.projectAge').attr('data-sort', ($(this).find('time').length ? Date.parse($(this).find('time').attr("datetime")) : 0) ); | |
}); | |
// Run Sort | |
new Tablesort($('table.projectList').get(0), {descending: true}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minified one-liner for easy copy-paste: