[ Index ]

PHP Cross Reference of Moodle 310

title

Body

[close]

/lib/form/amd/src/ -> events.js (source)

   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   * Contain the events the form component can trigger.
  18   *
  19   * @module core_form/events
  20   * @copyright 2021 Huong Nguyen <huongnv13@gmail.com>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @since 3.10
  23   */
  24  
  25  import {get_string as getString} from 'core/str';
  26  
  27  let changesMadeString;
  28  getString('changesmadereallygoaway', 'moodle').then(string => {
  29      changesMadeString = string;
  30      return string;
  31  }).catch();
  32  
  33  /**
  34   * Prevent user navigate away when upload progress still running.
  35   * @param {Event} e The event
  36   */
  37  const changesMadeCheck = e => {
  38      if (e) {
  39          e.returnValue = changesMadeString;
  40      }
  41  };
  42  
  43  /**
  44   * List of the events.
  45   **/
  46  export const types = {
  47      uploadStarted: 'core_form/uploadStarted',
  48      uploadCompleted: 'core_form/uploadCompleted',
  49      uploadChanged: 'core_form/uploadChanged'
  50  };
  51  
  52  /**
  53   * Trigger upload start event.
  54   *
  55   * @param {String} elementId
  56   * @returns {CustomEvent<unknown>}
  57   */
  58  export const triggerUploadStarted = elementId => {
  59      // Add an additional check for changes made.
  60      window.addEventListener('beforeunload', changesMadeCheck);
  61      const customEvent = new CustomEvent(types.uploadStarted, {
  62          bubbles: true,
  63          cancellable: false
  64      });
  65      const element = document.getElementById(elementId);
  66      element.dispatchEvent(customEvent);
  67  
  68      return customEvent;
  69  };
  70  
  71  /**
  72   * Trigger upload complete event.
  73   *
  74   * @param {String} elementId
  75   * @returns {CustomEvent<unknown>}
  76   */
  77  export const triggerUploadCompleted = elementId => {
  78      // Remove the additional check for changes made.
  79      window.removeEventListener('beforeunload', changesMadeCheck);
  80      const customEvent = new CustomEvent(types.uploadCompleted, {
  81          bubbles: true,
  82          cancellable: false
  83      });
  84      const element = document.getElementById(elementId);
  85      element.dispatchEvent(customEvent);
  86  
  87      return customEvent;
  88  };
  89  
  90  /**
  91   * Trigger an event to notify the file upload field has been changed.
  92   *
  93   * @method
  94   * @param {String} elementId The element which was changed
  95   * @returns {CustomEvent}
  96   */
  97   export const notifyUploadChanged = elementId => {
  98  
  99      const customEvent = new CustomEvent(types.uploadChanged, {
 100          bubbles: true,
 101          cancellable: false
 102      });
 103  
 104      const element = document.getElementById(elementId);
 105      element.dispatchEvent(customEvent);
 106  
 107      return customEvent;
 108  };


Generated: Wed Jan 22 11:59:49 2025 Cross-referenced by PHPXref 0.7.1