diff --git a/webodf/lib/odf/OdfContainer.js b/webodf/lib/odf/OdfContainer.js index 6b16ef29..e3bc09ed 100644 --- a/webodf/lib/odf/OdfContainer.js +++ b/webodf/lib/odf/OdfContainer.js @@ -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 +}; diff --git a/webodf/tests/odf/LayoutTests.js b/webodf/tests/odf/LayoutTests.js index fce2a8f8..b060f6e1 100644 --- a/webodf/tests/odf/LayoutTests.js +++ b/webodf/tests/odf/LayoutTests.js @@ -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; diff --git a/webodf/tests/odf/OdfContainerTests.js b/webodf/tests/odf/OdfContainerTests.js index 6e03b399..c383d35b 100644 --- a/webodf/tests/odf/OdfContainerTests.js +++ b/webodf/tests/odf/OdfContainerTests.js @@ -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 ]); }; diff --git a/webodf/tests/ops/OperationTests.js b/webodf/tests/ops/OperationTests.js index 753aebc5..7b70c487 100644 --- a/webodf/tests/ops/OperationTests.js +++ b/webodf/tests/ops/OperationTests.js @@ -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(); diff --git a/webodf/tests/ops/SessionTests.js b/webodf/tests/ops/SessionTests.js index a56cd0bc..fa2a9d88 100644 --- a/webodf/tests/ops/SessionTests.js +++ b/webodf/tests/ops/SessionTests.js @@ -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 () { diff --git a/webodf/tests/ops/TransformationTests.js b/webodf/tests/ops/TransformationTests.js index 83b0ba4c..5df0e817 100644 --- a/webodf/tests/ops/TransformationTests.js +++ b/webodf/tests/ops/TransformationTests.js @@ -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();