Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/* global ns */
2
/**
3
 * Creates a coordinates picker for the form.
4
 *
5
 * @param {mixed} parent
6
 * @param {object} field
7
 * @param {mixed} params
8
 * @param {function} setValue
9
 * @returns {ns.Coordinates}
10
 */
11
ns.Coordinates = function (parent, field, params, setValue) {
12
  var that = this;
13
 
14
  this.parent = parent;
15
  this.field = H5P.cloneObject(field, true); // TODO: Cloning is a quick fix, make sure this field doesn't change semantics!
16
 
17
  // Find image field to get max size from.
18
  // TODO: Use followField?
19
  this.findImageField('max', function (field) {
20
    if (field instanceof ns.File) {
21
      if (field.params !== undefined) {
22
        that.setMax(field.params.width, field.params.height);
23
      }
24
 
25
      field.changes.push(function (file) {
26
        if (file === undefined) {
27
          return;
28
        }
29
        // TODO: This callback should be removed when this item is removed.
30
        that.setMax(file.params.width, file.params.height);
31
      });
32
    }
33
    else if (field instanceof ns.Dimensions) {
34
      if (field.params !== undefined) {
35
        that.setMax(field.params.width, field.params.height);
36
      }
37
 
38
      field.changes.push(function (width, height) {
39
        // TODO: This callback should be removed when this item is removed.
40
        that.setMax(width, height);
41
      });
42
    }
43
  });
44
 
45
  this.params = params;
46
  this.setValue = setValue;
47
};
48
 
49
/**
50
 * Set max coordinates.
51
 *
52
 * @param {string} x
53
 * @param {string} y
54
 * @returns {undefined}
55
 */
56
ns.Coordinates.prototype.setMax = function (x, y) {
57
  this.field.max = {
58
    x: parseInt(x),
59
    y: parseInt(y)
60
  };
61
  if (this.params !== undefined) {
62
    this.$errors.html('');
63
    this.validate();
64
  }
65
};
66
 
67
/**
68
 * Find the image field for the given property and then run the callback.
69
 *
70
 * @param {string} property
71
 * @param {function} callback
72
 * @returns {unresolved}
73
 */
74
ns.Coordinates.prototype.findImageField = function (property, callback) {
75
  var that = this;
76
  var str = 'string';
77
 
78
  if (typeof this.field[property] !== str) {
79
    return;
80
  }
81
 
82
  // Find field when tree is ready.
83
  this.parent.ready(function () {
84
    if (typeof that.field[property] !== str) {
85
      if (that.field[property] !== undefined) {
86
        callback(that.field[property]);
87
      }
88
      return; // We've already found this field before.
89
    }
90
    var path = that.field[property];
91
 
92
    that.field[property] = ns.findField(that.field[property], that.parent);
93
    if (!that.field[property]) {
94
      throw ns.t('core', 'unknownFieldPath', {':path': path});
95
    }
96
    if (that.field[property].field.type !== 'image' && that.field[property].field.widget !== 'dimensions') {
97
      throw ns.t('core', 'notImageOrDimensionsField', {':path': path});
98
    }
99
 
100
    callback(that.field[property]);
101
  });
102
};
103
 
104
/**
105
 * Append the field to the wrapper.
106
 *
107
 * @param {jQuery} $wrapper
108
 * @returns {undefined}
109
 */
110
ns.Coordinates.prototype.appendTo = function ($wrapper) {
111
  var that = this;
112
 
113
  this.$item = ns.$(this.createHtml()).appendTo($wrapper);
114
  this.$inputs = this.$item.find('input');
115
  this.$errors = this.$item.children('.h5p-errors');
116
 
117
  this.$inputs.change(function () {
118
    // Validate
119
    var value = that.validate();
120
 
121
    if (value) {
122
      // Set param
123
      that.params = value;
124
      that.setValue(that.field, value);
125
    }
126
  }).click(function () {
127
    return false;
128
  }).click(function () {
129
    return false;
130
  });
131
};
132
 
133
/**
134
 * Create HTML for the coordinates picker.
135
 */
136
ns.Coordinates.prototype.createHtml = function () {
137
  const id = ns.getNextFieldId(this.field);
138
  const descriptionId = (this.field.description !== undefined ? ns.getDescriptionId(id) : undefined)
139
 
140
  var input =
141
    ns.createText(this.params !== undefined ? this.params.x : undefined, 15, 'X', id, descriptionId) +
142
    ' , ' +
143
    ns.createText(this.params !== undefined ? this.params.y : undefined, 15, 'Y', undefined, descriptionId);
144
 
145
  return ns.createFieldMarkup(this.field, input, id);
146
};
147
 
148
/**
149
 * Validate the current values.
150
 */
151
ns.Coordinates.prototype.validate = function () {
152
  var that = this;
153
  var coordinates = {};
154
 
155
  this.$inputs.each(function (i) {
156
    var $input = ns.$(this);
157
    var value = H5P.trim($input.val());
158
    var property = i ? 'y' : 'x';
159
 
160
    if (that.field.optional && !value.length) {
161
      return true;
162
    }
163
 
164
    if ((that.field.optional === undefined || !that.field.optional) && !value.length) {
165
      that.$errors.append(ns.createError(ns.t('core', 'requiredProperty', {':property': property})));
166
      return false;
167
    }
168
 
169
    if (value.length && !value.match(new RegExp('^[0-9]+$'))) {
170
      that.$errors.append(ns.createError(ns.t('core', 'onlyNumbers', {':property': property})));
171
      return false;
172
    }
173
 
174
    value = parseInt(value);
175
    if (that.field.max !== undefined && value > that.field.max[property]) {
176
      that.$errors.append(ns.createError(ns.t('core', 'exceedsMax', {':property': property, ':max': that.field.max[property]})));
177
      return false;
178
    }
179
 
180
    coordinates[property] = value;
181
  });
182
 
183
  return ns.checkErrors(this.$errors, this.$inputs, coordinates);
184
};
185
 
186
/**
187
 * Remove this item.
188
 */
189
ns.Coordinates.prototype.remove = function () {
190
  this.$item.remove();
191
};
192
 
193
// Tell the editor what widget we are.
194
ns.widgets.coordinates = ns.Coordinates;