UNPKG

causeway-standard-theme

Version:

308 lines (255 loc) 7.56 kB
(function ($, window) { 'use strict'; $.fn.tableHeadFixer = function (param) { var defaults = { head: true, foot: false, left: 0, right: 0, 'z-index': 0 }, settings = $.extend({}, defaults, param); /* This function solver z-index problem in corner cell where fix row and column at the same time, set corner cells z-index 1 more then other fixed cells */ function solverLeftColspan(row, action) { var fixColumn = settings.left, inc = 1, i, nth, cell, colspan; for (i = 1; i <= fixColumn; i = i + inc) { nth = inc > 1 ? i - 1 : i; cell = $(row).find("> *:nth-child(" + nth + ")"); colspan = cell.prop("colspan"); if (cell.cellPos().left < fixColumn) { action(cell); } inc = colspan; } } function solveRightColspan(row, action) { var fixColumn = settings.right, inc = 1, i, nth, cell, colspan; for (i = 1; i <= fixColumn; i = i + inc) { nth = inc > 1 ? i - 1 : i; cell = $(row).find("> *:nth-last-child(" + nth + ")"); colspan = cell.prop("colspan"); action(cell); inc = colspan; } } function setCorner() { var table = $(settings.table), tr; if (settings.head) { if (settings.left > 0) { tr = table.find("thead tr"); tr.each(function (row) { solverLeftColspan(row, function (cell) { $(cell).css("z-index", settings['z-index'] + 1); }); }); } if (settings.right > 0) { tr = table.find("thead tr"); tr.each(function (row) { solveRightColspan(row, function (cell) { $(cell).css("z-index", settings['z-index'] + 1); }); }); } } if (settings.foot) { if (settings.left > 0) { tr = table.find("tfoot tr"); tr.each(function (row) { solverLeftColspan(row, function (cell) { $(cell).css("z-index", settings['z-index']); }); }); } if (settings.right > 0) { tr = table.find("tfoot tr"); tr.each(function (row) { solveRightColspan(row, function (cell) { $(cell).css("z-index", settings['z-index']); }); }); } } } // Set style of table parent function setParent() { var parent = $(settings.parent), table = $(settings.table); parent.append(table); parent .css({ 'overflow-x': 'auto', 'overflow-y': 'auto' }); parent.scroll(function () { var scrollWidth = parent[0].scrollWidth, clientWidth = parent[0].clientWidth, scrollHeight = parent[0].scrollHeight, clientHeight = parent[0].clientHeight, top = parent.scrollTop(), left = parent.scrollLeft(); if (settings.head) { this.find("thead tr > *").css("top", top); } if (settings.foot) { this.find("tfoot tr > *").css("bottom", scrollHeight - clientHeight - top); } if (settings.left > 0) { settings.leftColumns.css("left", left); } if (settings.right > 0) { settings.rightColumns.css("right", scrollWidth - clientWidth - left); } }.bind(table)); } // Set fixed cells backgrounds function setBackground(elements) { elements.each(function (element) { element = $(element); element.css("background-color", ''); }); } // Set table head fixed function fixHead() { var thead = $(settings.table).find("thead"), cells = thead.find("tr > *"); setBackground(cells); cells.css({ 'position': 'relative' }); } // Set table foot fixed function fixFoot() { var tfoot = $(settings.table).find("tfoot"), cells = tfoot.find("tr > *"); setBackground(cells); cells.css({ 'position': 'relative' }); } // Set table left column fixed function fixLeft() { var table = $(settings.table), tr = table.find("tr"), column = settings.leftColumns; // var fixColumn = settings.left; settings.leftColumns = $(); tr.each(function (row) { solverLeftColspan(row, function (cell) { settings.leftColumns = settings.leftColumns.add(cell); }); }); column.each(function (cell) { cell = $(cell); setBackground(cell); cell.css({ 'position': 'relative' }); }); } // Set table right column fixed function fixRight() { var table = $(settings.table), tr = table.find("tr"), column = settings.rightColumns; settings.rightColumns = $(); tr.each(function (row) { solveRightColspan(row, function (cell) { settings.rightColumns = settings.rightColumns.add(cell); }); }); column.each(function (cell) { cell = $(cell); setBackground(cell); cell.css({ 'position': 'relative' }); }); } return this.each(function () { settings.table = this; settings.parent = $(settings.table).parent(); setParent(); if (settings.head === true) { fixHead(); } if (settings.foot === true) { fixFoot(); } if (settings.left > 0) { fixLeft(); } if (settings.right > 0) { fixRight(); } setCorner(); $(settings.parent).trigger("scroll"); $(window).resize(function () { $(settings.parent).trigger("scroll"); }); }); }; }(jQuery, window)); /* cellPos jQuery plugin --------------------- Get visual position of cell in HTML table (or its block like thead). Return value is object with "top" and "left" properties set to row and column index of top-left cell corner. Example of use: $("#myTable tbody td").each(function(){ $(this).text( $(this).cellPos().top +", "+ $(this).cellPos().left ); }); */ (function ($) { 'use strict'; /* scan individual table and set "cellPos" data in the form { left: x-coord, top: y-coord } */ function scanTable($table) { var m = []; $table.children("tr").each(function (y, row) { $(row).children("td, th").each(function (x, cell) { var $cell = $(cell), cspan = $cell.attr("colspan") || 0, rspan = $cell.attr("rowspan") || 0, tx, ty; cspan = cspan || 1; rspan = rspan || 1; for (;m[y] && m[y][x]; ++x); //skip already occupied cells in current row for (tx = x; tx < x + cspan; ++tx) { //mark matrix elements occupied by current cell with true for (ty = y; ty < y + rspan; ++ty) { if (!m[ty]) { //fill missing rows m[ty] = []; } m[ty][tx] = true; } } var pos = {top: y, left: x}; $cell.data("cellPos", pos); }); }); } /* plugin */ $.fn.cellPos = function (rescan) { var $cell = this.first(), pos = $cell.data("cellPos"); if (!pos || rescan) { var $table = $cell.closest("table, thead, tbody, tfoot"); scanTable($table); } pos = $cell.data("cellPos"); return pos; }; })(jQuery);