jQuery表の色:パリティ表示、マウスのサスペンション、固定色をクリック

4987 ワード


//      :-------------------------------------------------START
/*
   :
 		$(function() {
 			_tableColorDecorator.initTableColor(tblIdArr, colorArr, captionRowspan, enableClickEvent, clickCount, clickColor);
 		});
 		
 		tblIdArr:     table id  ["table_id1", "table_id2"]
		colorArr:     [],   :    ,   ,   ,mouseover, ""       
 		captionRowspan:     ,
 		enableClickEvent:         ,      
 		clickCount:  1/2/"",   ,  ,   
 		clickColor:   ,“”       
 */
var _tableColorDecorator = {
	captionColor: "#6492DD",
	evenColor: "#F11FFF",
	oddColor: "#aaFFFF",
	mouseoverColor: "#FF1111",
	clickColor: "FFFF11",
	tmpColor: "#FFFFFF",
	$clickedTr: null,
	tmpClickColor: "#FFFFFF", 
	enableClickEvent: false, 
	clickCount: 1,

	setCaptionTrColor: function($tbl, captionRowspan) {
		$tbl.find("tr:lt(" + (captionRowspan) + ")").css("backgroundColor", _tableColorDecorator.captionColor);
	},
			
	setContentTrColor: function($tbl, captionRowspan) {
		$tbl.find("tr:gt(" + (captionRowspan - 1) + "):even").css("backgroundColor", _tableColorDecorator.evenColor);
		$tbl.find("tr:gt(" + (captionRowspan - 1) + "):odd").css("backgroundColor", _tableColorDecorator.oddColor);
	},
			
	bindEventHandler: function($tbl, captionRowspan) {
		var $contentTrs = $tbl.find("tr:gt(" + (captionRowspan - 1) + ")");
		
		$contentTrs.bind("mouseover", function() {
			if(_tableColorDecorator.$clickedTr == null ||
				(_tableColorDecorator.$clickedTr && _tableColorDecorator.$clickedTr.get(0).rowIndex != this.rowIndex)) {
				_tableColorDecorator.tmpColor = $(this).css("backgroundColor");
				$(this).css("backgroundColor", _tableColorDecorator.mouseoverColor);
			}
		});
		
		$contentTrs.bind("mouseout", function() {
			if(_tableColorDecorator.$clickedTr == null ||
				(_tableColorDecorator.$clickedTr && _tableColorDecorator.$clickedTr.get(0).rowIndex != this.rowIndex)) {
				$(this).css("backgroundColor", _tableColorDecorator.tmpColor);
			}
		});
		
		if(_tableColorDecorator.enableClickEvent) {
			var tmpEventName = _tableColorDecorator.clickCount == 1 ? "click" : "dblclick";
			$contentTrs.bind(tmpEventName, function() {
				if(_tableColorDecorator.$clickedTr == null) {
					_tableColorDecorator.tmpClickColor = _tableColorDecorator.tmpColor;
					$(this).css("backgroundColor", _tableColorDecorator.clickColor);
					_tableColorDecorator.$clickedTr = $(this);
				} else {
					if(_tableColorDecorator.$clickedTr.get(0).rowIndex == this.rowIndex) {
						$(this).css("backgroundColor", _tableColorDecorator.tmpClickColor);
						_tableColorDecorator.$clickedTr = null;
					} else {
						_tableColorDecorator.$clickedTr.css("backgroundColor", _tableColorDecorator.tmpClickColor);
						_tableColorDecorator.tmpClickColor = _tableColorDecorator.tmpColor;
						$(this).css("backgroundColor", _tableColorDecorator.clickColor);
						_tableColorDecorator.$clickedTr = $(this);
					}
				}
			});	
			
		}
	},

	initVar: function(colorArr, enableClickEvent, clickCount, clickColor){
		if(colorArr && colorArr.length == 4) {
			_tableColorDecorator.captionColor = colorArr[0];
			_tableColorDecorator.evenColor = colorArr[1];
			_tableColorDecorator.oddColor = colorArr[2];
			_tableColorDecorator.mouseoverColor = colorArr[3];
		}
		
		if(enableClickEvent && enableClickEvent != "") _tableColorDecorator.enableClickEvent = enableClickEvent; 
		if(clickCount && clickCount != "") _tableColorDecorator.clickCount = clickCount;
		if(clickColor && clickColor != "") _tableColorDecorator.clickColor = clickColor;
	},
	
	initTableColor: function(tblIdArr, colorArr, captionRowspan, enableClickEvent, clickCount, clickColor) {
		_tableColorDecorator.initVar(colorArr, enableClickEvent, clickCount, clickColor);
		
		for(var i = 0; i < tblIdArr.length; i++) {
			$tbl = $("#" + tblIdArr[i]);
			if($tbl) {
				_tableColorDecorator.setCaptionTrColor($tbl, captionRowspan);					
				_tableColorDecorator.setContentTrColor($tbl, captionRowspan);					
				_tableColorDecorator.bindEventHandler($tbl, captionRowspan);					
			}
		}
	}
}
			//      :-------------------------------------------------END