Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Reading Progress Bar #896

Open
wants to merge 13 commits into
base: release-candidate
Choose a base branch
from
1 change: 1 addition & 0 deletions inc/class-header-footer-elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ public function includes() {

// Load the widgets.
require HFE_DIR . 'inc/widgets-manager/class-widgets-loader.php';
require HFE_DIR . 'inc/widgets-manager/class-extensions-loader.php';
}

/**
Expand Down
48 changes: 48 additions & 0 deletions inc/js/progress-bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(function (jQuery) {
jQuery(document).ready(function () {
// Function to calculate the scroll progress
function updateProgressBar() {
var scrollTop = jQuery(window).scrollTop(); // Current scroll position
var windowHeight = jQuery(window).height(); // Height of the window
var documentHeight = jQuery(document).height(); // Total height of the document

// Calculate the scroll percentage
var scrollPercent = (scrollTop / (documentHeight - windowHeight)) * 100;

// Log the values for debugging
console.log('Scroll Top:', scrollTop);
console.log('Window Height:', windowHeight);
console.log('Document Height:', documentHeight);
console.log('Scroll Percent:', scrollPercent + '%');

// Update the width of the progress bar
jQuery('.hfe-reading-progress-fill').css('width', scrollPercent + '%');
console.log('Progress bar updated to:', scrollPercent + '%');
}

// Initial call
updateProgressBar();

// Call the function on scroll
jQuery(window).on('scroll', function () {
console.log('Scroll event detected'); // Log when the user scrolls
updateProgressBar();
});

// Set CSS variables for colors
function setProgressBarColors(bgColor, fillColor) {
jQuery('.hfe-reading-progress').css('--progress-bar-bg-color', bgColor);
jQuery('.hfe-reading-progress-fill').css('--progress-bar-fill-color', fillColor);
console.log('Progress bar colors set: BG Color:', bgColor, 'Fill Color:', fillColor);
}

// Example: Set colors from user settings (replace these with actual settings)
const userSettings = {
bgColor: '#e0e0e0', // Replace with actual value from settings
fillColor: '#1fd18e' // Replace with actual value from settings
};

setProgressBarColors(userSettings.bgColor, userSettings.fillColor);
});
})(jQuery);

30 changes: 30 additions & 0 deletions inc/widgets-css/progress-bar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.hfe-reading-progress-wrap {
position: fixed;
z-index: 9999;
left: 0;
width: 100%;
transition: top 0.3s ease, bottom 0.3s ease;
}

/* Positioning options: */
.hfe-reading-progress-wrap[data-position="top"] {
top: 0;
}

.hfe-reading-progress-wrap[data-position="bottom"] {
bottom: 0;
}


.hfe-reading-progress {
background-color: var(--progress-bar-bg-color, #eee);
position: relative;
width: 100%;
}

.hfe-reading-progress-fill {
background-color: var(--progress-bar-fill-color, #1fd18e);
height: 5px;
transition: width 0.5s ease;
}

96 changes: 96 additions & 0 deletions inc/widgets-manager/class-extensions-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Extensions loader for Header Footer Elementor.
*
* @package HFE
* @author HFE
* @copyright Copyright (c) 2018, HFE
* @link http://brainstormforce.com/
* @since HFE 1.2.0
*/

namespace HFE\WidgetsManager;

use Elementor\Plugin;

defined( 'ABSPATH' ) || exit;

/**
* Set up Extensions Loader class
*/
class Extensions_Loader {

/**
* Instance of Extensions_Loader.
*
* @since 1.2.0
* @var null
*/
private static $_instance = null;

/**
* Get instance of Extensions_Loader
*
* @since 1.2.0
* @return Extensions_Loader
*/
public static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self();
}

return self::$_instance;
}

/**
* Setup actions and filters.
*
* @since 1.2.0
* @access private
*/
private function __construct() {

$this->include_extensions_files();

}


/**
* Returns Script array.
*
* @return array()
* @since 1.3.0
*/
public static function get_extensions_list() {
$extensions_list = [
'progress-bar',
];

return $extensions_list;
}

/**
* Include Extensions files
*
* Load Extensions files
*
* @since 1.2.0
* @access public
* @return void
*/
public function include_extensions_files() {
$extensions_list = $this->get_extensions_list();

if ( ! empty( $extensions_list ) ) {
foreach ( $extensions_list as $handle => $data ) {
require_once HFE_DIR . '/inc/widgets-manager/extensions/class-' . $data . '.php';
}
}
}

}

