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