LaravelTest
557 строк · 24.2 Кб
1/*! FixedColumns 4.0.0
2* 2019-2020 SpryMedia Ltd - datatables.net/license
3*/
4(function () {5'use strict';6
7var $;8var dataTable;9function setJQuery(jq) {10$ = jq;11dataTable = $.fn.dataTable;12}13var FixedColumns = /** @class */ (function () {14function FixedColumns(settings, opts) {15var _this = this;16// Check that the required version of DataTables is included17if (!dataTable || !dataTable.versionCheck || !dataTable.versionCheck('1.10.0')) {18throw new Error('StateRestore requires DataTables 1.10 or newer');19}20var table = new dataTable.Api(settings);21this.classes = $.extend(true, {}, FixedColumns.classes);22// Get options from user23this.c = $.extend(true, {}, FixedColumns.defaults, opts);24// Backwards compatibility for deprecated leftColumns25if (opts.left === undefined && this.c.leftColumns !== undefined) {26this.c.left = this.c.leftColumns;27}28// Backwards compatibility for deprecated rightColumns29if (opts.right === undefined && this.c.rightColumns !== undefined) {30this.c.right = this.c.rightColumns;31}32this.s = {33barWidth: 0,34dt: table,35rtl: $(table.table().node()).css('direction') === 'rtl'36};37// Set the bar width if vertical scrolling is enabled38if (this.s.dt.settings()[0].oInit.scrollY === true) {39this.s.barWidth = this.s.dt.settings()[0].oBrowser.barWidth;40}41// Common CSS for all blockers42var blockerCSS = {43'background-color': 'white',44'bottom': '0px',45'display': 'block',46'position': 'absolute',47'width': this.s.barWidth + 1 + 'px'48};49this.dom = {50leftBottomBlocker: $('<div>')51.css(blockerCSS)52.css('left', 0)53.addClass(this.classes.leftBottomBlocker),54leftTopBlocker: $('<div>')55.css(blockerCSS)56.css({57left: 0,58top: 059})60.addClass(this.classes.leftTopBlocker),61rightBottomBlocker: $('<div>')62.css(blockerCSS)63.css('right', 0)64.addClass(this.classes.rightBottomBlocker),65rightTopBlocker: $('<div>')66.css(blockerCSS)67.css({68right: 0,69top: 070})71.addClass(this.classes.rightTopBlocker)72};73if (this.s.dt.settings()[0]._bInitComplete) {74// Fixed Columns Initialisation75this._addStyles();76this._setKeyTableListener();77}78else {79table.one('preInit.dt', function () {80// Fixed Columns Initialisation81_this._addStyles();82_this._setKeyTableListener();83});84}85// Make class available through dt object86table.settings()[0]._fixedColumns = this;87return this;88}89/**90* Getter/Setter for the fixedColumns.left property
91*
92* @param newVal Optional. If present this will be the new value for the number of left fixed columns
93* @returns The number of left fixed columns
94*/
95FixedColumns.prototype.left = function (newVal) {96// If the value is to change97if (newVal !== undefined) {98// Set the new values and redraw the columns99this.c.left = newVal;100this._addStyles();101}102return this.c.left;103};104/**105* Getter/Setter for the fixedColumns.left property
106*
107* @param newVal Optional. If present this will be the new value for the number of right fixed columns
108* @returns The number of right fixed columns
109*/
110FixedColumns.prototype.right = function (newVal) {111// If the value is to change112if (newVal !== undefined) {113// Set the new values and redraw the columns114this.c.right = newVal;115this._addStyles();116}117return this.c.right;118};119/**120* Iterates over the columns, fixing the appropriate ones to the left and right
121*/
122FixedColumns.prototype._addStyles = function () {123var parentDiv = null;124// Get the header and it's height125var header = this.s.dt.column(0).header();126var headerHeight = null;127if (header !== null) {128header = $(header);129headerHeight = header.outerHeight() + 1;130parentDiv = $(header.closest('div.dataTables_scroll')).css('position', 'relative');131}132// Get the footer and it's height133var footer = this.s.dt.column(0).footer();134var footerHeight = null;135if (footer !== null) {136footer = $(footer);137footerHeight = footer.outerHeight();138// Only attempt to retrieve the parentDiv if it has not been retrieved already139if (parentDiv === null) {140parentDiv = $(footer.closest('div.dataTables_scroll')).css('position', 'relative');141}142}143// Get the number of columns in the table - this is used often so better to only make 1 api call144var numCols = this.s.dt.columns().data().toArray().length;145// Tracker for the number of pixels should be left to the left of the table146var distLeft = 0;147// Get all of the row elements in the table148var rows = $(this.s.dt.table().node()).children('tbody').children('tr');149var invisibles = 0;150// Iterate over all of the columns151for (var i = 0; i < numCols; i++) {152var column = this.s.dt.column(i);153if (!column.visible()) {154invisibles++;155continue;156}157// Get the columns header and footer element158var colHeader = $(column.header());159var colFooter = $(column.footer());160// If i is less than the value of left then this column should be fixed left161if (i < this.c.left) {162$(this.s.dt.table().node()).addClass(this.classes.tableFixedLeft);163parentDiv.addClass(this.classes.tableFixedLeft);164// Add the width of the previous node - only if we are on atleast the second column165if (i !== 0) {166var prevCol = this.s.dt.column(i - 1);167if (prevCol.visible()) {168distLeft += $(prevCol.nodes()[0]).outerWidth();169}170}171// Iterate over all of the rows, fixing the cell to the left172for (var _i = 0, rows_1 = rows; _i < rows_1.length; _i++) {173var row = rows_1[_i];174$($(row).children()[i - invisibles])175.css(this._getCellCSS(false, distLeft, 'left'))176.addClass(this.classes.fixedLeft);177}178// Add the css for the header and the footer179colHeader
180.css(this._getCellCSS(true, distLeft, 'left'))181.addClass(this.classes.fixedLeft);182colFooter
183.css(this._getCellCSS(true, distLeft, 'left'))184.addClass(this.classes.fixedLeft);185}186else {187// Iteriate through all of the rows, making sure they aren't currently trying to fix left188for (var _a = 0, rows_2 = rows; _a < rows_2.length; _a++) {189var row = rows_2[_a];190var cell = $($(row).children()[i - invisibles]);191// If the cell is trying to fix to the left, remove the class and the css192if (cell.hasClass(this.classes.fixedLeft)) {193cell
194.css(this._clearCellCSS('left'))195.removeClass(this.classes.fixedLeft);196}197}198// Make sure the header for this column isn't fixed left199if (colHeader.hasClass(this.classes.fixedLeft)) {200colHeader
201.css(this._clearCellCSS('left'))202.removeClass(this.classes.fixedLeft);203}204// Make sure the footer for this column isn't fixed left205if (colFooter.hasClass(this.classes.fixedLeft)) {206colFooter
207.css(this._clearCellCSS('left'))208.removeClass(this.classes.fixedLeft);209}210}211}212// If there is a header with the index class and reading rtl then add left top blocker213if (header !== null && !header.hasClass('index')) {214if (this.s.rtl) {215this.dom.leftTopBlocker.outerHeight(headerHeight);216parentDiv.append(this.dom.leftTopBlocker);217}218else {219this.dom.rightTopBlocker.outerHeight(headerHeight);220parentDiv.append(this.dom.rightTopBlocker);221}222}223// If there is a footer with the index class and reading rtl then add left bottom blocker224if (footer !== null && !footer.hasClass('index')) {225if (this.s.rtl) {226this.dom.leftBottomBlocker.outerHeight(footerHeight);227parentDiv.append(this.dom.leftBottomBlocker);228}229else {230this.dom.rightBottomBlocker.outerHeight(footerHeight);231parentDiv.append(this.dom.rightBottomBlocker);232}233}234var distRight = 0;235invisibles = 0;236for (var i = numCols - 1; i >= 0; i--) {237var column = this.s.dt.column(i);238// Get the columns header and footer element239var colHeader = $(column.header());240var colFooter = $(column.footer());241if (!column.visible()) {242invisibles++;243continue;244}245if (i >= numCols - this.c.right) {246$(this.s.dt.table().node()).addClass(this.classes.tableFixedRight);247parentDiv.addClass(this.classes.tableFixedLeft);248// Add the widht of the previous node, only if we are on atleast the second column249if (i !== numCols - 1) {250var prevCol = this.s.dt.column(i + 1);251if (prevCol.visible()) {252distRight += $(prevCol.nodes()[0]).outerWidth();253}254}255// Iterate over all of the rows, fixing the cell to the right256for (var _b = 0, rows_3 = rows; _b < rows_3.length; _b++) {257var row = rows_3[_b];258$($(row).children()[i + invisibles])259.css(this._getCellCSS(false, distRight, 'right'))260.addClass(this.classes.fixedRight);261}262// Add the css for the header and the footer263colHeader
264.css(this._getCellCSS(true, distRight, 'right'))265.addClass(this.classes.fixedRight);266colFooter
267.css(this._getCellCSS(true, distRight, 'right'))268.addClass(this.classes.fixedRight);269}270else {271// Iteriate through all of the rows, making sure they aren't currently trying to fix right272for (var _c = 0, rows_4 = rows; _c < rows_4.length; _c++) {273var row = rows_4[_c];274var cell = $($(row).children()[i + invisibles]);275// If the cell is trying to fix to the right, remove the class and the css276if (cell.hasClass(this.classes.fixedRight)) {277cell
278.css(this._clearCellCSS('right'))279.removeClass(this.classes.fixedRight);280}281}282// Make sure the header for this column isn't fixed right283if (colHeader.hasClass(this.classes.fixedRight)) {284colHeader
285.css(this._clearCellCSS('right'))286.removeClass(this.classes.fixedRight);287}288// Make sure the footer for this column isn't fixed right289if (colFooter.hasClass(this.classes.fixedRight)) {290colFooter
291.css(this._clearCellCSS('right'))292.removeClass(this.classes.fixedRight);293}294}295}296// If there is a header with the index class and reading rtl then add right top blocker297if (header) {298if (!this.s.rtl) {299this.dom.rightTopBlocker.outerHeight(headerHeight);300parentDiv.append(this.dom.rightTopBlocker);301}302else {303this.dom.leftTopBlocker.outerHeight(headerHeight);304parentDiv.append(this.dom.leftTopBlocker);305}306}307// If there is a footer with the index class and reading rtl then add right bottom blocker308if (footer) {309if (!this.s.rtl) {310this.dom.rightBottomBlocker.outerHeight(footerHeight);311parentDiv.append(this.dom.rightBottomBlocker);312}313else {314this.dom.leftBottomBlocker.outerHeight(footerHeight);315parentDiv.append(this.dom.leftBottomBlocker);316}317}318};319/**320* Gets the correct CSS for the cell, header or footer based on options provided
321*
322* @param header Whether this cell is a header or a footer
323* @param dist The distance that the cell should be moved away from the edge
324* @param lr Indicator of fixing to the left or the right
325* @returns An object containing the correct css
326*/
327FixedColumns.prototype._getCellCSS = function (header, dist, lr) {328if (lr === 'left') {329return !this.s.rtl ?330{331left: dist + 'px',332position: 'sticky'333} :334{335position: 'sticky',336right: dist + (header ? this.s.barWidth : 0) + 'px'337};338}339else {340return !this.s.rtl ?341{342position: 'sticky',343right: dist + (header ? this.s.barWidth : 0) + 'px'344} :345{346left: dist + 'px',347position: 'sticky'348};349}350};351/**352* Gets the css that is required to clear the fixing to a side
353*
354* @param lr Indicator of fixing to the left or the right
355* @returns An object containing the correct css
356*/
357FixedColumns.prototype._clearCellCSS = function (lr) {358if (lr === 'left') {359return !this.s.rtl ?360{361left: '',362position: ''363} :364{365position: '',366right: ''367};368}369else {370return !this.s.rtl ?371{372position: '',373right: ''374} :375{376left: '',377position: ''378};379}380};381FixedColumns.prototype._setKeyTableListener = function () {382var _this = this;383this.s.dt.on('key-focus', function (e, dt, cell) {384var cellPos = $(cell.node()).offset();385var scroll = $($(_this.s.dt.table().node()).closest('div.dataTables_scrollBody'));386// If there are fixed columns to the left387if (_this.c.left > 0) {388// Get the rightmost left fixed column header, it's position and it's width389var rightMost = $(_this.s.dt.column(_this.c.left - 1).header());390var rightMostPos = rightMost.offset();391var rightMostWidth = rightMost.outerWidth();392// If the current highlighted cell is left of the rightmost cell on the screen393if (cellPos.left < rightMostPos.left + rightMostWidth) {394// Scroll it into view395var currScroll = scroll.scrollLeft();396scroll.scrollLeft(currScroll - (rightMostPos.left + rightMostWidth - cellPos.left));397}398}399// If there are fixed columns to the right400if (_this.c.right > 0) {401// Get the number of columns and the width of the cell as doing right side calc402var numCols = _this.s.dt.columns().data().toArray().length;403var cellWidth = $(cell.node()).outerWidth();404// Get the leftmost right fixed column header and it's position405var leftMost = $(_this.s.dt.column(numCols - _this.c.right).header());406var leftMostPos = leftMost.offset();407// If the current highlighted cell is right of the leftmost cell on the screen408if (cellPos.left + cellWidth > leftMostPos.left) {409// Scroll it into view410var currScroll = scroll.scrollLeft();411scroll.scrollLeft(currScroll - (leftMostPos.left - (cellPos.left + cellWidth)));412}413}414});415// Whenever a draw occurs there is potential for the data to have changed and therefore also the column widths416// Therefore it is necessary to recalculate the values for the fixed columns417this.s.dt.on('draw', function () {418_this._addStyles();419});420this.s.dt.on('column-reorder', function () {421_this._addStyles();422});423this.s.dt.on('column-visibility', function () {424_this._addStyles();425});426};427FixedColumns.version = '4.0.0';428FixedColumns.classes = {429fixedLeft: 'dtfc-fixed-left',430fixedRight: 'dtfc-fixed-right',431leftBottomBlocker: 'dtfc-left-bottom-blocker',432leftTopBlocker: 'dtfc-left-top-blocker',433rightBottomBlocker: 'dtfc-right-bottom-blocker',434rightTopBlocker: 'dtfc-right-top-blocker',435tableFixedLeft: 'dtfc-has-left',436tableFixedRight: 'dtfc-has-right'437};438FixedColumns.defaults = {439i18n: {440button: 'FixedColumns'441},442left: 1,443right: 0444};445return FixedColumns;446}());447
448/*! FixedColumns 4.0.0449* 2019-2020 SpryMedia Ltd - datatables.net/license
450*/
451// DataTables extensions common UMD. Note that this allows for AMD, CommonJS452// (with window and jQuery being allowed as parameters to the returned453// function) or just default browser loading.454(function (factory) {455if (typeof define === 'function' && define.amd) {456// AMD457define(['jquery', 'datatables.net'], function ($) {458return factory($, window, document);459});460}461else if (typeof exports === 'object') {462// CommonJS463module.exports = function (root, $) {464if (!root) {465root = window;466}467if (!$ || !$.fn.dataTable) {468// eslint-disable-next-line @typescript-eslint/no-var-requires469$ = require('datatables.net')(root, $).$;470}471return factory($, root, root.document);472};473}474else {475// Browser - assume jQuery has already been loaded476factory(window.jQuery, window, document);477}478}(function ($, window, document) {479setJQuery($);480var dataTable = $.fn.dataTable;481$.fn.dataTable.FixedColumns = FixedColumns;482$.fn.DataTable.FixedColumns = FixedColumns;483var apiRegister = $.fn.dataTable.Api.register;484apiRegister('fixedColumns()', function () {485return this;486});487apiRegister('fixedColumns().left()', function (newVal) {488var ctx = this.context[0];489if (newVal !== undefined) {490ctx._fixedColumns.left(newVal);491return this;492}493else {494return ctx._fixedColumns.left();495}496});497apiRegister('fixedColumns().right()', function (newVal) {498var ctx = this.context[0];499if (newVal !== undefined) {500ctx._fixedColumns.right(newVal);501return this;502}503else {504return ctx._fixedColumns.right();505}506});507$.fn.dataTable.ext.buttons.fixedColumns = {508action: function (e, dt, node, config) {509if ($(node).attr('active')) {510$(node).removeAttr('active').removeClass('active');511dt.fixedColumns().left(0);512dt.fixedColumns().right(0);513}514else {515$(node).attr('active', true).addClass('active');516dt.fixedColumns().left(config.config.left);517dt.fixedColumns().right(config.config.right);518}519},520config: {521left: 1,522right: 0523},524init: function (dt, node, config) {525if (dt.settings()[0]._fixedColumns === undefined) {526_init(dt.settings(), config);527}528$(node).attr('active', true).addClass('active');529dt.button(node).text(config.text || dt.i18n('buttons.fixedColumns', dt.settings()[0]._fixedColumns.c.i18n.button));530},531text: null532};533function _init(settings, options) {534if (options === void 0) { options = null; }535var api = new dataTable.Api(settings);536var opts = options537? options538: api.init().fixedColumns || dataTable.defaults.fixedColumns;539var fixedColumns = new FixedColumns(api, opts);540return fixedColumns;541}542// Attach a listener to the document which listens for DataTables initialisation543// events so we can automatically initialise544$(document).on('init.dt.dtfc', function (e, settings) {545if (e.namespace !== 'dt') {546return;547}548if (settings.oInit.fixedColumns ||549dataTable.defaults.fixedColumns) {550if (!settings._fixedColumns) {551_init(settings, null);552}553}554});555}));556
557}());558