📄 render.grid.js
字号:
/** * Component rendering peer: Grid */EchoAppRender.GridSync = Core.extend(EchoRender.ComponentSync, { $static: { _createPrototypeTable: function() { var tableElement = document.createElement("table"); tableElement.style.outlineStyle = "none"; tableElement.tabIndex = "-1"; tableElement.style.borderCollapse = "collapse"; var colGroupElement = document.createElement("colgroup"); tableElement.appendChild(colGroupElement); var tbodyElement = document.createElement("tbody"); tableElement.appendChild(tbodyElement); return tableElement; }, Processor: Core.extend({ $static: { Cell: Core.extend({ $construct: function(component, index, xSpan, ySpan) { this.component = component; this.index = index; this.xSpan = xSpan; this.ySpan = ySpan; } }) }, $construct: function(grid) { this.grid = grid; this.cellArrays = []; this.horizontalOrientation = grid.render("orientation") != EchoApp.Grid.ORIENTATION_VERTICAL; var cells = this.createCells(); if (cells == null) { // Special case: empty Grid. this.gridXSize = 0; this.gridYSize = 0; return; } this.renderCellMatrix(cells); this.calculateExtents(); this.reduceY(); this.reduceX(); }, calculateExtents: function() { var xProperty = this.horizontalOrientation ? "columnWidth" : "rowHeight"; var yProperty = this.horizontalOrientation ? "rowHeight" : "columnWidth"; this.xExtents = []; for (var i = 0; i < this.gridXSize; ++i) { this.xExtents.push(this.grid.renderIndex(xProperty, i)); } this.yExtents = []; for (var i = 0; i < this.gridYSize; ++i) { this.yExtents.push(this.grid.renderIndex(yProperty, i)); } }, createCells: function() { var childCount = this.grid.getComponentCount(); if (childCount == 0) { // Abort if Grid is empty. return null; } var cells = []; for (var i = 0; i < childCount; ++i) { var child = this.grid.getComponent(i); var layoutData = child.render("layoutData"); if (layoutData) { var xSpan = this.horizontalOrientation ? layoutData.columnSpan : layoutData.rowSpan; var ySpan = this.horizontalOrientation ? layoutData.rowSpan : layoutData.columnSpan; cells.push(new EchoAppRender.GridSync.Processor.Cell(child, i, xSpan ? xSpan : 1, ySpan ? ySpan : 1)); } else { cells.push(new EchoAppRender.GridSync.Processor.Cell(child, i, 1, 1)); } } return cells; }, /** * Returns an array representing the cells at the specified y-index. * If no array currently exists, one is created. * * @param {Integer} y the y-index * @return the array of cells. * @type {Array} */ _getCellArray: function(y) { while (y >= this.cellArrays.length) { this.cellArrays.push(new Array(this.gridXSize)); } return this.cellArrays[y]; }, /** * Returns the number of columns that should be rendered. * * @return the number of rendered columns * @type Integer */ getColumnCount: function() { return this.horizontalOrientation ? this.gridXSize : this.gridYSize; }, /** * Returns the cell that should be rendered at the * specified position. * * @param {Integer} column the column index * @param {Integer} row the row index * @return the cell * @type EchoAppRender.GridSync.Processor.Cell */ getCell: function(column, row) { if (this.horizontalOrientation) { return this.cellArrays[row][column]; } else { return this.cellArrays[column][row]; } }, /** * Returns the number of rows that should be rendered. * * @return the number of rendered rows * @type Integer */ getRowCount: function() { return this.horizontalOrientation ? this.gridYSize : this.gridXSize; }, /** * Remove duplicates from the x-axis where all cells simply * "span over" a given x-axis coordinate. */ reduceX: function() { // Determine duplicate cell sets on x-axis. var xRemoves = []; var x = 1; var length = this.cellArrays[0].length; while (x < length) { var y = 0; var identical = true; while (y < this.cellArrays.length) { if (this.cellArrays[y][x] != this.cellArrays[y][x - 1]) { identical = false; break; } ++y; } if (identical) { xRemoves[x] = true; } ++x; } // If no reductions are necessary on the x-axis, do nothing. if (xRemoves.length == 0) { return; } for (var removedX = this.gridXSize - 1; removedX >= 0; --removedX) { if (!xRemoves[removedX]) { continue; } for (var y = 0; y < this.gridYSize; ++y) { if (y == 0 || this.cellArrays[y][removedX - 1] != this.cellArrays[y - 1][removedX - 1]) { // Reduce x-span, taking care not to reduce it multiple times if cell has a y-span. if (this.cellArrays[y][removedX - 1] != null) { --this.cellArrays[y][removedX - 1].xSpan; } } for (x = removedX; x < this.gridXSize - 1; ++x) { this.cellArrays[y][x] = this.cellArrays[y][x + 1]; } } //FIXME. Add extent-size recalc. --this.gridXSize; } }, /** * Remove duplicates from the y-axis where all cells simply * "span over" a given y-axis coordinate. */ reduceY: function() { var yRemoves = []; var y = 1; var size = this.cellArrays.length; var previousCellArray; var currentCellArray = this.cellArrays[0]; while (y < size) { previousCellArray = currentCellArray; currentCellArray = this.cellArrays[y]; var x = 0; var identical = true; while (x < currentCellArray.length) { if (currentCellArray[x] != previousCellArray[x]) { identical = false; break; } ++x; } if (identical) { yRemoves[y] = true; } ++y; } // If no reductions are necessary on the y-axis, do nothing. if (yRemoves.length == 0) { return; } for (var removedY = this.gridYSize - 1; removedY >= 0; --removedY) { if (!yRemoves[removedY]) { continue; } // Shorten the y-spans of the cell array that will be retained to // reflect the fact that a cell array is being removed. var retainedCellArray = this.cellArrays[removedY - 1]; for (var x = 0; x < this.gridXSize; ++x) { if (x == 0 || retainedCellArray[x] != retainedCellArray[x - 1]) { // Reduce y-span, taking care not to reduce it multiple times if cell has an x-span. if (retainedCellArray[x] != null) { --retainedCellArray[x].ySpan; } } } // Remove the duplicate cell array. this.cellArrays.splice(removedY, 1); //FIXME. insert code here for extent adjustment. // Decrement the grid size to reflect cell array removal. --this.gridYSize; } }, renderCellMatrix: function(cells) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -