added properties

This commit is contained in:
Dolan Miu
2016-04-03 20:00:30 +01:00
parent 9b6f6082f5
commit 4d8e443962
6 changed files with 240 additions and 5 deletions

View File

@ -18,6 +18,12 @@ interface DocumentAttributesProperties {
wne?: string; wne?: string;
wps?: string; wps?: string;
Ignorable?: string; Ignorable?: string;
cp?: string;
dc?: string;
dcterms?: string;
dcmitype?: string;
xsi?: string;
type?: string;
} }
export class DocumentAttributes implements XmlComponent { export class DocumentAttributes implements XmlComponent {
@ -40,7 +46,13 @@ export class DocumentAttributes implements XmlComponent {
wpi: 'xmlns:wpi', wpi: 'xmlns:wpi',
wne: 'xmlns:wne', wne: 'xmlns:wne',
wps: 'xmlns:wps', 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) { constructor(properties?: DocumentAttributesProperties) {

View File

@ -12,7 +12,7 @@ export abstract class Packer {
private style: Style; private style: Style;
private properties: Properties; private properties: Properties;
constructor(document: Document, style: Style, properties: Properties) { constructor(document: Document, style: any, properties: Properties) {
this.formatter = new Formatter(); this.formatter = new Formatter();
this.document = document; this.document = document;
this.style = style; this.style = style;

157
ts/properties/components.ts Normal file
View File

@ -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<XmlComponent>;
xmlKeys = {
revision: "dcterms:created"
}
constructor() {
super();
this.created = new Array<XmlComponent>();
this.created.push(new DocumentAttributes({
type: "dcterms:W3CDTF"
}));
this.created.push(this.getCurrentDate());
}
}
export class Modified extends DateComponent implements XmlComponent {
private modified: Array<XmlComponent>;
xmlKeys = {
modified: "dcterms:modified"
}
constructor() {
super();
this.modified = new Array<XmlComponent>();
this.modified.push(new DocumentAttributes({
type: "dcterms:W3CDTF"
}));
this.modified.push(this.getCurrentDate());
}
}

View File

@ -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<XmlComponent>;
xmlKeys = {
coreProperties: "cp:coreProperties"
}
constructor(options: PropertiesOptions) {
this.coreProperties = new Array<XmlComponent>();
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());
}
} }

View File

@ -13,7 +13,7 @@ function jsonify(obj: Object) {
return JSON.parse(stringifiedJson); return JSON.parse(stringifiedJson);
} }
describe.only("Formatter", () => { describe("Formatter", () => {
var formatter: Formatter; var formatter: Formatter;
beforeEach(() => { beforeEach(() => {

View File

@ -0,0 +1,28 @@
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
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");
});
})
});