course/format/amd/src/local/content/section.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. * Course section format component.
  17. *
  18. * @module core_courseformat/local/content/section
  19. * @class core_courseformat/local/content/section
  20. * @copyright 2021 Ferran Recio <ferran@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import Header from 'core_courseformat/local/content/section/header';
  24. import DndSection from 'core_courseformat/local/courseeditor/dndsection';
  25. import Templates from 'core/templates';
  26. import Pending from "core/pending";
  27. export default class extends DndSection {
  28. /**
  29. * Constructor hook.
  30. */
  31. create() {
  32. // Optional component name for debugging.
  33. this.name = 'content_section';
  34. // Default query selectors.
  35. this.selectors = {
  36. ACTIONMENU: '.section-actions',
  37. SECTION_ITEM: `[data-for='section_title']`,
  38. CM: `[data-for="cmitem"]`,
  39. SECTIONINFO: `[data-for="sectioninfo"]`,
  40. SECTIONBADGES: `[data-region="sectionbadges"]`,
  41. SHOWSECTION: `[data-action="sectionShow"]`,
  42. HIDESECTION: `[data-action="sectionHide"]`,
  43. ACTIONTEXT: `.menu-action-text`,
  44. ICON: `.icon`,
  45. };
  46. // Most classes will be loaded later by DndCmItem.
  47. this.classes = {
  48. LOCKED: 'editinprogress',
  49. HASDESCRIPTION: 'description',
  50. HIDE: 'd-none',
  51. HIDDEN: 'hidden',
  52. CURRENT: 'current',
  53. };
  54. // We need our id to watch specific events.
  55. this.id = this.element.dataset.id;
  56. }
  57. /**
  58. * Initial state ready method.
  59. *
  60. * @param {Object} state the initial state
  61. */
  62. stateReady(state) {
  63. this.configState(state);
  64. // Drag and drop is only available for components compatible course formats.
  65. if (this.reactive.isEditing && this.reactive.supportComponents) {
  66. // Section zero and other formats sections may not have a title to drag.
  67. const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
  68. if (sectionItem) {
  69. // Init the inner dragable element.
  70. const headerComponent = new Header({
  71. ...this,
  72. element: sectionItem,
  73. fullregion: this.element,
  74. });
  75. this.configDragDrop(headerComponent);
  76. }
  77. }
  78. this._openSectionIfNecessary();
  79. }
  80. /**
  81. * Open the section if the anchored activity is inside.
  82. */
  83. async _openSectionIfNecessary() {
  84. const pageCmInfo = this.reactive.getPageAnchorCmInfo();
  85. if (!pageCmInfo || pageCmInfo.sectionid !== this.id) {
  86. return;
  87. }
  88. await this.reactive.dispatch('sectionContentCollapsed', [this.id], false);
  89. const pendingOpen = new Pending(`courseformat/section:openSectionIfNecessary`);
  90. setTimeout(() => {
  91. document.querySelector("#" + pageCmInfo.anchor).scrollIntoView();
  92. this.reactive.dispatch('setPageItem', 'cm', pageCmInfo.id);
  93. pendingOpen.resolve();
  94. }, 250);
  95. }
  96. /**
  97. * Component watchers.
  98. *
  99. * @returns {Array} of watchers
  100. */
  101. getWatchers() {
  102. return [
  103. {watch: `section[${this.id}]:updated`, handler: this._refreshSection},
  104. ];
  105. }
  106. /**
  107. * Validate if the drop data can be dropped over the component.
  108. *
  109. * @param {Object} dropdata the exported drop data.
  110. * @returns {boolean}
  111. */
  112. validateDropData(dropdata) {
  113. // If the format uses one section per page sections dropping in the content is ignored.
  114. if (dropdata?.type === 'section' && this.reactive.sectionReturn !== null) {
  115. return false;
  116. }
  117. return super.validateDropData(dropdata);
  118. }
  119. /**
  120. * Get the last CM element of that section.
  121. *
  122. * @returns {element|null}
  123. */
  124. getLastCm() {
  125. const cms = this.getElements(this.selectors.CM);
  126. // DndUpload may add extra elements so :last-child selector cannot be used.
  127. if (!cms || cms.length === 0) {
  128. return null;
  129. }
  130. const lastCm = cms[cms.length - 1];
  131. // If it is a delegated section return the last item overall.
  132. if (this.section.component !== null) {
  133. return lastCm;
  134. }
  135. // If it is a regular section and the last item overall has a parent cm, return the parent instead.
  136. const parentSection = lastCm.parentNode.closest(this.selectors.CM);
  137. return parentSection ?? lastCm;
  138. }
  139. /**
  140. * Get a fallback element when there is no CM in the section.
  141. *
  142. * @returns {element|null} the las course module element of the section.
  143. */
  144. getLastCmFallback() {
  145. // The sectioninfo is always present, even when the section is empty.
  146. return this.getElement(this.selectors.SECTIONINFO);
  147. }
  148. /**
  149. * Update a content section using the state information.
  150. *
  151. * @param {object} param
  152. * @param {Object} param.element details the update details.
  153. */
  154. _refreshSection({element}) {
  155. // Update classes.
  156. this.element.classList.toggle(this.classes.DRAGGING, element.dragging ?? false);
  157. this.element.classList.toggle(this.classes.LOCKED, element.locked ?? false);
  158. this.element.classList.toggle(this.classes.HIDDEN, !element.visible ?? false);
  159. this.element.classList.toggle(this.classes.CURRENT, element.current ?? false);
  160. this.locked = element.locked;
  161. // The description box classes depends on the section state.
  162. const sectioninfo = this.getElement(this.selectors.SECTIONINFO);
  163. if (sectioninfo) {
  164. sectioninfo.classList.toggle(this.classes.HASDESCRIPTION, element.hasrestrictions);
  165. }
  166. // Update section badges and menus.
  167. this._updateBadges(element);
  168. this._updateActionsMenu(element);
  169. }
  170. /**
  171. * Update a section badges using the state information.
  172. *
  173. * @param {object} section the section state.
  174. */
  175. _updateBadges(section) {
  176. const current = this.getElement(`${this.selectors.SECTIONBADGES} [data-type='iscurrent']`);
  177. current?.classList.toggle(this.classes.HIDE, !section.current);
  178. const hiddenFromStudents = this.getElement(`${this.selectors.SECTIONBADGES} [data-type='hiddenfromstudents']`);
  179. hiddenFromStudents?.classList.toggle(this.classes.HIDE, section.visible);
  180. }
  181. /**
  182. * Update a section action menus.
  183. *
  184. * @param {object} section the section state.
  185. */
  186. async _updateActionsMenu(section) {
  187. let selector;
  188. let newAction;
  189. if (section.visible) {
  190. selector = this.selectors.SHOWSECTION;
  191. newAction = 'sectionHide';
  192. } else {
  193. selector = this.selectors.HIDESECTION;
  194. newAction = 'sectionShow';
  195. }
  196. // Find the affected action.
  197. const affectedAction = this._getActionMenu(selector);
  198. if (!affectedAction) {
  199. return;
  200. }
  201. // Change action.
  202. affectedAction.dataset.action = newAction;
  203. // Change text.
  204. const actionText = affectedAction.querySelector(this.selectors.ACTIONTEXT);
  205. if (affectedAction.dataset?.swapname && actionText) {
  206. const oldText = actionText?.innerText;
  207. actionText.innerText = affectedAction.dataset.swapname;
  208. affectedAction.dataset.swapname = oldText;
  209. }
  210. // Change icon.
  211. const icon = affectedAction.querySelector(this.selectors.ICON);
  212. if (affectedAction.dataset?.swapicon && icon) {
  213. const newIcon = affectedAction.dataset.swapicon;
  214. affectedAction.dataset.swapicon = affectedAction.dataset.icon;
  215. affectedAction.dataset.icon = newIcon;
  216. if (newIcon) {
  217. const pixHtml = await Templates.renderPix(newIcon, 'core');
  218. Templates.replaceNode(icon, pixHtml, '');
  219. }
  220. }
  221. }
  222. /**
  223. * Get the action menu element from the selector.
  224. *
  225. * @param {string} selector The selector to find the action menu.
  226. * @returns The action menu element.
  227. */
  228. _getActionMenu(selector) {
  229. return document.querySelector(`${this.selectors.ACTIONMENU}[data-sectionid='${this.id}'] ${selector}`);
  230. }
  231. }