mod/assign/amd/src/grading_form_change_checker.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. * Simple method to check for changes to a form between two points in time.
  17. *
  18. * @module mod_assign/grading_form_change_checker
  19. * @copyright 2016 Damyon Wiese <damyon@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 3.1
  22. */
  23. define(['jquery'], function($) {
  24. return {
  25. /**
  26. * Save the values in the form to a data attribute so they can be compared later for changes.
  27. *
  28. * @method saveFormState
  29. * @param {String} selector The selector for the form element.
  30. */
  31. saveFormState: function(selector) {
  32. $(selector).trigger('save-form-state');
  33. var data = $(selector).serialize();
  34. $(selector).data('saved-form-state', data);
  35. },
  36. /**
  37. * Compare the current values in the form to the previously saved state.
  38. *
  39. * @method checkFormForChanges
  40. * @param {String} selector The selector for the form element.
  41. * @return {Boolean} True if there are changes to the form data.
  42. */
  43. checkFormForChanges: function(selector) {
  44. $(selector).trigger('save-form-state');
  45. var data = $(selector).serialize(),
  46. previousdata = $(selector).data('saved-form-state'),
  47. unresolvederror = $(selector).data('unresolved-error');
  48. if (unresolvederror) {
  49. return true;
  50. }
  51. if (typeof previousdata === 'undefined') {
  52. return false;
  53. }
  54. return (previousdata != data);
  55. }
  56. };
  57. });