Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Sticky footer module.
18
 *
19
 * @module     theme_boost/sticky-footer
20
 * @copyright  2022 Ferran Recio <ferran@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import Pending from 'core/pending';
25
import {registerManager, init as defaultInit} from 'core/sticky-footer';
26
 
27
const SELECTORS = {
28
    STICKYFOOTER: '.stickyfooter',
29
    PAGE: '#page',
30
};
31
 
32
const CLASSES = {
33
    HASSTICKYFOOTER: 'hasstickyfooter',
34
};
35
 
36
let initialized = false;
37
 
38
let previousScrollPosition = 0;
39
 
40
let enabled = false;
41
 
42
/**
43
 * Scroll handler.
44
 * @package
45
 */
46
const scrollSpy = () => {
47
    if (!enabled) {
48
        return;
49
    }
50
    // Ignore scroll if page size is not small.
51
    if (document.body.clientWidth >= 768) {
52
        return;
53
    }
54
    // Detect if scroll is going down.
55
    let scrollPosition = window.scrollY;
56
    if (scrollPosition > previousScrollPosition) {
57
        hideStickyFooter();
58
    } else {
59
        showStickyFooter();
60
    }
61
    previousScrollPosition = scrollPosition;
62
};
63
 
64
/**
65
 * Return if the sticky footer must be enabled by default or not.
66
 * @returns {Boolean} true if the sticky footer is enabled automatic.
67
 */
68
const isDisabledByDefault = () => {
69
    const footer = document.querySelector(SELECTORS.STICKYFOOTER);
70
    if (!footer) {
71
        return false;
72
    }
73
    return !!footer.dataset.disable;
74
};
75
 
76
/**
77
 * Show the sticky footer in the page.
78
 */
79
const showStickyFooter = () => {
80
    // We need some seconds to make sure the CSS animation is ready.
81
    const pendingPromise = new Pending('theme_boost/sticky-footer:enabling');
82
    const footer = document.querySelector(SELECTORS.STICKYFOOTER);
83
    const page = document.querySelector(SELECTORS.PAGE);
84
    if (footer && page) {
85
        document.body.classList.add(CLASSES.HASSTICKYFOOTER);
86
        page.classList.add(CLASSES.HASSTICKYFOOTER);
87
    }
88
    setTimeout(() => pendingPromise.resolve(), 1000);
89
};
90
 
91
/**
92
 * Hide the sticky footer in the page.
93
 */
94
const hideStickyFooter = () => {
95
    document.body.classList.remove(CLASSES.HASSTICKYFOOTER);
96
    const page = document.querySelector(SELECTORS.PAGE);
97
    page?.classList.remove(CLASSES.HASSTICKYFOOTER);
98
};
99
 
100
/**
101
 * Enable sticky footer in the page.
102
 */
103
export const enableStickyFooter = () => {
104
    enabled = true;
105
    showStickyFooter();
106
};
107
 
108
/**
109
 * Disable sticky footer in the page.
110
 */
111
export const disableStickyFooter = () => {
112
    enabled = false;
113
    hideStickyFooter();
114
};
115
 
116
/**
117
 * Initialize the module.
118
 */
119
export const init = () => {
120
    // Prevent sticky footer in behat.
121
    if (initialized || document.body.classList.contains('behat-site')) {
122
        defaultInit();
123
        return;
124
    }
125
    initialized = true;
126
    if (!isDisabledByDefault()) {
127
        enableStickyFooter();
128
    }
129
 
130
    document.addEventListener("scroll", scrollSpy);
131
 
132
    registerManager({
133
        enableStickyFooter,
134
        disableStickyFooter,
135
    });
136
};