docxtemplater-link-module-free
Version:
Open Source Link Module for docxtemplater 3.x
94 lines (85 loc) • 4.03 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var DocUtils = require('docxtemplater').DocUtils;
var rels = {
getPrefix: function getPrefix(fileType) {
return fileType === 'docx' ? 'word' : 'ppt';
},
getFileTypeName: function getFileTypeName(fileType) {
return fileType === 'docx' ? 'document' : 'presentation';
},
getRelsFileName: function getRelsFileName(fileName) {
return fileName.replace(/^.*?([a-zA-Z0-9]+)\.xml$/, '$1') + '.xml.rels';
},
getRelsFilePath: function getRelsFilePath(fileName, fileType) {
var relsFileName = rels.getRelsFileName(fileName);
var prefix = fileType === 'pptx' ? 'ppt/slides' : 'word';
return prefix + '/_rels/' + relsFileName;
}
};
module.exports = function () {
function LinkManager(zip, fileName, xmlDocuments, fileType) {
_classCallCheck(this, LinkManager);
this.nbLinks = 1;
this.fileName = fileName;
this.prefix = rels.getPrefix(fileType);
this.zip = zip;
this.xmlDocuments = xmlDocuments;
this.fileTypeName = rels.getFileTypeName(fileType);
var relsFilePath = rels.getRelsFilePath(fileName, fileType);
this.relsDoc = xmlDocuments[relsFilePath] || this.createEmptyRelsDoc(xmlDocuments, relsFilePath);
}
_createClass(LinkManager, [{
key: 'createEmptyRelsDoc',
value: function createEmptyRelsDoc(xmlDocuments, relsFileName) {
var mainRels = this.prefix + '/_rels/' + this.fileTypeName + '.xml.rels';
var doc = xmlDocuments[mainRels];
if (!doc) {
var err = new Error('Could not copy from empty relsdoc');
err.properties = {
mainRels: mainRels,
relsFileName: relsFileName,
files: Object.keys(this.zip.files)
};
throw err;
}
var relsDoc = DocUtils.str2xml(DocUtils.xml2str(doc));
var relationships = relsDoc.getElementsByTagName('Relationships')[0];
var relationshipChilds = relationships.getElementsByTagName('Relationship');
for (var i = 0, l = relationshipChilds.length; i < l; i++) {
relationships.removeChild(relationshipChilds[i]);
}
xmlDocuments[relsFileName] = relsDoc;
return relsDoc;
}
}, {
key: 'loadLinkRels',
value: function loadLinkRels() {
var iterable = this.relsDoc.getElementsByTagName('Relationship');
return Array.prototype.reduce.call(iterable, function (max, relationship) {
var id = relationship.getAttribute('Id');
if (/^rId[0-9]+$/.test(id)) {
return Math.max(max, parseInt(id.substr(3), 10));
}
return max;
}, 0);
}
}, {
key: 'addLinkRels',
value: function addLinkRels(linkValue) {
var relationships = this.relsDoc.getElementsByTagName('Relationships')[0];
var relationshipChilds = relationships.getElementsByTagName('Relationship');
var newTag = this.relsDoc.createElement('Relationship');
newTag.namespaceURI = null;
var rId = 'link_generated_' + (relationshipChilds.length + 1);
newTag.setAttribute('Id', rId);
newTag.setAttribute('Type', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink');
newTag.setAttribute('Target', linkValue);
newTag.setAttribute('TargetMode', 'External');
relationships.appendChild(newTag);
return rId;
}
}]);
return LinkManager;
}();