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 |
* Our basic form manager for when a user either enters
|
|
|
18 |
* their profile url or just wants to browse.
|
|
|
19 |
*
|
|
|
20 |
* This file is a mishmash of JS functions we need for both the standalone (M3.7, M3.8)
|
|
|
21 |
* plugin & Moodle 3.9 functions. The 3.9 Functions have a base understanding that certain
|
|
|
22 |
* things exist i.e. directory structures for templates. When this feature goes 3.9+ only
|
|
|
23 |
* The goal is that we can quickly gut all AMD modules into bare JS files and use ES6 guidelines.
|
|
|
24 |
* Till then this will have to do.
|
|
|
25 |
*
|
|
|
26 |
* @module tool_moodlenet/instance_form
|
|
|
27 |
* @copyright 2020 Mathew May <mathew.solutions>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
|
|
|
31 |
define(['tool_moodlenet/validator',
|
|
|
32 |
'tool_moodlenet/selectors',
|
|
|
33 |
'core/loadingicon',
|
|
|
34 |
'core/templates',
|
|
|
35 |
'core/notification',
|
|
|
36 |
'jquery'],
|
|
|
37 |
function(Validator,
|
|
|
38 |
Selectors,
|
|
|
39 |
LoadingIcon,
|
|
|
40 |
Templates,
|
|
|
41 |
Notification,
|
|
|
42 |
$) {
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Add the event listeners to our form.
|
|
|
46 |
*
|
|
|
47 |
* @method registerListenerEvents
|
|
|
48 |
* @param {HTMLElement} page The whole page element for our form area
|
|
|
49 |
*/
|
|
|
50 |
var registerListenerEvents = function registerListenerEvents(page) {
|
|
|
51 |
page.addEventListener('click', function(e) {
|
|
|
52 |
|
|
|
53 |
// Our fake submit button / browse button.
|
|
|
54 |
if (e.target.matches(Selectors.action.submit)) {
|
|
|
55 |
var input = page.querySelector('[data-var="mnet-link"]');
|
|
|
56 |
var overlay = page.querySelector(Selectors.region.spinner);
|
|
|
57 |
var validationArea = document.querySelector(Selectors.region.validationArea);
|
|
|
58 |
|
|
|
59 |
overlay.classList.remove('d-none');
|
|
|
60 |
var spinner = LoadingIcon.addIconToContainerWithPromise(overlay);
|
|
|
61 |
Validator.validation(input)
|
|
|
62 |
.then(function(result) {
|
|
|
63 |
spinner.resolve();
|
|
|
64 |
overlay.classList.add('d-none');
|
|
|
65 |
if (result.result) {
|
|
|
66 |
input.classList.remove('is-invalid'); // Just in case the class has been applied already.
|
|
|
67 |
input.classList.add('is-valid');
|
|
|
68 |
validationArea.innerText = result.message;
|
|
|
69 |
validationArea.classList.remove('text-danger');
|
|
|
70 |
validationArea.classList.add('text-success');
|
|
|
71 |
// Give the user some time to see their input is valid.
|
|
|
72 |
setTimeout(function() {
|
|
|
73 |
window.location = result.domain;
|
|
|
74 |
}, 1000);
|
|
|
75 |
} else {
|
|
|
76 |
input.classList.add('is-invalid');
|
|
|
77 |
validationArea.innerText = result.message;
|
|
|
78 |
validationArea.classList.add('text-danger');
|
|
|
79 |
}
|
|
|
80 |
return;
|
|
|
81 |
}).catch();
|
|
|
82 |
}
|
|
|
83 |
});
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Given a user wishes to see the MoodleNet profile url form transition them there.
|
|
|
88 |
*
|
|
|
89 |
* @method chooserNavigateToMnet
|
|
|
90 |
* @param {HTMLElement} showMoodleNet The chooser's area for ment
|
|
|
91 |
* @param {Object} footerData Our footer object to render out
|
|
|
92 |
* @param {jQuery} carousel Our carousel instance to manage
|
|
|
93 |
* @param {jQuery} modal Our modal instance to manage
|
|
|
94 |
*/
|
|
|
95 |
var chooserNavigateToMnet = function(showMoodleNet, footerData, carousel, modal) {
|
|
|
96 |
showMoodleNet.innerHTML = '';
|
|
|
97 |
|
|
|
98 |
// Add a spinner.
|
|
|
99 |
var spinnerPromise = LoadingIcon.addIconToContainer(showMoodleNet);
|
|
|
100 |
|
|
|
101 |
// Used later...
|
|
|
102 |
var transitionPromiseResolver = null;
|
|
|
103 |
var transitionPromise = new Promise(resolve => {
|
|
|
104 |
transitionPromiseResolver = resolve;
|
|
|
105 |
});
|
|
|
106 |
|
|
|
107 |
$.when(
|
|
|
108 |
spinnerPromise,
|
|
|
109 |
transitionPromise
|
|
|
110 |
).then(function() {
|
|
|
111 |
Templates.replaceNodeContents(showMoodleNet, footerData.customcarouseltemplate, '');
|
|
|
112 |
return;
|
|
|
113 |
}).catch(Notification.exception);
|
|
|
114 |
|
|
|
115 |
// We apply our handlers in here to minimise plugin dependency in the Chooser.
|
|
|
116 |
registerListenerEvents(showMoodleNet);
|
|
|
117 |
|
|
|
118 |
// Move to the next slide, and resolve the transition promise when it's done.
|
|
|
119 |
carousel.one('slid.bs.carousel', function() {
|
|
|
120 |
transitionPromiseResolver();
|
|
|
121 |
});
|
|
|
122 |
// Trigger the transition between 'pages'.
|
|
|
123 |
carousel.carousel(2);
|
|
|
124 |
modal.setFooter(Templates.render('tool_moodlenet/chooser_footer_close_mnet', {}));
|
|
|
125 |
};
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Given a user no longer wishes to see the MoodleNet profile url form transition them from there.
|
|
|
129 |
*
|
|
|
130 |
* @method chooserNavigateFromMnet
|
|
|
131 |
* @param {jQuery} carousel Our carousel instance to manage
|
|
|
132 |
* @param {jQuery} modal Our modal instance to manage
|
|
|
133 |
* @param {Object} footerData Our footer object to render out
|
|
|
134 |
*/
|
|
|
135 |
var chooserNavigateFromMnet = function(carousel, modal, footerData) {
|
|
|
136 |
// Trigger the transition between 'pages'.
|
|
|
137 |
carousel.carousel(0);
|
|
|
138 |
modal.setFooter(footerData.customfootertemplate);
|
|
|
139 |
};
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Create the custom listener that would handle anything in the footer.
|
|
|
143 |
*
|
|
|
144 |
* @param {Event} e The event being triggered.
|
|
|
145 |
* @param {Object} footerData The data generated from the exporter.
|
|
|
146 |
* @param {Object} modal The chooser modal.
|
|
|
147 |
*/
|
|
|
148 |
var footerClickListener = function(e, footerData, modal) {
|
|
|
149 |
if (e.target.matches(Selectors.action.showMoodleNet) || e.target.closest(Selectors.action.showMoodleNet)) {
|
|
|
150 |
e.preventDefault();
|
|
|
151 |
const carousel = $(modal.getBody()[0].querySelector(Selectors.region.carousel));
|
|
|
152 |
const showMoodleNet = carousel.find(Selectors.region.moodleNet)[0];
|
|
|
153 |
|
|
|
154 |
chooserNavigateToMnet(showMoodleNet, footerData, carousel, modal);
|
|
|
155 |
}
|
|
|
156 |
// From the help screen go back to the module overview.
|
|
|
157 |
if (e.target.matches(Selectors.action.closeOption)) {
|
|
|
158 |
const carousel = $(modal.getBody()[0].querySelector(Selectors.region.carousel));
|
|
|
159 |
|
|
|
160 |
chooserNavigateFromMnet(carousel, modal, footerData);
|
|
|
161 |
}
|
|
|
162 |
};
|
|
|
163 |
|
|
|
164 |
return {
|
|
|
165 |
footerClickListener: footerClickListener
|
|
|
166 |
};
|
|
|
167 |
});
|