course/format/amd/src/courseeditor.js

  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. * Generic reactive module used in the course editor.
  17. *
  18. * @module core_courseformat/courseeditor
  19. * @copyright 2021 Ferran Recio <ferran@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import DefaultMutations from 'core_courseformat/local/courseeditor/mutations';
  23. import CourseEditor from 'core_courseformat/local/courseeditor/courseeditor';
  24. import events from 'core_course/events';
  25. // A map with all the course editor instances.
  26. const courseEditorMap = new Map();
  27. // Map with all the state keys the backend send us to know if the frontend cache is valid or not.
  28. const courseStateKeyMap = new Map();
  29. /**
  30. * Trigger a state changed event.
  31. *
  32. * This function will be moved to core_course/events module
  33. * when the file is migrated to the new JS events structure proposed in MDL-70990.
  34. *
  35. * @method dispatchStateChangedEvent
  36. * @param {object} detail the full state
  37. * @param {object} target the custom event target (document if none provided)
  38. */
  39. function dispatchStateChangedEvent(detail, target) {
  40. if (target === undefined) {
  41. target = document;
  42. }
  43. target.dispatchEvent(new CustomEvent(events.stateChanged, {
  44. bubbles: true,
  45. detail: detail,
  46. }));
  47. }
  48. /**
  49. * Setup the current view settings
  50. *
  51. * The backend cache state revision is a combination of the course->cacherev, the
  52. * user course preferences and completion state. The backend updates that number
  53. * everytime some change in the course affects the user course state.
  54. *
  55. * @param {number} courseId the course id
  56. * @param {setup} setup format, page and course settings
  57. * @param {boolean} setup.editing if the page is in edit mode
  58. * @param {boolean} setup.supportscomponents if the format supports components for content
  59. * @param {String} setup.statekey the backend cached state revision
  60. * @param {Array} setup.overriddenStrings optional overridden strings
  61. */
  62. export const setViewFormat = (courseId, setup) => {
  63. courseId = parseInt(courseId);
  64. // Caches are ignored in edit mode.
  65. if (!setup.editing) {
  66. courseStateKeyMap.set(courseId, setup.statekey);
  67. }
  68. const editor = getCourseEditor(courseId);
  69. editor.setViewFormat(setup);
  70. };
  71. /**
  72. * Get a specific course editor reactive instance.
  73. *
  74. * @param {number} courseId the course id
  75. * @returns {CourseEditor}
  76. */
  77. export const getCourseEditor = (courseId) => {
  78. courseId = parseInt(courseId);
  79. if (!courseEditorMap.has(courseId)) {
  80. courseEditorMap.set(
  81. courseId,
  82. new CourseEditor({
  83. name: `CourseEditor${courseId}`,
  84. eventName: events.stateChanged,
  85. eventDispatch: dispatchStateChangedEvent,
  86. // Mutations can be overridden by the format plugin using setMutations
  87. // but we need the default one at least.
  88. mutations: new DefaultMutations(),
  89. })
  90. );
  91. courseEditorMap.get(courseId).loadCourse(courseId, courseStateKeyMap.get(courseId));
  92. }
  93. return courseEditorMap.get(courseId);
  94. };
  95. /**
  96. * Get the current course reactive instance.
  97. *
  98. * @returns {CourseEditor}
  99. */
  100. export const getCurrentCourseEditor = () => getCourseEditor(M.cfg.courseId);