mod/lti/amd/src/form-field.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 module that enables the setting of form field values on the client side.
  17. *
  18. * @module mod_lti/form-field
  19. * @copyright 2016 Jun Pataleta <jun@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 3.2
  22. */
  23. define(['jquery'],
  24. function($) {
  25. /**
  26. * Form field class.
  27. *
  28. * @param {string} name Field name.
  29. * @param {number} type The field type.
  30. * @param {boolean} resetIfUndefined Flag to reset the field to the default value if undefined in the return data.
  31. * @param {string|number|boolean} defaultValue The default value to use for the field.
  32. * @constructor
  33. */
  34. var FormField = function(name, type, resetIfUndefined, defaultValue) {
  35. this.name = name;
  36. this.id = 'id_' + this.name;
  37. this.selector = '#' + this.id;
  38. this.type = type;
  39. this.resetIfUndefined = resetIfUndefined;
  40. this.defaultValue = defaultValue;
  41. };
  42. /**
  43. * Form field types.
  44. *
  45. * @type {{TEXT: number, SELECT: number, CHECKBOX: number, EDITOR: number}}
  46. */
  47. FormField.TYPES = {
  48. TEXT: 1,
  49. SELECT: 2,
  50. CHECKBOX: 3,
  51. EDITOR: 4
  52. };
  53. /**
  54. * Sets the values for a form field.
  55. *
  56. * @param {string|boolean|number} value The value to be set into the field.
  57. */
  58. FormField.prototype.setFieldValue = function(value) {
  59. if (value === null) {
  60. if (this.resetIfUndefined) {
  61. value = this.defaultValue;
  62. } else {
  63. // No need set the field value if value is null and there's no need to reset the field.
  64. return;
  65. }
  66. }
  67. switch (this.type) {
  68. case FormField.TYPES.CHECKBOX:
  69. if (value) {
  70. $(this.selector).prop('checked', true);
  71. } else {
  72. $(this.selector).prop('checked', false);
  73. }
  74. break;
  75. case FormField.TYPES.EDITOR:
  76. if (typeof value.text !== 'undefined') {
  77. /* global tinyMCE:false */
  78. // Set text in editor's editable content, if applicable.
  79. // Check if it is an Atto editor.
  80. var attoEditor = $(this.selector + 'editable');
  81. if (attoEditor.length) {
  82. attoEditor.html(value.text);
  83. } else if (typeof tinyMCE !== 'undefined') {
  84. // If the editor is not Atto, try to fallback to TinyMCE.
  85. if (tinyMCE.majorVersion == "3") {
  86. // Tiny 3.
  87. tinyMCE.execInstanceCommand(this.id, 'mceInsertContent', false, value.text);
  88. } else {
  89. // Tiny 4+.
  90. tinyMCE.get(this.id).setContent(value.text);
  91. }
  92. }
  93. // Set text to actual editor text area.
  94. $(this.selector).val(value.text);
  95. }
  96. break;
  97. default:
  98. $(this.selector).val(value);
  99. break;
  100. }
  101. };
  102. return FormField;
  103. }
  104. );