Changeset 3050050
- Timestamp:
- 03/12/2024 08:41:43 PM (21 months ago)
- Location:
- hash-link-scroll-offset
- Files:
-
- 17 added
- 4 edited
-
tags/0.3.0 (added)
-
tags/0.3.0/assets (added)
-
tags/0.3.0/assets/js (added)
-
tags/0.3.0/assets/js/hash-link-scroll-offset.js (added)
-
tags/0.3.0/assets/js/hash-link-scroll-offset.min.asset.php (added)
-
tags/0.3.0/assets/js/hash-link-scroll-offset.min.js (added)
-
tags/0.3.0/assets/js/src (added)
-
tags/0.3.0/assets/js/src/hash-link-scroll-offset.js (added)
-
tags/0.3.0/hash-link-scroll-offset.php (added)
-
tags/0.3.0/languages (added)
-
tags/0.3.0/languages/hash_link_scroll_offset.pot (added)
-
tags/0.3.0/readme.txt (added)
-
tags/0.3.0/src (added)
-
tags/0.3.0/src/hash-link-scroll-offset.js (added)
-
trunk/assets/js/hash-link-scroll-offset.js (modified) (5 diffs)
-
trunk/assets/js/hash-link-scroll-offset.min.asset.php (added)
-
trunk/assets/js/hash-link-scroll-offset.min.js (modified) (1 diff)
-
trunk/hash-link-scroll-offset.php (modified) (9 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/src (added)
-
trunk/src/hash-link-scroll-offset.js (added)
Legend:
- Unmodified
- Added
- Removed
-
hash-link-scroll-offset/trunk/assets/js/hash-link-scroll-offset.js
r2668460 r3050050 1 /*! Hash Link Scroll Offset - v0.1.8 - 2022-01-28 1 /** 2 * Hash Link Scroll Offset 2 3 * http://webdevstudios.com 3 * Copyright (c) 2022; * Licensed GPLv2+ */ 4 * 5 * Copyright (c) 2014 WebDevStudios 6 * Licensed under the GPLv2+ license. 7 */ 8 4 9 /*jslint browser: true */ 5 /*global jQuery:false */6 10 7 11 window.Hash_Link_Scroll_Offset = window.Hash_Link_Scroll_Offset || {}; 8 12 9 ( function ( window, document, $, app ){13 ( function ( window, document, app ) { 10 14 'use strict'; 11 15 … … 20 24 } 21 25 22 app.init = function() { 23 26 app.init = function () { 24 27 app.offset = app.getOffset(); 25 28 26 29 // cache jQuery selector results 27 app. $html_and_body = $('html, body');30 app.htmlAndBody = document.querySelectorAll( 'html, body' ); 28 31 29 $( 'a[href*="#"]:not(.no-scroll)' ).on( 'click', function( evt ) { 30 app.hash = this.hash; 32 const checkScroll = document.querySelectorAll( 'a:not(.no-scroll)' ); 33 checkScroll.forEach( function ( a ) { 34 a.addEventListener( 'click', function ( evt ) { 35 app.hash = this.hash; 36 if ( ! app.hash ) { 37 return; 38 } 39 app.scrollToHash( app.hash, evt ); 40 } ); 41 } ); 31 42 32 if ( ! $( app.hash ) ) { 43 if ( app.initialScroll ) { 44 app.hash = window.location.hash; 45 const elementToScrollTo = app.getHashElement( app.hash ); 46 47 if ( ! elementToScrollTo ) { 33 48 return; 34 49 } 35 50 36 app.scrollToHash( app.hash, evt ); 37 }); 38 39 if ( app.initialScroll ) { 40 app.hash = window.location.hash; 41 var $element_to_scroll_to = app.getHashElement( app.hash ); 42 43 if ( ! $element_to_scroll_to ) { 44 return; 45 } 46 47 setTimeout( function() { 51 setTimeout( function () { 48 52 app.isScrolling = true; 49 53 window.scrollTo( 0, 0 ); 50 app.scrollToHash( $element_to_scroll_to );54 app.scrollToHash( elementToScrollTo ); 51 55 }, 10 ); 52 56 } 53 54 57 }; 55 58 56 app.getOffset = function () {57 var offset = window.hlso_offset ? window.hlso_offset.offset : 0;59 app.getOffset = function () { 60 let offset = typeof hlsOffset !== 'undefined' ? hlsOffset.offset : 0; // eslint-disable-line no-undef 58 61 59 if ( $( '#wpadminbar' ).length) {62 if ( document.getElementById( 'wpadminbar' ) ) { 60 63 offset = ( parseInt( offset, 10 ) + 32 ).toString(); 61 64 } … … 64 67 }; 65 68 66 app.getHashElement = function ( hash ) {67 var isEl = ( hash instanceof jQuery );69 app.getHashElement = function ( hash ) { 70 const isEl = typeof hash === 'object'; 68 71 69 72 // Check if linking to ID 70 var $element_to_scroll_to = isEl ? hash : $( hash ); 73 let elementToScrollTo = isEl 74 ? hash 75 : document.getElementById( hash.substr( 1 ) ); 71 76 72 77 // If not.. 73 if ( ! $element_to_scroll_to.length&& ! isEl ) {78 if ( ! elementToScrollTo && ! isEl ) { 74 79 // Check if linking to a named anchor 75 $element_to_scroll_to = $( '[name="' + hash.substr(1) + '"]' ); 80 elementToScrollTo = document.querySelector( 81 '[name="' + hash.substr( 1 ) + '"]' 82 ); 76 83 } 77 84 78 if ( ! $element_to_scroll_to.length) {85 if ( ! elementToScrollTo ) { 79 86 return false; 80 87 } 81 88 82 if ( $element_to_scroll_to.hasClass( '.no-scroll' ) || $element_to_scroll_to.parents( '.no-scroll-wrap' ).length ) { 89 if ( 90 elementToScrollTo.classList.contains( 'no-scroll' ) || 91 elementToScrollTo.closest( '.no-scroll-wrap' ) 92 ) { 83 93 return false; 84 94 } 85 95 86 return $element_to_scroll_to;96 return elementToScrollTo; 87 97 }; 88 98 89 app.scrollToHash = function( hash, evt ) { 90 var $element_to_scroll_to = hash instanceof jQuery ? hash : app.getHashElement( hash ); 99 app.scrollToHash = function ( hash, evt ) { 100 const elementToScrollTo = 101 typeof hash === 'object' ? hash : app.getHashElement( hash ); 91 102 92 if ( ! $element_to_scroll_to || ! $element_to_scroll_to.length) {103 if ( ! elementToScrollTo ) { 93 104 return; 94 105 } … … 96 107 app.isScrolling = true; 97 108 98 app.scrollTo = $element_to_scroll_to.offset().top - app.offset; 109 app.scrollTo = 110 elementToScrollTo.getBoundingClientRect().top - app.offset; 99 111 100 app.$html_and_body.trigger( 'hash_link_scroll_offset.scroll_to', app.scrollTo ); 112 const event = new Event( 'hash_link_scroll_offset.scroll_to' ); 113 app.htmlAndBody.forEach( function ( el ) { 114 el.dispatchEvent( event ); 115 } ); 101 116 102 117 app.scroll( app.scrollTo ); … … 106 121 window.location.hash = app.hash; 107 122 } 108 109 123 }; 110 124 111 app.scroll = function( scrollTo ) { 112 app.$html_and_body.stop().animate({ 113 'scrollTop': scrollTo 114 }, 900, 'swing', function( evt ) { 115 app.initialScroll = app.isScrolling = false; 116 app.$html_and_body.trigger( 'hash_link_scroll_offset.complete', evt ); 125 app.scroll = function ( scrollTo ) { 126 window.scrollTo( { 127 top: scrollTo, 128 behavior: 'smooth', 129 } ); 130 app.initialScroll = app.isScrolling = false; 131 const event = new Event( 'hash_link_scroll_offset.complete' ); 132 133 app.htmlAndBody.forEach( function ( el ) { 134 el.dispatchEvent( event ); 117 135 } ); 118 136 }; 119 137 120 $( app.init);138 app.init(); 121 139 122 140 return app; 123 124 } )( window, document, jQuery, window.Hash_Link_Scroll_Offset ); 141 } )( window, document, window.Hash_Link_Scroll_Offset ); -
hash-link-scroll-offset/trunk/assets/js/hash-link-scroll-offset.min.js
r2668460 r3050050 1 /*! Hash Link Scroll Offset - v0.1.8 - 2022-01-28 2 * http://webdevstudios.com 3 * Copyright (c) 2022; * Licensed GPLv2+ */ 4 window.Hash_Link_Scroll_Offset=window.Hash_Link_Scroll_Offset||{},function(a,b,c,d){"use strict";d.scrollTo=0,d.initialScroll=!1,d.isScrolling=!1,d.hash=null,a.location.hash&&(d.initialScroll=!0),d.init=function(){if(d.offset=d.getOffset(),d.$html_and_body=c("html, body"),c('a[href*="#"]:not(.no-scroll)').on("click",function(a){d.hash=this.hash,c(d.hash)&&d.scrollToHash(d.hash,a)}),d.initialScroll){d.hash=a.location.hash;var b=d.getHashElement(d.hash);if(!b)return;setTimeout(function(){d.isScrolling=!0,a.scrollTo(0,0),d.scrollToHash(b)},10)}},d.getOffset=function(){var b=a.hlso_offset?a.hlso_offset.offset:0;return c("#wpadminbar").length&&(b=(parseInt(b,10)+32).toString()),b},d.getHashElement=function(a){var b=a instanceof jQuery,d=b?a:c(a);return d.length||b||(d=c('[name="'+a.substr(1)+'"]')),!!d.length&&(!d.hasClass(".no-scroll")&&!d.parents(".no-scroll-wrap").length&&d)},d.scrollToHash=function(b,c){var e=b instanceof jQuery?b:d.getHashElement(b);e&&e.length&&(d.isScrolling=!0,d.scrollTo=e.offset().top-d.offset,d.$html_and_body.trigger("hash_link_scroll_offset.scroll_to",d.scrollTo),d.scroll(d.scrollTo),c&&c.preventDefault&&(c.preventDefault(),a.location.hash=d.hash))},d.scroll=function(a){d.$html_and_body.stop().animate({scrollTop:a},900,"swing",function(a){d.initialScroll=d.isScrolling=!1,d.$html_and_body.trigger("hash_link_scroll_offset.complete",a)})},c(d.init)}(window,document,jQuery,window.Hash_Link_Scroll_Offset); 1 window.Hash_Link_Scroll_Offset=window.Hash_Link_Scroll_Offset||{},function(t,o,l){"use strict";l.scrollTo=0,l.initialScroll=!1,l.isScrolling=!1,l.hash=null,t.location.hash&&(l.initialScroll=!0),l.init=function(){if(l.offset=l.getOffset(),l.htmlAndBody=o.querySelectorAll("html, body"),o.querySelectorAll("a:not(.no-scroll)").forEach((function(t){t.addEventListener("click",(function(t){l.hash=this.hash,l.hash&&l.scrollToHash(l.hash,t)}))})),l.initialScroll){l.hash=t.location.hash;const o=l.getHashElement(l.hash);if(!o)return;setTimeout((function(){l.isScrolling=!0,t.scrollTo(0,0),l.scrollToHash(o)}),10)}},l.getOffset=function(){let t="undefined"!=typeof hlsOffset?hlsOffset.offset:0;return o.getElementById("wpadminbar")&&(t=(parseInt(t,10)+32).toString()),t},l.getHashElement=function(t){const l="object"==typeof t;let n=l?t:o.getElementById(t.substr(1));return n||l||(n=o.querySelector('[name="'+t.substr(1)+'"]')),!!n&&!n.classList.contains("no-scroll")&&!n.closest(".no-scroll-wrap")&&n},l.scrollToHash=function(o,n){const s="object"==typeof o?o:l.getHashElement(o);if(!s)return;l.isScrolling=!0,l.scrollTo=s.getBoundingClientRect().top-l.offset;const e=new Event("hash_link_scroll_offset.scroll_to");l.htmlAndBody.forEach((function(t){t.dispatchEvent(e)})),l.scroll(l.scrollTo),n&&n.preventDefault&&(n.preventDefault(),t.location.hash=l.hash)},l.scroll=function(o){t.scrollTo({top:o,behavior:"smooth"}),l.initialScroll=l.isScrolling=!1;const n=new Event("hash_link_scroll_offset.complete");l.htmlAndBody.forEach((function(t){t.dispatchEvent(n)}))},l.init()}(window,document,window.Hash_Link_Scroll_Offset); -
hash-link-scroll-offset/trunk/hash-link-scroll-offset.php
r3030666 r3050050 1 <?php 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName 2 2 /** 3 3 * Plugin Name: Hash Link Scroll Offset 4 4 * Plugin URI: http://webdevstudios.com 5 5 * Description: Offset the scroll position of anchored links. Handy if you have a sticky header that covers linked material. 6 * Version: 0. 2.26 * Version: 0.3.0 7 7 * Author: WebDevStudios 8 8 * Author URI: http://webdevstudios.com … … 38 38 class Hash_Link_Scroll_Offset { 39 39 40 const VERSION = '0. 2.1';40 const VERSION = '0.3.0'; 41 41 42 42 /** … … 46 46 * @var string 47 47 */ 48 public static $url = '';48 public static $url = ''; 49 49 50 50 /** … … 72 72 // Useful variables. 73 73 self::$url = trailingslashit( plugin_dir_url( __FILE__ ) ); 74 self::$path = trailingslashit( dirname( __FILE__ ));74 self::$path = trailingslashit( __DIR__ ); 75 75 self::$name = __( 'Hash Link Scroll Offset', 'hash_link_scroll_offset' ); 76 76 } … … 109 109 update_option( 'hash_link_scroll_offset', 0 ); 110 110 } 111 add_option( 'hash_link_scroll_offset_msg', 1, null, 'no' );111 add_option( 'hash_link_scroll_offset_msg', 1, '', 'no' ); 112 112 } 113 113 … … 122 122 } 123 123 delete_option( 'hash_link_scroll_offset_msg' ); 124 // translators: %s is the name of the plugin. 124 125 $settings_link = sprintf( '<a href="%s">%s</a>', $this->settings_url(), sprintf( __( 'update the "%s" setting', 'hash_link_scroll_offset' ), self::$name ) ); 125 echo ' 126 <div id="message" class="updated"> 127 <p>' . sprintf( __( 'The "%s" plugin has been activated. Please %s.', 'hash_link_scroll_offset' ), self::$name, $settings_link ) . '</p> 128 </div> 129 '; 126 127 echo wp_kses_post( 128 '<div id="message" class="updated"> 129 <p>' . /* translators: %1$s is the name of the plugin, %2$s is a link to the settings page. */ sprintf( __( 'The "%1$s" plugin has been activated. Please %2$s.', 'hash_link_scroll_offset' ), self::$name, $settings_link ) . '</p> 130 </div>' 131 ); 130 132 } 131 133 … … 189 191 </style> 190 192 <?php endif; ?> 191 <div class="hash_link_scroll_offset_setting_wrap<?php echo $class; ?>">192 <input class="small-text" placeholder="50" type="number" step="1" min="1" id="hash_link_scroll_offset" name="hash_link_scroll_offset" value="<?php echo get_option( 'hash_link_scroll_offset', 0); ?>"> <?php esc_html_e( 'pixels', 'hash_link_scroll_offset' ); ?>193 <div class="hash_link_scroll_offset_setting_wrap<?php echo esc_attr( $class ); ?>"> 194 <input class="small-text" placeholder="50" type="number" step="1" min="1" id="hash_link_scroll_offset" name="hash_link_scroll_offset" value="<?php echo esc_attr( get_option( 'hash_link_scroll_offset', 0 ) ); ?>"> <?php esc_html_e( 'pixels', 'hash_link_scroll_offset' ); ?> 193 195 </div> 194 196 <p class="description"><?php esc_html_e( 'When the Admin Bar is displayed in your theme, this value is automatically increased by 32px.', 'hash_link_scroll_offset' ); ?></p> … … 203 205 public function enqueue_js() { 204 206 $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 205 wp_enqueue_script( 'hash_link_scroll_offset', self::$url . "assets/js/hash-link-scroll-offset$min.js", [ 'jquery' ], self::VERSION, true ); 206 wp_localize_script( 'hash_link_scroll_offset', 'hlso_offset', [ 'offset' => get_option( 'hash_link_scroll_offset', 0 ) ] ); 207 208 // Automatically load imported dependencies and assets version. 209 $asset_file = require plugin_dir_path( __FILE__ ) . 'assets/js/hash-link-scroll-offset.min.asset.php'; 210 211 wp_register_script( 212 'hash_link_scroll_offset', 213 plugins_url( "assets/js/hash-link-scroll-offset$min.js", __FILE__ ), 214 $asset_file['dependencies'], 215 $asset_file['version'], 216 true 217 ); 218 wp_enqueue_script( 'hash_link_scroll_offset' ); 219 wp_add_inline_script( 'hash_link_scroll_offset', 'const hlsOffset = ' . wp_json_encode( [ 'offset' => get_option( 'hash_link_scroll_offset', 0 ) ] ) . ';', 'before' ); 207 220 } 208 221 … … 217 230 return admin_url( 'options-general.php?hash_link_scroll_offset' ); 218 231 } 219 220 232 } 221 233 222 234 // Init our class. 223 $Hash_Link_Scroll_Offset = new Hash_Link_Scroll_Offset(); 224 $Hash_Link_Scroll_Offset->hooks(); 225 235 $hash_link_scroll_offset = new Hash_Link_Scroll_Offset(); 236 $hash_link_scroll_offset->hooks(); -
hash-link-scroll-offset/trunk/readme.txt
r3030666 r3050050 4 4 Tags: 5 5 Requires at least: 5.5 6 Tested up to: 6. 4.37 Stable tag: 0. 2.26 Tested up to: 6.5.0 7 Stable tag: 0.3.0 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 34 34 35 35 == Changelog == 36 37 = 0.3.0 = 38 * Updated: Removed jQuery dependency in our javascript 39 * Updated: Escaping and sanitizing of output. 36 40 37 41 = 0.2.2 =
Note: See TracChangeset
for help on using the changeset viewer.