Files
WebODF/webodf/lib/ops/OpInsertTable.js
T
Friedrich W. H. Kossebau be545658d7 Trigger an update of the canvas size on changes of the content/styles
The root node element of the flat ODF, <document>, is not a direct child of the
variable "sizer" in the odf.OdfCanvas code) which is used to implement the
zooming feature, by using the scale() transformation via the styles.
Problem is that this scaling does not also scale the actual space used by the
element. To fix this there is a method fixContainerSize() which is used to adapt
the size of the canvas element to the actual rendering size of the document
content. This code then sets the width and height properties to fixed values.
But all this results in another problem: the canvas element is not resized
automatically by the usual DOM behaviour if the content below the <document>
 node or the CSS applied to it parts is changed.

The initial plan was to use DOMSubtreeModified & friends, to have the
odf.OdfCanvas care for updates itself. But as it seems outdated and has quite
some overhead on larger changes to the document, in the end it was decided to go
for now with the approach that was chosen for the style changes, to rely on the
document modifying ops to trigger the needed canvas updates themselves. Not
perfect, but seemed consistent at least.
2013-04-24 11:20:08 +02:00

138 lines
4.9 KiB
JavaScript

/*global ops*/
/**
* @constructor
* @implements ops.Operation
*/
ops.OpInsertTable = function OpInsertTable() {
"use strict";
var memberid, timestamp, initialRows, initialColumns, position, tableName, tableStyleName,
tableColumnStyleName, tableCellStyleMatrix,
/** @const */ tablens = "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
/** @const */ textns = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
this.init = function (data) {
memberid = data.memberid;
timestamp = data.timestamp;
position = data.position;
initialRows = data.initialRows;
initialColumns = data.initialColumns;
tableName = data.tableName;
tableStyleName = data.tableStyleName;
tableColumnStyleName = data.tableColumnStyleName;
tableCellStyleMatrix = data.tableCellStyleMatrix;
};
function getCellStyleName(row, column) {
var rowStyles;
if (tableCellStyleMatrix.length === 1) {
rowStyles = tableCellStyleMatrix[0];
} else if (tableCellStyleMatrix.length === 3) {
switch (row) {
case 0:
rowStyles = tableCellStyleMatrix[0];
break;
case initialRows - 1:
rowStyles = tableCellStyleMatrix[2];
break;
default:
rowStyles = tableCellStyleMatrix[1];
break;
}
} else {
rowStyles = tableCellStyleMatrix[row];
}
if (rowStyles.length === 1) {
return rowStyles[0];
}
if (rowStyles.length === 3) {
switch (column) {
case 0:
return rowStyles[0];
case initialColumns - 1:
return rowStyles[2];
default:
return rowStyles[1];
}
}
return rowStyles[column];
}
function createTableNode(document) {
var tableNode = document.createElementNS(tablens, "table:table"),
columns = document.createElementNS(tablens, "table:table-column"),
row, cell, paragraph,
rowCounter, columnCounter, cellStyleName;
if (tableStyleName) {
tableNode.setAttributeNS(tablens, "table:style-name", tableStyleName);
}
if (tableName) {
tableNode.setAttributeNS(tablens, "table:name", tableName);
}
columns.setAttributeNS(tablens, "table:number-columns-repeated", initialColumns);
if (tableColumnStyleName) {
columns.setAttributeNS(tablens, "table:style-name", tableColumnStyleName);
}
tableNode.appendChild(columns);
for (rowCounter = 0; rowCounter < initialRows; rowCounter += 1) {
row = document.createElementNS(tablens, "table:table-row");
for (columnCounter = 0; columnCounter < initialColumns; columnCounter += 1) {
cell = document.createElementNS(tablens, "table:table-cell");
cellStyleName = getCellStyleName(rowCounter, columnCounter);
if (cellStyleName) {
cell.setAttributeNS(tablens, "table:style-name", cellStyleName);
}
paragraph = document.createElementNS(textns, "text:p");
cell.appendChild(paragraph);
row.appendChild(cell);
}
tableNode.appendChild(row);
}
return tableNode;
}
this.execute = function (odtDocument) {
var domPosition = odtDocument.getPositionInTextNode(position),
rootNode = odtDocument.getRootNode(),
previousSibling,
tableNode;
if (domPosition) {
tableNode = createTableNode(odtDocument.getDOM());
// For now assume the table should be inserted after the current paragraph
// or failing that, as the first element in the root node
previousSibling = odtDocument.getParagraphElement(domPosition.textNode);
rootNode.insertBefore(tableNode, previousSibling ? previousSibling.nextSibling : undefined);
odtDocument.getOdfCanvas().refreshSize();
odtDocument.emit(ops.OdtDocument.signalTableAdded, {
tableElement: tableNode,
memberId: memberid,
timeStamp: timestamp
});
return true;
}
return false;
};
this.spec = function () {
return {
optype: "InsertTable",
memberid: memberid,
timestamp: timestamp,
position: position,
initialRows: initialRows,
initialColumns: initialColumns,
tableName: tableName,
tableStyleName: tableStyleName,
tableColumnStyleName: tableColumnStyleName,
tableCellStyleMatrix: tableCellStyleMatrix
};
};
};