diff --git a/course/amd/build/sectionlistener.min.js b/course/amd/build/sectionlistener.min.js new file mode 100644 index 00000000000..e1b6611c456 --- /dev/null +++ b/course/amd/build/sectionlistener.min.js @@ -0,0 +1 @@ +define(["jquery"],function(a){var b=function(b){var d=a(this);if("sectionname"===d.data("itemtype")){var e=M.course.format.get_config(),f=d.closest("."+e.section_class);c(f,b.ajaxreturn.value)}},c=function(a,b){a.attr("aria-label",b),a.find(".hidden.sectionname").text(b)};a("body").on("updated",".inplaceeditable",b)}); \ No newline at end of file diff --git a/course/amd/src/sectionlistener.js b/course/amd/src/sectionlistener.js new file mode 100644 index 00000000000..81708f061d9 --- /dev/null +++ b/course/amd/src/sectionlistener.js @@ -0,0 +1,56 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Define listeners related to sections. + * + * @module core_course/sectionlistener + * @package core_course + * @copyright 2020 University of Nottingham + * @author Neill Magill + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +define(['jquery'], function($) { + /** + * Processes an updated event. + * + * We only want to handle events related to a section, and not activities. + * + * @param {Event} e The updated event + */ + var processUpdatedEvent = function(e) { + var editable = $(this); + if (editable.data('itemtype') === 'sectionname') { + var config = M.course.format.get_config(); + // A section name got updated. + var section = editable.closest('.' + config.section_class); + updateSectionName(section, e.ajaxreturn.value); + } + // Ignore other types of inplace updates, for example activity name changes. + }; + + /** + * Updates the name of the section in places outside of the inplace editable field. + * + * @param {jQuery} section The section that was updated. + * @param {String} newname The new name for the section. + */ + var updateSectionName = function(section, newname) { + section.attr('aria-label', newname); + section.find('.hidden.sectionname').text(newname); + }; + + $('body').on('updated', '.inplaceeditable', processUpdatedEvent); +}); \ No newline at end of file diff --git a/course/format/topics/format.php b/course/format/topics/format.php index 7af285109dc..da0f8010a7b 100644 --- a/course/format/topics/format.php +++ b/course/format/topics/format.php @@ -59,3 +59,4 @@ if (!empty($displaysection)) { // Include course format js module $PAGE->requires->js('/course/format/topics/format.js'); +$PAGE->requires->js_call_amd('core_course/sectionlistener'); diff --git a/course/format/weeks/format.php b/course/format/weeks/format.php index da82d080a72..6cfad23b86c 100644 --- a/course/format/weeks/format.php +++ b/course/format/weeks/format.php @@ -50,3 +50,4 @@ if (!empty($displaysection)) { } $PAGE->requires->js('/course/format/weeks/format.js'); +$PAGE->requires->js_call_amd('core_course/sectionlistener');