| 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 for question_text_format question bank control.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     qbank_viewquestiontext/question_text_format
  | 
        
        
            | 
            | 
           20 | 
            * @copyright  2023 Catalyst IT Europe Ltd.
  | 
        
        
            | 
            | 
           21 | 
            * @author     Mark Johnson <mark.johnson@catalyst-eu.net>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           import * as repository from 'qbank_viewquestiontext/repository';
  | 
        
        
            | 
            | 
           26 | 
           import RefreshUi from 'core_question/refresh_ui';
  | 
        
        
            | 
            | 
           27 | 
           import Notification from 'core/notification';
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           const SELECTORS = {
  | 
        
        
            | 
            | 
           30 | 
               formatSelectId: 'question-text-format',
  | 
        
        
            | 
            | 
           31 | 
               returnUrl: '[name=returnurl]',
  | 
        
        
            | 
            | 
           32 | 
           };
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           let uiRoot;
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
           /**
  | 
        
        
            | 
            | 
           37 | 
            * Save the selected format via a web service call, and refresh the UI.
  | 
        
        
            | 
            | 
           38 | 
            *
  | 
        
        
            | 
            | 
           39 | 
            * @param {Event} e Select field change event.
  | 
        
        
            | 
            | 
           40 | 
            * @return {Promise<void>}
  | 
        
        
            | 
            | 
           41 | 
            */
  | 
        
        
            | 
            | 
           42 | 
           const handleFormatChange = async(e) => {
  | 
        
        
            | 
            | 
           43 | 
               const value = e.target.value;
  | 
        
        
            | 
            | 
           44 | 
               try {
  | 
        
        
            | 
            | 
           45 | 
                   await repository.setQuestionTextFormat(value);
  | 
        
        
            | 
            | 
           46 | 
                   const returnUrlInput = e.target.closest('form').querySelector(SELECTORS.returnUrl);
  | 
        
        
            | 
            | 
           47 | 
                   const returnUrl = new URL(returnUrlInput.value);
  | 
        
        
            | 
            | 
           48 | 
                   await RefreshUi.refresh(uiRoot, returnUrl);
  | 
        
        
            | 
            | 
           49 | 
               } catch (ex) {
  | 
        
        
            | 
            | 
           50 | 
                   Notification.exception(ex);
  | 
        
        
            | 
            | 
           51 | 
               }
  | 
        
        
            | 
            | 
           52 | 
           };
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
           /**
  | 
        
        
            | 
            | 
           55 | 
            * Initialise question text format widget.
  | 
        
        
            | 
            | 
           56 | 
            *
  | 
        
        
            | 
            | 
           57 | 
            * Find the uiRoot element and attach a change listener to the question text format selector.
  | 
        
        
            | 
            | 
           58 | 
            *
  | 
        
        
            | 
            | 
           59 | 
            * @param {String} uiRootId
  | 
        
        
            | 
            | 
           60 | 
            */
  | 
        
        
            | 
            | 
           61 | 
           export const init = (uiRootId) => {
  | 
        
        
            | 
            | 
           62 | 
               uiRoot = document.getElementById(uiRootId);
  | 
        
        
            | 
            | 
           63 | 
               const select = document.getElementById(SELECTORS.formatSelectId);
  | 
        
        
            | 
            | 
           64 | 
               select.addEventListener('change', handleFormatChange);
  | 
        
        
            | 
            | 
           65 | 
           };
  |