mod/data/amd/src/templateseditor.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. * Javascript module to control the template editor.
  17. *
  18. * @module mod_data/templateseditor
  19. * @copyright 2021 Mihail Geshoski <mihail@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {getString} from 'core/str';
  23. import {prefetchStrings} from 'core/prefetch';
  24. import {relativeUrl} from 'core/url';
  25. import {saveCancel, deleteCancel} from 'core/notification';
  26. import Templates from 'core/templates';
  27. prefetchStrings('admin', ['confirmation']);
  28. prefetchStrings('mod_data', [
  29. 'resettemplateconfirmtitle',
  30. 'enabletemplateeditorcheck',
  31. 'editorenable'
  32. ]);
  33. prefetchStrings('core', [
  34. 'reset',
  35. ]);
  36. /**
  37. * Template editor constants.
  38. */
  39. const selectors = {
  40. toggleTemplateEditor: 'input[name="useeditor"]',
  41. resetTemplateAction: '[data-action="resettemplate"]',
  42. resetTemplate: 'input[name="defaultform"]',
  43. resetAllTemplates: 'input[name="resetall"]',
  44. resetAllCheck: 'input[name="resetallcheck"]',
  45. editForm: '#edittemplateform',
  46. };
  47. /**
  48. * Register event listeners for the module.
  49. *
  50. * @param {Number} instanceId The database ID
  51. * @param {string} mode The template mode
  52. */
  53. const registerEventListeners = (instanceId, mode) => {
  54. registerResetButton(mode);
  55. registerEditorToggler(instanceId, mode);
  56. };
  57. const registerResetButton = (mode) => {
  58. const editForm = document.querySelector(selectors.editForm);
  59. const resetTemplate = document.querySelector(selectors.resetTemplate);
  60. const resetAllTemplates = document.querySelector(selectors.resetAllTemplates);
  61. const resetTemplateAction = document.querySelector(selectors.resetTemplateAction);
  62. if (!resetTemplateAction || !resetTemplate || !editForm) {
  63. return;
  64. }
  65. prefetchStrings('mod_data', [
  66. mode
  67. ]);
  68. resetTemplateAction.addEventListener('click', async(event) => {
  69. event.preventDefault();
  70. const params = {
  71. resetallname: "resetallcheck",
  72. templatename: await getString(mode, 'mod_data'),
  73. };
  74. deleteCancel(
  75. getString('resettemplateconfirmtitle', 'mod_data'),
  76. Templates.render('mod_data/template_editor_resetmodal', params),
  77. getString('reset', 'core'),
  78. () => {
  79. resetTemplate.value = "true";
  80. editForm.submit();
  81. },
  82. null,
  83. {triggerElement: event.target}
  84. );
  85. });
  86. // The reset all checkbox is inside a modal so we need to capture at document level.
  87. if (!resetAllTemplates) {
  88. return;
  89. }
  90. document.addEventListener('change', (event) => {
  91. if (event.target.matches(selectors.resetAllCheck)) {
  92. resetAllTemplates.value = (event.target.checked) ? "true" : "";
  93. }
  94. });
  95. };
  96. const registerEditorToggler = (instanceId, mode) => {
  97. const toggleTemplateEditor = document.querySelector(selectors.toggleTemplateEditor);
  98. if (!toggleTemplateEditor) {
  99. return;
  100. }
  101. toggleTemplateEditor.addEventListener('click', async(event) => {
  102. event.preventDefault();
  103. // Whether the event action attempts to enable or disable the template editor.
  104. const enableTemplateEditor = event.target.checked;
  105. if (enableTemplateEditor) {
  106. // Display a confirmation dialog before enabling the template editor.
  107. saveCancel(
  108. getString('confirmation', 'admin'),
  109. getString('enabletemplateeditorcheck', 'mod_data'),
  110. getString('editorenable', 'mod_data'),
  111. () => {
  112. window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: true});
  113. },
  114. null,
  115. {triggerElement: event.target}
  116. );
  117. } else {
  118. window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: false});
  119. }
  120. });
  121. };
  122. /**
  123. * Initialize the module.
  124. *
  125. * @param {int} instanceId The database ID
  126. * @param {string} mode The template mode
  127. */
  128. export const init = (instanceId, mode) => {
  129. registerEventListeners(instanceId, mode);
  130. };