Каталог файловtilda-menu-1.1.js
1/**2 * highlight active links after page loading3 *4 * @param {string} selector - can be '.t456__list_item a'5 */6 7function t_menuBase__highlightActiveLinks(selector) {8 var url = window.location.href;9 var urlWithoutSlash;10 var path = window.location.pathname;11 12 if (url[url.length - 1] === '/') urlWithoutSlash = url.slice(0, -1);13
История файла и сравнение версий
if (path[path.length - 1] === '/') path = path.slice(0, -1);
14 if (path[0] === '/') path = path.slice(1); 15 if (path === '') path = '/'; 17 var shouldBeActiveElements = document.querySelectorAll(selector); 19 // link parameter href returns full path, but href by getAttribute returns only part, placed in href 20 // element.href = 'https://google.com/search' 21 // element.getAttribute('href') = '/search' 22 Array.prototype.forEach.call(shouldBeActiveElements, function (element) { 23 var slicedPath = element.getAttribute('href'); 24 if (!slicedPath) return; 25 var fullPath = element.href; 26 var pathWithoutFirstSlash = slicedPath[0] === '/' ? slicedPath.slice(1) : slicedPath; 28 // обработка случая, когда ссылка ведет на отдельный пост фида 30 if (path.indexOf('tpost') !== -1) { 31 tpost = '/' + path.slice(0, path.indexOf('tpost')); 38 slicedPath === path || 39 pathWithoutFirstSlash === path || 40 urlWithoutSlash === url || 41 urlWithoutSlash === path || 44 element.classList.add('t-active'); 50 * main function to update active links. 51 * In this part only find navlinks and send them to the t_menuBase__updateActiveLinks 53 * @param {string} recid 54 * @param {string} selector - can be '.t456__list_item a' 57function t_menuBase__findAnchorLinks(recid, selector) { 58 var rec = document.getElementById('rec' + recid); 60 if (!t_menuBase__isBlockVisible(rec)) return; 61 var anchorSelector = selector + '[href*="#"]:not(.tooltipstered)'; 62 var navLinks = rec ? rec.querySelectorAll(anchorSelector) : []; 63 if (navLinks.length) t_menuBase__updateActiveLinks(navLinks, selector); 67 * update active links by scroll 69 * @param {any} navLinks 70 * @param {string} selector 72function t_menuBase__updateActiveLinks(navLinks, selector) { 73 var menuClassName = selector.slice(2); 74 menuClassName = parseInt(menuClassName, 10); 75 menuClassName = '.t' + menuClassName; 76 navLinks = Array.prototype.slice.call(navLinks); 77 var clickedSectionID = null; 79 var sectionToNavigationLinkID = {}; 80 navLinks = navLinks.reverse(); 82 navLinks.forEach(function (link) { 83 var currentSection = t_menuBase__getSectionByHref(link); 84 if (currentSection && currentSection.id) { 85 sections.push(currentSection); 86 sectionToNavigationLinkID[currentSection.id] = link; 90 t_menuBase__updateSectionsOffsets(sections); 92 sections.sort(function (a, b) { 93 var firstTopOffset = parseInt(a.getAttribute('data-offset-top'), 10) || 0; 94 var secondTopOffset = parseInt(b.getAttribute('data-offset-top'), 10) || 0; 95 return secondTopOffset - firstTopOffset; 98 window.addEventListener( 100 t_throttle(function () {