/* * jQuery columnManager plugin * Version: 0.2.5 * * Copyright (c) 2007 Roman Weich * http://p.sohei.org * * Dual licensed under the MIT and GPL licenses * (This means that you can choose the license that best suits your project, and use it accordingly): * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * Changelog: * v 0.2.5 - 2008-01-17 * -change: added options "show" and "hide". with these functions the user can control the way to show or hide the cells * -change: added $.fn.showColumns() and $.fn.hideColumns which allows to explicitely show or hide any given number of columns * v 0.2.4 - 2007-12-02 * -fix: a problem with the on/off css classes when manually toggling columns which were not in the column header list * -fix: an error in the createColumnHeaderList function incorectly resetting the visibility state of the columns * -change: restructured some of the code * v 0.2.3 - 2007-12-02 * -change: when a column header has no text but some html markup as content, the markup is used in the column header list instead of "undefined" * v 0.2.2 - 2007-11-27 * -change: added the ablity to change the on and off CSS classes in the column header list through $().toggleColumns() * -change: to avoid conflicts with other plugins, the table-referencing data in the column header list is now stored as an expando and not in the class name as before * v 0.2.1 - 2007-08-14 * -fix: handling of colspans didn't work properly for the very first spanning column * -change: altered the cookie handling routines for easier management * v 0.2.0 - 2007-04-14 * -change: supports tables with colspanned and rowspanned cells now * v 0.1.4 - 2007-04-11 * -change: added onToggle option to specify a custom callback function for the toggling over the column header list * v 0.1.3 - 2007-04-05 * -fix: bug when saving the value in a cookie * -change: toggleColumns takes a number or an array of numbers as argument now * v 0.1.2 - 2007-04-02 * -change: added jsDoc style documentation and examples * -change: the column index passed to toggleColumns() starts at 1 now (conforming to the values passed in the hideInList and colsHidden options) * v 0.1.1 - 2007-03-30 * -change: changed hideInList and colsHidden options to hold integer values for the column indexes to be affected * -change: made the toggleColumns function accessible through the jquery object, to toggle the state without the need for the column header list * -fix: error when not finding the passed listTargetID in the dom * v 0.1.0 - 2007-03-27 */ (function($) { var defaults = { listTargetID : null, onClass : '', offClass : '', hideInList: [], colsHidden: [], saveState: false, onToggle: null, show: function(cell){ showCell(cell); }, hide: function(cell){ hideCell(cell); } }; var idCount = 0; var cookieName = 'columnManagerC'; /** * Saves the current state for the table in a cookie. * @param {element} table The table for which to save the current state. */ var saveCurrentValue = function(table) { var val = '', i = 0, colsVisible = table.cMColsVisible; if ( table.cMSaveState && table.id && colsVisible && $.cookie ) { for ( ; i < colsVisible.length; i++ ) { val += ( colsVisible[i] == false ) ? 0 : 1; } $.cookie(cookieName + table.id, val, {expires: 9999}); } }; /** * Hides a cell. * It rewrites itself after the browsercheck! * @param {element} cell The cell to hide. */ var hideCell = function(cell) { if ( jQuery.browser.msie ) { (hideCell = function(c) { c.style.setAttribute('display', 'none'); })(cell); } else { (hideCell = function(c) { c.style.display = 'none'; })(cell); } }; /** * Makes a cell visible again. * It rewrites itself after the browsercheck! * @param {element} cell The cell to show. */ var showCell = function(cell) { if ( jQuery.browser.msie ) { (showCell = function(c) { c.style.setAttribute('display', 'block'); })(cell); } else { (showCell = function(c) { c.style.display = 'table-cell'; })(cell); } }; /** * Returns the visible state of a cell. * It rewrites itself after the browsercheck! * @param {element} cell The cell to test. */ var cellVisible = function(cell) { if ( jQuery.browser.msie ) { return (cellVisible = function(c) { return c.style.getAttribute('display') != 'none'; })(cell); } else { return (cellVisible = function(c) { return c.style.display != 'none'; })(cell); } }; /** * Returns the cell element which has the passed column index value. * @param {element} table The table element. * @param {array} cells The cells to loop through. * @param {integer} col The column index to look for. */ var getCell = function(table, cells, col) { for ( var i = 0; i < cells.length; i++ ) { if ( cells[i].realIndex === undefined ) //the test is here, because rows/cells could get added after the first run { fixCellIndexes(table); } if ( cells[i].realIndex == col ) { return cells[i]; } } return null; }; /** * Calculates the actual cellIndex value of all cells in the table and stores it in the realCell property of each cell. * Thats done because the cellIndex value isn't correct when colspans or rowspans are used. * Originally created by Matt Kruse for his table library - Big Thanks! (see http://www.javascripttoolbox.com/) * @param {element} table The table element. */ var fixCellIndexes = function(table) { var rows = table.rows; var len = rows.length; var matrix = []; for ( var i = 0; i < len; i++ ) { var cells = rows[i].cells; var clen = cells.length; for ( var j = 0; j < clen; j++ ) { var c = cells[j]; var rowSpan = c.rowSpan || 1; var colSpan = c.colSpan || 1; var firstAvailCol = -1; if ( !matrix[i] ) { matrix[i] = []; } var m = matrix[i]; // Find first available column in the first row while ( m[++firstAvailCol] ) {} c.realIndex = firstAvailCol; for ( var k = i; k < i + rowSpan; k++ ) { if ( !matrix[k] ) { matrix[k] = []; } var matrixrow = matrix[k]; for ( var l = firstAvailCol; l < firstAvailCol + colSpan; l++ ) { matrixrow[l] = 1; } } } } }; /** * Manages the column display state for a table. * * Features: * Saves the state and recreates it on the next visit of the site (requires cookie-plugin). * Extracts all headers and builds an unordered(