/** * A class which represents a graphical list of DataCells. * This class allows list, queue and stack operations on it. */ /* ## class DataList implements IDataSet */ /** * DataList constructor */ function DataList(dataCellsCount, width, height) { var divName = "dataList" + getNewGUID(); this.frameDiv = document.createElement('DIV'); this.frameDiv.id = divName; this.frameDiv.style.width = width + "px"; this.frameDiv.style.height = height + "px"; this.frameDiv.style.borderWidth = "1px"; this.frameDiv.style.borderStyle = "Solid"; this.frameDiv.style.borderColor = "#000000"; this.frameDiv.style.backgroundColor = CELL_COLOR; this.frameDiv.style.overflow = "auto"; this.frameDiv.list = this; this.enabled = true; this.cursor = 0; this.dataSetType = "DataList"; var that = this; /** * Adds the given DataCell to the end of the list */ this.addDataCell = function(dataCell) { that.frameDiv.appendChild(dataCell.itemDiv); dataCell.parent = that; dataCell.onenterpressed = function() { if (dataCell.itemDiv.nextSibling) dataCell.itemDiv.nextSibling.onmousedown(); else { var newCell = new DataCell(); that.addDataCell(newCell); newCell.itemDiv.onmousedown(); } } dataCell.ondatachange = function() { if (dataCell.itemDiv.nextSibling) { return true; } else { var newCell = new DataCell(); that.addDataCell(newCell); //newCell.itemDiv.onmousedown(); } } } /** * Pushes a given DataCell to the top of the list */ this.push = function(dataCell) { if (!dataCell || !(dataCell.getValue())) return; if (that.frameDiv.firstChild) { that.frameDiv.insertBefore(dataCell.itemDiv, that.frameDiv.firstChild); dataCell.parent = that; dataCell.onenterpressed = function() { dataCell.itemDiv.nextSibling.onmousedown(); } } else { that.addDataCell(dataCell); } } /** * Pushes a given DataCell to the top of the list, * but keeps the size of the list by removing last DataCell */ this.pushKeepSize = function(dataCell) { if (!dataCell || !(dataCell.getValue())) return; that.push(dataCell); that.frameDiv.lastChild.dataCell.parent = null; that.frameDiv.removeChild(that.frameDiv.lastChild); } /** * Pops the top DataCell from the top of the list * Returns the poped cell */ this.pop = function() { var oldFirst = that.frameDiv.firstChild; if (!oldFirst) return null; if (oldFirst.dataCell.getValue() == undefined) return null; that.frameDiv.removeChild(oldFirst); oldFirst.dataCell.parent = null; return oldFirst.dataCell; } /** * Pops the top DataCell from the top of the list, * but keeps the size of the list by adding new DataCell to its end * value - A value for the new DataCell, null for empty * Returns the poped cell */ this.popKeepSize = function(value) { var poped = that.pop(); if (!poped) return null; var dc = new DataCell(); dc.setValue(value); that.addDataCell(dc); return poped; } /** * Returns the DataCell at the specified index */ this.getCellAt = function(index) { var i = 0; var cell = that.frameDiv.firstChild; if (!cell) return null; while (i < index) { cell = cell.nextSibling; if (!cell) return null; i++; } return cell.dataCell; } /** * Returns the current DataCell at cursor's position */ this.getCurrent = function() { return that.getCellAt(that.cursor); } /** * Increments the cursor to the next cell */ this.goNext = function() { var nextCellDiv = that.getCurrent().itemDiv.nextSibling; var nextCell = null; if (nextCellDiv) nextCell = nextCellDiv.dataCell; if (nextCell && (that.getCurrent().getValue() != undefined)) { that.cursor++; return true; } else if (!nextCell && (that.getCurrent().getValue() != undefined)) { that.cursor++; that.addDataCell(new DataCell()); } return false; } /** * Decrements the cursor to the previous cell */ this.goBack = function() { if (that.cursor > 0) { that.cursor--; return true; } return false; } /** * Clears a given cell in the list */ this.clearCell = function(dataCell) { //find cell index var i = 0; var cell = that.frameDiv.firstChild; while (cell && cell != dataCell.itemDiv) { cell = cell.nextSibling; i++; } //remove it that.frameDiv.removeChild(dataCell.itemDiv); //that.addDataCell(new DataCell()); //if index < cursor, goBack if (that.cursor > i) that.cursor--; //set highlight var current = that.getCurrent(); if (current) current.highlight(true); } /** * Returns an array representation of this DataList */ this.toArray = function() { var result = []; var c = that.getCellAt(0); for (var i=0; c && (c.getValue() != undefined); i++) { result[i] = c.getValue(); var b = c.itemDiv.nextSibling; if (b) c = b.dataCell; else c = null; } return result; } /** * Dehighlights the whole list */ this.dehighlight = function() { var x = that.frameDiv.firstChild; while (x) { x.dataCell.highlight(false); x = x.nextSibling; } } /** * Clears the list and adds one new empty DataCell. */ this.clearList = function() { while (that.pop()) { } that.push(new DataCell()); that.cursor = 0; } this.showValues = function() { var cell = that.frameDiv.firstChild; while (cell) { cell.dataCell.showValue(); cell = cell.nextSibling; } } //Init list if a size was given if (dataCellsCount) { for (var i = 0; i < dataCellsCount; i++) { this.addDataCell(new DataCell()); } } }