lib/amd/src/modal_save_cancel.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 save/cancel modal.
  17. *
  18. * @module core/modal_save_cancel
  19. * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Modal from 'core/modal';
  23. import Notification from 'core/notification';
  24. /**
  25. * The Save/Cancel Modal.
  26. *
  27. * @class
  28. * @extends module:core/modal
  29. */
  30. export default class ModalSaveCancel extends Modal {
  31. static TYPE = 'SAVE_CANCEL';
  32. static TEMPLATE = 'core/modal_save_cancel';
  33. constructor(root) {
  34. super(root);
  35. if (!this.getFooter().find(this.getActionSelector('save')).length) {
  36. Notification.exception({message: 'No save button found'});
  37. }
  38. if (!this.getFooter().find(this.getActionSelector('cancel')).length) {
  39. Notification.exception({message: 'No cancel button found'});
  40. }
  41. }
  42. /**
  43. * Register all event listeners.
  44. */
  45. registerEventListeners() {
  46. // Call the parent registration.
  47. super.registerEventListeners();
  48. // Register to close on save/cancel.
  49. this.registerCloseOnSave();
  50. this.registerCloseOnCancel();
  51. }
  52. /**
  53. * Override parent implementation to prevent changing the footer content.
  54. */
  55. setFooter() {
  56. Notification.exception({message: 'Can not change the footer of a save cancel modal'});
  57. return;
  58. }
  59. /**
  60. * Set the title of the save button.
  61. *
  62. * @param {String|Promise} value The button text, or a Promise which will resolve it
  63. * @returns{Promise}
  64. */
  65. setSaveButtonText(value) {
  66. return this.setButtonText('save', value);
  67. }
  68. }
  69. ModalSaveCancel.registerModalType();