/**
* Initiate the class.
*/
Extensions_Loader::instance();

Check failure on line 96 in inc/widgets-manager/class-extensions-loader.php

View workflow job for this annotation

GitHub Actions / CI (14.15, 7.4)

File must end with a newline character
2 changes: 2 additions & 0 deletions inc/widgets-manager/class-widgets-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static function instance() {
* @access private
*/
private function __construct() {

// Register category.
add_action( 'elementor/elements/categories_registered', [ $this, 'register_widget_category' ] );

Expand All @@ -71,6 +72,7 @@ private function __construct() {

add_filter( 'woocommerce_add_to_cart_fragments', [ $this, 'wc_refresh_mini_cart_count' ] );
}

}

/**
Expand Down
118 changes: 118 additions & 0 deletions inc/widgets-manager/extensions/class-progress-bar-render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Elementor Reading Progress Bar Rendering Class.
*
* @package header-footer-elementor
*/

namespace HFE\WidgetsManager\Extensions;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* HFE Reading Progress Bar.
*
* @since x.x.x
*/
class Progress_Bar_Render {

/**
* Instance of Progress_Bar_Render.
*
* @since x.x.x
* @var null
*/
private static $_instance = null;

/**
* Get instance of Progress_Bar_Render.
*
* @since x.x.x
* @return Progress_Bar_Render
*/
public static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self();
}

return self::$_instance;
}

/**
* Constructor function.
*
* @since x.x.x
*/
private function __construct() {
add_action( 'wp_footer', [ $this, 'render_reading_progress_bar' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
}

/**
* Enqueue necessary scripts for reading progress bar.
*
* @since x.x.x
*/
public function enqueue_scripts() {
// Enqueue your styles and scripts here.
wp_enqueue_style( 'hfe-progress-bar-style', get_template_directory_uri() . '/css/progress-bar.css' );
wp_enqueue_script( 'hfe-progress-bar-script', get_template_directory_uri() . '/js/progress-bar.js', [ 'jquery' ], '1.0.0', true );
}

/**
* Render the reading progress bar.
*
* @since x.x.x
*/
public function render_reading_progress_bar() {
// Check if the option to enable the progress bar is set.
$enable_progress_bar = get_option( 'enable_reading_progress_bar', 'no' );
if ( 'yes' !== $enable_progress_bar ) {
return;
}

// Position and height for the progress bar.
$progress_position = get_option( 'hfe_reading_progress_bar_position', 'top' );
var_dump($progress_position);

Check failure on line 78 in inc/widgets-manager/extensions/class-progress-bar-render.php

View workflow job for this annotation

GitHub Actions / CI (14.15, 7.4)

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 78 in inc/widgets-manager/extensions/class-progress-bar-render.php

View workflow job for this annotation

GitHub Actions / CI (14.15, 7.4)

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 78 in inc/widgets-manager/extensions/class-progress-bar-render.php

View workflow job for this annotation

GitHub Actions / CI (14.15, 7.4)

Expected 1 spaces before closing parenthesis; 0 found
$progress_height = get_option( 'hfe_reading_progress_bar_height', 5 );
$fill_color = get_option( 'hfe_reading_progress_bar_fill_color', '#1fd18e' );
$bg_color = get_option( 'hfe_reading_progress_bar_bg_color', '' );

// Inline styles for the progress bar.
$progress_styles = sprintf(
'height: %dpx; background-color: %s;',
esc_attr( $progress_height ),
esc_attr( $bg_color )
);

$fill_styles = sprintf(
'background-color: %s;',
esc_attr( $fill_color )
);

// Render the progress bar markup.
?>
<div class="hfe-reading-progress-wrap" style="<?php echo esc_attr( $progress_styles ); ?>">
<div class="hfe-reading-progress" style="height: <?php echo esc_attr( $progress_height ); ?>px;">
<div class="hfe-reading-progress-fill" style="<?php echo esc_attr( $fill_styles ); ?>"></div>
</div>
</div>

<script type="text/javascript">
jQuery(document).ready(function($) {
var progressBar = $('.hfe-reading-progress-fill');
var totalHeight = $(document).height() - $(window).height();
$(window).on('scroll', function() {
var scrollTop = $(window).scrollTop();
var progress = (scrollTop / totalHeight) * 100;
progressBar.css('width', progress + '%');
});
});
</script>
<?php
}
}

Progress_Bar_Render::instance();
Loading
Loading