| 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 contacting the site DPO
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module      tool_dataprivacy/contactdpo
  | 
        
        
            | 
            | 
           20 | 
            * @copyright   2021 Paul Holden <paulh@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 | 
           import {add as addToast} from 'core/toast';
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           const SELECTORS = {
  | 
        
        
            | 
            | 
           30 | 
               CONTACT_DPO: '[data-action="contactdpo"]',
  | 
        
        
            | 
            | 
           31 | 
           };
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           /**
  | 
        
        
            | 
            | 
           34 | 
            * Initialize module
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
            | 
            | 
           36 | 
           export const init = () => {
  | 
        
        
            | 
            | 
           37 | 
               const triggerElement = document.querySelector(SELECTORS.CONTACT_DPO);
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
               triggerElement.addEventListener('click', event => {
  | 
        
        
            | 
            | 
           40 | 
                   event.preventDefault();
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
                   const modalForm = new ModalForm({
  | 
        
        
            | 
            | 
           43 | 
                       modalConfig: {
  | 
        
        
            | 
            | 
           44 | 
                           title: getString('contactdataprotectionofficer', 'tool_dataprivacy'),
  | 
        
        
            | 
            | 
           45 | 
                       },
  | 
        
        
            | 
            | 
           46 | 
                       formClass: 'tool_dataprivacy\\form\\contactdpo',
  | 
        
        
            | 
            | 
           47 | 
                       saveButtonText: getString('send', 'tool_dataprivacy'),
  | 
        
        
            | 
            | 
           48 | 
                       returnFocus: triggerElement,
  | 
        
        
            | 
            | 
           49 | 
                   });
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   // Show a toast notification when the form is submitted.
  | 
        
        
            | 
            | 
           52 | 
                   modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
  | 
        
        
            | 
            | 
           53 | 
                       if (event.detail.result) {
  | 
        
        
            | 
            | 
           54 | 
                           getString('requestsubmitted', 'tool_dataprivacy').then(addToast).catch();
  | 
        
        
            | 
            | 
           55 | 
                       } else {
  | 
        
        
            | 
            | 
           56 | 
                           const warningMessages = event.detail.warnings.map(warning => warning.message);
  | 
        
        
            | 
            | 
           57 | 
                           Notification.addNotification({
  | 
        
        
            | 
            | 
           58 | 
                               type: 'error',
  | 
        
        
            | 
            | 
           59 | 
                               message: warningMessages.join('<br>')
  | 
        
        
            | 
            | 
           60 | 
                           });
  | 
        
        
            | 
            | 
           61 | 
                       }
  | 
        
        
            | 
            | 
           62 | 
                   });
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   modalForm.show();
  | 
        
        
            | 
            | 
           65 | 
               });
  | 
        
        
            | 
            | 
           66 | 
           };
  |