function Printer(serverData){ // dekoduje data data = JsonPack.decodeData(serverData); // selector na DOM element this.DOM = document.getElementById('printdocument'); // auxiliary data this.auxiliary = data.auxiliary; // objekt subjekt this.subject = new ObjectList().addObjects(data.subject.objects, data.subject.virtuals, data.auxiliary).getObjectByIndex(0); // objekt dokument this.document = new ObjectList().addObjects(data.document.objects, data.document.virtuals, data.auxiliary).getObjectByIndex(0); // adresa this.address = new ObjectList().addObjects(data.address.objects, data.address.virtuals, data.auxiliary).getObjectByIndex(0); // list sekci this.sections = new ObjectList().addObjects(data.sections.objects, data.sections.virtuals, data.auxiliary); // tiskova data this.printNodes = this.createPrintNodes(this.document, 1); } Printer.prototype.createPrintNodes = function(object, hlevel){ var result = { }; var id = object.getId(); var type = object.isInCategory(17) ? 'document' : 'section'; // dokument if(type === 'document'){ result = { node : 'document', name : this.document.getAttributeView(3).value || '', subject : this.subject.getAttributeView(3).value || '', logo : this.subject.getAttributeView(4).value || '', address : this.address ? (this.address.getAttributeView(133).value || '') : '', order : 0, children : [] }; } // sekce else{ // format var format = object.getAttributeView(534).raw; result = { node : 'section', format : format, order : object.getAttributeView(533).value || 0, values : this.createValuesNode(object, hlevel), children : [] }; if(format === 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 printNode = this.createPrintNodes(item, hlevel); // pokud nema zadny format, preskoci if(!printNode.format) continue; // prida do potomku nody result.children.push(printNode); } // seradi potomky result.children.sort(function(a, b) { return a.order - b.order; }); return result; } Printer.prototype.createValuesNode = function(object, hlevel){ // format sekce var format = object.getAttributeView(534).raw; // pokud je sekce nadpis if(format === 1524) return { headline : { value : object.getAttributeView(530).value || '', level : hlevel } }; // pokud je sekce cokoli jineho // vysledek var result = { }; // NADPIS respektive PODNADPIS var subheadline = object.getAttributeView(544).value; if(subheadline) result.subheadline = subheadline; // TEXT var text = object.getAttributeView(530).value; if(text) result.text = text; // OBRAZEK var picture = object.getAttributeView(531).value; if(picture) result.picture = picture; // 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){ result.enum = { type : enumType === 1540 ? 'desc' : 'numeric' }; // vyhleda prvni sloupec var column = this.auxiliary.attrtypes[543]['columns'][0]; result.enum.items = multiattr.reduce(function(arr, curr){ return curr[column] === null ? arr : [...arr, curr[column]]; }, []); } // 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 result.table = multiattr.reduce(function(arr, curr){ var row = usedColumns.map(function(key){ return curr[key]; }); return [...arr, row]; }, []); } } return result; }; Printer.prototype.render = function(){ document.title = this.printNodes.name; history.replaceState(history.state, '', '/'); this.DOM.appendChild(this.renderNode(this.printNodes)); window.print(); return this; }; Printer.prototype.renderNode = function(node){ var self = this; // vytvori elemnt nody var element = document.createElement('div'); element.setAttribute('a-node', node.node); // DOKUMENT if(node.node === 'document'){ element.className = 'document'; // hlavicka var header = document.createElement('div'); element.appendChild(header); header.className = 'header'; header.innerHTML = '
'+node.subject+'
'+node.address+'
'; } // POKUD JDE O SECTION if(node.node === 'section'){ element.className = 'section'; // NADPIS if(node.values.headline){ element.appendChild(this.createHtmlHeadline(node.values.headline)); } // PODNADPIS if(node.values.subheadline){ element.appendChild(this.createHtmlSubheadline(node.values.subheadline)); } // TEXT if(node.values.text){ element.appendChild(this.createHtmlText(node.values.text)); } // TABLE if(node.values.table){ element.appendChild(this.createHtmlTable(node.values.table)); } // VYCET if(node.values.enum){ element.appendChild(this.createHtmlEnum(node.values.enum)); } // PICTURE if(node.values.picture){ element.appendChild(this.createHtmlPicture(node.values.picture)); } } // vyhleda vnorene nody node.children.forEach(function(item){ element.appendChild(self.renderNode(item)); }); return element; } // html pro nadpis Printer.prototype.createHtmlHeadline = function(headline){ // element var element = document.createElement('h'+headline.level); element.className = 'section_headline'; element.innerHTML = headline.value; return element; } // html pro podnadpis Printer.prototype.createHtmlSubheadline = function(text){ // element var element = document.createElement('div'); element.className = 'section_subheadline'; element.innerHTML = text; return element; } // html pro text Printer.prototype.createHtmlText = function(text){ // element var element = document.createElement('div'); element.className = 'section_text'; element.innerHTML = text; return element; } Printer.prototype.createHtmlTable = function(table){ // element var element = document.createElement('table'); element.className = 'section_table'; // vlozi radky 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; } // html vycet Printer.prototype.createHtmlEnum = function(data){ // 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; } // html pro picture Printer.prototype.createHtmlPicture = function(url){ // element var element = document.createElement('img'); element.className = 'section_picture'; element.src = url; return element; }