Skip to content

Instantly share code, notes, and snippets.

@dccampbell
Last active August 29, 2015 14:26
Show Gist options
  • Save dccampbell/350b27eff01b7488864b to your computer and use it in GitHub Desktop.
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.
// ==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);
@dccampbell
Copy link
Author

Minified one-liner for easy copy-paste:

!function(t){t("table.projectList").length<1||(!function(){function t(e,r){if(!(this instanceof t))return new t(e,r);if(!e||"TABLE"!==e.tagName)throw new Error("Element must be a table");this.init(e,r||{})}var e=[],r=function(t){var e;return window.CustomEvent&&"function"==typeof window.CustomEvent?e=new CustomEvent(t):(e=document.createEvent("CustomEvent"),e.initCustomEvent(t,!1,!1,void 0)),e},o=function(t){return t.getAttribute("data-sort")||t.textContent||t.innerText||""},n=function(t,e){return t=t.toLowerCase(),e=e.toLowerCase(),t===e?0:e>t?1:-1},s=function(t,e){return function(r,o){var n=t(r.td,o.td);return 0===n?e?o.index-r.index:r.index-o.index:n}};t.extend=function(t,r,o){if("function"!=typeof r||"function"!=typeof o)throw new Error("Pattern and sort must be a function");e.push({name:t,pattern:r,sort:o})},t.prototype={init:function(t,e){var r,o,n,s,a=this;if(a.table=t,a.thead=!1,a.options=e,t.rows&&t.rows.length>0&&(t.tHead&&t.tHead.rows.length>0?(r=t.tHead.rows[t.tHead.rows.length-1],a.thead=!0):r=t.rows[0]),r){var i=function(){a.current&&a.current!==this&&(a.current.classList.remove("sort-up"),a.current.classList.remove("sort-down")),a.current=this,a.sortTable(this)};for(n=0;n<r.cells.length;n++)s=r.cells[n],s.classList.contains("no-sort")||(s.classList.add("sort-header"),s.tabindex=0,s.addEventListener("click",i,!1),s.classList.contains("sort-default")&&(o=s));o&&(a.current=o,a.sortTable(o))}},sortTable:function(t,a){var i,d=this,c=t.cellIndex,l=n,u="",f=[],h=d.thead?0:1,p=t.getAttribute("data-sort-method");if(d.table.dispatchEvent(r("beforeSort")),a?i=t.classList.contains("sort-up")?"sort-up":"sort-down":(i=t.classList.contains("sort-up")?"sort-down":t.classList.contains("sort-down")?"sort-up":d.options.descending?"sort-up":"sort-down",t.classList.remove("sort-down"===i?"sort-up":"sort-down"),t.classList.add(i)),!(d.table.rows.length<2)){if(!p){for(;f.length<3&&h<d.table.tBodies[0].rows.length;)u=o(d.table.tBodies[0].rows[h].cells[c]),u=u.trim(),u.length>0&&f.push(u),h++;if(!f)return}for(h=0;h<e.length;h++)if(u=e[h],p){if(u.name===p){l=u.sort;break}}else if(f.every(u.pattern)){l=u.sort;break}d.col=c;var w,b=[],m={},v=0,g=0;for(h=0;h<d.table.tBodies.length;h++)for(w=0;w<d.table.tBodies[h].rows.length;w++)u=d.table.tBodies[h].rows[w],u.classList.contains("no-sort")?m[v]=u:b.push({tr:u,td:o(u.cells[d.col]),index:v}),v++;for("sort-down"===i?(b.sort(s(l,!0)),b.reverse()):b.sort(s(l,!1)),h=0;v>h;h++)m[h]?(u=m[h],g++):u=b[h-g].tr,d.table.tBodies[0].appendChild(u);d.table.dispatchEvent(r("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=t:window.Tablesort=t}(),!function(){var t=function(t){return t=t.replace(/\-/g,"/"),t=t.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),new Date(t).getTime()||-1};Tablesort.extend("date",function(e){return(-1!==e.search(/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i)||-1!==e.search(/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/)||-1!==e.search(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i))&&!isNaN(t(e))},function(e,r){return e=e.toLowerCase(),r=r.toLowerCase(),t(r)-t(e)})}(),t("tr.categoryRow").remove(),t("table.projectList").prepend(t("<thead>")),t("tr.projectHeader").detach().appendTo("thead"),t("tr.projectHeader").find("th").eq(3).addClass("sort-default"),t("tr.projectRow").each(function(){t(this).removeClass("light").removeClass("dark").find(".indent").removeClass("indent"),t(this).find("td").addClass("no-sort"),t(this).find("td.projectAge").removeClass("no-sort"),t(this).find("td.projectAge").attr("data-sort-method","date"),t(this).find("td.projectAge").attr("data-sort",t(this).find("time").length?Date.parse(t(this).find("time").attr("datetime")):0)}),new Tablesort(t("table.projectList").get(0),{descending:!0}))}(jQuery);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment