Skip to content

Instantly share code, notes, and snippets.

@stlsmiths
Created June 10, 2011 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stlsmiths/1019657 to your computer and use it in GitHub Desktop.
Save stlsmiths/1019657 to your computer and use it in GitHub Desktop.
YUI 2 DataTable keyboard navigation prototypes
//--------------------------------------------------------------------------------------
// TODD'S Table Nav Stuff ...... BEGIN
//
// Functions allow cell navigation within a DataTable, first developed in pmgr/trans_grid.html
//
//
//
YAHOO.widget.DataTable.prototype.getFirstVisTdEl = function(cell) {
if ( !cell ) return null;
var cols = this.getColumnSet().keys;
for(var i=0; i<cols.length; i++)
if ( !cols[i].hidden ) break;
return this.getTdEl( {record:this.getRecord(cell), column:this.getColumn(i)} );
};
YAHOO.widget.DataTable.prototype.getLastVisTdEl = function(cell) {
if ( !cell ) return null;
var cols = this.getColumnSet().keys;
for(var i=cols.length-1; i>=0; i--)
if ( !cols[i].hidden ) break;
return this.getTdEl( {record:this.getRecord(cell), column:this.getColumn(i)} );
};
YAHOO.widget.DataTable.prototype.getTopVisTdEl = function(cell) {
if ( !cell ) return null;
return this.getTdEl( {record:this.getFirstTrEl(), column:this.getColumn(cell)} );
};
YAHOO.widget.DataTable.prototype.getBottomVisTdEl = function(cell) {
if ( !cell ) return null;
return this.getTdEl( {record:this.getLastTrEl(), column:this.getColumn(cell)} );
};
YAHOO.widget.DataTable.prototype.getLeftVisTdEl = function(cell) {
var newCell = this.getPreviousTdEl( cell );
if ( newCell === null ) return cell;
if ( this.getColumn(newCell).hidden ) {
if ( cell === this.getFirstVisTdEl(cell) )
return this.getLastVisTdEl(cell);
//
// there may be hidden / visible cells to the LEFT of us ...
//
var cols = this.getColumnSet().keys,
col_index = this.getColumn(newCell).getIndex();
var inext = null;
for(var i=col_index-1; i>=0; i--) {
if (!cols[i].hidden) {
inext = i;
break;
}
}
if ( i === -1 ) // should never get here
newCell = this.getLastVisTdEl(cell);
else {
newCell = this.getTdEl({record:this.getRecord(cell), column:this.getColumn(inext)})
}
} else {
// not hidden, make sure we are on same record index
if ( this.getRecordIndex(newCell) !== this.getRecordIndex(cell) )
newCell = this.getLastVisTdEl(cell);
}
return newCell;
};
YAHOO.widget.DataTable.prototype.getRightVisTdEl = function(cell) {
var newCell = this.getNextTdEl( cell );
if ( newCell === null ) return cell;
if ( this.getColumn(newCell).hidden ) {
if ( cell === this.getLastVisTdEl(cell) )
return this.getFirstVisTdEl(cell);
//
// there may be hidden / visible cells LEFT of us ...
//
var cols = this.getColumnSet().keys,
col_index = this.getColumn(newCell).getIndex();
var inext = null;
for(var i=col_index; i<cols.length; i++) {
if (!cols[i].hidden) {
inext = i;
break;
}
}
if ( i === -1 ) // should never get here
newCell = this.getFirstVisTdEl(cell);
else {
newCell = this.getTdEl({record:this.getRecord(cell), column:this.getColumn(inext)})
}
} else {
// not hidden, make sure we are on same record index
if ( this.getRecordIndex(newCell) !== this.getRecordIndex(cell) )
newCell = this.getFirstVisTdEl(cell);
}
return newCell;
};
YAHOO.widget.DataTable.prototype.getUpVisTdEl = function(cell) {
if ( cell === this.getTopVisTdEl(cell) )
return this.getBottomVisTdEl(cell);
else
return this.getAboveTdEl(cell);
};
YAHOO.widget.DataTable.prototype.getDownVisTdEl = function(cell) {
if ( cell === this.getBottomVisTdEl(cell) )
return this.getTopVisTdEl(cell);
else
return this.getBelowTdEl(cell);
};
/**
* method setVisKeyListeners
*
*
*/
YAHOO.widget.DataTable.prototype.setVisKeyListeners = function() {
var YEvent = YAHOO.util.Event,
YUtil = YAHOO.util,
elCont = this.getTbodyEl();
// elCont = this.getContainerEl();
YEvent.addListener( elCont, "keydown", function (evt) {
var KEYS = YUtil.KeyListener.KEY,
cur_cell = this.getSelectedTdEls()[0],
new_cell=null,
cell_edit=false;
// if ( evt.keyCode === KEYS.ENTER || (evt.ctrlKey && evt.keyCode>36 && evt.keyCode<41) ) {
if ( evt.keyCode === KEYS.ENTER || ( evt.keyCode>36 && evt.keyCode<41) ) {
switch (evt.keyCode) {
case KEYS.ENTER :
/*
// if this._oCellEditor
*/
var col = this.getColumn(cur_cell);
if ( col.editor ) {
this.showCellEditor( cur_cell );
cell_edit = true;
}
break;
case KEYS.UP : // up
new_cell = this.getUpVisTdEl(cur_cell);
break;
case KEYS.DOWN : // down
new_cell = this.getDownVisTdEl(cur_cell);
break;
case KEYS.LEFT: // left
new_cell = this.getLeftVisTdEl(cur_cell);
break;
case KEYS.RIGHT: // right
new_cell = this.getRightVisTdEl(cur_cell);
break;
}
// If a direction key (not ENTER), then update cell position
if ( !cell_edit ) {
new_cell = ( !new_cell ) ? cur_cell : new_cell;
this.unselectCell(cur_cell);
this.selectCell( new_cell );
cur_cell = new_cell;
this.fireEvent( "updateVisCellPosition", {event:evt, newCell:new_cell} );
// If scrolling, update scroll position
if ( this instanceof YAHOO.widget.ScrollingDataTable )
this.scrollTo( new_cell );
}
}
YEvent.stopEvent(evt);
}, this, true);
};
//
// TODD'S Table Nav Stuff ...... END
//--------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment