-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathjquery.stickysort.min.js
More file actions
1 lines (1 loc) · 4.31 KB
/
jquery.stickysort.min.js
File metadata and controls
1 lines (1 loc) · 4.31 KB
1
(function(a){a.fn.stickySort=function(c){var b=a.extend(true,{threshold:{rows:3,viewport:0.25,px:false,allowanceEval:"min"},sortable:false,scrollThrottle:15,resizeThrottle:250},c);this.each(function(){if(a(this).is("table")&&a(this).find("thead").length>0&&a(this).find("th").length>0){var f=a(window),i=a(this),j=i.find("thead").clone(),m=i.find("thead, tbody").clone();i.wrap('<div class="sticky-wrap" />').parent().addClass(i.attr("class")).end().addClass("sticky-enabled");if(i.hasClass("overflow-y")){i.removeClass("overflow-y").parent().addClass("overflow-y")}i.after('<div class="sticky-thead"><table /></div>');if(i.find("tbody th").length>0){i.after('<div class="sticky-col"><table /></div><div class="sticky-intersect"><table /></div>')}var g=a(this).siblings(".sticky-thead"),h=a(this).siblings(".sticky-col"),n=a(this).siblings(".sticky-intersect"),k=a(this).parent(".sticky-wrap");g.find("table").append(j);h.find("table").append(m).find("thead th:gt(0)").remove().end().find("tbody td").remove();n.html("<thead><tr><th>"+i.find("thead th:first-child").html()+"</th></tr></thead>");var e=function(){i.find("thead th").each(function(o){g.find("th").eq(o).width(a(this).width())}).end().find("tr").each(function(o){h.find("tr").eq(o).height(a(this).height())});g.width(k.width()).find("table").width(i.width());h.find("th").add(n.find("th")).width(i.find("thead th").width());h.find("table").css({left:k.offset().left})},d=function(){var o=l();if(i.height()>k.height()){if(k.scrollTop()>0){g.add(n).css({opacity:1,"pointer-events":"auto",position:"fixed",top:k.offset().top-f.scrollTop(),left:k.offset().left}).find("table").css({left:-k.scrollLeft()})}else{g.add(n).css({opacity:0,"pointer-events":"none"})}}else{if(f.scrollTop()>i.offset().top&&f.scrollTop()<i.offset().top+i.outerHeight()-o){g.add(n).css({opacity:1,"pointer-events":"auto",position:"fixed",left:k.offset().left}).find("table").css({left:-k.scrollLeft()})}else{g.add(n).css({opacity:0,"pointer-events":"none",position:"absolute",left:0})}}if(k.scrollLeft()>0){h.css({position:"fixed",left:k.offset().left,top:k.offset().top-f.scrollTop(),height:k.height()}).find("table").css({top:-k.scrollTop(),left:0}).end().add(n).css({opacity:1,"pointer-events":"auto"})}else{h.css({opacity:0,"pointer-events":"none"})}},l=function(){var p=0,o=[];i.find("tbody tr:lt("+b.threshold.rows+")").each(function(){p+=a(this).height()});o.push(p);o.push(f.height()*b.threshold.viewport);if(b.threshold.px){o.push(b.threshold.px)}if(b.threshold.allowanceEval=="min"){return Math.min.apply(null,o)}else{return Math.max.apply(null,o)}};e();i.parent(".sticky-wrap").scroll(a.throttle(b.scrollThrottle,d));f.load(e).resize(a.debounce(b.resizeThrottle,function(){e();d()})).scroll(a.throttle(b.scrollThrottle,d));if(b.sortable||(i.data("sortable")!=undefined||i.hasClass("sortable"))){i.find("tbody tr").each(function(o){a(this).attr("data-sortOrder",o)});k.addClass("sortable").find("thead th").addClass("sort-default").data("sortState",1).append('<a href="#" class="sort-handle"></a>').find(".sort-handle").click(function(o){o.preventDefault()});k.on("click",".sticky-enabled thead th, .sticky-thead thead th, .sticky-col thead th, .sticky-intersect thead th",function(){var q=a(this),p=q.index(),r=k.find("thead th").eq(p),o=a.makeArray(i.find("tbody tr"));if(q.data("sortState")%3==0){o.sort(function(u,t){return parseInt(a(u).attr("data-sortOrder"))-parseInt(a(t).attr("data-sortOrder"))});q.add(r).removeClass().addClass("sort-default")}else{o.sort(function(u,t){var v=A(a(u).children().eq(p).text()),y=A(a(t).children().eq(p).text());function A(F){var I=[],C=0,H=-1,G=0,E,D;while(E=(D=F.charAt(C++)).charCodeAt(0)){var B=(E==46||(E>=48&&E<=57));if(B!==G){I[++H]="";G=B}I[H]+=D}return I}for(x=0;v[x]&&y[x];x++){if(v[x]!==y[x]){var z=Number(v[x]),w=Number(y[x]);if(z==v[x]&&w==y[x]){return z-w}else{return(v[x]>y[x])?1:-1}}}return v.length-y.length});if(q.data("sortState")%3==2){o=o.reverse();q.add(r).removeClass().addClass("sort-desc")}else{q.add(r).removeClass().addClass("sort-asc")}}var s=q.data("sortState")+1;q.add(r).data("sortState",s%3);k.find("table").each(function(t){a(this).find("thead th").not(":eq("+p+")").data("sortState",1).removeClass().addClass("sort-default")});i.find("tbody").html(o);i.find("tbody tr").each(function(t){h.find("tbody tr").eq(t).find("th").html(a(this).find("th").first().html())})})}}});return this}})(jQuery);