Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/* global ns */
2
/**
3
 * Create a text field for the form.
4
 *
5
 * @param {mixed} parent
6
 * @param {Object} field
7
 * @param {mixed} params
8
 * @param {function} setValue
9
 * @returns {ns.Textarea}
10
 */
11
ns.Textarea = function (parent, field, params, setValue) {
12
  this.parent = parent;
13
  this.field = field;
14
  this.value = params;
15
  this.setValue = setValue;
16
};
17
 
18
/**
19
 * Append field to wrapper.
20
 *
21
 * @param {jQuery} $wrapper
22
 * @returns {undefined}
23
 */
24
ns.Textarea.prototype.appendTo = function ($wrapper) {
25
  var that = this;
26
 
27
  this.$item = ns.$(this.createHtml()).appendTo($wrapper);
28
  this.$input = this.$item.find('textarea');
29
  this.$errors = this.$item.find('.h5p-errors');
30
 
31
  ns.bindImportantDescriptionEvents(this, this.field.name, this.parent);
32
 
33
  this.$input.change(function () {
34
    // Validate
35
    var value = that.validate();
36
 
37
    if (value !== false) {
38
      // Set param
39
      that.setValue(that.field, ns.htmlspecialchars(value));
40
    }
41
  });
42
};
43
 
44
/**
45
 * Create HTML for the text field.
46
 */
47
ns.Textarea.prototype.createHtml = function () {
48
  const id = ns.getNextFieldId(this.field);
49
 
50
  var input = '<textarea cols="30" rows="4" id="' + id + '"';
51
  if (this.field.description !== undefined) {
52
    input += ' aria-describedby="' + ns.getDescriptionId(id) + '"';
53
  }
54
  if (this.field.placeholder !== undefined) {
55
    input += ' placeholder="' + this.field.placeholder + '"';
56
  }
57
  input += '>';
58
  if (this.value !== undefined) {
59
    input += this.value;
60
  }
61
  input += '</textarea>';
62
 
63
  return ns.createFieldMarkup(this.field, ns.createImportantDescription(this.field.important) + input, id);
64
};
65
 
66
/**
67
 * Validate the current text field.
68
 */
69
ns.Textarea.prototype.validate = function () {
70
  var value = H5P.trim(this.$input.val());
71
  var valid = true;
72
 
73
  // Clear errors before showing new ones
74
  this.$errors.html('');
75
 
76
  if ((this.field.optional === undefined || !this.field.optional) && !value.length) {
77
    this.$errors.append(ns.createError(ns.t('core', 'requiredProperty', {':property': ns.t('core', 'textField')})));
78
    valid = false;
79
  }
80
  else if (value.length > this.field.maxLength) {
81
    this.$errors.append(ns.createError(ns.t('core', 'tooLong', {':max': this.field.maxLength})));
82
    valid = false;
83
  }
84
  else if (this.field.regexp !== undefined && !value.match(new RegExp(this.field.regexp.pattern, this.field.regexp.modifiers))) {
85
    this.$errors.append(ns.createError(ns.t('core', 'invalidFormat')));
86
    valid = false;
87
  }
88
 
89
  this.$input.toggleClass('error', !valid);
90
 
91
  return ns.checkErrors(this.$errors, this.$input, value);
92
};
93
 
94
/**
95
 * Remove this item.
96
 */
97
ns.Textarea.prototype.remove = function () {
98
  this.$item.remove();
99
};
100
 
101
// Tell the editor what semantic field we are.
102
ns.widgets.textarea = ns.Textarea;