1jQuery(document).ready(function ($) { 2 3 function penci_init_toc() { 4 5 if (typeof PenciTOC != 'undefined') { 6 7 $('article.post').each(function () { 8 var $post = $(this), 9 $sticky = $post.find('.penci-toc-container-wrapper'), 10 $stickyrStopper = $post.find('.post-entry'), 11 $stickC = $sticky.clone(); 12 13 14 $stickC.addClass('penci-sticky-toc'); 15 $stickC.find('.penci-toc-container') 16 .attr('id', 'penci-toc-sticky-container') 17 .addClass('hide-table'); 18 $stickC.find('a.penci-toc-toggle').addClass('sticky-toggle'); 19 if ($('body').hasClass('penci-aw-sticky')) { 20 $stickC.addClass('tocSticky'); 21 } 22 $stickyrStopper.append($stickC); 23 24 if (!!$sticky.offset()) { // make sure ".sticky" element exists 25 26 var asticky = $('body').hasClass('penci-aw-sticky'), 27 generalSidebarHeight = asticky ? 0 : $sticky.innerHeight(), 28 stickyTop = asticky ? 0 : $sticky.offset().top, 29 stickOffset = asticky ? 0 : 120, 30 innerset = stickOffset - generalSidebarHeight - 60, 31 outerset = stickOffset - $stickyrStopper.innerHeight() + 60; 32 33 $(window).scroll(function () { 34 var windowTop = $(window).scrollTop(), 35 36 shouldAddClass = asticky ? stickyTop > windowTop + outerset : stickyTop < windowTop + innerset && stickyTop > windowTop + outerset; 37 38 $stickC.toggleClass('tocSticky', shouldAddClass); 39 40 41 42 }); 43 44 } 45 }); 46 47 if (window.location.hash) { 48 49 var path = window.location.hash; 50 51 if (path.startsWith("#" + PenciTOC.prefix)) { 52 53 var offset = -120, 54 hash = window.location.hash.substring(1); 55 56 if (!hash) { 57 return; 58 } 59 60 var sel = '[id="' + hash + '"], a[name="' + hash + '"]'; 61 var currentOffset = $(sel).offset().top; 62 63 // smooth scroll to the anchor id 64 $('html, body').animate({ 65 scrollTop: (currentOffset + offset) + 'px' 66 }, 1000, 'swing'); 67 } 68 } 69 70 71 $.fn.shrinkTOCWidth = function () { 72 73 $(this).css({ 74 width: 'auto', 75 display: 'table' 76 }); 77 78 if (/MSIE 7\./.test(navigator.userAgent)) 79 $(this).css('width', ''); 80 }; 81 82 var smoothScroll = parseInt(PenciTOC.smooth_scroll); 83 84 if (1 === smoothScroll) { 85 86 $('a.penci-toc-link').off().on('click', function () { 87 88 var self = $(this); 89 90 var target = ''; 91 var hostname = self.prop('hostname'); 92 var pathname = self.prop('pathname'); 93 var qs = self.prop('search'); 94 var hash = self.prop('hash'); 95 96 // ie strips out the preceding / from pathname 97 if (pathname.length > 0) { 98 if (pathname.charAt(0) !== '/') { 99 pathname = '/' + pathname; 100 } 101 } 102 103 if ((window.location.hostname === hostname) && 104 (window.location.pathname === pathname) && 105 (window.location.search === qs) && 106 (hash !== '') 107 ) { 108 // var id = decodeURIComponent( hash.replace( '#', '' ) ); 109 //target = '[id="' + hash.replace('#', '') + '"]'; 110 target = $(this).attr('href'); 111 112 var parentClass; 113 if ($('.penci-single-infiscroll').length) { 114 parentClass = $(this).closest('.penci-single-block').find('#main article').attr('id'); 115 } 116 117 if (typeof parentClass !== 'undefined' && parentClass !== false) { 118 target = '#' + parentClass + ' ' + target; 119 } 120 121 // verify it exists 122 if ($(target).length === 0) { 123 console.log('PenciTOC scrollTarget Not Found: ' + target); 124 target = ''; 125 } 126 127 // check offset setting 128 if (typeof PenciTOC.scroll_offset != 'undefined') { 129 130 var offset = -1 * PenciTOC.scroll_offset; 131 132 } else { 133 134 var adminbar = $('#wpadminbar'); 135 136 if (adminbar.length > 0) { 137 138 if (adminbar.is(':visible')) 139 offset = -30; // admin bar exists, give it the default 140 else 141 offset = 0; // there is an admin bar but it's hidden, so no offset! 142 143 } else { 144 145 // no admin bar, so no offset! 146 offset = 0; 147 } 148 } 149 150 if (target) { 151 $.smoothScroll({ 152 scrollTarget: target, 153 offset: offset, 154 beforeScroll: deactivateSetActivePenciTocListElement, 155 afterScroll: function () {
156 setActivePenciTocListElement(); 157 activateSetActivePenciTocListElement(); 158 } 159 }); 160 } 161 } 162 }); 163 } 164 165 if (typeof PenciTOC.visibility_hide_by_default != 'undefined') { 166 167 $('article.post').each(function () { 168 169 var post = $(this), 170 toc = post.find('.penci-toc-container-wrapper:not(.penci-sticky-toc) ul.penci-toc-list'), 171 toggle = post.find('a.penci-toc-toggle:not(.sticky-toggle)'), 172 wrapper = post.find('.penci-toc-container-wrapper:not(.penci-sticky-toc) .penci-toc-wrapper'), 173 swrapper = post.find('.penci-sticky-toc .penci-toc-wrapper'), 174 mswrapper = post.find('.penci-sticky-toc'), 175 stoggle = post.find('.penci-sticky-toc a.sticky-toggle'), 176 stoc = post.find('.penci-sticky-toc ul.penci-toc-list'), 177 invert = PenciTOC.visibility_hide_by_default; 178 179 toggle.css('display', 'inline'); 180 stoggle.css('display', 'inline'); 181 182 if (Cookies) { 183 184 Cookies.get('penciTOC_hidetoc') == 1 ? toggle.data('visible', false) : toggle.data('visible', true); 185 186 } else { 187 188 toggle.data('visible', true); 189 } 190 191 if (invert) { 192 193 toggle.data('visible', false) 194 } 195 196 if (!toggle.data('visible')) { 197 198 toc.hide(); 199 wrapper.removeClass('show-table').addClass('hide-table'); 200 } 201 202 toggle.off().on('click', function (event) { 203 204 event.preventDefault(); 205 206 if ($(this).data('visible')) { 207 208 $(this).data('visible', false); 209 210 if (Cookies) { 211 212 if (invert) 213 Cookies.set('penciTOC_hidetoc', null, {path: '/'}); 214 else 215 Cookies.set('penciTOC_hidetoc', '1', {expires: 30, path: '/'}); 216 } 217 218 toc.hide('fast'); 219 wrapper.removeClass('show-table').addClass('hide-table'); 220 221 } else { 222 223 $(this).data('visible', true); 224 225 if (Cookies) { 226 227 if (invert) 228 Cookies.set('penciTOC_hidetoc', '1', {expires: 30, path: '/'}); 229 else 230 Cookies.set('penciTOC_hidetoc', null, {path: '/'}); 231 } 232 233 toc.show('fast'); 234 wrapper.removeClass('hide-table').addClass('show-table'); 235 236 } 237 238 }); 239 240 stoggle.data('visible', false); 241 stoc.hide(); 242 mswrapper.addClass('hide-table'); 243 244 stoggle.off().on('click', function (event) { 245 event.preventDefault(); 246 if ($(this).data('visible')) { 247 $(this).data('visible', false); 248 stoc.hide('fast'); 249 swrapper.removeClass('show-table').addClass('hide-table'); 250 mswrapper.removeClass('show-table').addClass('hide-table'); 251 } else { 252 $(this).data('visible', true); 253 stoc.show('fast'); 254 swrapper.removeClass('hide-table').addClass('show-table'); 255 mswrapper.removeClass('hide-table').addClass('show-table'); 256 } 257 }); 258 }); 259 } 260 261 // ====================================== 262 // Set active heading in penci-toc-widget list 263 // ====================================== 264 265 var getPenciTocListElementLinkByHeading = function (heading) { 266 return $('.penci-sticky-toc .penci-toc-list a[href="#' + $(heading).attr('id') + '"]'); 267 } 268 269 var getHeadingToListElementLinkMap = function (headings) { 270 return headings.reduce(function (map, heading) { 271 map[heading.id] = getPenciTocListElementLinkByHeading(heading); 272 return map; 273 }, {}); 274 } 275 276 var headings = $('span.penci-toc-section').toArray(); 277 var headingToListElementLinkMap = getHeadingToListElementLinkMap(headings); 278 var listElementLinks = $.map(headingToListElementLinkMap, function (value, key) { 279 return value 280 }); 281 282 var getScrollOffset = function () { 283 var scrollOffset = 5; // so if smooth offset is off, the correct title is set as active 284 if (typeof PenciTOC.smooth_scroll != 'undefined' && parseInt(PenciTOC.smooth_scroll) === 1) { 285 scrollOffset = (typeof PenciTOC.scroll_offset != 'undefined') ? parseInt(PenciTOC.scroll_offset) : 30; 286 } 287 288 var adminbar = $('#wpadminbar'); 289 290 if (adminbar.length) { 291 scrollOffset += adminbar.height(); 292 } 293 return scrollOffset; 294 } 295 296 var scrollOffset = getScrollOffset(); 297 298 var activateSetActivePenciTocListElement = function () { 299 if (headings.length > 0 && $('.penci-sticky-toc').length) { 300 $(window).on('load resize scroll', setActivePenciTocListElement); 301 } 302 } 303 304 activateSetActivePenciTocListElement(); 305 306 var setActivePenciTocListElement = function () { 307 var activeHeading = getActiveHeading(scrollOffset, headings); 308 if (activeHeading) { 309 var activeListElementLink = headingToListElementLinkMap[activeHeading.id]; 310 removeStyleFromNonActiveListElement(activeListElementLink, listElementLinks); 311 setStyleForActiveListElementElement(activeListElementLink); 312 } 313 } 314 315 var deactivateSetActivePenciTocListElement = function () { 316 $(window).off('load resize scroll', setActivePenciTocListElement); 317 } 318 319 320 var getActiveHeading = function (topOffset, headings) { 321 var scrollTop = $(window).scrollTop(); 322 var relevantOffset = scrollTop + topOffset + 1; 323 var activeHeading = headings[0]; 324 var closestHeadingAboveOffset = relevantOffset - $(activeHeading).offset().top;
325 headings.forEach(function (section) { 326 var topOffset = relevantOffset - $(section).offset().top; 327 if (topOffset > 0 && topOffset < closestHeadingAboveOffset) { 328 closestHeadingAboveOffset = topOffset; 329 activeHeading = section; 330 } 331 }); 332 return activeHeading; 333 } 334 335 var removeStyleFromNonActiveListElement = function (activeListElementLink, listElementLinks) { 336 listElementLinks.forEach(function (listElementLink) { 337 if (activeListElementLink !== listElementLink && listElementLink.parent().hasClass('active')) { 338 listElementLink.parent().removeClass('active'); 339 } 340 }); 341 } 342 343 var setStyleForActiveListElementElement = function (activeListElementLink) { 344 var activeListElement = activeListElementLink.parent(); 345 if (!activeListElement.hasClass('active')) { 346 activeListElement.addClass('active'); 347 } 348 } 349 } 350 } 351 352 penci_init_toc(); 353 354 $('body').on('single_loaded_more', function () { 355 penci_init_toc(); 356 }); 357});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.