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 group of fields.
4
 *
5
 * @param {mixed} parent
6
 * @param {object} field
7
 * @param {mixed} params
8
 * @param {function} setValue
9
 * @returns {ns.Group}
10
 */
11
ns.Group = function (parent, field, params, setValue) {
12
  // Support for events
13
  H5P.EventDispatcher.call(this);
14
 
15
  if (field.label === undefined) {
16
    field.label = field.name;
17
  }
18
  else if (field.label === 0) {
19
    field.label = '';
20
  }
21
 
22
  this.parent = parent;
23
  this.passReadies = true;
24
  this.params = params;
25
  this.setValue = setValue;
26
  this.library = parent.library + '/' + field.name;
27
 
28
  if (field.deprecated !== undefined && field.deprecated) {
29
    this.field = H5P.cloneObject(field, true);
30
    var empties = 0;
31
    for (var i = 0; i < this.field.fields.length; i++) {
32
      var f = this.field.fields[i];
33
      if (params !== undefined && params[f.name] === '') {
34
        delete params[f.name];
35
      }
36
      if (params === undefined || params[f.name] === undefined) {
37
        f.widget = 'none';
38
        empties++;
39
      }
40
    }
41
    if (i === empties) {
42
      this.field.fields = [];
43
    }
44
  }
45
  else {
46
    this.field = field;
47
  }
48
 
49
  if (this.field.optional === true) {
50
    // If this field is optional, make sure child fields are as well
51
    for (var j = 0; j < this.field.fields.length; j++) {
52
      this.field.fields[j].optional = true;
53
    }
54
  }
55
};
56
 
57
// Extends the event dispatcher
58
ns.Group.prototype = Object.create(H5P.EventDispatcher.prototype);
59
ns.Group.prototype.constructor = ns.Group;
60
 
61
/**
62
 * Append group to its wrapper.
63
 *
64
 * @param {jQuery} $wrapper
65
 * @returns {undefined}
66
 */
67
ns.Group.prototype.appendTo = function ($wrapper) {
68
  var that = this;
69
 
70
  if (this.field.fields.length === 0) {
71
    // No fields or all are deprecated
72
    this.setValue(this.field);
73
    return;
74
  }
75
 
76
  // Add fieldset wrapper for group
77
  this.$group = ns.$('<fieldset/>', {
78
    'class': 'field group ' + H5PEditor.createImportance(this.field.importance) + ' field-name-' + this.field.name,
79
    appendTo: $wrapper
80
  });
81
 
82
  // Add title expand/collapse button
83
  this.$title = ns.$('<div/>', {
84
    'class': 'title',
85
    'aria-expanded': 'false',
86
    title: ns.t('core', 'expandCollapse'),
87
    role: 'button',
88
    tabIndex: 0,
89
    on: {
90
      click: function () {
91
        that.toggle();
92
      },
93
      keypress: function (event) {
94
        if ((event.charCode || event.keyCode) === 32) {
95
          that.toggle();
96
          event.preventDefault();
97
        }
98
      }
99
    },
100
    appendTo: this.$group
101
  });
102
 
103
  // Add content container
104
  this.$content = ns.$('<div/>', {
105
    'class': 'content',
106
    appendTo: this.$group
107
  });
108
 
109
  if (this.hasSingleChild() && !this.isSubContent()) {
110
    this.$content.addClass('h5peditor-single');
111
    this.children = [];
112
    var field = this.field.fields[0];
113
    var widget = field.widget === undefined ? field.type : field.widget;
114
    this.children[0] = new ns.widgets[widget](this, field, this.params, function (field, value) {
115
      that.setValue(that.field, value);
116
    });
117
    this.children[0].appendTo(this.$content);
118
  }
119
  else {
120
    if (this.params === undefined) {
121
      this.params = {};
122
      this.setValue(this.field, this.params);
123
    }
124
 
125
    this.params = this.initSubContent(this.params);
126
 
127
    ns.processSemanticsChunk(this.field.fields, this.params, this.$content, this);
128
  }
129
 
130
  // Set summary
131
  this.findSummary();
132
 
133
  // Check if group should be expanded.
134
  // Default is to be collapsed unless explicity defined in semantics by optional attribute expanded
135
  if (this.field.expanded === true) {
136
    this.expand();
137
  }
138
};
139
 
