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 |
* Dynamic Tabs UI element with AJAX loading of tabs content
|
|
|
18 |
*
|
|
|
19 |
* @module core/dynamic_tabs
|
|
|
20 |
* @copyright 2021 David Matamoros <davidmc@moodle.com> based on code from Marina Glancy
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import $ from 'jquery';
|
|
|
25 |
import Templates from 'core/templates';
|
|
|
26 |
import {addIconToContainer} from 'core/loadingicon';
|
|
|
27 |
import Notification from 'core/notification';
|
|
|
28 |
import Pending from 'core/pending';
|
|
|
29 |
import {getStrings} from 'core/str';
|
|
|
30 |
import {getContent} from 'core/local/repository/dynamic_tabs';
|
|
|
31 |
import {isAnyWatchedFormDirty, resetAllFormDirtyStates} from 'core_form/changechecker';
|
|
|
32 |
|
|
|
33 |
const SELECTORS = {
|
|
|
34 |
dynamicTabs: '.dynamictabs',
|
|
|
35 |
activeTab: '.dynamictabs .nav-link.active',
|
|
|
36 |
allActiveTabs: '.dynamictabs .nav-link[data-toggle="tab"]:not(.disabled)',
|
|
|
37 |
tabContent: '.dynamictabs .tab-pane [data-tab-content]',
|
|
|
38 |
tabToggle: 'a[data-toggle="tab"]',
|
|
|
39 |
tabPane: '.dynamictabs .tab-pane',
|
|
|
40 |
};
|
|
|
41 |
|
|
|
42 |
SELECTORS.forTabName = tabName => `.dynamictabs [data-tab-content="${tabName}"]`;
|
|
|
43 |
SELECTORS.forTabId = tabName => `.dynamictabs [data-toggle="tab"][href="#${tabName}"]`;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Initialises the tabs view on the page (only one tabs view per page is supported)
|
|
|
47 |
*/
|
|
|
48 |
export const init = () => {
|
|
|
49 |
const tabToggle = $(SELECTORS.tabToggle);
|
|
|
50 |
|
|
|
51 |
// Listen to click, warn user if they are navigating away with unsaved form changes.
|
|
|
52 |
tabToggle.on('click', (event) => {
|
|
|
53 |
if (!isAnyWatchedFormDirty()) {
|
|
|
54 |
return;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
event.preventDefault();
|
|
|
58 |
event.stopPropagation();
|
|
|
59 |
|
|
|
60 |
getStrings([
|
|
|
61 |
{key: 'changesmade', component: 'moodle'},
|
|
|
62 |
{key: 'changesmadereallygoaway', component: 'moodle'},
|
|
|
63 |
{key: 'confirm', component: 'moodle'},
|
|
|
64 |
]).then(([strChangesMade, strChangesMadeReally, strConfirm]) =>
|
|
|
65 |
// Reset form dirty state on confirmation, re-trigger the event.
|
|
|
66 |
Notification.confirm(strChangesMade, strChangesMadeReally, strConfirm, null, () => {
|
|
|
67 |
resetAllFormDirtyStates();
|
|
|
68 |
$(event.target).trigger(event.type);
|
|
|
69 |
})
|
|
|
70 |
).catch(Notification.exception);
|
|
|
71 |
});
|
|
|
72 |
|
|
|
73 |
// This code listens to Bootstrap events 'show.bs.tab' and 'shown.bs.tab' which is triggered using JQuery and
|
|
|
74 |
// can not be converted yet to native events.
|
|
|
75 |
tabToggle
|
|
|
76 |
.on('show.bs.tab', function() {
|
|
|
77 |
// Clean content from previous tab.
|
|
|
78 |
const previousTabName = getActiveTabName();
|
|
|
79 |
if (previousTabName) {
|
|
|
80 |
const previousTab = document.querySelector(SELECTORS.forTabName(previousTabName));
|
|
|
81 |
previousTab.textContent = '';
|
|
|
82 |
}
|
|
|
83 |
})
|
|
|
84 |
.on('shown.bs.tab', function() {
|
|
|
85 |
const tab = $($(this).attr('href'));
|
|
|
86 |
if (tab.length !== 1) {
|
|
|
87 |
return;
|
|
|
88 |
}
|
|
|
89 |
loadTab(tab.attr('id'));
|
|
|
90 |
});
|
|
|
91 |
|
|
|
92 |
if (!openTabFromHash()) {
|
|
|
93 |
const tabs = document.querySelector(SELECTORS.allActiveTabs);
|
|
|
94 |
if (tabs) {
|
|
|
95 |
openTab(tabs.getAttribute('aria-controls'));
|
|
|
96 |
} else {
|
|
|
97 |
// We may hide tabs if there is only one available, just load the contents of the first tab.
|
|
|
98 |
const tabPane = document.querySelector(SELECTORS.tabPane);
|
|
|
99 |
if (tabPane) {
|
|
|
100 |
tabPane.classList.add('active', 'show');
|
|
|
101 |
loadTab(tabPane.getAttribute('id'));
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
};
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Returns id/name of the currently active tab
|
|
|
109 |
*
|
|
|
110 |
* @return {String|null}
|
|
|
111 |
*/
|
|
|
112 |
const getActiveTabName = () => {
|
|
|
113 |
const element = document.querySelector(SELECTORS.activeTab);
|
|
|
114 |
return element?.getAttribute('aria-controls') || null;
|
|
|
115 |
};
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Returns the id/name of the first tab
|
|
|
119 |
*
|
|
|
120 |
* @return {String|null}
|
|
|
121 |
*/
|
|
|
122 |
const getFirstTabName = () => {
|
|
|
123 |
const element = document.querySelector(SELECTORS.tabContent);
|
|
|
124 |
return element?.dataset.tabContent || null;
|
|
|
125 |
};
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Loads contents of a tab using an AJAX request
|
|
|
129 |
*
|
|
|
130 |
* @param {String} tabName
|
|
|
131 |
*/
|
|
|
132 |
const loadTab = (tabName) => {
|
|
|
133 |
// If tabName is not specified find the active tab, or if is not defined, the first available tab.
|
|
|
134 |
tabName = tabName ?? getActiveTabName() ?? getFirstTabName();
|
|
|
135 |
const tab = document.querySelector(SELECTORS.forTabName(tabName));
|
|
|
136 |
if (!tab) {
|
|
|
137 |
return;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
const pendingPromise = new Pending('core/dynamic_tabs:loadTab:' + tabName);
|
|
|
141 |
|
|
|
142 |
addIconToContainer(tab)
|
|
|
143 |
.then(() => {
|
|
|
144 |
let tabArgs = {...tab.dataset};
|
|
|
145 |
delete tabArgs.tabClass;
|
|
|
146 |
delete tabArgs.tabContent;
|
|
|
147 |
return getContent(tab.dataset.tabClass, JSON.stringify(tabArgs));
|
|
|
148 |
})
|
|
|
149 |
.then(response => Promise.all([
|
|
|
150 |
$.parseHTML(response.javascript, null, true).map(node => node.innerHTML).join("\n"),
|
|
|
151 |
Templates.renderForPromise(response.template, JSON.parse(response.content)),
|
|
|
152 |
]))
|
|
|
153 |
.then(([responseJs, {html, js}]) => Templates.replaceNodeContents(tab, html, js + responseJs))
|
|
|
154 |
.then(() => pendingPromise.resolve())
|
|
|
155 |
.catch(Notification.exception);
|
|
|
156 |
};
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Return the tab given the tab name
|
|
|
160 |
*
|
|
|
161 |
* @param {String} tabName
|
|
|
162 |
* @return {HTMLElement}
|
|
|
163 |
*/
|
|
|
164 |
const getTab = (tabName) => {
|
|
|
165 |
return document.querySelector(SELECTORS.forTabId(tabName));
|
|
|
166 |
};
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Return the tab pane given the tab name
|
|
|
170 |
*
|
|
|
171 |
* @param {String} tabName
|
|
|
172 |
* @return {HTMLElement}
|
|
|
173 |
*/
|
|
|
174 |
const getTabPane = (tabName) => {
|
|
|
175 |
return document.getElementById(tabName);
|
|
|
176 |
};
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Open the tab on page load. If this script loads before theme_boost/tab we need to open tab ourselves
|
|
|
180 |
*
|
|
|
181 |
* @param {String} tabName
|
|
|
182 |
* @return {Boolean}
|
|
|
183 |
*/
|
|
|
184 |
const openTab = (tabName) => {
|
|
|
185 |
const tab = getTab(tabName);
|
|
|
186 |
if (!tab) {
|
|
|
187 |
return false;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
loadTab(tabName);
|
|
|
191 |
tab.classList.add('active');
|
|
|
192 |
getTabPane(tabName).classList.add('active', 'show');
|
|
|
193 |
return true;
|
|
|
194 |
};
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* If there is a location hash that is the same as the tab name - open this tab.
|
|
|
198 |
*
|
|
|
199 |
* @return {Boolean}
|
|
|
200 |
*/
|
|
|
201 |
const openTabFromHash = () => {
|
|
|
202 |
const hash = document.location.hash;
|
|
|
203 |
if (hash.match(/^#\w+$/g)) {
|
|
|
204 |
return openTab(hash.replace(/^#/g, ''));
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
return false;
|
|
|
208 |
};
|