Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 * 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',
1441 ariadna 36
        'jquery',
37
        'theme_boost/bootstrap/carousel',
38
        'core/normalise'],
1 efrain 39
    function(Validator,
40
             Selectors,
41
             LoadingIcon,
42
             Templates,
43
             Notification,
1441 ariadna 44
             $,
45
             Carousel,
46
             Normalise) {
1 efrain 47
 
48
    /**
49
     * Add the event listeners to our form.
50
     *
51
     * @method registerListenerEvents
52
     * @param {HTMLElement} page The whole page element for our form area
53
     */
54
    var registerListenerEvents = function registerListenerEvents(page) {
55
        page.addEventListener('click', function(e) {
56
 
57
            // Our fake submit button / browse button.
58
            if (e.target.matches(Selectors.action.submit)) {
59
                var input = page.querySelector('[data-var="mnet-link"]');
60
                var overlay = page.querySelector(Selectors.region.spinner);
61
                var validationArea = document.querySelector(Selectors.region.validationArea);
62
 
63
                overlay.classList.remove('d-none');
64
                var spinner = LoadingIcon.addIconToContainerWithPromise(overlay);
65
                Validator.validation(input)
66
                    .then(function(result) {
67
                        spinner.resolve();
68
                        overlay.classList.add('d-none');
69
                        if (result.result) {
70
                            input.classList.remove('is-invalid'); // Just in case the class has been applied already.
71
                            input.classList.add('is-valid');
72
                            validationArea.innerText = result.message;
73
                            validationArea.classList.remove('text-danger');
74
                            validationArea.classList.add('text-success');
75
                            // Give the user some time to see their input is valid.
76
                            setTimeout(function() {
77
                                window.location = result.domain;
78
                            }, 1000);
79
                        } else {
80
                            input.classList.add('is-invalid');
81
                            validationArea.innerText = result.message;
82
                            validationArea.classList.add('text-danger');
83
                        }
84
                        return;
1441 ariadna 85
                }).catch(Notification.exception);
1 efrain 86
            }
87
        });
88
    };
89
 
90
    /**
91
     * Given a user wishes to see the MoodleNet profile url form transition them there.
92
     *
93
     * @method chooserNavigateToMnet
94
     * @param {HTMLElement} showMoodleNet The chooser's area for ment
95
     * @param {Object} footerData Our footer object to render out
1441 ariadna 96
     * @param {Element} carousel Our carousel instance to manage
1 efrain 97
     * @param {jQuery} modal Our modal instance to manage
98
     */
99
    var chooserNavigateToMnet = function(showMoodleNet, footerData, carousel, modal) {
100
        showMoodleNet.innerHTML = '';
101
 
102
        // Add a spinner.
103
        var spinnerPromise = LoadingIcon.addIconToContainer(showMoodleNet);
104
 
105
        // Used later...
106
        var transitionPromiseResolver = null;
107
        var transitionPromise = new Promise(resolve => {
108
            transitionPromiseResolver = resolve;
109
        });
110
 
111
        $.when(
112
            spinnerPromise,
113
            transitionPromise
114
        ).then(function() {
115
                Templates.replaceNodeContents(showMoodleNet, footerData.customcarouseltemplate, '');
116
                return;
117
        }).catch(Notification.exception);
118
 
119
        // We apply our handlers in here to minimise plugin dependency in the Chooser.
120
        registerListenerEvents(showMoodleNet);
121
 
122
        // Move to the next slide, and resolve the transition promise when it's done.
1441 ariadna 123
        carousel.addEventListener('slid.bs.carousel', function() {
1 efrain 124
            transitionPromiseResolver();
1441 ariadna 125
        }, {once: true});
1 efrain 126
        // Trigger the transition between 'pages'.
1441 ariadna 127
        Carousel.getInstance(carousel).to(2);
1 efrain 128
        modal.setFooter(Templates.render('tool_moodlenet/chooser_footer_close_mnet', {}));
129
    };
130
 
131
    /**
132
     * Given a user no longer wishes to see the MoodleNet profile url form transition them from there.
133
     *
134
     * @method chooserNavigateFromMnet
1441 ariadna 135
     * @param {Element} carousel Our carousel instance to manage
1 efrain 136
     * @param {jQuery} modal Our modal instance to manage
137
     * @param {Object} footerData Our footer object to render out
138
     */
139
    var chooserNavigateFromMnet = function(carousel, modal, footerData) {
140
        // Trigger the transition between 'pages'.
1441 ariadna 141
        Carousel.getInstance(carousel).to(0);
1 efrain 142
        modal.setFooter(footerData.customfootertemplate);
143
    };
144
 
145
        /**
146
         * Create the custom listener that would handle anything in the footer.
147
         *
148
         * @param {Event} e The event being triggered.
149
         * @param {Object} footerData The data generated from the exporter.
150
         * @param {Object} modal The chooser modal.
151
         */
152
    var footerClickListener = function(e, footerData, modal) {
1441 ariadna 153
        const modalBody = Normalise.getFirst(modal.getBody());
1 efrain 154
        if (e.target.matches(Selectors.action.showMoodleNet) || e.target.closest(Selectors.action.showMoodleNet)) {
155
            e.preventDefault();
1441 ariadna 156
            const carousel = modalBody.querySelector(Selectors.region.carousel);
157
            const showMoodleNet = carousel.querySelector(Selectors.region.moodleNet);
1 efrain 158
 
159
            chooserNavigateToMnet(showMoodleNet, footerData, carousel, modal);
160
        }
161
        // From the help screen go back to the module overview.
162
        if (e.target.matches(Selectors.action.closeOption)) {
1441 ariadna 163
            const carousel = modalBody.querySelector(Selectors.region.carousel);
1 efrain 164
 
165
            chooserNavigateFromMnet(carousel, modal, footerData);
166
        }
167
    };
168
 
169
    return {
170
        footerClickListener: footerClickListener
171
    };
172
});