all files / es6/ docxtemplater.js

90.36% Statements 75/83
81.25% Branches 13/16
100% Functions 16/16
90.36% Lines 75/83
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140        65×     65× 65× 65×     12× 12×     77× 77× 231× 231×   77× 12×   77×     64×     64× 64× 64×     80× 80× 80×     32× 32×     76× 76× 76×     31× 31× 150×   31× 162×   31×         31× 162×   31×   31× 162×       31× 80× 80×       31× 162×     31× 80× 80× 80× 80× 80× 80×     31×         31×     30× 30×         105× 105×     105×     105× 315×   105× 105× 105×     25×            
"use strict";
 
const DocUtils = require("./doc-utils");
DocUtils.traits = require("./traits");
DocUtils.moduleWrapper = require("./module-wrapper");
const wrapper = DocUtils.moduleWrapper;
 
const Docxtemplater = class Docxtemplater {
	constructor() {
		Iif (arguments.length > 0) {
			throw new Error("The constructor with parameters have been removed in docxtemplater 3.0, please check the upgrade guide.");
		}
		this.compiled = {};
		this.modules = [];
		this.setOptions({});
	}
	attachModule(module) {
		this.modules.push(wrapper(module));
		return this;
	}
	setOptions(options) {
		this.options = options;
		Object.keys(DocUtils.defaults).forEach((key) => {
			const defaultValue = DocUtils.defaults[key];
			this[key] = (this.options[key] != null) ? this.options[key] : defaultValue;
		});
		if (this.zip) {
			this.updateFileTypeConfig();
		}
		return this;
	}
	loadZip(zip) {
		Iif (zip.loadAsync) {
			throw new Error("Docxtemplater doesn't handle JSZip version >=3, see changelog");
		}
		this.zip = zip;
		this.updateFileTypeConfig();
		return this;
	}
	compileFile(fileName) {
		const currentFile = this.createTemplateClass(fileName);
		currentFile.parse();
		this.compiled[fileName] = currentFile;
	}
	compile() {
		this.templatedFiles = this.fileTypeConfig.getTemplatedFiles(this.zip);
		return this;
	}
	updateFileTypeConfig() {
		this.fileType = this.zip.files["word/document.xml"] ? "docx" : "pptx";
		this.fileTypeConfig = this.options.fileTypeConfig || Docxtemplater.FileTypeConfig[this.fileType];
		return this;
	}
	render() {
		this.options.xmlFileNames = [];
		this.modules = this.fileTypeConfig.baseModules.map(function (moduleFunction) {
			return moduleFunction();
		}).concat(this.modules);
		this.options = this.modules.reduce((options, module) => {
			return module.optionsTransformer(options, this);
		}, this.options);
		this.xmlDocuments = this.options.xmlFileNames.reduce((xmlDocuments, fileName) => {
			const content = this.zip.files[fileName].asText();
			xmlDocuments[fileName] = DocUtils.str2xml(content);
			return xmlDocuments;
		}, {});
		this.modules.forEach((module) => {
			module.set({zip: this.zip, xmlDocuments: this.xmlDocuments, data: this.data});
		});
		this.compile();
 
		this.modules.forEach((module) => {
			module.set({compiled: this.compiled});
		});
		// Loop inside all templatedFiles (ie xml files with content).
		// Sometimes they don't exist (footer.xml for example)
		this.templatedFiles.forEach((fileName) => {
			Eif (this.zip.files[fileName] != null) {
				this.compileFile(fileName);
			}
		});
 
		this.mapper = this.modules.reduce(function (value, module) {
			return module.getRenderedMap(value);
		}, {});
 
		Object.keys(this.mapper).forEach((to) => {
			const mapped = this.mapper[to];
			const from = mapped.from;
			const currentFile = this.compiled[from];
			currentFile.setTags(mapped.data);
			currentFile.render(to);
			this.zip.file(to, currentFile.content);
		});
 
		Object.keys(this.xmlDocuments).forEach((fileName) => {
			this.zip.remove(fileName);
			const content = DocUtils.xml2str(this.xmlDocuments[fileName]);
			return this.zip.file(fileName, content, {});
		});
		return this;
	}
	setData(data) {
		this.data = data;
		return this;
	}
	getZip() {
		return this.zip;
	}
	createTemplateClass(path) {
		const usedData = this.zip.files[path].asText();
		return this.createTemplateClassFromContent(usedData, path);
	}
	createTemplateClassFromContent(content, filePath) {
		const xmltOptions = {
			filePath,
		};
		Object.keys(DocUtils.defaults).forEach((key) => {
			xmltOptions[key] = this[key];
		});
		xmltOptions.fileTypeConfig = this.fileTypeConfig;
		xmltOptions.modules = this.modules;
		return new Docxtemplater.XmlTemplater(content, xmltOptions);
	}
	getFullText(path) {
		return this.createTemplateClass(path || this.fileTypeConfig.textPath).getFullText();
	}
	getTemplatedFiles() {
		this.compile();
		return this.templatedFiles;
	}
};
 
Docxtemplater.DocUtils = require("./doc-utils");
Docxtemplater.Errors = require("./errors");
Docxtemplater.XmlTemplater = require("./xml-templater");
Docxtemplater.FileTypeConfig = require("./file-type-config");
Docxtemplater.XmlMatcher = require("./xml-matcher");
module.exports = Docxtemplater;