/** * Файл создает страницу со списоком всех курсов и закрытых страниц доступных пользователю */ /* exported tma__resources__initPagesList */ // TODO ST-1047 Удалить старый файл для отображения списка ресурсов /** * Устанавливает список доступных ресурсво пользователю */ function tma__resources__initPagesList() { var rootElem = tma__checkContainerDiv('tlk-resources'); var objProfile = tma__getProfileObjFromLS(); var style; if (window.tildaMembers.settingStyles) { style = window.tildaMembers.settingStyles.welcomepage; } // Есть пользователи у которых в homepage лежит строка 0 if ( objProfile.courses.length === 0 && objProfile.groups.length === 1 && (objProfile.groups[0].homepage || objProfile.groups[0].homepage !== '0') && objProfile.groups[0].index ) { // TODO: удалить фолбэк после обновления tilda-members-init.min.js if (typeof tma__getGroupPageUrl === 'function') { document.location.replace(tma__getGroupPageUrl(objProfile.groups[0].index) || '/members/'); } else { document.location.replace('/' + objProfile.groups[0].index); } return; } tma__onFuncLoad('tma__userbar__drawWidget', function () { tma__userbar__drawWidget(); }); tma__resources__addHeaderFooter(); tma__resources__createStyles(); var html = tma__resources__getPagesListHtml(objProfile, style); rootElem.innerHTML = tma__translate(html, 'resources_dict'); document.title = tma__translate('{{resources_title_page}}', 'resources_dict'); if (style && style.backgroundColor && style.backgroundImage) { rootElem.insertAdjacentHTML('afterend', '
'); } tma__resources__initEvents(); var urlParams = new URLSearchParams(window.location.search); if (urlParams.has('need_email')) { var redirectUrl = urlParams.get('need_email'); if (redirectUrl) { tma__resources__drawCheckEmailPopup(redirectUrl); } } } function tma__resources__initEvents() { var checkEmailBeforeEnterLinks = document.querySelectorAll('.js-check-email-before-enter'); checkEmailBeforeEnterLinks.forEach(function (link) { link.addEventListener('click', function (event) { if (!window.mauser.email) { var url = link.getAttribute('href'); tma__resources__drawCheckEmailPopup(url); event.preventDefault(); } }); }); } /** * Рисует попап для ввода email и редиректит после успеха на заданный url * @param {string} url Ссылка для редиректа */ function tma__resources__drawCheckEmailPopup(url) { var html = ''; html += '
{{check_email_title}}
'; html += '
'; html += '
'; html += '
'; html += '
{{check_email_text}}
'; html += ''; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += ''; html += '
'; html += '
'; html = tma__translate(html, 'resources_dict'); tma__drawPopup({ id: 'check-email-popup', content: html, className: 'tlk-popup_check-email', closeBtns: [], }); var form = document.getElementById('check-email-form'); form.addEventListener('submit', function (event) { event.preventDefault(); tma__profile__submitEditProfile(form, { onSuccess: function () { // TODO: удалить фолбэк после обновления tilda-members-init.min.js if (typeof tma__getSafeRedirectUrl === 'function') { var safeUrl = tma__getSafeRedirectUrl(url); if (safeUrl) { window.location.assign(safeUrl); } } else if (url) { window.location.assign(url); } }, }); }); } /** * Шаблон списка страниц * * @param {object} props - данные доступных ресурсов * @param {object} style - данные стилей * @returns {string} - список страниц */ function tma__resources__getPagesListHtml(props, style) { var html = ''; html += '
'; if (style && (!style.typeId || style.typeId === 'custom') && style.textTop) { html += '
' + style.textTop + '
'; } html += '
'; if (props.courses.length === 0 && props.groups.length === 0) { html += '
'; html += '
'; html += '
'; html += ''; html += ''; html += ''; html += ''; html += ''; html += '
'; html += '
'; html += '{{resources_empty_text_first}}
{{resources_empty_text_second}}'; html += '
'; html += '
'; html += '
'; } else { if (props.courses.length !== 0) { html += '
'; for (var i = 0; i < props.courses.length; i++) { html += tma__resources__getCourseItemHtml(props.courses[i]); } html += '
'; } if (props.groups.length !== 0) { html += '
'; for (var i = 0; i < props.groups.length; i++) { html += tma__resources__getGroupItemHtml(props.groups[i]); } html += '
'; } } html += '
'; if (style && (!style.typeId || style.typeId === 'custom') && style.textBottom) { html += '
' + style.textBottom + '
'; } html += '
'; return html; } /** * Шаблон элемента курса * * @param {object} props - данные страницы курса * @returns {string} - шаблон страницы курса */ function tma__resources__getCourseItemHtml(props) { var html = ''; html += '
'; html += '
'; html += ''; html += props.name; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += '
'; html += '
'; return html; } /** * Шаблон элемента группы * * @param {object} props - данные страницы группы * @returns {string} - шаблон страницы группы */ function tma__resources__getGroupItemHtml(props) { var html = ''; html += '
'; html += '
'; if (props.homepage && props.index) { // TODO: удалить фолбэк после обновления tilda-members-init.min.js var groupPageUrl = typeof tma__getGroupPageUrl === 'function' ? tma__getGroupPageUrl(props.index) || '/members/' : '/' + props.index; html += ''; html += props.name; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; } else { html += props.name; } html += props.expired ? ' ({{resources_title_group_to}} ' + tma__getformatDate(props.expired) + ')' : ''; html += '
'; // TODO: есть пользователи у которых в homepage лежит строка 0 if ( (props.pages.length > 0 || props.docs.length > 0) && (!props.homepage || props.homepage === '0') && !props.index ) { html += '
'; for (var i = 0; i < props.pages.length; i++) { var page = props.pages[i]; html += '
'; html += ''; html += ''; html += ''; html += '' + page.title + ''; html += '
'; } for (var i = 0; i < props.docs.length; i++) { var docs = props.docs[i]; html += '
'; html += ''; html += ''; html += ''; html += '' + docs.title + ''; html += '
'; } html += '
'; } html += '
'; return html; } /** * Добавляет header/footer на страницу * * @returns {void} */ function tma__resources__addHeaderFooter() { var settings = window.tildaMembers.settingStyles; var header; var footer; if (settings && !settings.elements) return; if (settings.elements.header) header = settings.elements.header; if (settings.elements.footer) footer = settings.elements.footer; document.body.classList.add('t-body'); var allRecords = document.getElementById('allrecords'); if (header && header.body) { tma__resources__loadHeaderFooterFiles(header); allRecords.insertAdjacentHTML('afterbegin', header.body.replace('id="allrecords"', 'id="t-header"')); var main = document.getElementById('t-header'); tma__resources__addAttributeAllrecords(main); tma__resources__addScripts(main); } if (footer && footer.body) { tma__resources__loadHeaderFooterFiles(footer); allRecords.insertAdjacentHTML('beforeend', footer.body.replace('id="allrecords"', 'id="t-footer"')); var main = document.getElementById('t-footer'); tma__resources__addAttributeAllrecords(main); tma__resources__addScripts(main); } if (typeof tma__userbar__hideWidgetButton === 'function') { var userBar = document.querySelector('.tlk-userbar'); tma__userbar__hideWidgetButton(userBar); } if (typeof tma__userbar__handleWidgetsUserbar === 'function') { tma__userbar__handleWidgetsUserbar(); } if (typeof tma__userbar__addWidgetsPositions === 'function') { tma__userbar__addWidgetsPositions(); } } /** * Добавляет скрипты на страницу от header/footer * * @param {object} element - данные скриптов */ function tma__resources__loadHeaderFooterFiles(element) { if (element.files) { if (element.files.css) { for (var i = 0; i < element.files.css.length; i++) { var css = element.files.css[i]; tma__loadCSS(css.from); } } if (element.files.js) { for (var i = 0; i < element.files.js.length; i++) { var js = element.files.js[i]; tma__loadJS(js.from); } } } } /** * Добавляет данные атрибутов с header/footer в allrecords * * @param {HTMLElement} main - элемент в котором необходимо искать скрипты */ function tma__resources__addAttributeAllrecords(main) { var allRecords = document.getElementById('allrecords'); for (var i = 0; i < main.attributes.length; i++) { var attr = main.attributes[i]; if ( attr.name !== 'id' && attr.name !== 'class' && attr.name !== 'data-tilda-project-id' && attr.name !== 'data-tilda-page-id' ) { allRecords.setAttribute(attr.name, attr.value); } } } /** * Добавляет скрипты на страницу и запускает их * * @param {HTMLElement} main - элемент в котором необходимо искать скрипты */ function tma__resources__addScripts(main) { var scripts = main.querySelectorAll('script'); for (var i = 0; i < scripts.length; i++) { var oldScript = scripts[i]; var newScript = document.createElement('script'); var src = ''; for (var j = 0; j < oldScript.attributes.length; j++) { var attr = oldScript.attributes[j]; if (attr.name === 'src') { src = attr.value; break; } } if (src && oldScript.innerHTML.length === 0) { tma__loadJS(src); } else { newScript.innerHTML = oldScript.innerHTML; oldScript.parentNode.appendChild(newScript); setTimeout(function () { oldScript.remove(); }, 500); } } } /** * Создает стили для кастомизации индексной страницы * * @returns {string} - стили страницы */ function tma__resources__createStyles() { var styles = window.tildaMembers.settingStyles; var mainPage = styles.welcomepage; if (!mainPage || !mainPage.typeId || mainPage.typeId !== 'custom') return; var textStyle = ''; textStyle += '.tlk-resources__container {\n'; textStyle += ' max-width: 1199px;\n'; textStyle += '}\n'; textStyle += '[data-field="tlk-link"] svg, .tlk-resources__groups-pages-item-icon {\n'; textStyle += ' display: none'; textStyle += '}\n'; if (mainPage.textMargin) { var margin = Number(mainPage.textMargin.replace(/[^0-9]/g, '')); textStyle += '.tlk-resources__item + .tlk-resources__item {\n'; textStyle += ' margin-top: ' + mainPage.textMargin + ';\n'; textStyle += '}\n'; textStyle += '.tlk-resources__wrap > div + div {\n'; textStyle += ' margin-top: ' + margin * 2 + 'px;\n'; textStyle += '}\n'; textStyle += '.tlk-resources__groups-pages {\n'; textStyle += ' margin-top: ' + margin * 0.4 + 'px;\n'; textStyle += '}\n'; textStyle += '.tlk-resources__groups-pages-item + .tlk-resources__groups-pages-item {\n'; textStyle += ' margin-top: ' + margin * 0.24 + 'px;\n'; textStyle += '}\n'; } if (mainPage.backgroundColor || mainPage.backgroundImage) { textStyle += '#app {\n'; if (mainPage.backgroundColor && !mainPage.backgroundImage) { textStyle += ' background-color: rgba(' + tma__resources__hexToRgb(mainPage.backgroundColor).join(', ') + ', ' + tma__resources__getOpacity(mainPage.backgroundColorOpacity) + ');\n'; } if (mainPage.backgroundImage) { textStyle += 'background-image: url(' + mainPage.backgroundImage + ');\n'; if (mainPage.useBackgroundPattern) { textStyle += 'background-size: auto;\n'; textStyle += 'background-repeat: repeat;\n'; } else { textStyle += 'background-position: center;\n'; textStyle += 'background-size: cover;\n'; textStyle += 'background-repeat: no-repeat;\n'; } } textStyle += '}\n'; } if (mainPage.backgroundColor && mainPage.backgroundImage) { textStyle += '.tlk-app-bg {\n'; textStyle += ' background-color: rgba(' + tma__resources__hexToRgb(mainPage.backgroundColor).join(', ') + ', ' + tma__resources__getOpacity(mainPage.backgroundColorOpacity) + ');\n'; textStyle += '}\n'; } textStyle += '.tlk-resources__wrap {\n'; textStyle += ' min-height: 100%;\n'; textStyle += ' padding: 60px;\n'; if (mainPage.boxBackgroundColor || mainPage.boxBackgroundColorOpacity) { var boxBackgroundColor = mainPage.boxBackgroundColor ? tma__resources__hexToRgb(mainPage.boxBackgroundColor).join(', ') : '255, 255, 255'; textStyle += ' background-color: rgba(' + boxBackgroundColor + ', ' + tma__resources__getOpacity(mainPage.boxBackgroundColorOpacity) + ');\n'; } if (mainPage.boxRadius) { textStyle += ' border-radius: ' + mainPage.boxRadius + ';\n'; } if (mainPage.boxShadowColor || mainPage.boxShadowOpacity) { var boxShadowColor = mainPage.boxShadowColor ? tma__resources__hexToRgb(mainPage.boxShadowColor).join(', ') : '255, 255, 255'; var boxShadowColorOpacity = tma__resources__getOpacity(mainPage.boxShadowOpacity); textStyle += ' -webkit-box-shadow: 0 0 10px 0 rgba(' + boxShadowColor + ', ' + boxShadowColorOpacity + ');\n'; textStyle += ' box-shadow: 0 0 10px 0 rgba(' + boxShadowColor + ', ' + boxShadowColorOpacity + ');\n'; textStyle += ' -webkit-box-sizing: border-box;\n'; } if (mainPage.textAlign === 'center') { textStyle += ' text-align: center;\n'; } else if (mainPage.textAlign === 'right') { textStyle += ' text-align: right;\n'; } if (mainPage.textSize) { textStyle += ' font-size: ' + mainPage.textSize + ';\n'; } textStyle += '}\n'; if (mainPage.textSizeMobile) { textStyle += '@media screen and (max-width: 767px) {\n'; textStyle += ' .tlk-resources__wrap {\n'; textStyle += ' font-size: ' + mainPage.textSizeMobile + ';\n'; textStyle += ' }\n'; textStyle += '}\n'; } if (mainPage.textColor) { textStyle += '#allrecords [data-field="tlk-link"], [data-field="tlk-link"], .tlk-resources__title, .tlk-resources__empty-text {\n'; textStyle += ' color: ' + mainPage.textColor + ';\n'; textStyle += '}\n'; textStyle += '.tlk-resources__title span {\n'; textStyle += ' color: rgba(' + tma__resources__hexToRgb(mainPage.textColor).join(', ') + ', 0.6);\n'; textStyle += '}\n'; textStyle += '.tlk-resources__empty-image line, .tlk-resources__empty-image polyline {\n'; textStyle += ' stroke: ' + mainPage.textColor + ';\n'; textStyle += '}\n'; } if (mainPage.textHoverColor) { textStyle += '#allrecords [data-field="tlk-link"]:hover, [data-field="tlk-link"]:hover {\n'; textStyle += ' color: ' + mainPage.textHoverColor + ';\n'; textStyle += '}\n'; } document.head.insertAdjacentHTML( 'beforeend', '' ); } /** * Конвертирует hex на rgb цвет * * @param {string} hex - цвет в кодировке hex * @returns {array} - цвет в кодировке rgb */ function tma__resources__hexToRgb(hex) { var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; var newHex = hex.replace(shorthandRegex, function (m, r, g, b) { return r + r + g + g + b + b; }); var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(newHex); var aaa = result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16), } : {}; return result ? [aaa.r, aaa.g, aaa.b] : []; } /** * Возвращает значение прозрачности * * @param {string} opacity - значение opacity * @returns {number} */ function tma__resources__getOpacity(opacity) { if (opacity && opacity !== '0') { return +opacity / 100; } else if (opacity === '0') { return 0; } else { return 1; } }