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 number picker field for the form.
4
 *
5
 * @param {mixed} parent
6
 * @param {Object} field
7
 * @param {mixed} params
8
 * @param {function} setValue
9
 * @returns {ns.Number}
10
 */
11
ns.Number = function (parent, field, params, setValue) {
12
  this.field = field;
13
  this.value = params;
14
  this.setValue = setValue;
15
};
16
 
17
/**
18
 * Append field to wrapper.
19
 *
20
 * @param {jQuery} $wrapper
21
 * @returns {undefined}
22
 */
23
ns.Number.prototype.appendTo = function ($wrapper) {
24
  var that = this;
25
 
26
  this.$item = ns.$(this.createHtml()).appendTo($wrapper);
27
  this.$errors = this.$item.find('.h5p-errors');
28
  var $inputs = this.$item.find('input');
29
  if ($inputs.length === 1) {
30
    this.$input = $inputs;
31
  }
32
  else {
33
    this.$range = $inputs.filter(':first');
34
    this.$input = this.$range.next();
35
  }
36
 
37
  this.$input.change(function () {
38
    // Validate
39
    var value = that.validate();
40
 
41
    if (value !== false) {
42
      // Set param
43
      that.value = value;
44
      that.setValue(that.field, value);
45
      if (that.$range !== undefined) {
46
        that.$range.val(value);
47
      }
48
      if (value !== undefined && that.field.unit) {
49
        that.$input.val(value + ' ' + that.field.unit);
50
      }
51
    }
52
  });
53
 
54
  if (this.$range !== undefined) {
55
    if (this.$range.attr('type') === 'range') {
56
      this.$range.change(function () {
57
        that.$input.val(that.$range.val()).change();
58
      });
59
 
60
      // Add some styles for IE.
61
      if (H5PEditor.isIE) {
62
        this.$range.css('margin-top', 0);
63
        this.$input.css('margin-top', '7px');
64
      }
65
    }
66
    else {
67
      this.$range.remove();
68
    }
69
  }
70
};
71
 
72
/**
73
 * Create HTML for the field.
74
 */
75
ns.Number.prototype.createHtml = function () {
76
  const id = ns.getNextFieldId(this.field);
77
  const descriptionId = (this.field.description !== undefined ? ns.getDescriptionId(id) : undefined)
78
  const value = this.field.unit && this.value !== undefined ? (this.value + ' ' + this.field.unit) : this.value;
79
  var input = ns.createText(value, 15, undefined, id, descriptionId);
80
  /* TODO: Add back in when FF gets support for input:range....
81
   *if (this.field.min !== undefined && this.field.max !== undefined && this.field.step !== undefined) {
82
    input = '<input type="range" min="' + this.field.min + '" max="' + this.field.max + '" step="' + this.field.step + '"' + (this.value === undefined ? '' : ' value="' + this.value + '"') + '/>' + input;
83
  }
84
   */
85
 
86
  return ns.createFieldMarkup(this.field, input, id);
87
};
88
 
89
/**
90
 * Validate the current text field.
91
 */
92
ns.Number.prototype.validate = function () {
93
  var that = this;
94
 
95
  var value = H5P.trim(this.$input.val());
96
  var decimals = this.field.decimals !== undefined && this.field.decimals;
97
 
98
  if (this.field.unit) {
99
    value = value.replace(new RegExp(' *' + this.field.unit + '$'), '');
100
  }
101
 
102
  // Clear errors before showing new ones
103
  this.$errors.html('');
104
 
105
  // Determine property name
106
  var propertyName = (that.field.label === 0 ? ns.t('core', 'numberField') : that.field.label);
107
 
108
  if (!value.length) {
109
    if (that.field.optional === true) {
110
      // Field is optional and does not have a value, nothing more to validate
111
      this.$input.removeClass('error');
112
      return;
113
    }
114
 
115
    // Field must have a value
116
    this.$errors.append(ns.createError(ns.t('core', 'requiredProperty', {':property': ns.t('core', 'numberField')})));
117
  }
118
  else {
119
    var isInt = value.match(new RegExp('^-?[0-9]+$'));
120
    if (decimals && !isInt && !value.match(new RegExp('^-?[0-9]+(.|,)[0-9]{1,' + decimals + '}$'))) {
121
      this.$errors.append(ns.createError(ns.t('core', 'illegalDecimalNumber', {
122
        ':property': propertyName,
123
        ':decimals': decimals
124
      })));
125
    }
126
    else if (!decimals && !isInt) {
127
      this.$errors.append(ns.createError(ns.t('core', 'onlyNumbers', {':property': propertyName})));
128
    }
129
    else {
130
      if (decimals) {
131
        value = parseFloat(value.replace(',', '.'));
132
      }
133
      else {
134
        value = parseInt(value);
135
      }
136
 
137
      if (this.field.max !== undefined && value > this.field.max) {
138
        this.$errors.append(ns.createError(ns.t('core', 'exceedsMax', {':property': propertyName, ':max': this.field.max})));
139
      }
140
      else if (this.field.min !== undefined && value < this.field.min) {
141
        this.$errors.append(ns.createError(ns.t('core', 'belowMin', {':property': propertyName, ':min': this.field.min})));
142
      }
143
      else if (this.field.step !== undefined && value % this.field.step)  {
144
        this.$errors.append(ns.createError(ns.t('core', 'outOfStep', {':property': propertyName, ':step': this.field.step})));
145
      }
146
    }
147
  }
148
 
149
  this.$input.toggleClass('error', this.$errors.html().length > 0);
150
 
151
  return ns.checkErrors(this.$errors, this.$input, value);
152
};
153
 
154
/**
155
 * Remove this item.
156
 */
157
ns.Number.prototype.remove = function () {
158
  this.$item.remove();
159
};
160
 
161
// Tell the editor what widget we are.
162
ns.widgets.number = ns.Number;