From 4d8e4439625580d63daf407f0cac4f1d747b7da0 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Sun, 3 Apr 2016 20:00:30 +0100 Subject: [PATCH] added properties --- ts/docx/xml-components/document-attributes.ts | 14 +- ts/export/packer/packer.ts | 2 +- ts/properties/components.ts | 157 ++++++++++++++++++ ts/properties/index.ts | 42 ++++- ts/tests/formatterTest.ts | 2 +- ts/tests/propertiesTest.ts | 28 ++++ 6 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 ts/properties/components.ts create mode 100644 ts/tests/propertiesTest.ts diff --git a/ts/docx/xml-components/document-attributes.ts b/ts/docx/xml-components/document-attributes.ts index 49a6c13108..a68991c59a 100644 --- a/ts/docx/xml-components/document-attributes.ts +++ b/ts/docx/xml-components/document-attributes.ts @@ -18,6 +18,12 @@ interface DocumentAttributesProperties { wne?: string; wps?: string; Ignorable?: string; + cp?: string; + dc?: string; + dcterms?: string; + dcmitype?: string; + xsi?: string; + type?: string; } export class DocumentAttributes implements XmlComponent { @@ -40,7 +46,13 @@ export class DocumentAttributes implements XmlComponent { wpi: 'xmlns:wpi', wne: 'xmlns:wne', wps: 'xmlns:wps', - Ignorable: 'mc:Ignorable' + Ignorable: 'mc:Ignorable', + cp: 'xmlns:cp', + dc: 'xmlns:dc', + dcterms: 'xmlns:dcterms', + dcmitype: 'xmlns:dcmitype', + xsi: 'xmlns:xsi', + type: 'xsi:type' }; constructor(properties?: DocumentAttributesProperties) { diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index 159f8bc595..6c03cb6011 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -12,7 +12,7 @@ export abstract class Packer { private style: Style; private properties: Properties; - constructor(document: Document, style: Style, properties: Properties) { + constructor(document: Document, style: any, properties: Properties) { this.formatter = new Formatter(); this.document = document; this.style = style; diff --git a/ts/properties/components.ts b/ts/properties/components.ts new file mode 100644 index 0000000000..c91a3790b5 --- /dev/null +++ b/ts/properties/components.ts @@ -0,0 +1,157 @@ +import {XmlComponent} from "../docx/xml-components"; +import {DocumentAttributes} from "../docx/xml-components/document-attributes"; + +abstract class Component { + protected createNullBlockOrValue(value: string): Object { + if (value === undefined) { + return [{}]; + } else { + return value; + } + } +} +export class Title extends Component implements XmlComponent { + private title: Object; + + xmlKeys = { + title: "dc:title" + } + + constructor(value: string) { + super(); + var title = this.createNullBlockOrValue(value); + this.title = title; + } +} + +export class Subject extends Component implements XmlComponent { + private subject: Object; + + xmlKeys = { + subject: "dc:subject" + } + + constructor(value: string) { + super(); + var subject = this.createNullBlockOrValue(value); + this.subject = subject; + } +} + +export class Creator extends Component implements XmlComponent { + private creator: Object; + + xmlKeys = { + creator: "dc:creator" + } + + constructor(value: string) { + super(); + var creator = this.createNullBlockOrValue(value); + this.creator = creator; + } +} + +export class Keywords extends Component implements XmlComponent { + private keywords: Object; + + xmlKeys = { + keywords: "dc:keywords" + } + + constructor(value: string) { + super(); + var keywords = this.createNullBlockOrValue(value); + this.keywords = keywords; + } +} + +export class Description extends Component implements XmlComponent { + private description: Object; + + xmlKeys = { + description: "dc:description" + } + + constructor(value: string) { + super(); + var description = this.createNullBlockOrValue(value); + this.description = description; + } +} + +export class LastModifiedBy extends Component implements XmlComponent { + private lastModifiedBy: Object; + + xmlKeys = { + lastModifiedBy: "dc:lastModifiedBy" + } + + constructor(value: string) { + super(); + var lastModifiedBy = this.createNullBlockOrValue(value); + this.lastModifiedBy = lastModifiedBy; + } +} + +export class Revision extends Component implements XmlComponent { + private revision: Object; + + xmlKeys = { + revision: "dc:revision" + } + + constructor(value: string) { + super(); + var revision = this.createNullBlockOrValue(value); + this.revision = revision; + } +} + +abstract class DateComponent { + protected getCurrentDate(): any { + var date = new Date(), + year = date.getFullYear(), + month = ('0' + (date.getMonth() + 1)).slice(-2), + day = ('0' + date.getDate()).slice(-2), + hours = ('0' + date.getHours()).slice(-2), + minutes = ('0' + date.getMinutes()).slice(-2), + seconds = ('0' + date.getSeconds()).slice(-2); + + return year + '-' + month + '-' + day + 'T' + hours + ':' + minutes + ':' + seconds + 'Z'; + } +} + +export class Created extends DateComponent implements XmlComponent { + private created: Array; + + xmlKeys = { + revision: "dcterms:created" + } + + constructor() { + super(); + this.created = new Array(); + this.created.push(new DocumentAttributes({ + type: "dcterms:W3CDTF" + })); + this.created.push(this.getCurrentDate()); + } +} + +export class Modified extends DateComponent implements XmlComponent { + private modified: Array; + + xmlKeys = { + modified: "dcterms:modified" + } + + constructor() { + super(); + this.modified = new Array(); + this.modified.push(new DocumentAttributes({ + type: "dcterms:W3CDTF" + })); + this.modified.push(this.getCurrentDate()); + } +} \ No newline at end of file diff --git a/ts/properties/index.ts b/ts/properties/index.ts index ef40cc5b5f..251b83404e 100644 --- a/ts/properties/index.ts +++ b/ts/properties/index.ts @@ -1,3 +1,41 @@ -export class Properties { - +import {XmlComponent} from "../docx/xml-components"; +import {DocumentAttributes} from "../docx/xml-components/document-attributes"; +import {Title, Subject, Creator, Keywords, Description, LastModifiedBy, Revision, Created, Modified} from "./components"; + +interface PropertiesOptions { + title?: string; + subject?: string; + creator?: string; + keywords?: string; + description?: string; + lastModifiedBy?: string; + revision?: string; +} + +export class Properties implements XmlComponent { + private coreProperties: Array; + + xmlKeys = { + coreProperties: "cp:coreProperties" + } + + constructor(options: PropertiesOptions) { + this.coreProperties = new Array(); + this.coreProperties.push(new DocumentAttributes({ + cp: "http://schemas.openxmlformats.org/package/2006/metadata/core-properties", + dc: "http://purl.org/dc/elements/1.1/", + dcterms: "http://purl.org/dc/terms/", + dcmitype: "http://purl.org/dc/dcmitype/", + xsi: "http://www.w3.org/2001/XMLSchema-instance" + })); + this.coreProperties.push(new Title(options.title)); + this.coreProperties.push(new Subject(options.subject)); + this.coreProperties.push(new Creator(options.creator)); + this.coreProperties.push(new Keywords(options.keywords)); + this.coreProperties.push(new Description(options.description)); + this.coreProperties.push(new LastModifiedBy(options.lastModifiedBy)); + this.coreProperties.push(new Revision(options.revision)); + this.coreProperties.push(new Created()); + this.coreProperties.push(new Modified()); + } } \ No newline at end of file diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index bfc60de749..9b24e750b6 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -13,7 +13,7 @@ function jsonify(obj: Object) { return JSON.parse(stringifiedJson); } -describe.only("Formatter", () => { +describe("Formatter", () => { var formatter: Formatter; beforeEach(() => { diff --git a/ts/tests/propertiesTest.ts b/ts/tests/propertiesTest.ts new file mode 100644 index 0000000000..9b304254d4 --- /dev/null +++ b/ts/tests/propertiesTest.ts @@ -0,0 +1,28 @@ +/// +/// +import {Properties} from "../properties"; +import {assert} from "chai"; + +function jsonify(obj: Object) { + var stringifiedJson = JSON.stringify(obj); + return JSON.parse(stringifiedJson); +} + +describe("Properties", () => { + var properties: Properties; + + beforeEach(() => { + + }); + + describe("#constructor()", () => { + it("should create properties with a title", () => { + properties = new Properties({ + title: "test document" + }); + var newJson = jsonify(properties); + + assert(newJson.coreProperties[1].title === "test document"); + }); + }) +}); \ No newline at end of file