reportbuilder/amd/src/editor.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. * Report builder editor
  17. *
  18. * @module core_reportbuilder/editor
  19. * @copyright 2021 David Matamoros <davidmc@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. "use strict";
  23. import $ from 'jquery';
  24. import 'core/inplace_editable';
  25. import {addIconToContainer} from 'core/loadingicon';
  26. import Notification from 'core/notification';
  27. import Pending from 'core/pending';
  28. import Templates from 'core/templates';
  29. import {getString} from 'core/str';
  30. import {add as addToast} from 'core/toast';
  31. import * as reportSelectors from 'core_reportbuilder/local/selectors';
  32. import {init as columnsEditorInit} from 'core_reportbuilder/local/editor/columns';
  33. import {init as conditionsEditorInit} from 'core_reportbuilder/local/editor/conditions';
  34. import {init as filtersEditorInit} from 'core_reportbuilder/local/editor/filters';
  35. import {init as sortingEditorInit} from 'core_reportbuilder/local/editor/sorting';
  36. import {init as cardviewEditorInit} from 'core_reportbuilder/local/editor/card_view';
  37. import {getReport} from 'core_reportbuilder/local/repository/reports';
  38. import {createReportModal} from 'core_reportbuilder/local/repository/modals';
  39. let initialized = false;
  40. /**
  41. * Initialise editor and all it's modules
  42. */
  43. export const init = () => {
  44. columnsEditorInit(initialized);
  45. conditionsEditorInit(initialized);
  46. filtersEditorInit(initialized);
  47. sortingEditorInit(initialized);
  48. cardviewEditorInit(initialized);
  49. // Ensure we only add our listeners once (can be called multiple times by mustache template).
  50. if (initialized) {
  51. return;
  52. }
  53. // Add event handlers to generic report editor elements.
  54. document.addEventListener('click', event => {
  55. // Toggle between edit and preview mode.
  56. const toggleEditViewMode = event.target.closest(reportSelectors.actions.toggleEditPreview);
  57. if (toggleEditViewMode) {
  58. event.preventDefault();
  59. const reportElement = event.target.closest(reportSelectors.regions.report);
  60. const pendingPromise = new Pending('core_reportbuilder/reports:get');
  61. const toggledEditMode = toggleEditViewMode.dataset.editMode !== "1";
  62. addIconToContainer(toggleEditViewMode)
  63. .then(() => getReport(reportElement.dataset.reportId, toggledEditMode))
  64. .then(response => Promise.all([
  65. $.parseHTML(response.javascript, null, true).map(node => node.innerHTML).join("\n"),
  66. Templates.renderForPromise('core_reportbuilder/local/dynamictabs/editor', response),
  67. ]))
  68. .then(([responseJs, {html, js}]) => Templates.replaceNode(reportElement, html, js + responseJs))
  69. .then(() => pendingPromise.resolve())
  70. .catch(Notification.exception);
  71. }
  72. // Edit report details modal.
  73. const reportEdit = event.target.closest(reportSelectors.actions.reportEdit);
  74. if (reportEdit) {
  75. event.preventDefault();
  76. const reportModal = createReportModal(event.target, getString('editreportdetails', 'core_reportbuilder'),
  77. reportEdit.dataset.reportId);
  78. reportModal.addEventListener(reportModal.events.FORM_SUBMITTED, () => {
  79. getString('reportupdated', 'core_reportbuilder')
  80. .then(addToast)
  81. .then(() => {
  82. return window.location.reload();
  83. })
  84. .catch(Notification.exception);
  85. });
  86. reportModal.show();
  87. }
  88. });
  89. initialized = true;
  90. };