Skip to content

Instantly share code, notes, and snippets.

@amree
Last active December 14, 2020 08:03
Show Gist options
  • Save amree/183f179164704d9120895d10529e818c to your computer and use it in GitHub Desktop.
Save amree/183f179164704d9120895d10529e818c to your computer and use it in GitHub Desktop.
/** @preserve jQuery.floatThead 2.2.1 - https://mkoryak.github.io/floatThead/ - Copyright (c) 2012 - 2020 Misha Koryak **/ ! function(bt) {
bt.floatThead = bt.floatThead || {}, bt.floatThead.defaults = {
headerCellSelector: "tr:visible:first>*:visible",
zIndex: 1001,
position: "auto",
top: 0,
bottom: 0,
scrollContainer: function(t) {
return bt([])
},
responsiveContainer: function(t) {
return bt([])
},
getSizingRow: function(t, e, o) {
return t.find("tbody tr:visible:first>*:visible")
},
ariaLabel: function(t, e, o) {
return e.text()
},
floatTableClass: "floatThead-table",
floatWrapperClass: "floatThead-wrapper",
floatContainerClass: "floatThead-container",
copyTableClass: !0,
autoReflow: !1,
debug: !1,
support: {
bootstrap: !0,
datatables: !0,
jqueryUI: !0,
perfectScrollbar: !0
},
floatContainerCss: {
"overflow-x": "hidden"
}
};
var wt = function() {
var n = {},
o = Object.prototype.hasOwnProperty;
n.has = function(t, e) {
return o.call(t, e)
}, n.keys = Object.keys || function(t) {
if (t !== Object(t)) throw new TypeError("Invalid object");
var e = [];
for (var o in t) n.has(t, o) && e.push(o);
return e
};
var r = 0;
return n.uniqueId = function(t) {
var e = ++r + "";
return t ? t + e : e
}, bt.each(["Arguments", "Function", "String", "Number", "Date", "RegExp"], function() {
var e = this;
n["is" + e] = function(t) {
return Object.prototype.toString.call(t) === "[object " + e + "]"
}
}), n.debounce = function(o, n, r) {
var a, i, l, s, d;
return function() {
l = this, i = arguments, s = new Date;
var e = function() {
var t = new Date - s;
t < n ? a = setTimeout(e, n - t) : (a = null, r || (d = o.apply(l, i)))
},
t = r && !a;
return a || (a = setTimeout(e, n)), t && (d = o.apply(l, i)), d
}
}, n
}(),
gt = "undefined" != typeof MutationObserver,
mt = function() {
for (var t = 3, e = document.createElement("b"), o = e.all || []; t = 1 + t, e.innerHTML = "\x3c!--[if gt IE " + t + "]><i><![endif]--\x3e", o[0];);
return 4 < t ? t : document.documentMode
}(),
t = /Gecko\//.test(navigator.userAgent),
yt = /WebKit\//.test(navigator.userAgent),
Tt = /rtl/i.test(document.documentElement.dir || "");
mt || t || yt || (mt = 11);
var l = function() {
if (yt) {
var t = bt("<div>").css("width", 0).append(bt("<table>").css("max-width", "100%").append(bt("<tr>").append(bt("<th>").append(bt("<div>").css("min-width", 100).text("X")))));
bt("body").append(t);
var e = 0 === t.find("table").width();
return t.remove(), e
}
return !1
},
Ct = !t && !mt,
xt = bt(window),
Lt = t && window.matchMedia;
if (!window.matchMedia || Lt) {
var e = window.onbeforeprint,
o = window.onafterprint;
window.onbeforeprint = function() {
e && e(), xt.triggerHandler("fth-beforeprint")
}, window.onafterprint = function() {
o && o(), xt.triggerHandler("fth-afterprint")
}
}
function St(t) {
var e = t[0].parentElement;
do {
if ("visible" !== window.getComputedStyle(e).getPropertyValue("overflow")) break
} while (e = e.parentElement);
return e === document.body ? bt([]) : bt(e)
}
function jt(t) {
window && window.console && window.console.error && window.console.error("jQuery.floatThead: " + t)
}
function zt(t) {
var e = t.getBoundingClientRect();
return e.width || e.right - e.left
}
function It() {
var t = document.createElement("scrolltester");
t.style.cssText = "width:100px;height:100px;overflow:scroll!important;position:absolute;top:-9999px;display:block", document.body.appendChild(t);
var e = t.offsetWidth - t.clientWidth;
return document.body.removeChild(t), e
}
function Ht(t, e, o) {
var n = o ? "outerWidth" : "width";
if (l && t.css("max-width")) {
var r = 0;
o && (r += parseInt(t.css("borderLeft"), 10), r += parseInt(t.css("borderRight"), 10));
for (var a = 0; a < e.length; a++) r += zt(e.get(a));
return r
}
return t[n]()
}
bt.fn.floatThead = function(t) {
if (t = t || {}, mt < 8) return this;
if (wt.isFunction(l) && (l = l()), wt.isString(t)) {
var r = t,
a = Array.prototype.slice.call(arguments, 1),
i = this;
return this.filter("table").each(function() {
var t = bt(this),
e = t.data("floatThead-lazy");
e && t.floatThead(e);
var o = t.data("floatThead-attached");
if (o && wt.isFunction(o[r])) {
var n = o[r].apply(this, a);
void 0 !== n && (i = n)
}
}), i
}
var vt = bt.extend({}, bt.floatThead.defaults || {}, t);
if (bt.each(t, function(t, e) {
t in bt.floatThead.defaults || !vt.debug || jt("Used [" + t + "] key to init plugin, but that param is not an option for the plugin. Valid options are: " + wt.keys(bt.floatThead.defaults).join(", "))
}), vt.debug) {
var e = bt.fn.jquery.split(".");
1 === parseInt(e[0], 10) && parseInt(e[1], 10) <= 7 && jt("jQuery version " + bt.fn.jquery + " detected! This plugin supports 1.8 or better, or 1.7.x with jQuery UI 1.8.24 -> http://jqueryui.com/resources/download/jquery-ui-1.8.24.zip")
}
return this.filter(":not(." + vt.floatTableClass + ")").each(function() {
var e = wt.uniqueId(),
m = bt(this);
if (m.data("floatThead-attached")) return !0;
if (!m.is("table")) throw new Error('jQuery.floatThead must be run on a table element. ex: $("table").floatThead();');
var o = vt.autoReflow && gt,
n = null,
d = m.children("thead:first"),
r = m.children("tbody:first");
if (0 === d.length || 0 === r.length) return vt.debug && (0 === d.length ? jt("The thead element is missing.") : jt("The tbody element is missing.")), m.data("floatThead-lazy", vt), void m.unbind("reflow").one("reflow", function() {
m.floatThead(vt)
});
m.data("floatThead-lazy") && m.unbind("reflow"), m.data("floatThead-lazy", !1);
var y, T, a = !0,
C = {
vertical: 0,
horizontal: 0
};
wt.isFunction(It) && (It = It());
var i = 0;
!0 === vt.scrollContainer && (vt.scrollContainer = St);
var x = vt.scrollContainer(m) || bt([]),
L = 0 < x.length,
S = L ? bt([]) : vt.responsiveContainer(m) || bt([]),
j = _(),
z = null;
"auto" === vt.position ? z = null : "fixed" === vt.position ? z = !1 : "absolute" === vt.position ? z = !0 : vt.debug && jt('Invalid value given to "position" option, valid is "fixed", "absolute" and "auto". You passed: ', vt.position), null == z && (z = L);
var I = m.find("caption"),
H = 1 === I.length;
if (H) var W = "top" === (I.css("caption-side") || I.attr("align") || "top");
var l = bt("<fthfoot>").css({
display: "table-footer-group",
"border-spacing": 0,
height: 0,
"border-collapse": "collapse",
visibility: "hidden"
}),
q = !1,
s = bt([]),
R = mt <= 9 && !L && z,
f = bt("<table/>"),
c = bt("<colgroup/>"),
u = m.children("colgroup:first"),
p = !0;
0 === u.length && (u = bt("<colgroup/>"), p = !1);
var h = p ? "col:visible" : "col",
v = bt("<fthtr>").css({
display: "table-row",
"border-spacing": 0,
height: 0,
"border-collapse": "collapse"
}),
E = bt("<div>").css(vt.floatContainerCss).attr("aria-hidden", "true"),
M = !1,
b = bt("<thead/>"),
w = bt('<tr class="size-row"/>'),
g = bt([]),
k = bt([]),
D = bt([]),
F = bt([]);
b.append(w), m.prepend(u), Ct && (l.append(v), m.append(l)), f.append(c), E.append(f), vt.copyTableClass && f.attr("class", m.attr("class")), f.attr({
cellpadding: m.attr("cellpadding"),
cellspacing: m.attr("cellspacing"),
border: m.attr("border")
});
var t = m.css("display");
if (f.css({
borderCollapse: m.css("borderCollapse"),
border: m.css("border"),
display: t
}), L || f.css("width", "auto"), "none" === t && (M = !0), f.addClass(vt.floatTableClass).css({
margin: 0,
"border-bottom-width": 0
}), z) {
var O = function(t, e) {
var o = t.css("position"),
n = t;
if (!("relative" === o || "absolute" === o) || e) {
var r = {
paddingLeft: t.css("paddingLeft"),
paddingRight: t.css("paddingRight")
};
E.css(r), n = t.data("floatThead-containerWrap") || t.wrap(bt("<div>").addClass(vt.floatWrapperClass).css({
position: "relative",
clear: "both"
})).parent(), t.data("floatThead-containerWrap", n), q = !0
}
return n
};
L ? (s = O(x, !0)).prepend(E) : (s = O(m), m.before(E))
} else m.before(E);
E.css({
position: z ? "absolute" : "fixed",
marginTop: 0,
top: z ? 0 : "auto",
zIndex: vt.zIndex,
willChange: "transform"
}), E.addClass(vt.floatContainerClass), V();
var N = {
"table-layout": "fixed"
},
A = {
"table-layout": m.css("tableLayout") || "auto"
},
Q = m[0].style.width || "",
U = m.css("minWidth") || "";
function G(t) {
return t + ".fth-" + e + ".floatTHead"
}
function P() {
var t = 0;
if (d.children("tr:visible").each(function() {
t += bt(this).outerHeight(!0)
}), "collapse" === m.css("border-collapse")) {
var e = parseInt(m.css("border-top-width"), 10);
parseInt(m.find("thead tr:first").find(">*:first").css("border-top-width"), 10) < e && (t -= e / 2)
}
w.outerHeight(t), g.outerHeight(t)
}
function V() {
y = (wt.isFunction(vt.top) ? vt.top(m) : vt.top) || 0, T = (wt.isFunction(vt.bottom) ? vt.bottom(m) : vt.bottom) || 0
}
function X() {
if (!a) {
if (a = !0, z) {
var t = Ht(m, F, !0);
s.width() < t && m.css("minWidth", t)
}
m.css(N), f.css(N), f.append(d), r.before(b), P()
}
}
function Y() {
a && (a = !1, z && m.width(Q), b.detach(), m.prepend(d), m.css(A), f.css(A), m.css("minWidth", U), m.css("minWidth", Ht(m, F)))
}
var B = !1;
function K(t) {
B !== t && (B = t, m.triggerHandler("floatThead", [t, E]))
}
function $(t) {
z !== t && (z = t, E.css({
position: z ? "absolute" : "fixed"
}))
}
function J() {
var l, s = function() {
var t, e = d.find(vt.headerCellSelector);
if (p ? t = u.find(h).length : (t = 0, e.each(function() {
t += parseInt(bt(this).attr("colspan") || 1, 10)
})), t !== i) {
i = t;
var o = [],
n = [];
w.empty();
for (var r = 0; r < t; r++) {
var a = document.createElement("th");
a.setAttribute("aria-label", vt.ariaLabel(m, e.eq(r), r)), a.className = "floatThead-col", w[0].appendChild(a), o.push("<col/>"), n.push(bt("<fthtd>").css({
display: "table-cell",
height: 0,
width: "auto"
}))
}
o = p ? u.html() : o.join(""), Ct && (v.empty(), v.append(n), F = v.find("fthtd")), g = w.find("th"), p || u.html(o), k = u.find(h), c.html(o), D = c.find(h)
}
return t
}();
return function() {
var t = E.scrollLeft();
k = u.find(h);
var e, o, n, r, a = (e = m, o = k, n = F, r = mt, Ct ? n : r ? vt.getSizingRow(e, o, n) : o);
if (a.length === s && 0 < s) {
if (!p)
for (l = 0; l < s; l++) k.eq(l).css("width", "");
Y();
var i = [];
for (l = 0; l < s; l++) i[l] = zt(a.get(l));
for (l = 0; l < s; l++) D.eq(l).width(i[l]), k.eq(l).width(i[l]);
X()
} else f.append(d), m.css(A), f.css(A), P();
E.scrollLeft(t), m.triggerHandler("reflowed", [E])
}
}
function Z(t) {
var e = x.css("border-" + t + "-width"),
o = 0;
return e && ~e.indexOf("px") && (o = parseInt(e, 10)), o
}
function _() {
return "auto" === S.css("overflow-x")
}
function tt() {
var i, l = x.scrollTop(),
s = 0,
d = H ? I.outerHeight(!0) : 0,
f = W ? d : -d,
c = E.height(),
u = m.offset(),
p = 0,
h = 0;
if (L) {
var t = x.offset();
s = u.top - t.top + l, H && W && (s += d), p = Z("left"), h = Z("top"), s -= h
} else i = u.top - y - c + T + C.horizontal;
var v = xt.scrollTop(),
b = xt.scrollLeft(),
w = function() {
return (_() ? S : x).scrollLeft() || 0
},
g = w();
return function(t) {
j = _();
var e = m[0].offsetWidth <= 0 && m[0].offsetHeight <= 0;
if (!e && M) return M = !1, setTimeout(function() {
m.triggerHandler("reflow")
}, 1), null;
if (e && (M = !0, !z)) return null;
if ("windowScroll" === t) v = xt.scrollTop(), b = xt.scrollLeft();
else if ("containerScroll" === t)
if (S.length) {
if (!j) return;
g = S.scrollLeft()
} else l = x.scrollTop(), g = x.scrollLeft();
else "init" !== t && (v = xt.scrollTop(), b = xt.scrollLeft(), l = x.scrollTop(), g = w());
if (!yt || !(v < 0 || Tt && 0 < b || !Tt && b < 0)) {
if (R) $("windowScrollDone" === t);
else if ("windowScrollDone" === t) return null;
var o, n;
u = m.offset(), H && W && (u.top += d);
var r = m.outerHeight();
if (L && z) {
if (l <= s) {
var a = s - l + h;
o = 0 < a ? a : 0, K(!1)
} else r - c < l - s ? o = r - c - l - s : (o = q ? h : l, K(!0));
n = p
} else !L && z ? (i + r + f < v ? o = r - c + f + T : u.top >= v + y ? (o = 0, Y(), K(!1)) : (o = y + v - u.top + s + (W ? d : 0), X(), K(!0)), n = g) : L && !z ? (l < s || r < l - s ? (o = u.top - v, Y(), K(!1)) : (o = u.top + l - v - s, X(), K(!0)), n = u.left + g - b) : L || z || (i + r + f < v ? o = r + y - v + i + f : u.top > v + y ? (o = u.top - v, X(), K(!1)) : (o = y, K(!0)), n = u.left + g - b);
return {
top: Math.round(o),
left: Math.round(n)
}
}
}
}
function et() {
var i = null,
l = null,
s = null;
return function(t, e, o) {
if (null != t && (i !== t.top || l !== t.left)) {
if (8 === mt) E.css({
top: t.top,
left: t.left
});
else {
var n = "translateX(" + t.left + "px) translateY(" + t.top + "px)",
r = {
"-webkit-transform": n,
"-moz-transform": n,
"-ms-transform": n,
"-o-transform": n,
transform: n,
top: 0,
left: 0
};
E.css(r)
}
i = t.top, l = t.left
}
e && function() {
var t = Ht(m, F, !0),
e = j ? S : x,
o = e.length ? zt(e[0]) : t,
n = "hidden" !== e.css("overflow-y") ? o - C.vertical : o;
if (E.width(n), L) {
var r = 100 * t / n;
f.css("width", r + "%")
} else f.css("width", t + "px")
}(), o && P();
var a = (j ? S : x).scrollLeft();
z && s === a || (E.scrollLeft(a), s = a)
}
}
function ot() {
if (x.length)
if (vt.support && vt.support.perfectScrollbar && x.data().perfectScrollbar) C = {
horizontal: 0,
vertical: 0
};
else {
if ("scroll" === x.css("overflow-x")) C.horizontal = It;
else {
var t = x.width(),
e = Ht(m, F),
o = n < r ? It : 0;
C.horizontal = t - o < e ? It : 0
}
if ("scroll" === x.css("overflow-y")) C.vertical = It;
else {
var n = x.height(),
r = m.height(),
a = t < e ? It : 0;
C.vertical = n - a < r ? It : 0
}
}
}
ot();
var nt = function() {
J()()
};
nt();
var rt = tt(),
at = et();
at(rt("init"), !0);
var it = wt.debounce(function() {
at(rt("windowScrollDone"), !1)
}, 1),
lt = function() {
at(rt("windowScroll"), !1), R && it()
},
st = function() {
at(rt("containerScroll"), !1)
},
dt = wt.debounce(function() {
m.is(":hidden") || (ot(), V(), nt(), rt = tt(), at(rt("reflow"), !0, !0))
}, 1),
ft = function() {
Y()
},
ct = function() {
X()
},
ut = function(t) {
t.matches ? ft() : ct()
},
pt = null;
if (window.matchMedia && window.matchMedia("print").addListener && !Lt ? (pt = window.matchMedia("print")).addListener(ut) : (xt.on("fth-beforeprint", ft), xt.on("fth-afterprint", ct)), L ? z ? x.on(G("scroll"), st) : (x.on(G("scroll"), st), xt.on(G("scroll"), lt)) : (S.on(G("scroll"), st), xt.on(G("scroll"), lt)), xt.on(G("load"), dt), function(t, e) {
if (8 === mt) {
var o = xt.width(),
n = wt.debounce(function() {
var t = xt.width();
o !== t && (o = t, e())
}, 1);
xt.on(t, n)
} else xt.on(t, wt.debounce(e, 1))
}(G("resize"), function() {
m.is(":hidden") || (V(), ot(), nt(), rt = tt(), (at = et())(rt("resize"), !0, !0))
}), m.on("reflow", dt), vt.support && vt.support.datatables && function(t) {
if (t.dataTableSettings)
for (var e = 0; e < t.dataTableSettings.length; e++) {
var o = t.dataTableSettings[e].nTable;
if (t[0] === o) return !0
}
return !1
}(m) && m.on("filter", dt).on("sort", dt).on("page", dt), vt.support && vt.support.bootstrap && xt.on(G("shown.bs.tab"), dt), vt.support && vt.support.jqueryUI && xt.on(G("tabsactivate"), dt), o) {
var ht = null;
wt.isFunction(vt.autoReflow) && (ht = vt.autoReflow(m, x)), ht || (ht = x.length ? x[0] : m[0]), (n = new MutationObserver(function(t) {
for (var e = function(t) {
return t && t[0] && ("THEAD" === t[0].nodeName || "TD" === t[0].nodeName || "TH" === t[0].nodeName)
}, o = 0; o < t.length; o++)
if (!e(t[o].addedNodes) && !e(t[o].removedNodes)) {
dt();
break
}
})).observe(ht, {
childList: !0,
subtree: !0
})
}
m.data("floatThead-attached", {
destroy: function() {
var t = ".fth-" + e;
return Y(), m.css(A), u.remove(), Ct && l.remove(), b.parent().length && b.replaceWith(d), K(!1), o && (n.disconnect(), n = null), m.off("reflow reflowed"), x.off(t), S.off(t), q && (x.length ? x.unwrap() : m.unwrap()), L ? x.data("floatThead-containerWrap", !1) : m.data("floatThead-containerWrap", !1), m.css("minWidth", U), E.remove(), m.data("floatThead-attached", !1), xt.off(t), xt.off("fth-beforeprint fth-afterprint"), pt && pt.removeListener(ut), ft = ct = function() {},
function() {
return m.floatThead(vt)
}
},
reflow: function() {
dt()
},
setHeaderHeight: function() {
P()
},
getFloatContainer: function() {
return E
},
getRowGroups: function() {
return a ? E.find(">table>thead").add(m.children("tbody,tfoot")) : m.children("thead,tbody,tfoot")
}
})
}), this
}
}(function() {
var t = window.jQuery;
console.log('t from plugin', t);
return "undefined" != typeof module && module.exports && !t && (t = require("jquery")), t
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment