mirror of
https://github.com/webodf/WebODF.git
synced 2026-11-30 16:47:58 +01:00
Add ability to create empty presentation and spreadsheet.
To create an empty document in odf.OdfContainer, the type of the document should be passed. If a string is passed, it is interpreted as url and an attempt is made to load a document from that url.
This commit is contained in:
@@ -314,18 +314,24 @@
|
||||
/**
|
||||
* The OdfContainer class manages the various parts that constitues an ODF
|
||||
* document.
|
||||
* The constructor takes a url or a type. If urlOrType is a type, an empty
|
||||
* document of that type is created. Otherwise, urlOrType is interpreted as
|
||||
* a url and loaded from that url.
|
||||
*
|
||||
* @constructor
|
||||
* @param {!string} url
|
||||
* @param {!string|!odf.OdfContainer.DocumentType} urlOrType
|
||||
* @param {?function(!odf.OdfContainer)=} onstatereadychange
|
||||
* @return {?}
|
||||
*/
|
||||
odf.OdfContainer = function OdfContainer(url, onstatereadychange) {
|
||||
odf.OdfContainer = function OdfContainer(urlOrType, onstatereadychange) {
|
||||
var self = this,
|
||||
/**@type {!core.Zip}*/
|
||||
zip,
|
||||
partMimetypes = {},
|
||||
/**@type {?Element}*/
|
||||
contentElement;
|
||||
contentElement,
|
||||
/**@type{!string}*/
|
||||
url = "";
|
||||
|
||||
// NOTE each instance of OdfContainer has a copy of the private functions
|
||||
// it would be better to have a class OdfContainerPrivate where the
|
||||
@@ -1133,16 +1139,17 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!string} type
|
||||
* @return {!core.Zip}
|
||||
*/
|
||||
function createEmptyTextDocument() {
|
||||
function createEmptyDocument(type) {
|
||||
var emptyzip = new core.Zip("", null),
|
||||
data = runtime.byteArrayFromString(
|
||||
"application/vnd.oasis.opendocument.text",
|
||||
"application/vnd.oasis.opendocument." + type,
|
||||
"utf8"
|
||||
),
|
||||
root = self.rootElement,
|
||||
text = document.createElementNS(officens, 'text');
|
||||
content = document.createElementNS(officens, type);
|
||||
emptyzip.save("mimetype", data, false, new Date());
|
||||
/**
|
||||
* @param {!string} memberName variant of the real local name which allows dot notation
|
||||
@@ -1167,8 +1174,8 @@
|
||||
addToplevelElement("automaticStyles", "automatic-styles");
|
||||
addToplevelElement("masterStyles", "master-styles");
|
||||
addToplevelElement("body");
|
||||
root.body.appendChild(text);
|
||||
partMimetypes["/"] = "application/vnd.oasis.opendocument.text";
|
||||
root.body.appendChild(content);
|
||||
partMimetypes["/"] = "application/vnd.oasis.opendocument." + type;
|
||||
partMimetypes["settings.xml"] = "text/xml";
|
||||
partMimetypes["meta.xml"] = "text/xml";
|
||||
partMimetypes["styles.xml"] = "text/xml";
|
||||
@@ -1177,6 +1184,7 @@
|
||||
setState(OdfContainer.DONE);
|
||||
return emptyzip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the zip with current data.
|
||||
* @return {undefined}
|
||||
@@ -1282,7 +1290,14 @@
|
||||
);
|
||||
|
||||
// initialize private variables
|
||||
if (url) {
|
||||
if (urlOrType === odf.OdfContainer.DocumentType.TEXT) {
|
||||
zip = createEmptyDocument("text");
|
||||
} else if (urlOrType === odf.OdfContainer.DocumentType.PRESENTATION) {
|
||||
zip = createEmptyDocument("presentation");
|
||||
} else if (urlOrType === odf.OdfContainer.DocumentType.SPREADSHEET) {
|
||||
zip = createEmptyDocument("spreadsheet");
|
||||
} else {
|
||||
url = /**@type{!string}*/(urlOrType);
|
||||
zip = new core.Zip(url, function (err, zipobject) {
|
||||
zip = zipobject;
|
||||
if (err) {
|
||||
@@ -1296,8 +1311,6 @@
|
||||
loadComponents();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
zip = createEmptyTextDocument();
|
||||
}
|
||||
};
|
||||
odf.OdfContainer.EMPTY = 0;
|
||||
@@ -1315,3 +1328,11 @@
|
||||
};
|
||||
return odf.OdfContainer;
|
||||
}());
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
odf.OdfContainer.DocumentType = {
|
||||
TEXT: 1,
|
||||
PRESENTATION: 2,
|
||||
SPREADSHEET: 3
|
||||
};
|
||||
|
||||
@@ -145,7 +145,7 @@ odf.LayoutTests = function LayoutTests(runner) {
|
||||
*/
|
||||
function fillDocument(test, callback) {
|
||||
var officens = odf.Namespaces.officens,
|
||||
odfContainer = new odf.OdfContainer("", null),
|
||||
odfContainer = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null),
|
||||
root = odfContainer.rootElement,
|
||||
path = test.name + ".odt",
|
||||
input = test.input;
|
||||
|
||||
@@ -75,8 +75,7 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
});
|
||||
}
|
||||
|
||||
function createNew() {
|
||||
t.odf = new odf.OdfContainer("", null);
|
||||
function testEmptyDocument() {
|
||||
r.shouldBe(t, "t.odf.state", "odf.OdfContainer.DONE");
|
||||
r.shouldBeNonNull(t, "t.odf.rootElement");
|
||||
r.shouldBeNonNull(t, "t.odf.rootElement.meta");
|
||||
@@ -89,9 +88,27 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
r.shouldBeNonNull(t, "t.odf.rootElement.body");
|
||||
}
|
||||
|
||||
function createNewText() {
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
testEmptyDocument();
|
||||
r.shouldBeNonNull(t, "t.odf.rootElement.text");
|
||||
}
|
||||
|
||||
function createNewPresentation() {
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.PRESENTATION, null);
|
||||
testEmptyDocument();
|
||||
r.shouldBeNonNull(t, "t.odf.rootElement.presentation");
|
||||
}
|
||||
|
||||
function createNewSpreadsheet() {
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.SPREADSHEET, null);
|
||||
testEmptyDocument();
|
||||
r.shouldBeNonNull(t, "t.odf.rootElement.spreadsheet");
|
||||
}
|
||||
|
||||
function setRootElement_OverwritesAllDocumentElements() {
|
||||
var originalProperties = {};
|
||||
t.odf = new odf.OdfContainer("", null);
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
r.shouldBe(t, "t.odf.state", "odf.OdfContainer.DONE");
|
||||
t.originalRoot = t.odf.rootElement;
|
||||
// The properties values for the original root will change when it is disconnected from the document
|
||||
@@ -117,7 +134,7 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
}
|
||||
|
||||
function createNewSaveAsAndLoad(callback) {
|
||||
t.odf = new odf.OdfContainer("", null);
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
r.shouldBe(t, "t.odf.state", "odf.OdfContainer.DONE");
|
||||
t.odf.saveAs("test.odt", function (err) {
|
||||
t.err = err;
|
||||
@@ -131,7 +148,7 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
}
|
||||
|
||||
function createNewSaveAsAndLoad_OptionalElement_SettingsXml(callback) {
|
||||
t.odf = new odf.OdfContainer("", null);
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
r.shouldBe(t, "t.odf.state", "odf.OdfContainer.DONE");
|
||||
t.odf.rootElement.settings = null;
|
||||
t.odf.saveAs("test.odt", function (err) {
|
||||
@@ -150,7 +167,7 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
}
|
||||
|
||||
function createNewSaveAsAndLoad_OptionalElement_MetaXml(callback) {
|
||||
t.odf = new odf.OdfContainer("", null);
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
r.shouldBe(t, "t.odf.state", "odf.OdfContainer.DONE");
|
||||
t.odf.rootElement.meta = null;
|
||||
t.odf.saveAs("test.odt", function (err) {
|
||||
@@ -168,7 +185,7 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
}
|
||||
|
||||
function doFontFaceDeclsSaveAsAndLoadRoundTrip(args, callback) {
|
||||
t.odf = new odf.OdfContainer("", null);
|
||||
t.odf = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
appendXmlsToNode(t.odf.rootElement.fontFaceDecls, args.keptFontFaceDecls);
|
||||
appendXmlsToNode(t.odf.rootElement.fontFaceDecls, args.droppedFontFaceDecls);
|
||||
appendXmlsToNode(t.odf.rootElement.styles, args.styles);
|
||||
@@ -331,7 +348,9 @@ odf.OdfContainerTests = function OdfContainerTests(runner) {
|
||||
*/
|
||||
this.tests = function () {
|
||||
return r.name([
|
||||
createNew,
|
||||
createNewText,
|
||||
createNewPresentation,
|
||||
createNewSpreadsheet,
|
||||
setRootElement_OverwritesAllDocumentElements
|
||||
]);
|
||||
};
|
||||
|
||||
@@ -416,7 +416,7 @@ ops.OperationTests = function OperationTests(runner) {
|
||||
t = {};
|
||||
testarea = core.UnitTest.provideTestAreaDiv();
|
||||
t.odfcanvas = new odf.OdfCanvas(testarea);
|
||||
t.odfContainer = new odf.OdfContainer("", null);
|
||||
t.odfContainer = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
t.odfcanvas.setOdfContainer(t.odfContainer);
|
||||
t.odtDocument = new ops.OdtDocument(t.odfcanvas);
|
||||
properties = new ops.MemberProperties();
|
||||
|
||||
@@ -49,7 +49,7 @@ ops.SessionTests = function SessionTests(runner) {
|
||||
t = {};
|
||||
testarea = core.UnitTest.provideTestAreaDiv();
|
||||
odfcanvas = new odf.OdfCanvas(testarea);
|
||||
odfcanvas.setOdfContainer(new odf.OdfContainer("", null));
|
||||
odfcanvas.setOdfContainer(new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null));
|
||||
t.odf = odfcanvas.odfContainer();
|
||||
};
|
||||
this.tearDown = function () {
|
||||
|
||||
@@ -381,7 +381,7 @@ ops.TransformationTests = function TransformationTests(runner) {
|
||||
i,
|
||||
op;
|
||||
|
||||
odfContainer = new odf.OdfContainer("", null);
|
||||
odfContainer = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
|
||||
t.odfcanvas.setOdfContainer(odfContainer);
|
||||
odtDocument = new ops.OdtDocument(t.odfcanvas);
|
||||
text = odtDocument.getRootNode();
|
||||
|
||||
Reference in New Issue
Block a user