1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Theme Space - JS code back to top button
|
|
|
18 |
*
|
|
|
19 |
* @module theme_monocolor/backtotopbutton
|
|
|
20 |
* @copyright 2023 Marcin Czaja
|
|
|
21 |
* @copyright based on code from theme_monocolor_campus by Kathrin Osswald.
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
define(['jquery', 'core/str', 'core/notification'], function($, str, Notification) {
|
|
|
26 |
"use strict";
|
|
|
27 |
|
|
|
28 |
// Remember if the back to top button is shown currently.
|
|
|
29 |
let buttonShown = false;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Initializing.
|
|
|
33 |
*/
|
|
|
34 |
function initBackToTop() {
|
|
|
35 |
// Get the string backtotop from language file.
|
|
|
36 |
let stringsPromise = str.get_string('backtotopbutton', 'theme_monocolor');
|
|
|
37 |
|
|
|
38 |
// If the string has arrived, add backtotop button to DOM and add scroll and click handlers.
|
|
|
39 |
$.when(stringsPromise).then(function(string) {
|
|
|
40 |
// Add a fontawesome icon after the footer as the back to top button.
|
|
|
41 |
$('#s-page-footer').after('<button id="back-to-top" ' +
|
|
|
42 |
'class="btn btn-icon icon-no-margin d-print-none"' +
|
|
|
43 |
'aria-label="' + string + '">' +
|
|
|
44 |
'<svg width="24" height="24" stroke-width="2" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M6 20h12M12 16V4m0 0l3.5 3.5M12 4L8.5 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg></button>');
|
|
|
45 |
|
|
|
46 |
// This function fades the button in when the page is scrolled down or fades it out
|
|
|
47 |
// if the user is at the top of the page again.
|
|
|
48 |
// Please note that Boost in Moodle 4.0 does not scroll the window object / whole body tag anymore,
|
|
|
49 |
// it scrolls the #page element instead.
|
|
|
50 |
$('#page').on('scroll', function() {
|
|
|
51 |
if ($('#page').scrollTop() > 1210) {
|
|
|
52 |
checkAndShow();
|
|
|
53 |
$('body').addClass('scrolled');
|
|
|
54 |
} else {
|
|
|
55 |
checkAndHide();
|
|
|
56 |
$('body').removeClass('scrolled');
|
|
|
57 |
}
|
|
|
58 |
});
|
|
|
59 |
|
|
|
60 |
// This function scrolls the page to top with a duration of 500ms.
|
|
|
61 |
$('#back-to-top').on('click', function(event) {
|
|
|
62 |
event.preventDefault();
|
|
|
63 |
$('#page').animate({scrollTop: 0}, 500);
|
|
|
64 |
$('#back-to-top').blur();
|
|
|
65 |
});
|
|
|
66 |
|
|
|
67 |
return true;
|
|
|
68 |
}).fail(Notification.exception);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Helper function to handle the button visibility when the page is scrolling up.
|
|
|
73 |
*/
|
|
|
74 |
function checkAndHide() {
|
|
|
75 |
// Check if the button is still shown.
|
|
|
76 |
if (buttonShown === true) {
|
|
|
77 |
// Fade it out and remember the status in the end.
|
|
|
78 |
// To be precise, the faceOut() function will be called multiple times as buttonShown is not set until the button is
|
|
|
79 |
// really faded out. However, as soon as it is faded out, it won't be called until the button is shown again.
|
|
|
80 |
$('#back-to-top').fadeOut(100, function() {
|
|
|
81 |
buttonShown = false;
|
|
|
82 |
});
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Helper function to handle the button visibility when the page is scrolling down.
|
|
|
88 |
*/
|
|
|
89 |
function checkAndShow() {
|
|
|
90 |
// Check if the button is not yet shown.
|
|
|
91 |
if (buttonShown === false) {
|
|
|
92 |
// Fade it in and remember the status in the end.
|
|
|
93 |
$('#back-to-top').fadeIn(300, function() {
|
|
|
94 |
buttonShown = true;
|
|
|
95 |
});
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
return {
|
|
|
100 |
init: function() {
|
|
|
101 |
initBackToTop();
|
|
|
102 |
}
|
|
|
103 |
};
|
|
|
104 |
});
|