mambax7/alumni-26x

View on GitHub
media/jquery/tablesorter-master/js/widgets/widget-columns.js

Summary

Maintainability
A
2 hrs
Test Coverage
/*! Widget: columns - updated 5/24/2017 (v2.28.11) */
;(function ($) {
    'use strict';
    var ts = $.tablesorter || {};

    ts.addWidget({
        id: 'columns',
        priority: 65,
        options : {
            columns : [ 'primary', 'secondary', 'tertiary' ]
        },
        format: function(table, c, wo) {
            var $tbody, tbodyIndex, $rows, rows, $row, $cells, remove, indx,
            $table = c.$table,
            $tbodies = c.$tbodies,
            sortList = c.sortList,
            len = sortList.length,
            // removed c.widgetColumns support
            css = wo && wo.columns || [ 'primary', 'secondary', 'tertiary' ],
            last = css.length - 1;
            remove = css.join(' ');
            // check if there is a sort (on initialization there may not be one)
            for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) {
                $tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // detach tbody
                $rows = $tbody.children('tr');
                // loop through the visible rows
                $rows.each(function() {
                    $row = $(this);
                    if (this.style.display !== 'none') {
                        // remove all columns class names
                        $cells = $row.children().removeClass(remove);
                        // add appropriate column class names
                        if (sortList && sortList[0]) {
                            // primary sort column class
                            $cells.eq(sortList[0][0]).addClass(css[0]);
                            if (len > 1) {
                                for (indx = 1; indx < len; indx++) {
                                    // secondary, tertiary, etc sort column classes
                                    $cells.eq(sortList[indx][0]).addClass( css[indx] || css[last] );
                                }
                            }
                        }
                    }
                });
                ts.processTbody(table, $tbody, false);
            }
            // add classes to thead and tfoot
            rows = wo.columns_thead !== false ? [ 'thead tr' ] : [];
            if (wo.columns_tfoot !== false) {
                rows.push('tfoot tr');
            }
            if (rows.length) {
                $rows = $table.find( rows.join(',') ).children().removeClass(remove);
                if (len) {
                    for (indx = 0; indx < len; indx++) {
                        // add primary. secondary, tertiary, etc sort column classes
                        $rows.filter('[data-column="' + sortList[indx][0] + '"]').addClass(css[indx] || css[last]);
                    }
                }
            }
        },
        remove: function(table, c, wo) {
            var tbodyIndex, $tbody,
                $tbodies = c.$tbodies,
                remove = (wo.columns || [ 'primary', 'secondary', 'tertiary' ]).join(' ');
            c.$headers.removeClass(remove);
            c.$table.children('tfoot').children('tr').children('th, td').removeClass(remove);
            for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) {
                $tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // remove tbody
                $tbody.children('tr').each(function() {
                    $(this).children().removeClass(remove);
                });
                ts.processTbody(table, $tbody, false); // restore tbody
            }
        }
    });

})(jQuery);