course/format/topics/amd/src/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. * Format topics section extra logic component.
  17. *
  18. * @module format_topics/section
  19. * @copyright 2022 Ferran Recio <ferran@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {BaseComponent} from 'core/reactive';
  23. import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
  24. import Templates from 'core/templates';
  25. class HighlightSection extends BaseComponent {
  26. /**
  27. * Constructor hook.
  28. */
  29. create() {
  30. // Optional component name for debugging.
  31. this.name = 'format_topics_section';
  32. // Default query selectors.
  33. this.selectors = {
  34. SETMARKER: `[data-action="sectionHighlight"]`,
  35. REMOVEMARKER: `[data-action="sectionUnhighlight"]`,
  36. ACTIONTEXT: `.menu-action-text`,
  37. ICON: `.icon`,
  38. };
  39. // Default classes to toggle on refresh.
  40. this.classes = {
  41. HIDE: 'd-none',
  42. };
  43. // The topics format section specific actions.
  44. this.formatActions = {
  45. HIGHLIGHT: 'sectionHighlight',
  46. UNHIGHLIGHT: 'sectionUnhighlight',
  47. };
  48. }
  49. /**
  50. * Component watchers.
  51. *
  52. * @returns {Array} of watchers
  53. */
  54. getWatchers() {
  55. return [
  56. {watch: `section.current:updated`, handler: this._refreshHighlight},
  57. ];
  58. }
  59. /**
  60. * Update a content section using the state information.
  61. *
  62. * @param {object} param
  63. * @param {Object} param.element details the update details.
  64. */
  65. async _refreshHighlight({element}) {
  66. let selector;
  67. let newAction;
  68. if (element.current) {
  69. selector = this.selectors.SETMARKER;
  70. newAction = this.formatActions.UNHIGHLIGHT;
  71. } else {
  72. selector = this.selectors.REMOVEMARKER;
  73. newAction = this.formatActions.HIGHLIGHT;
  74. }
  75. // Find the affected action.
  76. const affectedAction = this.getElement(`${selector}`, element.id);
  77. if (!affectedAction) {
  78. return;
  79. }
  80. // Change action, text and icon.
  81. affectedAction.dataset.action = newAction;
  82. const actionText = affectedAction.querySelector(this.selectors.ACTIONTEXT);
  83. if (affectedAction.dataset?.swapname && actionText) {
  84. const oldText = actionText?.innerText;
  85. actionText.innerText = affectedAction.dataset.swapname;
  86. affectedAction.dataset.swapname = oldText;
  87. }
  88. const icon = affectedAction.querySelector(this.selectors.ICON);
  89. if (affectedAction.dataset?.swapicon && icon) {
  90. const newIcon = affectedAction.dataset.swapicon;
  91. if (newIcon) {
  92. const pixHtml = await Templates.renderPix(newIcon, 'core');
  93. Templates.replaceNode(icon, pixHtml, '');
  94. affectedAction.dataset.swapicon = affectedAction.dataset.icon;
  95. affectedAction.dataset.icon = newIcon;
  96. }
  97. }
  98. }
  99. }
  100. export const init = () => {
  101. // Add component to the section.
  102. const courseEditor = getCurrentCourseEditor();
  103. if (courseEditor.supportComponents && courseEditor.isEditing) {
  104. new HighlightSection({
  105. element: document.getElementById('page'),
  106. reactive: courseEditor,
  107. });
  108. }
  109. };