calendar/amd/src/modal_delete.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. * Contain the logic for the delete modal.
  17. *
  18. * @module core_calendar/modal_delete
  19. * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define([
  23. 'jquery',
  24. 'core/notification',
  25. 'core/custom_interaction_events',
  26. 'core/modal',
  27. 'core/modal_events',
  28. 'core/modal_registry',
  29. 'core_calendar/events',
  30. ],
  31. function(
  32. $,
  33. Notification,
  34. CustomEvents,
  35. Modal,
  36. ModalEvents,
  37. ModalRegistry,
  38. CalendarEvents
  39. ) {
  40. var registered = false;
  41. var SELECTORS = {
  42. DELETE_ONE_BUTTON: '[data-action="deleteone"]',
  43. DELETE_ALL_BUTTON: '[data-action="deleteall"]',
  44. CANCEL_BUTTON: '[data-action="cancel"]',
  45. };
  46. /**
  47. * Constructor for the Modal.
  48. *
  49. * @class
  50. * @param {object} root The root jQuery element for the modal
  51. */
  52. var ModalDelete = function(root) {
  53. Modal.call(this, root);
  54. this.setRemoveOnClose(true);
  55. };
  56. ModalDelete.TYPE = 'core_calendar-modal_delete';
  57. ModalDelete.prototype = Object.create(Modal.prototype);
  58. ModalDelete.prototype.constructor = ModalDelete;
  59. /**
  60. * Set up all of the event handling for the modal.
  61. *
  62. * @method registerEventListeners
  63. */
  64. ModalDelete.prototype.registerEventListeners = function() {
  65. // Apply parent event listeners.
  66. Modal.prototype.registerEventListeners.call(this);
  67. this.getModal().on(CustomEvents.events.activate, SELECTORS.DELETE_ONE_BUTTON, function(e, data) {
  68. var saveEvent = $.Event(ModalEvents.save);
  69. this.getRoot().trigger(saveEvent, this);
  70. if (!saveEvent.isDefaultPrevented()) {
  71. this.hide();
  72. data.originalEvent.preventDefault();
  73. }
  74. }.bind(this));
  75. this.getModal().on(CustomEvents.events.activate, SELECTORS.DELETE_ALL_BUTTON, function(e, data) {
  76. var saveEvent = $.Event(CalendarEvents.deleteAll);
  77. this.getRoot().trigger(saveEvent, this);
  78. if (!saveEvent.isDefaultPrevented()) {
  79. this.hide();
  80. data.originalEvent.preventDefault();
  81. }
  82. }.bind(this));
  83. this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, function(e, data) {
  84. var cancelEvent = $.Event(ModalEvents.cancel);
  85. this.getRoot().trigger(cancelEvent, this);
  86. if (!cancelEvent.isDefaultPrevented()) {
  87. this.hide();
  88. data.originalEvent.preventDefault();
  89. }
  90. }.bind(this));
  91. };
  92. // Automatically register with the modal registry the first time this module is imported so that you can create modals
  93. // of this type using the modal factory.
  94. if (!registered) {
  95. ModalRegistry.register(ModalDelete.TYPE, ModalDelete, 'calendar/event_delete_modal');
  96. registered = true;
  97. }
  98. return ModalDelete;
  99. });