calendar/amd/src/summary_modal.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. * A javascript module to handle summary modal.
  17. *
  18. * @module core_calendar/summary_modal
  19. * @copyright 2017 Simey Lameze <simey@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import $ from 'jquery';
  23. import * as CustomEvents from 'core/custom_interaction_events';
  24. import Modal from 'core/modal';
  25. import CalendarEvents from './events';
  26. import * as CalendarCrud from 'core_calendar/crud';
  27. import * as ModalEvents from 'core/modal_events';
  28. const SELECTORS = {
  29. ROOT: "[data-region='summary-modal-container']",
  30. EDIT_BUTTON: '[data-action="edit"]',
  31. DELETE_BUTTON: '[data-action="delete"]',
  32. };
  33. export default class ModalEventSummary extends Modal {
  34. static TEMPLATE = 'core_calendar/event_summary_modal';
  35. static TYPE = 'core_calendar-event_summary';
  36. /**
  37. * Get the edit button element from the footer. The button is cached
  38. * as it's not expected to change.
  39. *
  40. * @method getEditButton
  41. * @return {object} button element
  42. */
  43. getEditButton() {
  44. if (typeof this.editButton == 'undefined') {
  45. this.editButton = this.getFooter().find(SELECTORS.EDIT_BUTTON);
  46. }
  47. return this.editButton;
  48. }
  49. /**
  50. * Get the delete button element from the footer. The button is cached
  51. * as it's not expected to change.
  52. *
  53. * @method getDeleteButton
  54. * @return {object} button element
  55. */
  56. getDeleteButton() {
  57. if (typeof this.deleteButton == 'undefined') {
  58. this.deleteButton = this.getFooter().find(SELECTORS.DELETE_BUTTON);
  59. }
  60. return this.deleteButton;
  61. }
  62. /**
  63. * Get the id for the event being shown in this modal. This value is
  64. * not cached because it will change depending on which event is
  65. * being displayed.
  66. *
  67. * @method getEventId
  68. * @return {int}
  69. */
  70. getEventId() {
  71. return this.getBody().find(SELECTORS.ROOT).attr('data-event-id');
  72. }
  73. /**
  74. * Get the title for the event being shown in this modal. This value is
  75. * not cached because it will change depending on which event is
  76. * being displayed.
  77. *
  78. * @method getEventTitle
  79. * @return {String}
  80. */
  81. getEventTitle() {
  82. return this.getBody().find(SELECTORS.ROOT).attr('data-event-title');
  83. }
  84. /**
  85. * Get the number of events in the series for the event being shown in
  86. * this modal. This value is not cached because it will change
  87. * depending on which event is being displayed.
  88. *
  89. * @method getEventCount
  90. * @return {int}
  91. */
  92. getEventCount() {
  93. return this.getBody().find(SELECTORS.ROOT).attr('data-event-count');
  94. }
  95. /**
  96. * Get the url for the event being shown in this modal.
  97. *
  98. * @method getEventUrl
  99. * @return {String}
  100. */
  101. getEditUrl() {
  102. return this.getBody().find(SELECTORS.ROOT).attr('data-edit-url');
  103. }
  104. /**
  105. * Is this an action event.
  106. *
  107. * @method getEventUrl
  108. * @return {String}
  109. */
  110. isActionEvent() {
  111. return (this.getBody().find(SELECTORS.ROOT).attr('data-action-event') == 'true');
  112. }
  113. /**
  114. * Set up all of the event handling for the modal.
  115. *
  116. * @method registerEventListeners
  117. */
  118. registerEventListeners() {
  119. // Apply parent event listeners.
  120. super.registerEventListeners(this);
  121. // We have to wait for the modal to finish rendering in order to ensure that
  122. // the data-event-title property is available to use as the modal title.
  123. M.util.js_pending('core_calendar/summary_modal:registerEventListeners:bodyRendered');
  124. this.getRoot().on(ModalEvents.bodyRendered, function() {
  125. this.getModal().data({
  126. eventTitle: this.getEventTitle(),
  127. eventId: this.getEventId(),
  128. eventCount: this.getEventCount(),
  129. })
  130. .attr('data-type', 'event');
  131. CalendarCrud.registerRemove(this.getModal());
  132. M.util.js_complete('core_calendar/summary_modal:registerEventListeners:bodyRendered');
  133. }.bind(this));
  134. $('body').on(CalendarEvents.deleted, function() {
  135. // Close the dialogue on delete.
  136. this.hide();
  137. }.bind(this));
  138. CustomEvents.define(this.getEditButton(), [
  139. CustomEvents.events.activate
  140. ]);
  141. this.getEditButton().on(CustomEvents.events.activate, function(e, data) {
  142. if (this.isActionEvent()) {
  143. // Action events cannot be edited on the event form and must be redirected to the module UI.
  144. $('body').trigger(CalendarEvents.editActionEvent, [this.getEditUrl()]);
  145. } else {
  146. // When the edit button is clicked we fire an event for the calendar UI to handle.
  147. // We don't care how the UI chooses to handle it.
  148. $('body').trigger(CalendarEvents.editEvent, [this.getEventId()]);
  149. }
  150. // There is nothing else for us to do so let's hide.
  151. this.hide();
  152. // We've handled this event so no need to propagate it.
  153. e.preventDefault();
  154. e.stopPropagation();
  155. data.originalEvent.preventDefault();
  156. data.originalEvent.stopPropagation();
  157. }.bind(this));
  158. }
  159. }
  160. ModalEventSummary.registerModalType();