net/sf/jsm/table/jsmTable.js
Summary
No overview generated for 'net/sf/jsm/table/jsmTable.js'
Class Summary
|
JsmTable |
Represents a table
|
function JsmTable(containerId, variableName, resizable) {
this.id=new Date().getTime() + "" + Math.random();
logger.info("Created JsmTable with id: " + this.id);
this._attributes=new Array();
this.containerId = (jsm.noud(containerId))?"myTableContainer":containerId;
this.variableName = (jsm.noud(variableName))?"myTable":variableName;
this._resizable = (jsm.noud(resizable))?false:resizable;
this._headerRow = null;
this._rows = new Array();
this._footer = new Array();
this._actionCellSide='right';
this._headerActionCells = new Array();
this._footerActionCells = new Array();
this._rowActionCells = new Array();
this._filters = new Array();
this._columnIdx2FilterGenerationConfig = new Array();
this._columnName2ColumnIdx = new Array();
this._columnIdx2ColumnName = new Array();
this._columnIdx2Widget = new Array();
this._columnIdx2PropertyPath = new Array();
this._columnIdx2Comparator = new Array();
this._attributes['border']=0;
this._attributes['cellSpacing']=0;
this._attributes['cellPadding']=0;
this._sortColumn=0;
this._sortOrder='desc';
this._nodeIterationCounter = 0;
this._showOnlyHeader=false;
this._pageingMaxRows=-1;
this._pageingCurrentStartRow=0;
this._noRowsToDisplayMessage="There are currently no rows to display";
this._dirty=false;
}
JsmTable.extendClass(JsmObject);
JsmTable.prototype.className="JsmTable";
JsmTable.prototype.toString = function() {
var retVal = "[JsmTable object: id=" + this.id + " rowCount=" + this._rows.length + "]\n";
if (!jsm.noud(this._headerRow)) {retVal=retVal+this._headerRow;}
for (var trIdx=0;trIdx<this._rows.length;trIdx++) {
retVal=retVal+this._rows[trIdx];
}
return retVal;
}
JsmTable.prototype.setVariableName = function(variableName) {
this.variableName = variableName;
return this;
}
JsmTable.prototype.getVariableName = function() {
return this.variableName;
}
JsmTable.prototype.setContainerId = function(containerId) {
this.containerId = containerId;
return this;
}
JsmTable.prototype.getContainerId = function() {
return this.containerId;
}
JsmTable.prototype.setNoRowsToDisplayMessage = function(noRowsToDisplayMessage) {
this._noRowsToDisplayMessage = noRowsToDisplayMessage;
return this;
}
JsmTable.prototype.setShowOnlyHeader = function(showOnlyHeader) {
this._showOnlyHeader = showOnlyHeader;
return this;
}
JsmTable.prototype.isShowOnlyHeader = function() {
return this._showOnlyHeader;
}
JsmTable.prototype.setSortColumn = function(sortColumn) {
this._sortColumn = sortColumn;
return this;
}
JsmTable.prototype.getSortColumn = function() {
return this._sortColumn;
}
JsmTable.prototype.setSortOrder = function(sortOrder) {
if (sortOrder != 'asc' && sortOrder !='desc') {sortOrder='asc';}
this._sortOrder = sortOrder;
return this;
}
JsmTable.prototype.getSortOrder = function() {
return this._sortOrder;
}
JsmTable.prototype.addFilter = function(filter) {
this.setPageingCurrentStartRow(0);
this._filters.push(filter);
return this;
}
JsmTable.prototype.getFilters = function() {
return this._filters;
}
JsmTable.prototype.removeFilters = function() {
this.setPageingCurrentStartRow(0);
this._filters = new Array();
return this;
}
JsmTable.prototype.addColumnName = function(columnName, columnIdx) {
this._columnIdx2ColumnName[columnIdx]=columnName;
this._columnName2ColumnIdx[columnName]=columnIdx;
return this;
}
JsmTable.prototype.getColumnName = function(columnIdx) {
return this._columnIdx2ColumnName[columnIdx];
}
JsmTable.prototype.getColumnIdx = function(columnName) {
return jsm.noud(this._columnName2ColumnIdx[columnName])?null:this._columnName2ColumnIdx[columnName];
}
JsmTable.prototype.addWidget = function(columnIdxOrName, widget) {
var columnIdx = isNaN(columnIdxOrName)?this.getColumnIdx(columnIdxOrName):columnIdxOrName;
this._columnIdx2Widget[columnIdx]=widget;
return this;
}
JsmTable.prototype.getWidget = function(columnIdxOrName) {
var columnIdx = isNaN(columnIdxOrName)?this.getColumnIdx(columnIdxOrName):columnIdxOrName;
return this._columnIdx2Widget[columnIdx];
}
JsmTable.prototype.addFilterConfig = function(columnIdxOrName, widget, filter) {
var columnIdx = isNaN(columnIdxOrName)?this.getColumnIdx(columnIdxOrName):columnIdxOrName;
this._columnIdx2FilterGenerationConfig[columnIdx]=[widget, filter];
return this;
}
JsmTable.prototype.getFilterConfig = function(columnIdx) {
var filterConfig = this._columnIdx2FilterGenerationConfig[columnIdx];
if (jsm.noud(filterConfig)) {filterConfig = [this.getWidget(columnIdx)];}
if (jsm.noud(filterConfig[1])) {filterConfig[1]=new JsmRegExFilter().setIgnoreCase(true);}
return filterConfig;
}
JsmTable.prototype.addPropertyPath = function(columnIdxOrName, propertyPath) {
var columnIdx = isNaN(columnIdxOrName)?this.getColumnIdx(columnIdxOrName):columnIdxOrName;
this._columnIdx2PropertyPath[columnIdx]=propertyPath;
return this;
}
JsmTable.prototype.getPropertyPath = function(columnIdx) {
return this._columnIdx2PropertyPath[columnIdx];
}
JsmTable.prototype.addComparator = function(columnIdxOrName, comparator) {
var columnIdx = isNaN(columnIdxOrName)?this.getColumnIdx(columnIdxOrName):columnIdxOrName;
this._columnIdx2Comparator[columnIdx]=comparator;
return this;
}
JsmTable.prototype.getComparator = function(columnIdx) {
var retVal = this._columnIdx2Comparator[columnIdx];
return (jsm.noud(retVal)?JsmAlphaComparator:retVal);
}
JsmTable.prototype.getValue = function(trIdx, tdIdx) {
try {
return this._rows[trIdx]._cells[tdIdx].value;
} catch (e) {logger.error("Can not getValue: " + trIdx + " - " + tdIdx + " - " + value); return "";}
}
JsmTable.prototype.setValue = function(trIdx, tdIdx, value) {
try {
this._rows[trIdx]._cells[tdIdx].value = value;
} catch (e) {logger.error("Can not setValue: " + trIdx + " - " + tdIdx + " - " + value);}
return this;
}
JsmTable.prototype.getTr = function(trIdx) {
return this._rows[trIdx];
}
JsmTable.prototype.getTd = function(trIdx, tdIdx) {
try {
return this._rows[trIdx]._cells[tdIdx];
} catch (e) {logger.error("Can not getTd: " + trIdx + " - " + tdIdx); return null;}
}
JsmTable.prototype.getTds = function(trIdx) {
try {
return this._rows[trIdx]._cells;
} catch (e) {logger.error("Can not getTds: " + trIdx); return null;}
}
JsmTable.prototype.getRow = function(trIdx) {
return this.getTr(trIdx);
}
JsmTable.prototype.getRows = function() {
return this._rows;
}
JsmTable.prototype.getRowCount = function() {
return this._rows.length;
}
JsmTable.prototype.getFilteredRows = function() {
var retVal = [];
for (var trIdx=0;trIdx<this._rows.length;trIdx++) {
var passedFilter = true;
var row = this._rows[trIdx];
for (var filter=0;filter<this.getFilters().length;filter++) {
passedFilter = this.getFilters()[filter].isValidRow(row);
if (!passedFilter) break;
}
if (passedFilter) {
retVal.push(row);
}
}
return retVal;
}
JsmTable.prototype.getFooter = function() {
return this._footer;
}
JsmTable.prototype.getHeaderRow = function() {
return this._headerRow;
}
JsmTable.prototype.setHeaderRow = function(tr) {
logger.debug("Add header row. Cell count: " + tr.getCells().length);
this._headerRow = tr;
tr.table = this;
tr.setMetaData('jsmRowType', 'header');
for(var i = 0; i < tr.getCells().length; i++) {
if (jsm.noud(this.getColumnName(i)) && !jsm.noud(tr.getCells()[i].getValue())) {
logger.debug("Add column name: " + tr.getCells()[i].getValue() + " to column: " + i);
this.addColumnName(tr.getCells()[i].getValue(), i);
}
}
return this;
}
JsmTable.prototype.addRow = function(tr, renderImmediately) {
this._rows.push(tr);
tr.table = this;
tr.idx=this._rows.length-1;
if (renderImmediately) {
var theTable = $(this.id);
if (!jsm.noud(theTable)) {
var tbody = theTable.getElementsByTagName('tbody')[0];
var specialClassName='jsmEven';
if(!jsm.noud(tbody.lastChild) && !jsm.noud(tbody.lastChild.className)) {
if (tbody.lastChild.className.indexOf('jsmEven')!=-1) {specialClassName='jsmOdd';}
}
tbody.appendChild(tr.getNode(specialClassName));
}
}
return this;
}
JsmTable.prototype.addRows = function(trs, renderImmediately) {
for (var rowIdx=0;rowIdx<trs.length;rowIdx++) {
this.addRow(trs[rowIdx], renderImmediately);
}
return this;
}
JsmTable.prototype.removeRow = function(idx) {
logger.debug("JsmTable: removeRow: at " + idx);
this._rows.splice(idx, 1);
this._updateRowIndex();
}
JsmTable.prototype.addFooter = function(tr) {
this._footer.push(tr);
tr.table = this;
tr.setMetaData('jsmRowType', 'footer');
return this;
}
JsmTable.prototype.setActionCellSide = function(leftOrRight) {
this._actionCellSide=leftOrRight;
return this;
}
JsmTable.prototype.getActionCellSide = function() {
return this._actionCellSide;
}
JsmTable.prototype.addHeaderActionCell = function(theActionCell) {
this._headerActionCells.push(theActionCell);
return this;
}
JsmTable.prototype.addFooterActionCell = function(theActionCell) {
this._footerActionCells.push(theActionCell);
return this;
}
JsmTable.prototype.addRowActionCell = function(theActionCell) {
this._rowActionCells.push(theActionCell);
return this;
}
JsmTable.prototype.setPageingMaxRows = function(pageingMaxRows) {
this._pageingMaxRows=pageingMaxRows;
return this;
}
JsmTable.prototype.getPageingMaxRows = function() {
return this._pageingMaxRows;
}
JsmTable.prototype.setPageingCurrentStartRow = function(pageingCurrentStartRow) {
this._pageingCurrentStartRow=pageingCurrentStartRow;
return this;
}
JsmTable.prototype.getPageingCurrentStartRow = function() {
return this._pageingCurrentStartRow;
}
JsmTable.prototype.render = function() {
var theContainer = document.getElementById(this.containerId)
if (jsm.noud(theContainer)) {
alert("Can not find container to hold table with id: " + this.containerId + " - make sure to define an html element with the appropriate id before calling the render() method.");
return;
}
theContainer.innerHTML='';
theContainer.appendChild(this.getNode());
window.setTimeout("jsmSelectWidgetNodes()", 10);
}
JsmTable.prototype.sort = function(columnIdxOrName) {
var columnIdx;
if (isNaN(columnIdxOrName)) {
columnIdx = this.getColumnIdx(columnIdxOrName);
} else {
columnIdx = columnIdxOrName;
}
if (jsm.noud(columnIdx)) {
if (isNaN(this._sortColumn)) {
this._sortColumn=this.getColumnIdx(this._sortColumn);
}
columnIdx=this._sortColumn;
}
if (this._sortColumn == columnIdx && this._sortOrder == 'asc') {
this._rows.reverse(this.getComparator(this._sortColumn));
this._sortOrder = 'desc';
} else {
this._sortColumn = columnIdx;
this._rows.sort(this.getComparator(this._sortColumn));
this._sortOrder = 'asc';
}
this._updateRowIndex();
return this;
}
JsmTable.prototype.getNode = function() {
var table = document.createElement("table");
table.setAttribute("id", this.id);
table.model=this;
this.addAttributesToViewComponent(table);
if (jsm.noud(this.getAttribute('className'))) {
table.className='jsmTable';
} else {
table.className=this.getAttribute('className') + ' jsmTable';
}
if (!jsm.noud(this._headerRow)) {
var thead = document.createElement("thead");
table.appendChild(thead);
thead.appendChild(this._headerRow.getNode());
}
var tbody = document.createElement("tbody");
table.appendChild(tbody);
if (!this.isShowOnlyHeader()) {
this._nodeIterationCounter=0;
var passedFilterCounter=-1;
var filteredRows = this.getFilteredRows();
jsmFilteredRowsCount = filteredRows.length;
logger.debug("JsmTable.getNode: filteredRows# :" + jsmFilteredRowsCount);
if (jsmFilteredRowsCount == 0) {
var messageTd = new JsmTd(this._noRowsToDisplayMessage).setAttribute('align', 'center');
if (!jsm.noud(this.getHeaderRow()) && !jsm.noud(this.getHeaderRow().getCells())) {messageTd.setAttribute('colspan', this.getHeaderRow().getCells().length);}
var messageTr = new JsmTr(messageTd);
messageTr.table = this;
tbody.appendChild(messageTr.getNode());
}
for (var idx=0;idx<filteredRows.length;idx++) {
var row = filteredRows[idx];
if (this.getPageingMaxRows()) {
logger.debug("JsmTable.getNode: pageing is activated: maxRows: " + this.getPageingMaxRows() + " current start row: " + this.getPageingCurrentStartRow());
if (idx >= this.getPageingCurrentStartRow()) {
logger.trace("JsmTable.getNode: add row from filteredRows: " + idx);
tbody.appendChild(row.getNode((this._nodeIterationCounter%2==0)?'jsmEven':'jsmOdd'));
this._nodeIterationCounter++;
}
} else {
tbody.appendChild(row.getNode());
this._nodeIterationCounter++;
}
if (this.getPageingMaxRows() != -1 && this.getPageingMaxRows()==this._nodeIterationCounter) {
break;
}
}
for (var trIdx=0;trIdx<this._footer.length;trIdx++) {
tbody.appendChild(this._footer[trIdx].getNode());
}
}
if (this._resizable) {
table.onmousemove=_jsmOnMouseMoveEventDelegate;
}
return table;
}
JsmTable.prototype._updateRowIndex = function() {
for (var trIdx=0;trIdx<this._rows.length;trIdx++) {
this._rows[trIdx].idx = trIdx;
}
return this;
}
JsmTable.prototype._isNumberColumn = function(columnIdx) {
logger.debug("JsmTable: isNumberColumn: columnIdx: " + columnIdx);
for (var trIdx=0;trIdx<this._rows.length;trIdx++) {
logger.debug("JsmTable: isNumberColumn: trIdx: " + trIdx + this._rows[trIdx] + this._rows[trIdx]._cells[columnIdx]);
var row = this._rows[trIdx];
if (jsm.noud(row)) {
logger.error("JsmTable: isNumberColumn: row is null");
return false;
}
var cell = row._cells[columnIdx];
if (jsm.noud(cell)) {
logger.error("JsmTable: isNumberColumn: cell is null");
return false;
}
var cellValue = this._rows[trIdx]._cells[columnIdx].value;
if (cellValue != '' && isNaN(cellValue)) {return false;}
}
return true;
}
JsmTable.prototype.cleanUpAfterSave = function() {
var theDiv = $('JsmDoubleSelectFieldDiv');
if (!jsm.noud(theDiv)) {theDiv.parentNode.removeChild(theDiv);}
this.unSetEditModeForAllRows();
return this;
}
JsmTable.prototype.saveAllRowsInEditMode = function() {
var theDiv = $('JsmDoubleSelectFieldDiv');
if (!jsm.noud(theDiv)) {theDiv.parentNode.removeChild(theDiv);}
for (var rowIndex=0;rowIndex<this.getRows().length;rowIndex++) {
if (this.getRow(rowIndex).isEditMode()) {
_jsmOnSaveRow(this, rowIndex, -1);
}
}
return this;
}
JsmTable.prototype.unSetEditModeForAllRows = function() {
for (var rowIndex=0;rowIndex<this.getRows().length;rowIndex++) {
this.getRow(rowIndex).setEditMode(false);
}
return this;
}
JsmTable.prototype.focusOnFirstWidget = function() {
if (this.getRow(1) && this.getRow(1).getCells()) {
var cellcount = this.getRow(1).getCells().length;
}
for (i=0;i<cellcount;i++) {
var theWidget = this.getWidget(i);
if (!jsm.noud(theWidget)) {
var theElement = document.getElementById(theWidget.getId());
if (!jsm.noud(theElement)) {theElement.focus();}
break;
}
}
return this;
}
JsmTable.prototype.generateFilter = function(filterContainerId, filterColumnIdxs, horizontalSize) {
if (jsm.noud(horizontalSize)) {horizontalSize=2;}
logger.debug("generateFilter: filterContainerId: " + filterContainerId + " filterColumnIdxs: " + filterColumnIdxs + " horizontalSize: " + horizontalSize);
var myFilterTable = new JsmTable(filterContainerId, this.getVariableName() + "Filter").setAttribute("width", "100%");
var myFilterTr = null;
var widgetCount = 0;
for (var i=0;i<filterColumnIdxs.length;i++) {
columnIdx=filterColumnIdxs[i];
var filterConfig = this.getFilterConfig(columnIdx);
var widget = filterConfig[0];
if (!jsm.noud(widget)) {
if (widgetCount%horizontalSize == 0) {
logger.debug("generateFilter: Add new Tr: widgetCount: " + widgetCount);
myFilterTr = new JsmTr();
myFilterTable.addRow(myFilterTr);
}
widget = widget.clone();
widget.setId("jsmFilter@" + this.getVariableName() + "@" + columnIdx);
widget.setAttribute('onkeyup', "if (!e) e=window.event; if(e.keyCode == 13) try {document.getElementById('jsmApplyFilterButton@" + this.getVariableName() + "').click();}catch(e){}");
widget.setAttribute('onchange', "try {document.getElementById('jsmApplyFilterButton@" + this.getVariableName() + "').click();}catch(e){}");
myFilterTr.addCell(new JsmTd(this.getColumnName(columnIdx) + ":").setTitle(this.getColumnName(columnIdx)).setAttribute('class', 'jsmLabel'));
myFilterTr.addCell(new JsmTd(widget).setTitle(this.getColumnName(columnIdx)));
widgetCount++;
}
}
while (widgetCount%horizontalSize != 0) {
logger.debug("generateFilter: Add empty tds to fill up filter widget row");
widgetCount++;
myFilterTr.addCell(new JsmTd(" "));
myFilterTr.addCell(new JsmTd(" "));
}
widgetCount = 1;
myFilterTr = new JsmTr();
myFilterTr.addCell(new JsmTd("<button class='jsmButton' onclick='jsm.jsmResetTableFilter(\"" + this.getVariableName() + "\")'>" + jsmButtonResetFilter + "</button> <button id='jsmApplyFilterButton@" + this.getVariableName() + "' class='jsmButton' onclick='jsm.jsmApplyTableFilter(\"" + this.getVariableName() + "\")'>" + jsmButtonApplyFilter + "</button>").setAttribute("colspan", horizontalSize*2).setAttribute('align', 'right').setTitle(" "));
myFilterTable.addRow(myFilterTr);
myFilterTable.render();
return this;
}
JsmTable.prototype.setDirty = function(dirty) {
logger.debug("Reset the dirty flag for table: " + this);
this._dirty = dirty;
if (!dirty) {
var rows = this.getRows();
for (var rowIdx=0;rowIdx<rows.length;rowIdx++) {
var row = rows[rowIdx];
row.setDirty(dirty, false);
}
}
return this;
}
JsmTable.prototype.isDirty = function() {
return this._dirty;
}
Documentation generated by
JSDoc on Tue Sep 26 08:42:57 2006