var grid;
function init(){
grid = new DataTable("grid");
}
DataTable = function(id){
this.init(id);
return this;
}
DataTable.prototype = {
wrapper : null,
table : null,
header : null,
tbody : null,
lastSelected : null,
selectedRows : [],
init : function(id){
this.wrapper = getEl(id);
this.table = this.wrapper.getChildrenByTagName("table")[0];
this.header = this.table.getChildrenByTagName("thead")[0];
this.tbody = this.table.getChildrenByTagName("tbody")[0];
var rows = this.tbody.getChildrenByTagName("tr");
for(var i = 0;i < rows.length;i++){
var row = rows[i];
row.rowNumber = i;
row.mon("click",this.row_onclick,this,true);
row.mon("mouseover",this.row_onMouseOver,this,true);
row.mon("mouseout",this.row_onMouseOut,this,true);
}
this.table.on("selectstart",function(){return false;},this,true);
this.table.setStyle("MozUserSelect","none");
},
getRows : function(){
var rows = this.tbody.getChildrenByTagName("tr");
return rows;
},
row_onclick : function(e){
e.stopEvent();
var target = e.getTarget();
var row = this.getRowObject( target,"TR");
if(e.shiftKey){
if(this.lastSelected){
if(!e.ctrlKey) this.clearSelection();
this.setRangeState(this.lastSelected,row,true);
}
}
else{
if(e.ctrlKey){
this.setRangeState(row,row,!this.isSelected(row.id));
}
else{
this.clearSelection();
this.setRangeState(row,row,true);
}
}
if(YAHOO.ext.util.Browser.isGecko) window.getSelection().removeAllRanges();
},
clearSelection : function(){
for(var i in this.selectedRows){
this.setRowState(this.selectedRows[i],false);
}
this.selectedRows = [];
},
setRangeState : function(from ,to,state){
var f,t;
if (from.rowNumber <= to.rowNumber){
f = from.rowNumber;
t = to.rowNumber;
}
else{
f = to.rowNumber;
t = from.rowNumber;
}
var rows = this.getRows();
for(i = f; i <= t;i++){
var row = rows[i] ;
this.setRowState(row,state);
}
if(state){
this.lastSelected = to;
}
},
setRowState : function(row,state){
if(state){
row.removeClass("grid_row_current");
row.removeClass("grid_row_selected_current");
row.addClass("grid_row_selected");
this.selectedRows[row.id] = row;
}
else{
row.removeClass("grid_row_current");
row.removeClass("grid_row_selected_current");
row.removeClass("grid_row_selected");
delete this.selectedRows[row.id] ;
}
},
row_onMouseOver : function(e){
e.preventDefault();
var target = e.getTarget();
var tr = this.getRowObject( target,"TR");
if(this.isSelected(tr.id)) tr.addClass("grid_row_selected_current");
else tr.addClass("grid_row_current");
//tr.addClass("grid_row_current");
},
row_onMouseOut : function(e){
e.preventDefault();
var target = e.getTarget();
var tr = this.getRowObject( target,"TR");
if(this.isSelected(tr.id)) tr.removeClass("grid_row_selected_current");
else tr.removeClass("grid_row_current");
// tr.removeClass("grid_row_current");
},
getRowObject : function (el,tag){
var current = el.parentNode;
while(current){
if(current.tagName == tag) break;
current = current.parentNode;
}
return new YAHOO.ext.Element( current );
},
isSelected : function(id){
var selected = this.selectedRows[id];
if(selected != null) return true;
else return false;
}
}