forked from mkoryak/floatThead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.floatThead.min.js
3 lines (3 loc) · 7.32 KB
/
jquery.floatThead.min.js
1
2
3
// @preserve jQuery.floatThead 1.2.2 - http://mkoryak.github.io/floatThead/ - Copyright (c) 2012 - 2014 Misha Koryak
// @license Licensed under http://creativecommons.org/licenses/by-sa/4.0/
!function(a){function b(a,b){i.bind("resize.floatTHead",_.debounce(b,a))}function c(a){window.console&&window.console&&window.console.log&&window.console.log(a)}function d(){var b=a('<div style="width:50px;height:50px;overflow-y:scroll;position:absolute;top:-200px;left:-200px;"><div style="height:100px;width:100%"></div>');a("body").append(b);var c=b.innerWidth(),d=a("div",b).innerWidth();return b.remove(),c-d}function e(a){if(a.dataTableSettings)for(var b=0;b<a.dataTableSettings.length;b++){var c=a.dataTableSettings[b].nTable;if(a[0]==c)return!0}return!1}a.floatThead={defaults:{cellTag:"th:visible",zIndex:1001,debounceResizeMs:1,useAbsolutePositioning:!0,scrollingTop:0,scrollingBottom:0,scrollContainer:function(){return a([])},getSizingRow:function(a){return a.find("tbody tr:visible:first>td")},floatTableClass:"floatThead-table",floatContainerClass:"floatThead-container",debug:!1}};var f=function(){for(var a=3,b=document.createElement("b"),c=b.all||[];b.innerHTML="<!--[if gt IE "+ ++a+"]><i><![endif]-->",c[0];);return a>4?a:document.documentMode}(),g=null,h=function(){if(f)return!1;var b=a("<table><colgroup><col></colgroup><tbody><tr><td style='width:10px'></td></tbody></table>");a("body").append(b);var c=b.find("col").width();return b.remove(),0==c},i=a(window),j=0;a.fn.floatThead=function(k){if(8>f)return this;if(null==g&&(g=h(),g&&(document.createElement("fthtr"),document.createElement("fthtd"),document.createElement("fthfoot"))),_.isString(k)){var l=k,m=this;return this.filter("table").each(function(){var b=a(this).data("floatThead-attached");if(b&&_.isFunction(b[l])){var c=b[l]();"undefined"!=typeof c&&(m=c)}}),m}var n=a.extend({},a.floatThead.defaults,k);return _.each(k,function(b,d){d in a.floatThead.defaults||!n.debug||c("jQuery.floatThead: used ["+d+"] key to init plugin, but that param is not an option for the plugin. Valid options are: "+_.keys(a.floatThead.defaults).join(", "))}),this.filter(":not(."+n.floatTableClass+")").each(function(){function c(){var a=w.outerHeight(!0);T.outerHeight(a),U.outerHeight(a)}function h(){var a=v.outerWidth(),b=E.width()||a;if(R.width(b-B.vertical),K){var c=100*a/(b-B.vertical);N.css("width",c+"%")}else N.outerWidth(a)}function k(){y=(_.isFunction(n.scrollingTop)?n.scrollingTop(v):n.scrollingTop)||0,z=(_.isFunction(n.scrollingBottom)?n.scrollingBottom(v):n.scrollingBottom)||0}function l(){var b=w.find("tr:first>"+n.cellTag),c=_.reduce(b,function(b,c){var d=parseInt(a(c).attr("colspan")||1,10);return b+d},0);if(c!=D){D=c;for(var d=[],e=[],f=[],h=0;c>h;h++)d.push('<th class="floatThead-col-'+h+'"/>'),e.push("<col/>"),f.push("<fthtd style='display:table-cell;height:0;width:auto;'/>");e=e.join(""),d=d.join(""),g&&(f=f.join(""),Q.html(f),X=Q.find("fthtd")),T.html(d),P.html(e),V=P.find("col"),O.html(e),W=O.find("col")}return c}function m(){A||(A=!0,v.css($),N.css($),N.append(w),x.before(S),c())}function o(){A&&(A=!1,S.detach(),v.prepend(w),v.css(ab),N.css(ab))}function p(a){F!=a&&(F=a,R.css({position:F?"absolute":"fixed"}))}function q(a,b,c,d){return g?c:d?n.getSizingRow(a,b,c):b}function r(){var a,b=l();return function(){var d=q(v,V,X,f);if(d.length==b&&b>0){for(o(),a=0;b>a;a++){var e=d.get(a),g=e.offsetWidth;W.eq(a).width(g),V.eq(a).width(g)}m()}else N.append(w),v.css(ab),N.css(ab),c()}}function s(){var a,b=E.scrollTop(),c=0,d=H?G.outerHeight(!0):0,e=I?d:-d,f=R.height(),h=v.offset();if(K){var j=E.offset();c=h.top-j.top+b,H&&I&&(c+=d)}else a=h.top-y-f+z+B.horizontal;var k=i.scrollTop(),l=i.scrollLeft(),n=E.scrollLeft();return b=E.scrollTop(),function(j){if("windowScroll"==j?(k=i.scrollTop(),l=i.scrollLeft()):"containerScroll"==j?(b=E.scrollTop(),n=E.scrollLeft()):"init"!=j&&(k=i.scrollTop(),l=i.scrollLeft(),b=E.scrollTop(),n=E.scrollLeft()),!g||!(0>k||0>l)){if(M)"windowScrollDone"==j?p(!0):p(!1);else if("windowScrollDone"==j)return null;h=v.offset(),H&&I&&(h.top+=d);var q,r,s;if(K&&F){if(c>=b){var t=c-b;t=t>0?t:0,q=t}else q=L?0:b;r=0}else!K&&F?(s=v.outerHeight(),k>a+s+e?q=s-f+e:h.top>k+y?(q=0,o()):(q=y+k-h.top+c+(I?d:0),m()),r=0):K&&!F?(c>b?(q=h.top-k,o()):(q=h.top+b-k-c,m()),r=h.left+n-l):K||F||(s=v.outerHeight(),k>a+s+e?q=s+y-k+a+e:h.top>k+y?(q=h.top-k,m()):q=y,r=h.left-l);return{top:q,left:r}}}}function t(){var a=null,b=null,d=null;return function(e,f,g){null==e||a==e.top&&b==e.left||(R.css({top:e.top,left:e.left}),a=e.top,b=e.left),f&&h(),g&&c();var i=E.scrollLeft();d!=i&&(R.scrollLeft(i),d=i)}}function u(){E.length&&(B.horizontal=E.width()<v.width()?C:0,B.vertical=E.height()<v.height()?C:0)}var v=a(this);if(v.data("floatThead-attached"))return!0;if(!v.is("table"))throw new Error('jQuery.floatThead must be run on a table element. ex: $("table").floatThead();');var w=v.find("thead:first"),x=v.find("tbody:first");if(0==w.length)throw new Error("jQuery.floatThead must be run on a table that contains a <thead> element");var y,z,A=!1,B={vertical:0,horizontal:0},C=d(),D=0,E=n.scrollContainer(v)||a([]),F=n.useAbsolutePositioning;null==F&&(F=n.scrollContainer(v).length);var G=v.find("caption"),H=1==G.length;if(H)var I="top"===(G.css("caption-side")||G.attr("align")||"top");var J=a('<fthfoot style="display:table-footer-group;"/>'),K=E.length>0,L=!1,M=9>=f&&!K&&F,N=a("<table/>"),O=a("<colgroup/>"),P=a("<colgroup/>"),Q=a('<fthrow style="display:table-row;height:0;"/>'),R=a('<div style="overflow: hidden;"></div>'),S=a("<thead/>"),T=a('<tr class="size-row"/>'),U=a([]),V=a([]),W=a([]),X=a([]);if(S.append(T),v.prepend(P),g&&(J.append(Q),v.append(J)),N.append(O),R.append(N),N.attr("class",v.attr("class")),N.addClass(n.floatTableClass).css("margin",0),F){var Y=function(a,b){var c=a.css("position"),d="relative"==c||"absolute"==c;if(!d||b){var e={paddingLeft:a.css("paddingLeft"),paddingRight:a.css("paddingRight")};R.css(e),a=a.wrap("<div style='position: relative; clear:both;'></div>").parent(),L=!0}return a};if(K){var Z=Y(E,!0);Z.append(R)}else Y(v),v.after(R)}else v.after(R);R.css({position:F?"absolute":"fixed",marginTop:0,top:F?0:"auto",zIndex:n.zIndex}),R.addClass(n.floatContainerClass),k();var $={"table-layout":"fixed"},ab={"table-layout":v.css("tableLayout")||"auto"};u();var bb,cb=function(){(bb=r())()};cb();var db=s(),eb=t();eb(db("init"),!0);var fb=_.debounce(function(){eb(db("windowScrollDone"),!1)},300),gb=function(){eb(db("windowScroll"),!1),fb()},hb=function(){eb(db("containerScroll"),!1)},ib=function(){k(),u(),cb(),db=s(),(eb=t())(db("resize"),!0,!0)},jb=_.debounce(function(){u(),k(),cb(),db=s(),eb(db("reflow"),!0)},1);K?F?E.bind("scroll.floatTHead",hb):(E.bind("scroll.floatTHead",hb),i.bind("scroll.floatTHead",gb)):i.bind("scroll.floatTHead",gb),i.bind("load.floatTHead",jb),b(n.debounceResizeMs,ib),v.bind("reflow",jb),e(v)&&v.bind("filter",jb).bind("sort",jb).bind("page",jb),v.data("floatThead-attached",{destroy:function(){v.css(ab),P.remove(),g&&J.remove(),S.parent().length&&S.replaceWith(w),v.unbind("reflow"),jb=ib=hb=gb=function(){},E.unbind("scroll.floatTHead"),R.remove(),v.data("floatThead-attached",!1),j--,0==j&&(i.unbind("scroll.floatTHead"),i.unbind("resize.floatTHead"),i.unbind("load.floatTHead"))},reflow:function(){jb()},setHeaderHeight:function(){c()},getFloatContainer:function(){return R},getRowGroups:function(){return A?R.find("thead").add(v.find("tbody,tfoot")):v.find("thead,tbody,tfoot")}}),j++}),this}}(jQuery);