/* ' ########################################################### ' JS for Highlight in the table ' Define "HiliteCross" for "table" ' If you need, define "NoMouseOver" for "tr" ' If you need, define "NoMouseOver" or "NoHilite" for "td" ' Create 2013.02.22 J.Ito ' Modify ' ########################################################### */ // iColCnt --> column count of table // iRowCnt --> row count of table var HiliteCross = function( iColCnt, iRowCnt) { // Array for saving cell state this.arrCell = new Array( iColCnt); for( var i=0; i < this.arrCell.length; i++) { this.arrCell[i] = new Array( iRowCnt); for( var k=0; k < this.arrCell[i].length; k++) this.arrCell[i][k] = 0; } this.InitCell = function() { for( var i=0; i < this.arrCell.length; i++) { for( var k=0; k < this.arrCell[i].length; k++) this.arrCell[i][k] = 0; } } this.MAX_CLICK_CNT = 5; // count of clicking cells this.iSelCnt = 0; this.AddCnt = function(){ this.iSelCnt++;} this.SetCnt = function( iCnt){ this.iSelCnt = iCnt;} this.GetCnt = function(){ return this.iSelCnt;} this.InitCellState = function( iX, iY){ this.arrCell[ iX][ iY] = 0;} this.SaveCellState = function( iX, iY){ this.arrCell[ iX][ iY] = this.iSelCnt;} this.GetCellState = function( iX, iY){ return this.arrCell[ iX][ iY];} }; // Highlight row // iRowCnt --> count of rows var HiliteRow = function( iRowCnt) { // Array that stores cell's state this.arrRow = new Array( iRowCnt); for( var i=0; i < this.arrRow.length; i++) this.arrRow[i] = 0; this.MAX_CLICK_CNT = 1; // the count of clicked cells this.iSelCnt = 0; this.AddCnt = function(){ this.iSelCnt++;} this.SetCnt = function( iCnt){ this.iSelCnt = iCnt;} this.GetCnt = function(){ return this.iSelCnt;} this.InitRowState = function( iRow){ this.arrRow[ iRow] = 0;} this.SaveRowState = function( iRow){ this.arrRow[ iRow] = this.iSelCnt;} this.GetRowState = function( iRow){ return this.arrRow[ iRow];} }; $( document).ready( function(){ // Create object the count of table with "HiliteCross" class var oTbl = $( "table.HiliteCross"); var oHiCrs = null; if( typeof( oTbl) != "undefined"){ oHiCrs = new Array( oTbl.length); for( i=0; i < oTbl.length; i++) { var iRowCnt = oTbl.eq(i).find( "tr:not( .NoMouseOver)").length; var iColCnt = oTbl.eq(i).find( "tr td").length; iColCnt = Math.ceil( iColCnt / iRowCnt); oHiCrs[i] = new HiliteCross( iColCnt, iRowCnt); } } var oRow; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ++++++++++++++++++++++ Hightlight Row ++++++++++++++++++++++++ // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $( "table.HiliteRow tr:not( .NoMouseOver) td:not( .NoMouseOver)").hover( function(){ // mouseover var oParentTr = $( this).closest( "tr"); oRow = oParentTr.children( "td:not( .NoHilite)"); oRow.addClass( "HiliteOver"); }, function(){ // mouseout oRow.removeClass( "HiliteOver"); } ); // click $( "table.HiliteRow tr:not( .NoMouseOver) td:not( .NoMouseOver)").click( function(){ ChangeRowColor( $( this)); } ); // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ++++++++++++++++ Highlight column & row ++++++++++++++++++++++ // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ var $this; var oCol; var oCellSpd; // Cell of rolling speed $( "table.HiliteCross tr:not( .NoMouseOver) td:not( .NoMouseOver)").hover( function(){ // mouseover $this = $( this); var oParentTr = $this.closest( "tr:not( .NoHilite)"); // row oRow = oParentTr.children( "td:not( .NoHilite)"); oRow.addClass( "HiliteOver"); // column var iColIdx = oRow.index( $this); // Sprocket teeth row var oSlideTd; oSlideTd = $this.parents( "table.HiliteCross").find( "tr.Left1Slide td"); if(oSlideTd.length > 0){ oCellSpd = $this.parents( "table.HiliteCross").find( "tr.Left1Slide td").filter( ":nth-child("+ ( iColIdx) + ")"); } oSlideTd = $this.parents( "table.HiliteCross").find( "tr.Left2Slide td"); if(oSlideTd.length > 0){ oCellSpd = $this.parents( "table.HiliteCross").find( "tr.Left2Slide td").filter( ":nth-child("+ ( iColIdx - 1) + ")"); } oCellSpd.not( ".NoHilite").addClass( "HiliteOver"); // other rows oCol = $this.parents( "table.HiliteCross").find( "tr:not( .Left1Slide, .Left2Slide) td").filter( ":nth-child("+ ( iColIdx + 1) + ")"); oCol.not( ".NoHilite").addClass( "HiliteOver"); } , function(){ // mouseout oRow.removeClass( "HiliteOver"); oCol.removeClass( "HiliteOver"); oCellSpd.removeClass( "HiliteOver"); } ); // click $( "table.HiliteCross tr:not( .NoMouseOver) td:not( .NoHilite)").click( function(){ var idx = $( this).closest( "table").index( "table.HiliteCross"); ChangeBgColor( $( this), oHiCrs[ idx]); } ); // double click $( "table.HiliteCross tr td").dblclick( function(){ // turn background color back to normal for( var i=1; i<=5; i++) $( this).parents( "table.HiliteCross").find( "tr td").removeClass( "HiliteClick" + i); } ); } ); // change background color of cells function ChangeBgColor( $this, oHi) { var iRow = $this.closest( "tr").index(); var iCol = $this.closest( "tr").children( "td:not( .NoMouseOver)").index( $this); //$( "#debug").html( "(" + iCol + "," + iRow + ")"); // sprocket teeth (row header, = left end) var oTeeth = $this.closest( "tr").children( "td:first").eq(0); // rolling speed of sprocket (col header, = top) var oRoSpeed = $this.parents( "table.HiliteCross").find( "tr.Left1Slide td, tr.Left2Slide td").eq( iCol); if( typeof( oRoSpeed) == "undefined"){ // no rowspan oRoSpeed = $this.parents( "table.HiliteCross").find( "tr.eq(1) td").eq( iCol); } var oClickColored = $this.closest( "tr").children( "td.clickcolored"); if(iCol >= 0){ if( oHi.GetCellState( iCol, iRow) >= 1){ // click selected cells var sCls = "HiliteClick" + oHi.GetCellState( iCol, iRow); oHi.InitCellState( iCol, iRow); // take cells back $this.removeClass( sCls); oTeeth.removeClass( sCls); oRoSpeed.removeClass( sCls); oClickColored.removeClass( sCls); }else{ // click unselected cells if( oHi.GetCnt() >= oHi.MAX_CLICK_CNT){ oHi.SetCnt( 0);} oHi.AddCnt(); oHi.SaveCellState( iCol, iRow); var sCls = "HiliteClick" + oHi.GetCellState( iCol, iRow); // change background color $this.addClass( sCls); oTeeth.addClass( sCls); oRoSpeed.addClass( sCls); oClickColored.addClass( sCls); } } } // change row color // $this --> selected cell function ChangeRowColor( $this) { var oRow = $this.closest( "tr"); // take background color back $this.parents( "table.HiliteRow").find( "td.HiliteRowClick1").removeClass( "HiliteRowClick1"); oRow.children( "td:not( .NoHilite)").addClass( "HiliteRowClick1"); }