https://reedgumenicklibrary.org/wp-content/plugins/trx_addons/components/shortcodes/anchor/anchor.js
1/** 2 * Shortcode Anchor 3 * 4 * @package WordPress 5 * @subpackage ThemeREX Addons 6 * @since v1.2 7 */ 8 9/* global jQuery:false */ 10/* global TRX_ADDONS_STORAGE:false */ 11 12// Init handlers 13jQuery(document).on('action.init_hidden_elements', function(e, container) { 14 15 "use strict"; 16 17 var toc_menu = jQuery('#toc_menu'); 18 if (toc_menu.length == 0) { 19 trx_addons_build_page_toc(); 20 toc_menu = jQuery('#toc_menu'); 21 if (toc_menu.length == 0) return; 22 } else if (toc_menu.hasClass('inited')) 23 return; 24 25 var toc_menu_items = toc_menu.addClass('inited').find('.toc_menu_item'); 26 27 trx_addons_detect_active_toc(); 28 29 var wheel_busy = false, wheel_time = 0; 30 31 // One page mode for menu links (scroll to anchor) 32 // Old case: toc_menu.on('click', 'a', function(e) { 33 // New case (allow add class 'toc_menu_item' in any menu to enable scroll): 34 jQuery('.toc_menu_item > a').on('click', function(e) { 35 if (trx_addons_scroll_to_anchor(jQuery(this), true)) { 36 e.preventDefault(); 37 return false; 38 } 39 }); 40 41 // Change active element then page is scrolled 42 jQuery(window).on('scroll', function() { 43 // Mark current item 44 trx_addons_mark_active_toc(); 45 }); 46 trx_addons_mark_active_toc(); 47 48 if (TRX_ADDONS_STORAGE['scroll_to_anchor'] == 1) { 49 var wheel_stop = false; 50 jQuery(document).on('action.stop_wheel_handlers', function(e) { 51 wheel_stop = true; 52 }); 53 jQuery(document).on('action.start_wheel_handlers', function(e) { 54 wheel_stop = false; 55 }); 56 jQuery(window).bind('mousewheel DOMMouseScroll', function(e) { 57 if (screen.width < 960 || jQuery(window).width() < 960 || wheel_stop || trx_addons_browser_is_ios()) { 58 return; 59 } 60 if (wheel_busy || wheel_time == e.timeStamp) { 61 e.preventDefault(); 62 return false; 63 } 64 wheel_time = e.timeStamp; 65 var wheel_dir = e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0 ? -1 : 1; 66 var items = trx_addons_detect_active_toc(); 67 var doit = false; 68 var scroll_offset = parseInt(jQuery(window).scrollTop(), 10); 69 var wh = jQuery(window).height(); 70 var ah = jQuery('#wpadminbar').length > 0 ? jQuery('#wpadminbar').height() : 0; 71 if (wheel_dir == -1) { // scroll up 72 doit = true; 73 setTimeout(function() { 74 if (items.prev >= 0 && items.prevOffset >= scroll_offset-wh-ah) 75 trx_addons_scroll_to_anchor(toc_menu_items.eq(items.prev).find('a'), false); 76 else 77 trx_addons_document_animate_to(Math.max(0, scroll_offset-wh)); 78 }, 10); 79 } else { // scroll down 80 doit = true; 81 setTimeout(function() { 82 if (items.next >= 0 && items.nextOffset <= scroll_offset+wh+ah) 83 trx_addons_scroll_to_anchor(toc_menu_items.eq(items.next).find('a'), false); 84 else 85 trx_addons_document_animate_to(Math.min(jQuery(document).height(), scroll_offset+wh)); 86 }, 10); 87 } 88 // Set busy flag while animating 89 if (doit) { 90 wheel_busy = true; 91 setTimeout(function() { wheel_busy = false; }, trx_addons_browser_is_ios() ? 1200 : 100); 92 e.preventDefault(); 93 return false; 94 } 95 }); 96 } 97 98 // Detect active TOC item 99 function trx_addons_detect_active_toc() { 100 var items = { 101 loc: '', 102 current: [], 103 prev: -1, 104 prevOffset: -1, 105 next: -1, 106 nextOffset: -1 107 }; 108 var fixed_rows_height = Math.ceil(trx_addons_fixed_rows_height()); 109 110 toc_menu_items.each(function(idx) { 111 var id = '#'+jQuery(this).data('id'); 112 var pos = id.indexOf('#'); 113 if (pos < 0 || id.length == 1) return; 114 var href = jQuery(this).find('a').attr('href'); 115 if (!trx_addons_is_local_link(href) || jQuery(id).length==0) return; 116 var off = jQuery(id).offset().top; 117 var id_next = jQuery(this).next().find('a').attr('href'); 118 var off_next = id_next && idx < toc_menu_items.length-1 && jQuery(id_next).length > 0
119 ? parseInt(jQuery(id_next).offset().top, 10) 120 : 1000000; 121 var scroll_offset = parseInt(jQuery(window).scrollTop(), 10); 122 if (off > scroll_offset + fixed_rows_height + 20) { 123 if (items.next < 0) { 124 items.next = idx; 125 items.nextOffset = off; 126 } 127 } else if (off < scroll_offset - fixed_rows_height - 20) { 128 items.prev = idx; 129 items.prevOffset = off; 130 } 131 if (off < scroll_offset + jQuery(window).height()*0.8 && scroll_offset < off_next - fixed_rows_height) { 132 items.current.push(idx); 133 if (items.loc == '') { 134 var loc = window.location.href; 135 var loc_pos = loc.indexOf('#'); 136 if (loc_pos > 0) loc = loc.substring(0, loc_pos); 137 items.loc = href.indexOf('#')==0 ? loc + id : id; 138 } 139 } 140 }); 141 return items; 142 } 143 144 // Mark active TOC item 145 function trx_addons_mark_active_toc() { 146 var items = trx_addons_detect_active_toc(); 147 toc_menu_items.removeClass('toc_menu_item_active'); 148 for (var i=0; i<items.current.length; i++) { 149 toc_menu_items.eq(items.current[i]).addClass('toc_menu_item_active'); 150 // Comment next line if on your device page jump when scrolling 151 if (items.loc !== '' && TRX_ADDONS_STORAGE['update_location_from_anchor']==1 && !trx_addons_browser_is_mobile() && !trx_addons_browser_is_ios() && !wheel_busy) 152 trx_addons_document_set_location(items.loc); 153 } 154 jQuery(document).trigger('action.toc_menu_item_active', [items.current]); 155 } 156 157 // Scroll to the anchor 158 function trx_addons_scroll_to_anchor(link_obj, click_event) { 159 var href = click_event ? link_obj.attr('href') : '#'+link_obj.parent().data('id'); 160 var pos = href.indexOf('#'); 161 if (pos >= 0 && href.length > 1 && trx_addons_is_local_link(href)) { 162 wheel_busy = true; 163 setTimeout(function() { wheel_busy = false; }, trx_addons_browser_is_ios() ? 1200 : 100); 164 trx_addons_document_animate_to(href.substr(pos), function() { 165 if (TRX_ADDONS_STORAGE['update_location_from_anchor']==1) { 166 var loc = window.location.href; 167 var loc_pos = loc.indexOf('#'); 168 if (loc_pos > 0) loc = loc.substring(0, loc_pos); 169 trx_addons_document_set_location(pos==0 ? loc + href : href); 170 } 171 }); 172 return true; 173 } 174 return false; 175 } 176 177 178 // Build page TOC from the tag's id 179 function trx_addons_build_page_toc() { 180 181 var toc = '', toc_count = 0; 182 183 jQuery('[id^="toc_menu_"],.sc_anchor').each(function(idx) { 184 var obj = jQuery(this); 185 var obj_id = obj.attr('id') || ('sc_anchor_'+Math.random()).replace('.', ''); 186 var row = obj.closest('.wpb_row,.elementor-row,.elementor-section').eq(0); 187 if (row.length == 0) row = !obj.parent().hasClass('entry-content') ? obj.parent() : obj; 188 var row_id = row.length>0 && row.attr('id') !== undefined && row.attr('id') !== '' ? row.attr('id') : ''; 189 var id = row_id || obj_id.substr(10); 190 if (row.length>0 && row_id == '') { 191 row.attr('id', id); 192 } 193 var url = obj.data('url'); 194 var icon = obj.data('vc-icon') || 'toc_menu_icon_default'; 195 var title = obj.attr('title'); 196 var description = obj.data('description'); 197 var separator = obj.data('separator'); 198 toc_count++; 199 toc += '<div class="toc_menu_item'+(separator=='yes' ? ' toc_menu_separator' : '')+'" data-id="'+id+'">' 200 + (title || description 201 ? '<a href="' + (url ? url : '#'+id) + '" class="toc_menu_description">' 202 + (title ? '<span class="toc_menu_description_title">' + title + '</span>' : '') 203 + (description ? '<span class="toc_menu_description_text">' + description + '</span>' : '') 204 + '</a>' 205 : '') 206 + '<a href="' + (url ? url : '#'+id) + '" class="toc_menu_icon '+icon+'"></a>' 207 + '</div>'; 208 }); 209 210 if (toc_count > 0) 211 jQuery('body').append('<div id="toc_menu" class="toc_menu"><div class="toc_menu_inner">'+toc+'</div></div>'); 212 } 213 214});
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.