admin/tool/dataprivacy/amd/src/add_category.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. * Module to add categories.
  17. *
  18. * @module tool_dataprivacy/add_category
  19. * @copyright 2018 David Monllao
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define([
  23. 'jquery',
  24. 'core/str',
  25. 'core/ajax',
  26. 'core/notification',
  27. 'core/modal_save_cancel',
  28. 'core/modal_events',
  29. 'core/fragment',
  30. 'core_form/changechecker',
  31. ], function(
  32. $,
  33. Str,
  34. Ajax,
  35. Notification,
  36. ModalSaveCancel,
  37. ModalEvents,
  38. Fragment,
  39. FormChangeChecker
  40. ) {
  41. var SELECTORS = {
  42. CATEGORY_LINK: '[data-add-element="category"]',
  43. };
  44. var AddCategory = function(contextId) {
  45. this.contextId = contextId;
  46. var stringKeys = [
  47. {
  48. key: 'addcategory',
  49. component: 'tool_dataprivacy'
  50. },
  51. {
  52. key: 'save',
  53. component: 'admin'
  54. }
  55. ];
  56. this.strings = Str.get_strings(stringKeys);
  57. this.registerEventListeners();
  58. };
  59. /**
  60. * @var {int} contextId
  61. * @private
  62. */
  63. AddCategory.prototype.contextId = 0;
  64. /**
  65. * @var {Promise}
  66. * @private
  67. */
  68. AddCategory.prototype.strings = 0;
  69. AddCategory.prototype.registerEventListeners = function() {
  70. var trigger = $(SELECTORS.CATEGORY_LINK);
  71. trigger.on('click', function() {
  72. this.strings.then(function(strings) {
  73. return Promise.all([
  74. ModalSaveCancel.create({
  75. title: strings[0],
  76. body: '',
  77. }),
  78. strings[1],
  79. ]).then(function([modal, string]) {
  80. this.setupFormModal(modal, string);
  81. return modal;
  82. }.bind(this));
  83. }.bind(this))
  84. .catch(Notification.exception);
  85. }.bind(this));
  86. };
  87. /**
  88. * @method getBody
  89. * @param {Object} formdata
  90. * @private
  91. * @return {Promise}
  92. */
  93. AddCategory.prototype.getBody = function(formdata) {
  94. var params = null;
  95. if (typeof formdata !== "undefined") {
  96. params = {jsonformdata: JSON.stringify(formdata)};
  97. }
  98. // Get the content of the modal.
  99. return Fragment.loadFragment('tool_dataprivacy', 'addcategory_form', this.contextId, params);
  100. };
  101. AddCategory.prototype.setupFormModal = function(modal, saveText) {
  102. modal.setLarge();
  103. modal.setSaveButtonText(saveText);
  104. // We want to reset the form every time it is opened.
  105. modal.getRoot().on(ModalEvents.hidden, this.destroy.bind(this));
  106. modal.setBody(this.getBody());
  107. // We catch the modal save event, and use it to submit the form inside the modal.
  108. // Triggering a form submission will give JS validation scripts a chance to check for errors.
  109. modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
  110. // We also catch the form submit event and use it to submit the form with ajax.
  111. modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
  112. this.modal = modal;
  113. modal.show();
  114. };
  115. /**
  116. * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
  117. *
  118. * @method submitForm
  119. * @param {Event} e Form submission event.
  120. * @private
  121. */
  122. AddCategory.prototype.submitForm = function(e) {
  123. e.preventDefault();
  124. this.modal.getRoot().find('form').submit();
  125. };
  126. AddCategory.prototype.submitFormAjax = function(e) {
  127. // We don't want to do a real form submission.
  128. e.preventDefault();
  129. // Convert all the form elements values to a serialised string.
  130. var formData = this.modal.getRoot().find('form').serialize();
  131. Ajax.call([{
  132. methodname: 'tool_dataprivacy_create_category_form',
  133. args: {jsonformdata: JSON.stringify(formData)},
  134. done: function(data) {
  135. if (data.validationerrors) {
  136. this.modal.setBody(this.getBody(formData));
  137. } else {
  138. this.close();
  139. }
  140. }.bind(this),
  141. fail: Notification.exception
  142. }]);
  143. };
  144. AddCategory.prototype.close = function() {
  145. this.destroy();
  146. document.location.reload();
  147. };
  148. AddCategory.prototype.destroy = function() {
  149. FormChangeChecker.resetAllFormDirtyStates();
  150. this.modal.destroy();
  151. };
  152. AddCategory.prototype.removeListeners = function() {
  153. $(SELECTORS.CATEGORY_LINK).off('click');
  154. };
  155. return /** @alias module:tool_dataprivacy/add_category */ {
  156. getInstance: function(contextId) {
  157. return new AddCategory(contextId);
  158. }
  159. };
  160. }
  161. );