course/format/amd/src/local/courseeditor/dndsection.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 index section component.
  17. *
  18. * This component is used to control specific course section interactions like drag and drop
  19. * in both course index and course content.
  20. *
  21. * @module core_courseformat/local/courseeditor/dndsection
  22. * @class core_courseformat/local/courseeditor/dndsection
  23. * @copyright 2021 Ferran Recio <ferran@moodle.com>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. import {BaseComponent, DragDrop} from 'core/reactive';
  27. import {getString} from 'core/str';
  28. import {prefetchStrings} from 'core/prefetch';
  29. import Templates from 'core/templates';
  30. // Load global strings.
  31. prefetchStrings('core', ['addfilehere']);
  32. export default class extends BaseComponent {
  33. /**
  34. * Save some values form the state.
  35. *
  36. * @param {Object} state the current state
  37. */
  38. configState(state) {
  39. this.id = this.element.dataset.id;
  40. this.section = state.section.get(this.id);
  41. this.course = state.course;
  42. }
  43. /**
  44. * Register state values and the drag and drop subcomponent.
  45. *
  46. * @param {BaseComponent} sectionitem section item component
  47. */
  48. configDragDrop(sectionitem) {
  49. // Drag and drop is only available for components compatible course formats.
  50. if (this.reactive.isEditing && this.reactive.supportComponents) {
  51. // Init the inner dragable element.
  52. this.sectionitem = sectionitem;
  53. // Init the dropzone.
  54. this.dragdrop = new DragDrop(this);
  55. // Save dropzone classes.
  56. this.classes = this.dragdrop.getClasses();
  57. }
  58. }
  59. /**
  60. * Remove all subcomponents dependencies.
  61. */
  62. destroy() {
  63. if (this.sectionitem !== undefined) {
  64. this.sectionitem.unregister();
  65. }
  66. if (this.dragdrop !== undefined) {
  67. this.dragdrop.unregister();
  68. }
  69. }
  70. /**
  71. * Get the last CM element of that section.
  72. *
  73. * @returns {element|null} the las course module element of the section.
  74. */
  75. getLastCm() {
  76. return null;
  77. }
  78. /**
  79. * Get a fallback element when there is no CM in the section.
  80. *
  81. * This is used to show the correct dropzone position.
  82. *
  83. * @returns {element|null} the las course module element of the section.
  84. */
  85. getLastCmFallback() {
  86. return null;
  87. }
  88. // Drag and drop methods.
  89. /**
  90. * The element drop start hook.
  91. *
  92. * @param {Object} dropdata the dropdata
  93. */
  94. dragStart(dropdata) {
  95. this.reactive.dispatch('sectionDrag', [dropdata.id], true);
  96. }
  97. /**
  98. * The element drop end hook.
  99. *
  100. * @param {Object} dropdata the dropdata
  101. */
  102. dragEnd(dropdata) {
  103. this.reactive.dispatch('sectionDrag', [dropdata.id], false);
  104. }
  105. /**
  106. * Validate if the drop data can be dropped over the component.
  107. *
  108. * @param {Object} dropdata the exported drop data.
  109. * @returns {boolean}
  110. */
  111. validateDropData(dropdata) {
  112. // We accept files.
  113. if (dropdata?.type === 'files') {
  114. return true;
  115. }
  116. // We accept any course module unless it can form a subsection loop.
  117. if (dropdata?.type === 'cm') {
  118. if (this.section?.component && dropdata?.hasdelegatedsection === true) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. if (dropdata?.type === 'section') {
  124. // Sections controlled by a plugin cannot accept sections.
  125. if (this.section.component !== null) {
  126. return false;
  127. }
  128. // We accept any section but yourself and the next one.
  129. return dropdata?.id != this.id && dropdata?.number != this.section.number + 1;
  130. }
  131. return false;
  132. }
  133. /**
  134. * Display the component dropzone.
  135. *
  136. * @param {Object} dropdata the accepted drop data
  137. */
  138. showDropZone(dropdata) {
  139. if (dropdata.type == 'files') {
  140. this.addOverlay({
  141. content: getString('addfilehere', 'core'),
  142. icon: Templates.renderPix('t/download', 'core'),
  143. }).then(() => {
  144. // Check if we still need the file dropzone.
  145. if (!this.dragdrop?.isDropzoneVisible()) {
  146. this.removeOverlay();
  147. }
  148. return;
  149. }).catch((error) => {
  150. throw error;
  151. });
  152. }
  153. if (dropdata.type == 'cm') {
  154. const lastCm = this.getLastCm();
  155. lastCm?.classList.add(this.classes.DROPDOWN);
  156. if (!lastCm) {
  157. this.getLastCmFallback()?.classList.add(this.classes.DROPDOWN);
  158. }
  159. }
  160. if (dropdata.type == 'section') {
  161. this.element.classList.remove(this.classes.DROPUP);
  162. this.element.classList.add(this.classes.DROPDOWN);
  163. }
  164. }
  165. /**
  166. * Hide the component dropzone.
  167. */
  168. hideDropZone() {
  169. this.getLastCm()?.classList.remove(this.classes.DROPDOWN);
  170. this.getLastCmFallback()?.classList.remove(this.classes.DROPDOWN);
  171. this.element.classList.remove(this.classes.DROPUP);
  172. this.element.classList.remove(this.classes.DROPDOWN);
  173. this.removeOverlay();
  174. }
  175. /**
  176. * Drop event handler.
  177. *
  178. * @param {Object} dropdata the accepted drop data
  179. * @param {Event} event the drop event
  180. */
  181. drop(dropdata, event) {
  182. // File handling.
  183. if (dropdata.type == 'files') {
  184. this.reactive.uploadFiles(
  185. this.section.id,
  186. this.section.number,
  187. dropdata.files
  188. );
  189. return;
  190. }
  191. // Call the move mutation.
  192. if (dropdata.type == 'cm') {
  193. const mutation = (event.altKey) ? 'cmDuplicate' : 'cmMove';
  194. this.reactive.dispatch(mutation, [dropdata.id], this.id);
  195. }
  196. if (dropdata.type == 'section') {
  197. this.reactive.dispatch('sectionMoveAfter', [dropdata.id], this.id);
  198. }
  199. }
  200. }