140
/**
141
 * Return whether this group is Sub Content
142
 *
143
 * @private
144
 * @return {boolean}
145
 */
146
ns.Group.prototype.hasSingleChild = function () {
147
  return this.field.fields.length === 1;
148
};
149
 
150
/**
151
 * Add generated 'subContentId' attribute, if group is "sub content (library-like embedded structure)"
152
 *
153
 * @param {object} params
154
 *
155
 * @private
156
 * @return {object}
157
 */
158
ns.Group.prototype.initSubContent = function (params) {
159
  // If group contains library-like sub content that needs UUIDs
160
  if (this.isSubContent()) {
161
    params['subContentId'] = params['subContentId'] || H5P.createUUID();
162
  }
163
 
164
  return params;
165
};
166
 
167
/**
168
 * Return whether this group is Sub Content
169
 *
170
 * @private
171
 * @return {boolean}
172
 */
173
ns.Group.prototype.isSubContent = function () {
174
  return this.field.isSubContent === true;
175
};
176
 
177
/**
178
 * Toggle expand/collapse for the given group.
179
 */
180
ns.Group.prototype.toggle = function () {
181
  if (this.preventToggle) {
182
    this.preventToggle = false;
183
    return;
184
  }
185
 
186
  if (this.$group.hasClass('expanded')) {
187
    this.collapse();
188
  }
189
  else {
190
    this.expand();
191
  }
192
};
193
 
194
/**
195
 * Expand the given group.
196
 */
197
ns.Group.prototype.expand = function () {
198
  this.$title.attr('aria-expanded', 'true');
199
  // Set timeout is necessary because aria-expanded status is not announced
200
  // when the :before element changes content because Firefox
201
  // re-creates the accessible element..
202
  // @see https://github.com/nvaccess/nvda/issues/8341
203
  // Should be fixeed by Firefox 70 (https://bugzilla.mozilla.org/show_bug.cgi?id=686400)
204
  setTimeout(function () {
205
    this.trigger('expanded');
206
    this.$group.addClass('expanded');
207
  }.bind(this), 100);
208
};
209
 
210
/**
211
 * Collapse the given group (if valid)
212
 */
213
ns.Group.prototype.collapse = function () {
214
  // Do not collapse before valid!
215
  var valid = true;
216
  for (var i = 0; i < this.children.length; i++) {
217
    if (this.children[i].validate() === false) {
218
      valid = false;
219
    }
220
  }
221
  if (valid) {
222
    this.$title.attr('aria-expanded', 'false');
223
    // Set timeout is necessary because aria-expanded status is not announced
224
    // when the :before element changes content because Firefox
225
    // re-creates the accessible element..
226
    // @see https://github.com/nvaccess/nvda/issues/8341
227
    // Should be fixeed by Firefox 70 (https://bugzilla.mozilla.org/show_bug.cgi?id=686400)
228
    setTimeout(function () {
229
      this.trigger('collapsed');
230
      this.$group.removeClass('expanded');
231
    }.bind(this), 100);
232
 
233
  }
234
};
235
 
236
/**
237
 * Find summary to display in group header.
238
 */
