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 wrapper module.
|
|
|
18 |
*
|
|
|
19 |
* Themes are responsible for implementing the sticky footer. However,
|
|
|
20 |
* modules can interact with the sticky footer using this module.
|
|
|
21 |
*
|
|
|
22 |
* @module core/sticky-footer
|
|
|
23 |
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
import {dispatchEvent} from 'core/event_dispatcher';
|
|
|
28 |
|
|
|
29 |
let manager = {};
|
|
|
30 |
|
|
|
31 |
let enabled = false;
|
|
|
32 |
|
|
|
33 |
let initialized = false;
|
|
|
34 |
|
|
|
35 |
export const SELECTORS = {
|
|
|
36 |
STICKYFOOTER: '.stickyfooter',
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
const CLASSES = {
|
|
|
40 |
INVISIBLE: 'v-hidden',
|
|
|
41 |
};
|
|
|
42 |
|
|
|
43 |
export const eventTypes = {
|
|
|
44 |
stickyFooterStateChanged: 'core/stickyfooter_state_changed',
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Enable sticky footer in the page.
|
|
|
49 |
*/
|
|
|
50 |
export const enableStickyFooter = () => {
|
|
|
51 |
enabled = true;
|
|
|
52 |
if (manager.enableStickyFooter === undefined) {
|
|
|
53 |
document.querySelector(SELECTORS.STICKYFOOTER)?.classList.remove(CLASSES.INVISIBLE);
|
|
|
54 |
return;
|
|
|
55 |
}
|
|
|
56 |
manager.enableStickyFooter();
|
|
|
57 |
notifyStickyFooterStateChange(true);
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Disable sticky footer in the page.
|
|
|
62 |
*/
|
|
|
63 |
export const disableStickyFooter = () => {
|
|
|
64 |
enabled = false;
|
|
|
65 |
if (manager.disableStickyFooter === undefined) {
|
|
|
66 |
document.querySelector(SELECTORS.STICKYFOOTER)?.classList.add(CLASSES.INVISIBLE);
|
|
|
67 |
return;
|
|
|
68 |
}
|
|
|
69 |
manager.disableStickyFooter();
|
|
|
70 |
notifyStickyFooterStateChange(false);
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Register the theme sticky footer methods.
|
|
|
75 |
*
|
|
|
76 |
* @param {Object} themeManager the manager object with all the needed methods.
|
|
|
77 |
* @param {Function} themeManager.enableStickyFooter enable sticky footer method
|
|
|
78 |
* @param {Function} themeManager.disableStickyFooter disable sticky footer method
|
|
|
79 |
*/
|
|
|
80 |
export const registerManager = (themeManager) => {
|
|
|
81 |
manager = themeManager;
|
|
|
82 |
if (enabled) {
|
|
|
83 |
enableStickyFooter();
|
|
|
84 |
}
|
|
|
85 |
};
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Initialize the module if the theme does not implement its own init.
|
|
|
89 |
*/
|
|
|
90 |
export const init = () => {
|
|
|
91 |
if (initialized) {
|
|
|
92 |
return;
|
|
|
93 |
}
|
|
|
94 |
initialized = true;
|
|
|
95 |
|
|
|
96 |
const isDisabled = document.querySelector(SELECTORS.STICKYFOOTER)?.dataset.disable;
|
|
|
97 |
if (isDisabled) {
|
|
|
98 |
disableStickyFooter();
|
|
|
99 |
} else {
|
|
|
100 |
enableStickyFooter();
|
|
|
101 |
}
|
|
|
102 |
};
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Dispatch an event to notify that the state (enabled/disabled) of the sticky footer has changed.
|
|
|
106 |
*
|
|
|
107 |
* @param {boolean} enabled Whether the sticky footer has been enabled or disabled.
|
|
|
108 |
*/
|
|
|
109 |
const notifyStickyFooterStateChange = (enabled) => {
|
|
|
110 |
dispatchEvent(eventTypes.stickyFooterStateChanged, {enabled: enabled}, document, {cancelable: false});
|
|
|
111 |
};
|