function Printer(options){ // set options this.auxiliary = options.auxiliary; this.DOMElement = options.DOMElement; this.doc = options.doc; this.sections = options.sections; this.attachments = options.conn_objs; this.conn = options.conn; this.personId = options.personId; this.personName = options.personName; this.personAddress = options.personAddress; this.personWeb = options.personWeb; this.personEmail = options.personEmail; this.personTel = options.personTel; this.personIco = options.personIco; this.docId = options.docId; this.docName = options.docName; this.docDesc = options.docDesc; this.docFormat = options.docFormat; this.docDate = options.docDate; this.subjectName = options.subjectName; this.subjectAddress = options.subjectAddress; this.subjectLogo = options.subjectLogo; // create document nodes this.documentNodes = this.createNodes(this.doc, 1); // print document this.printDocument(); } Printer.prototype.createNodes = function(object, hlevel){ var result = { }; var id = object.getId(); var node = object.isInCategory(17) ? 'document' : 'section'; if(node == 'document') result = { node : node, children : [ ] }; else result = { node : node, children : [ ], format : object.getAttributeView(534).raw, order : object.getAttributeView(533).value || 0, render : this.createRenderData(object, hlevel) }; if(object.getAttributeView(534).raw == 1524) hlevel++; // vyhleda potomky for(var i = 0; i < this.sections.getCountOfObjects(); i++){ var item = this.sections.getObjectByIndex(i); // pokud nepatri pod parenta if(item.parents[3] != id) continue; // data pro tisk nody var nodeData = this.createNodes(item, hlevel); // pokud nema zadny format, preskoci if(!nodeData.format) continue; // prida do potomku nody result.children.push(nodeData); } // seradi potomky result.children.sort(function(a, b) { return a.order - b.order; }); return result; } Printer.prototype.createRenderData = function(object, hlevel){ // result var result = []; // format sekce var format = object.getAttributeView(534).raw; var attachments = this.searchAttachments(object.getId()); // NADPIS if(format == 1524){ // POKUD JDE O DOKUMENT FORMATU PROJEKTOVA DOKUMENTACE A NAPIS JE PRVNI UROVNE if( (this.docFormat == 1543 || this.docFormat == 1547) && hlevel == 1){ var contact = [ ]; if(this.personTel) contact.push(this.personTel); if(this.personWeb) contact.push(this.personWeb); if(this.personEmail) contact.push(this.personEmail); var docDate = new Date(this.docDate); var dd = docDate.getDate(); var mm = docDate.getMonth() + 1; var yyyy = docDate.getFullYear(); result.push({ element : 'header1543', subject : this.personName, address : this.personAddress, contact : contact, ico : this.personIco, docName : this.docName, docDesc : this.docDesc, docDate : this.auxiliary.attrtypes[143]['at_name']+': '+dd+'.'+mm+'.'+yyyy, sectionName : object.getAttributeView(530).value || '', sectionNumber : object.getAttributeView(133).value || '', sectionDesc : object.getAttributeView(53).value || '' }); } result.push({ element : 'headline', level : hlevel, value : object.getAttributeView(530).value, attachments : attachments }); return result; } // pokud je cokoli jineho // PODNADPIS var subheadline = object.getAttributeView(544).value; if(subheadline) result.push({ element : 'subheadline', value : subheadline, attachments : attachments }); // TEXT var text = object.getAttributeView(530).value; if(text) result.push({ element : 'text', value : text, attachments : attachments }); // OBRAZEK var picture = object.getAttributeView(531).value; if(picture) result.push({ element : 'picture', url : picture, attachments : attachments }); // MULTIATTR var multiattr = object.getAttributeView(543).value; var enumType = object.getAttributeView(545).raw; // 1540 - vycet odrazky // 1542 - vycet ciselny // 1541 - tabulka // pokud je alespon jeden radek if(Array.isArray(multiattr)){ // VYCTY if(enumType == 1540 || enumType == 1542){ // vyhleda prvni sloupec var column = this.auxiliary.attrtypes[543]['columns'][0]; // polozky var items = multiattr.reduce(function(arr, curr){ return curr[column] == null ? arr : [...arr, curr[column]]; }, []); result.push({ element : 'list', type : enumType == 1540 ? 'desc' : 'numeric', items : items, attachments : attachments }); } // TABULKA else{ // vyhleda pouzite sloupce var usedColumns = multiattr.reduce(function(acc, curr_row){ var usedColumnsInRow = [ ]; for(var i in curr_row){ if(curr_row[i] != null) usedColumnsInRow.push(parseInt(i)); } return [...acc,...usedColumnsInRow].filter(function(value, index, arr){ return arr.indexOf(value) == index}) }, [ ]); // vyhleda definovane poradi sloupcu var orderColumns = this.auxiliary.attrtypes[543]['columns']; // seradi pouzite sloupce podle definice var usedColumns = orderColumns.filter(function(item){ return usedColumns.indexOf(item) >= 0 }); // sestavi tabulku pouzitych sloupcu var values = multiattr.reduce(function(arr, curr){ var row = usedColumns.map(function(key){ return curr[key]; }); return [...arr, row]; }, []); result.push({ element : 'table', table : values, attachments : attachments }); } } return result; } Printer.prototype.printDocument = function(){ // set page title document.title = this.docName; // set fake url history.replaceState(history.state, '', '/'); // header var header = this.createHeader(); if(header) this.DOMElement.appendChild(header); // insert html this.DOMElement.appendChild(this.printNode(this.documentNodes)); // print window.print(); return this; } Printer.prototype.printNode = function(node){ var self = this; // vytvori element nody var element = document.createElement('div'); element.setAttribute('a-node', node.node); element.className = 'section'; // render elements if(node.render) node.render.forEach(function(item){ var renderElement = self.createHtmlElement(item); if(renderElement) element.appendChild(renderElement); var renderAttachments = self.createHTMLAttachments(item.attachments) if(renderAttachments) element.appendChild(renderAttachments); }); // vyhleda vnorene nody node.children.forEach(function(item){ element.appendChild(self.printNode(item)); }); return element; }; Printer.prototype.createHtmlElement = function(data){ console.log(data) switch(data.element){ case 'headline' : { // element var element = document.createElement('h'+data.level); element.className = 'section_headline'; element.innerHTML = data.value; return element; } case 'subheadline' : { // element var element = document.createElement('div'); element.className = 'section_subheadline'; element.innerHTML = data.value; return element; } case 'text' : { // element var element = document.createElement('div'); element.className = 'section_text'; element.innerHTML = data.value; return element; } case 'picture' : { // element var element = document.createElement('img'); element.className = 'section_picture'; element.src = data.url; return element; } case 'list' : { // element var element = document.createElement(data.type == 'numeric' ? 'ol' : 'ul'); element.className = 'section_enum'; // vlozi radky data.items.forEach(function(value){ // item var item = document.createElement('li'); item.className = 'section_enum__item'; item.innerHTML = value; element.appendChild(item); }); return element; } case 'table' : { // element var element = document.createElement('table'); element.className = 'section_table'; // vlozi radky data.table.forEach(function(row){ var rowElement = document.createElement('tr'); rowElement.className = 'section_table__tr'; element.appendChild(rowElement); row.forEach(function(cell){ // item var cellElement = document.createElement('td'); cellElement.className = 'section_table__td'; cellElement.innerHTML = cell; rowElement.appendChild(cellElement); }); }); return element; } case 'header1543' : { var element = document.createElement('div'); element.className = 'section_header1543'; element.innerHTML = '
'+( this.subjectLogo ? '' : '' )+'
'+data.subject+'
'+data.address+'
'+data.contact.join(', ')+'
'+(data.ico ? 'IČO '+data.ico : '')+'
'+data.docName+'
'+data.sectionName+'
'+data.sectionNumber+'
'+data.sectionDesc+'
'+data.docDate+'
'; return element; } } } Printer.prototype.createHTMLAttachments = function(data){ if(!Array.isArray(data) || !data.length) return null; var element = document.createElement('div'); element.innerHTML = '
Přílohy:
'; element.className = 'section_attachments'; data.forEach(function(item){ var linkElement = document.createElement('a'); linkElement.text = item.name; linkElement.href = item.url; linkElement.className = 'section_attachments_link'; linkElement.setAttribute('target','_blank'); element.appendChild(linkElement); }); return element; } Printer.prototype.searchAttachments = function(id){ var self = this; var result = []; this.conn.connections.forEach(function(item){ var o = null; if(item.o1 == id) o = item.o2; if(item.o2 == id) o = item.o1; var obj = self.attachments.getObjectById(o); if(obj) result.push({ id : obj.getId(), type : obj.getAttributeView(5).value, name : obj.getAttributeView(3).value, url : obj.getAttributeView(4).value }); }); return result; } Printer.prototype.createHeader = function(){ if(this.docFormat != 1538 && this.docFormat != 1547) return null; var contact = [ ]; if(this.personTel) contact.push(this.personTel); if(this.personWeb) contact.push(this.personWeb); if(this.personEmail) contact.push(this.personEmail); var docDate = new Date(this.docDate); var dd = docDate.getDate(); var mm = docDate.getMonth() + 1; var yyyy = docDate.getFullYear(); var element = document.createElement('div'); element.className = 'documentreport'; element.innerHTML = '
'+( this.subjectLogo ? '' : '' )+'
'+this.subjectName+'
'+this.subjectAddress+'
'+contact.join(', ')+'
'+(this.personIco ? 'IČO '+this.personIco : '')+'
'+this.docName+'
'+this.docDesc+'
'+this.auxiliary.attrtypes[143]['at_name']+': '+dd+'.'+mm+'.'+yyyy+'
'; return element; }