| 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 |  * Javascript module for importing presets.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     mod_data/importpresets
 | 
        
           |  |  | 20 |  * @copyright  2022 Laurent David <laurent.david@moodle.com>
 | 
        
           |  |  | 21 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  */
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | import ModalForm from 'core_form/modalform';
 | 
        
           |  |  | 25 | import Notification from 'core/notification';
 | 
        
           |  |  | 26 | import {getString} from 'core/str';
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | const selectors = {
 | 
        
           |  |  | 29 |     importPresetButton: '[data-action="importpresets"]',
 | 
        
           |  |  | 30 | };
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | /**
 | 
        
           |  |  | 33 |  * Initialize module
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | export const init = () => {
 | 
        
           |  |  | 36 |     document.addEventListener('click', (event) => {
 | 
        
           |  |  | 37 |         const importPresetButton = event.target.closest(selectors.importPresetButton);
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |         if (!importPresetButton) {
 | 
        
           |  |  | 40 |             return;
 | 
        
           |  |  | 41 |         }
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 |         event.preventDefault();
 | 
        
           |  |  | 44 |         const modalForm = new ModalForm({
 | 
        
           |  |  | 45 |             modalConfig: {
 | 
        
           |  |  | 46 |                 title: getString('importpreset', 'mod_data'),
 | 
        
           |  |  | 47 |             },
 | 
        
           |  |  | 48 |             formClass: 'mod_data\\form\\import_presets',
 | 
        
           |  |  | 49 |             args: {cmid: importPresetButton.dataset.dataid},
 | 
        
           |  |  | 50 |             saveButtonText: getString('importandapply', 'mod_data'),
 | 
        
           |  |  | 51 |         });
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |         modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
 | 
        
           |  |  | 54 |             if (event.detail.result) {
 | 
        
           |  |  | 55 |                 window.location.assign(event.detail.url);
 | 
        
           |  |  | 56 |             } else {
 | 
        
           |  |  | 57 |                 Notification.addNotification({
 | 
        
           |  |  | 58 |                     type: 'error',
 | 
        
           |  |  | 59 |                     message: event.detail.errors.join('<br>')
 | 
        
           |  |  | 60 |                 });
 | 
        
           |  |  | 61 |             }
 | 
        
           |  |  | 62 |         });
 | 
        
           |  |  | 63 |         modalForm.show();
 | 
        
           |  |  | 64 |     });
 | 
        
           |  |  | 65 | };
 |