function t431_init(recId) { var table = document.querySelector('#rec' + recId + ' .t431 .t431__table'); if (!table) return; var dataPart1 = document.querySelector('#rec' + recId + ' .t431 .t431__data-part1'); var dataPart2 = document.querySelector('#rec' + recId + ' .t431 .t431__data-part2'); var tableHead = t431__escapeHTML((dataPart1 && dataPart1.innerHTML) || ''); var tableBody = t431__escapeHTML((dataPart2 && dataPart2.innerHTML) || ''); var tableColSize = table.getAttribute('data-table-width'); var hasTargetBlank = table.getAttribute('data-target-blank'); var tHead = t431_parseData(tableHead); var tBody = t431_parseData(tableBody); var colSize = t431_parseData(tableColSize); var maxColNum = t431__findMaxRowLengthInTable(tHead, tBody); var colWidth = t431__setColumnsWidth(colSize, maxColNum, recId); var html = ''; if (tHead) { html += t431__generateTable(tHead, 'th', hasTargetBlank, colWidth, maxColNum, recId); } if (tBody) { html += t431__generateTable(tBody, 'td', hasTargetBlank, colWidth, maxColNum, recId); } table.insertAdjacentHTML('beforeend', html); } function t431__findMaxRowLengthInTable(arrayHead, arrayData) { var headMaxLength = 0; var dataMaxLength = 0; if (arrayHead) { headMaxLength = t431__findMaxRowLengInArray(arrayHead); } if (arrayData) { dataMaxLength = t431__findMaxRowLengInArray(arrayData); } if (dataMaxLength > headMaxLength) { return dataMaxLength; } else { return headMaxLength; } } function t431__escapeHTML(string) { var html = string .replace(/</g, '<') .replace(/>/g, '>') .replace(/&/g, '&') .replace(/ /g, ' '); var result = ''; var allowedTags = ''; [ 'b', 'i', 'u', 'ul', 'li', 'ol', 'br', 'img', 's', 'sub', 'sup', 'span', 'hr', 'pre', 'code', 'mark', 'strong', 'small', ].forEach(function (value) { allowedTags += ':not(' + value + ')'; }); var allowedAttrs = ['alt', 'class', 'title', 'id', 'src', 'style', 'width', 'height', 'data-replace-key']; var parseHTML = function (str) { var fakeDOM = document.implementation.createHTMLDocument('fake'); fakeDOM.body.innerHTML = str; return fakeDOM.body.childNodes; }; var createdStructure; if (parseHTML(html)) { createdStructure = parseHTML(html); } else { createdStructure = []; } for (var i = 0; i < createdStructure.length; i++) { var element = createdStructure[i]; if (element !== allowedTags) { if (element.nodeType !== 3 && element.nodeType !== 8) { var temp = document.createElement(element.tagName); allowedAttrs.forEach(function (value) { if (element.getAttribute(value) !== null) { temp.setAttribute(value, element.getAttribute(value).replace(/javascript:/gi, '')); } }); temp.textContent = element.textContent; result += temp.outerHTML; } else { result += element.textContent; } } } return result; } function t431__findMaxRowLengInArray(currentArray) { var maxLength = 0; for (var i = 0; i < currentArray.length; i++) { if (currentArray[i].length > maxLength) { maxLength = currentArray[i].length; } } return maxLength; } function t431__setColumnsWidth(colWidth, colsNumber, recId) { if (colWidth) { return colWidth[0]; } else { var tableWidth = document.querySelector('#rec' + recId + ' .t431 .t-container .t-col'); var computedWidth = parseFloat(getComputedStyle(tableWidth, null).width.replace('px', '')); return computedWidth / colsNumber + 'px'; } } function t431__generateTable(arrayValues, colTag, hasTargetBlank, colWidth, maxColNumber, recId) { const tableWrapper = document.querySelector('#rec' + recId + ' .t431__table-wrapper'); const isNofollow = tableWrapper.getAttribute('data-nofollow'); let html = ''; let tag = ''; if (colTag == 'td') { tag = 'tbody'; } else { tag = 'thead'; } html += '<' + tag + ' class="t431__' + tag + '">'; for (var i = 0; i < arrayValues.length; i++) { /*add classes for striped table*/ if (colTag == 'td') { if ((i + 1) % 2 > 0) { html += ''; } else { html += ''; } } else { html += ''; } var addingCols = 0; if (arrayValues[i].length < maxColNumber) { addingCols = maxColNumber - arrayValues[i].length; } for (var j = 0; j < arrayValues[i].length + addingCols; j++) { const item = arrayValues[i][j]; if (item) { /*define col width*/ var currentWidth = ''; if (Array.isArray(colWidth) && colWidth[j]) { currentWidth = colWidth[j].myText; } else if (!Array.isArray(colWidth)) { currentWidth = colWidth; } var colWithAttr = ''; if (colTag == 'td') { colWithAttr = ''; } else { colWithAttr = ''; } if (item.myHref) { /*define link type*/ var linkWithAttr = ''; var linkCloseTag = ''; const targetAttr = hasTargetBlank ? 'target="_blank"' : ''; if (item.myHrefType == 'link') { linkWithAttr = ''; linkCloseTag = ''; html += colWithAttr + linkWithAttr + item.myText + linkCloseTag + ''; } else { const rec = document.querySelector('#rec' + recId); const btnTemplate = rec.querySelector('.t431__btn-template'); const button = btnTemplate ? createBtnFromTemplate(btnTemplate, item, targetAttr) : createBtnDefault(item, targetAttr); html += colWithAttr + button + linkCloseTag + ''; } } else { html += colWithAttr + item.myText + ''; } } else { html += '<' + colTag + ' class="t431__' + colTag + '" ' + (currentWidth ? 'width="' + currentWidth + '' : '') + '">' + ''; } } html += ''; } html += ''; function createBtnDefault(item, targetAttr) { return `
${item.myText}
`; } function createBtnFromTemplate(btnTemplate, item, targetAttr) { const btnContainer = document.createElement('div'); const btnElement = btnTemplate.content.cloneNode(true); const btn = btnElement.querySelector('.t-btn, .t-btntext'); const btnText = btnElement.querySelector('.t-btnflex__text, .t-btntext__text'); if (btnText) btnText.textContent = item.myText; if (btn) { btn.href = item.myHref; if (targetAttr) btn.setAttribute('target', '_blank'); btnContainer.appendChild(btn); } return btnContainer.innerHTML; } return html; } function t431_parseData(data) { if (data !== '' && typeof data != 'undefined') { data = t431__addBrTag(data); var arrayTable = []; var arrayRow = []; var currentItem = { myText: '', myHref: '', myHrefType: '', }; var hasLink = ''; var hasLinkWithSpace = ''; var hasButton = ''; var hasButtonWithSpace = ''; var endLine = ''; for (var i = 0; i < data.length; i++) { /*col end and check of special symbols: «>», «<», «&» and « »*/ if ( data[i] == ';' && !( data.slice(i - 4, i) == '<' || data.slice(i - 4, i) == '>' || data.slice(i - 5, i) == '&' || data.slice(i - 6, i) == ' ' ) ) { arrayRow.push(currentItem); currentItem = { myText: '', myHref: '', }; hasLink = ''; hasLinkWithSpace = ''; hasButton = ''; hasButtonWithSpace = ''; } else { if ( hasLink == 'link=' || hasLinkWithSpace == ' link=' || hasButton == 'button=' || hasButtonWithSpace == ' button=' ) { if (currentItem.myHref === '' && hasLink === 'link=') { currentItem.myText = currentItem.myText.slice(0, -5); currentItem.myHrefType = 'link'; } else if (currentItem.myHref === '' && hasLinkWithSpace === ' link=') { currentItem.myText = currentItem.myText.slice(0, -6); currentItem.myHrefType = 'link'; } else if (currentItem.myHref === '' && hasButton === 'button=') { currentItem.myText = currentItem.myText.slice(0, -7); currentItem.myHrefType = 'btn'; } else if (currentItem.myHref === '' && hasButtonWithSpace === ' button=') { currentItem.myText = currentItem.myText.slice(0, -8); currentItem.myHrefType = 'btn'; } currentItem.myHref += data[i]; } else { currentItem.myText += data[i]; hasLink = t431__checkSubstr('link=', hasLink, data[i]); hasLinkWithSpace = t431__checkSubstr(' link=', hasLinkWithSpace, data[i]); hasButton = t431__checkSubstr('button=', hasButton, data[i]); hasButtonWithSpace = t431__checkSubstr(' button=', hasButtonWithSpace, data[i]); } endLine = t431__checkSubstr('
', endLine, data[i]); if (endLine == '
') { if (currentItem.myHref) { currentItem.myHref = currentItem.myHref.slice(0, -6); } else { currentItem.myText = currentItem.myText.slice(0, -6); } arrayRow.push(currentItem); arrayTable.push(arrayRow); currentItem = { myText: '', myHref: '', }; hasLink = ''; hasLinkWithSpace = ''; hasButton = ''; hasButtonWithSpace = ''; arrayRow = []; } } } if (arrayRow.length > 0 || currentItem.myText !== '') { if (currentItem !== '') { arrayRow.push(currentItem); } arrayTable.push(arrayRow); } } return arrayTable; } /* checking a step by step combining of t431__targetSubstr*/ function t431__checkSubstr(targetSubstr, currentSubstr, curSymbol) { if (!currentSubstr && curSymbol == targetSubstr[0]) { return curSymbol; } else if (currentSubstr) { for (var i = 0; i < targetSubstr.length - 1; i++) { if (currentSubstr[currentSubstr.length - 1] == targetSubstr[i] && curSymbol == targetSubstr[i + 1]) { return (currentSubstr += curSymbol); } } } } function t431__addBrTag(oldStringItem) { var newStringItem = ''; if (oldStringItem) { for (var i = 0; i < oldStringItem.length; i++) { if (oldStringItem[i] == '\n' || oldStringItem[i] == '\r') { newStringItem += '
'; } else { newStringItem += oldStringItem[i]; } } } return newStringItem.replace(/ /g, ' '); }