-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathh5peditor-metadata-author-widget.js
160 lines (132 loc) · 3.95 KB
/
h5peditor-metadata-author-widget.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
/**
* Creates a widget to add author information to a form
*
* @param {object} semantics
* @param {object} params
* @param {object} group
* @param {mixed} parent used in processSemanticsChunk()
* @returns {ns.Coordinates}
*/
H5PEditor.metadataAuthorWidget = function (semantics, params, $wrapper, parent) {
if (!params.authors) {
params.authors = [];
}
const $ = H5PEditor.$;
const widget = $('<div class="field h5p-metadata-author-widget"></div>');
var $authorData = $('<div class="h5p-author-data"></div>');
widget.append($authorData);
H5PEditor.processSemanticsChunk(semantics, {}, $authorData, parent);
// Get references to the fields
var nameField = H5PEditor.findField('name', parent);
var roleField = H5PEditor.findField('role', parent);
var $button = $('<div class="field authorList">' +
'<button type="button" class="h5p-metadata-button inverted h5p-save-author">' +
H5PEditor.t('core', 'addAuthor') +
'</button>' +
'</div>').children('button').click(function (event) {
// Temporarily set name as mandatory to get the error messages only when
// clicking the Add Author button
nameField.field.optional = false;
var name = nameField.validate();
nameField.field.optional = true;
var role = roleField.validate();
if (!name) {
return;
}
// Don't add author if already in list with the same role
const authorDuplicate = params.authors.some(function (author) {
return author.name === name && author.role === role;
});
if (authorDuplicate) {
resetForm();
return;
}
addAuthor(name, role);
}).end();
$authorData.append($button);
var authorListWrapper = $('<div class="h5p-author-list-wrapper"><ul class="h5p-author-list"></ul></div>');
widget.append(authorListWrapper);
renderAuthorList();
widget.appendTo($wrapper);
/**
* Add an author to the list of authors
* @param {string} [name]
* @param {string} [role]
*/
function addAuthor(name, role) {
params.authors.push({
name: name,
role: role
});
renderAuthorList();
resetForm();
}
/**
* Add default/current author to list of authors
*
* @param {string} fallbackName Name to fallback to if there is no valid name chosen already
* @param {string} fallbackRole Role to fallback to if there is no valid role chosen already
*/
function addDefaultAuthor(fallbackName, fallbackRole) {
var name = nameField.validate();
if (!name) {
name = fallbackName;
}
var role = roleField.validate();
if (!role) {
role = fallbackRole;
}
addAuthor(name, role);
}
/**
* Resets the form
*/
function resetForm() {
nameField.$input.val('');
}
/**
* Remove author from list.
*
* @param {object} author - Author to be removed.
* @param {string} author.name - Author name.
* @param {string} author.role - Author role.
*/
function removeAuthor(author) {
params.authors = params.authors.filter(function (e) {
return (e !== author);
});
renderAuthorList();
}
function renderAuthorList() {
var wrapper = widget.find('.h5p-author-list-wrapper');
wrapper.empty();
const authorList = $('<ul></ul>');
params.authors.forEach(function (author) {
// Name and role
var listItem = $('<li>', {
html: H5PEditor.htmlspecialchars(author.name),
append: $('<span>', {
'class': 'h5p-metadata-role',
html: author.role
})
});
// The delete-button
$('<button>', {
type: 'button',
'class': 'h5p-metadata-icon-button',
click: function () {
if (confirm(H5PEditor.t('core', 'confirmRemoveAuthor'))) {
removeAuthor(author);
}
}
}).appendTo(listItem);
authorList.append(listItem);
});
wrapper.append(authorList);
}
return {
addAuthor: addAuthor,
addDefaultAuthor: addDefaultAuthor,
renderAuthorList: renderAuthorList
};
};