reportbuilder/amd/src/local/editor/card_view.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 card view editor
  17. *
  18. * @module core_reportbuilder/local/editor/card_view
  19. * @copyright 2021 Mikel Martín <mikel@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. "use strict";
  23. import DynamicForm from 'core_form/dynamicform';
  24. import {add as addToast} from 'core/toast';
  25. import {getString} from "core/str";
  26. import {subscribe as subscribe} from 'core/pubsub';
  27. import Notification from 'core/notification';
  28. import * as reportEvents from 'core_reportbuilder/local/events';
  29. import * as reportSelectors from 'core_reportbuilder/local/selectors';
  30. let cardViewForm = null;
  31. /**
  32. * Initialise card view form, must be called on each init because the form container is re-created when switching editor modes
  33. */
  34. const initCardViewForm = () => {
  35. const cardViewFormContainer = document.querySelector(reportSelectors.regions.settingsCardView);
  36. if (!cardViewFormContainer) {
  37. return;
  38. }
  39. cardViewForm = new DynamicForm(cardViewFormContainer, '\\core_reportbuilder\\form\\card_view');
  40. cardViewForm.addEventListener(cardViewForm.events.FORM_SUBMITTED, (event) => {
  41. event.preventDefault();
  42. getString('cardviewsettingssaved', 'core_reportbuilder')
  43. .then(addToast)
  44. .catch(Notification.exception);
  45. });
  46. };
  47. /**
  48. * Initialise module
  49. *
  50. * @param {Boolean} initialized Ensure we only add our listeners once
  51. */
  52. export const init = (initialized) => {
  53. initCardViewForm();
  54. if (initialized) {
  55. return;
  56. }
  57. // Update form each time a column is added or removed to the custom report.
  58. subscribe(reportEvents.publish.reportColumnsUpdated, () => {
  59. const reportElement = document.querySelector(reportSelectors.regions.report);
  60. cardViewForm.load({reportid: reportElement.dataset.reportId});
  61. });
  62. };