239
ns.Group.prototype.findSummary = function () {
240
  var that = this;
241
  var summary;
242
  for (var j = 0; j < this.children.length; j++) {
243
    var child = this.children[j];
244
    if (child.field === undefined) {
245
      continue;
246
    }
247
    var params = (that.hasSingleChild() && !that.isSubContent()) ? this.params : this.params[child.field.name];
248
    var widget = ns.getWidgetName(child.field);
249
 
250
    if (widget === 'text' || widget === 'html') {
251
      if (params !== undefined && params !== '') {
252
        summary = params.replace(/(<([^>]+)>)/ig, "");
253
      }
254
 
255
      child.$input.change(function () {
256
        var params = (that.hasSingleChild() && !that.isSubContent()) ? that.params : that.params[child.field.name];
257
        if (params !== undefined && params !== '') {
258
          that.setSummary(params.replace(/(<([^>]+)>)/ig, ""));
259
        }
260
      });
261
      break;
262
    }
263
    else if (widget === 'library') {
264
      let lastLib;
265
      if (child.params !== undefined) {
266
        summary = child.$select.children(':selected').text();
267
        if (child.params.metadata && child.params.metadata.title) {
268
          // The given title usually makes more sense than the type name
269
          summary = child.params.metadata.title + (!child.libraries || (child.libraries.length > 1 && child.params.metadata.title.indexOf(summary) === -1) ? ' (' +  summary + ')' : '');
270
        }
271
        else if (!child.params.library) {
272
          // Nothing selected
273
          summary = that.field.label;
274
        }
275
      }
276
      const setSummary = function () {
277
        if (child.params && child.params.metadata && child.params.metadata.title) {
278
          // The given title usually makes more sense than the type name
279
          that.setSummary(child.params.metadata.title + (child.libraries.length > 1 && child.params.metadata.title.indexOf(lastLib.title) === -1 ? ' (' +  lastLib.title + ')' : ''));
280
        }
281
        else {
282
          that.setSummary(lastLib ? lastLib.title : that.field.label);
283
        }
284
      };
285
      if (child.metadataForm) {
286
        child.metadataForm.on('titlechange', setSummary);
287
      }
288
      child.change(function (library) {
289
        lastLib = library;
290
        setSummary();
291
 
292
        if (child.metadataForm) {
293
          // Update summary when metadata title changes
294
          child.metadataForm.off('titlechange', setSummary);
295
          child.metadataForm.on('titlechange', setSummary);
296
        }
297
      });
298
      break;
299
    }
300
  }
301
  this.setSummary(summary);
302
};
303
 
304
/**
305
 * Set the given group summary.
306
 *
307
 * @param {string} summary
308
 * @returns {undefined}
309
 */
310
ns.Group.prototype.setSummary = function (summary) {
311
  var summaryText;
312
 
313
  // Parse html
314
  var summaryTextNode = ns.$.parseHTML(summary);
315
 
316
  if (summaryTextNode !== null && summaryTextNode.length) {
317
    summaryText = summaryTextNode[0].nodeValue;
318
  }
319
 
320
  // Make it possible for parent to monitor summary changes
321
  this.trigger('summary', summaryText);
322
 
323
  if (summaryText !== undefined) {
324
    summaryText = (summaryText.length > 48 ? summaryText.substr(0, 45) + '...' : summaryText);
325
  }
326
  else {
327
    summaryText = this.field.label;
328
  }
329
 
330
  this.$title.text(summaryText);
331
};
332
 
333
/**
334
 * Validate all children.
335
 */
336
ns.Group.prototype.validate = function () {
337
  var valid = true;
338
 
339
  if (this.children !== undefined) {
340
    for (var i = 0; i < this.children.length; i++) {
341
      if (this.children[i].validate() === false) {
342
        valid = false;
343
      }
344
    }
345
  }
346
 
347
  return valid;
348
};
349
 
350
/**
351
 * Allows ancestors and widgets to do stuff with our children.
352
 *
353
 * @public
354
 * @param {Function} task
355
 */
356
ns.Group.prototype.forEachChild = function (task) {
357
  for (var i = 0; i < this.children.length; i++) {
358
    task(this.children[i], i);
359
  }
360
};
361
 
362
/**
363
 * Collect functions to execute once the tree is complete.
364
 *
365
 * @param {function} ready
366
 * @returns {undefined}
367
 */
368
ns.Group.prototype.ready = function (ready) {
369
  this.parent.ready(ready);
370
};
371
 
372
/**
373
 * Remove this item.
374
 */
375
ns.Group.prototype.remove = function () {
376
  if (this.$group !== undefined) {
377
    ns.removeChildren(this.children);
378
    this.$group.remove();
379
  }
380
};
381
 
382
/**
383
 * Get a copy of the fields semantics used by this group.
384
 * @return {Array}
385
 */
386
ns.Group.prototype.getFields = function () {
387
  return H5PEditor.$.extend(true, [], this.field.fields);
388
};
389
 
390
/**
391
 * When someone from the outside wants to set a value.
392
 *
393
 * @param {Object} value
394
 */
395
ns.Group.prototype.forceValue = function (value) {
396
  for (let i = 0; i < this.children.length; i++) {
397
    this.children[i].forceValue(value[this.children[i].field.name]);
398
  }
399
};
400
 
401
// Tell the editor what widget we are.
402
ns.widgets.group = ns.Group;