1441 |
ariadna |
1 |
/* global ns */
|
|
|
2 |
H5PEditor.ListEditor = (function ($) {
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Draws the list.
|
|
|
6 |
*
|
|
|
7 |
* @class
|
|
|
8 |
* @param {List} list
|
|
|
9 |
*/
|
|
|
10 |
function ListEditor(list) {
|
|
|
11 |
var self = this;
|
|
|
12 |
|
|
|
13 |
var entity = list.getEntity();
|
|
|
14 |
// Create list html
|
|
|
15 |
var $list = $('<ul/>', {
|
|
|
16 |
id: list.getId(),
|
|
|
17 |
'aria-describedby': list.getDescriptionId(),
|
|
|
18 |
'class': 'h5p-ul'
|
|
|
19 |
});
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Add group collapse functionality to list editor if items are groups.
|
|
|
23 |
*/
|
|
|
24 |
const addGroupCollapseFunctionality = () => {
|
|
|
25 |
if (
|
|
|
26 |
list.field?.field?.type === 'group' &&
|
|
|
27 |
(list.getValue() ?? []).length
|
|
|
28 |
) {
|
|
|
29 |
self.addGroupCollapseListener();
|
|
|
30 |
self.addCollapseButtons();
|
|
|
31 |
}
|
|
|
32 |
};
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Find closest parent list.
|
|
|
36 |
* @param {object} library H5PEditor field instance.
|
|
|
37 |
* @returns {object|boolean} Closest parent list or false if none found.
|
|
|
38 |
*/
|
|
|
39 |
const findClosestParentList = (library) => {
|
|
|
40 |
const parent = library?.parent;
|
|
|
41 |
if (!parent) {
|
|
|
42 |
return false;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if (!parent.field?.type) {
|
|
|
46 |
return false;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
if (parent.field.type === 'list') {
|
|
|
50 |
return parent;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return findClosestParentList(parent);
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Set width of collapse button.
|
|
|
58 |
*
|
|
|
59 |
* The width of the button should not change when the label is changed,
|
|
|
60 |
* so the button is rendered offsite with both labels and the longest one
|
|
|
61 |
* is used to determine the button width.
|
|
|
62 |
*/
|
|
|
63 |
const setcollapseButtonMainWidth = () => {
|
|
|
64 |
if (!this.collapseButtonMain) {
|
|
|
65 |
return; // Button not available
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// The width should not need to be computed more than once
|
|
|
69 |
if (this.fixedMainButtonWidth) {
|
|
|
70 |
this.collapseButtonMain.style.width = `${this.fixedMainButtonWidth}px`;
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
const offsiteH5PEditorDOM = document.createElement('div');
|
|
|
75 |
offsiteH5PEditorDOM.classList.add('h5peditor', 'offsite');
|
|
|
76 |
|
|
|
77 |
const offsiteH5PEditorFlexWrapper = document.createElement('div');
|
|
|
78 |
offsiteH5PEditorFlexWrapper.classList.add('h5p-editor-flex-wrapper');
|
|
|
79 |
offsiteH5PEditorDOM.append(offsiteH5PEditorFlexWrapper);
|
|
|
80 |
|
|
|
81 |
const offsiteButton1 = document.createElement('button');
|
|
|
82 |
offsiteButton1.classList.add(
|
|
|
83 |
'h5peditor-button',
|
|
|
84 |
'h5peditor-button-textual',
|
|
|
85 |
'h5peditor-button-collapse',
|
|
|
86 |
'collapsed'
|
|
|
87 |
);
|
|
|
88 |
offsiteH5PEditorFlexWrapper.append(offsiteButton1);
|
|
|
89 |
|
|
|
90 |
const offsiteIcon1 = document.createElement('span');
|
|
|
91 |
offsiteIcon1.classList.add('icon');
|
|
|
92 |
offsiteButton1.append(offsiteIcon1);
|
|
|
93 |
|
|
|
94 |
const offsiteLabel1 = document.createElement('div');
|
|
|
95 |
offsiteLabel1.classList.add('label');
|
|
|
96 |
offsiteLabel1.innerText = H5PEditor.t('core', 'expandAllContent');
|
|
|
97 |
offsiteButton1.append(offsiteLabel1);
|
|
|
98 |
|
|
|
99 |
const offsiteButton2 = document.createElement('button');
|
|
|
100 |
offsiteButton2.classList.add(
|
|
|
101 |
'h5peditor-button',
|
|
|
102 |
'h5peditor-button-textual',
|
|
|
103 |
'h5peditor-button-collapse'
|
|
|
104 |
);
|
|
|
105 |
offsiteH5PEditorFlexWrapper.append(offsiteButton2);
|
|
|
106 |
|
|
|
107 |
const offsiteIcon2 = document.createElement('span');
|
|
|
108 |
offsiteIcon2.classList.add('icon');
|
|
|
109 |
offsiteButton2.append(offsiteIcon2);
|
|
|
110 |
|
|
|
111 |
const offsiteLabel2 = document.createElement('div');
|
|
|
112 |
offsiteLabel2.classList.add('label');
|
|
|
113 |
offsiteLabel2.innerText = H5PEditor.t('core', 'collapseAllContent');
|
|
|
114 |
offsiteButton2.append(offsiteLabel2);
|
|
|
115 |
|
|
|
116 |
document.body.append(offsiteH5PEditorDOM);
|
|
|
117 |
|
|
|
118 |
// FontFaceSet API is used to ensure font of icon is loaded
|
|
|
119 |
document.fonts.ready.then(() => {
|
|
|
120 |
const width1 = offsiteButton1.getBoundingClientRect().width;
|
|
|
121 |
const width2 = offsiteButton2.getBoundingClientRect().width;
|
|
|
122 |
|
|
|
123 |
this.fixedMainButtonWidth = Math.ceil(Math.max(width1, width2));
|
|
|
124 |
|
|
|
125 |
this.collapseButtonMain.style.width = `${this.fixedMainButtonWidth}px`;
|
|
|
126 |
|
|
|
127 |
offsiteH5PEditorDOM?.remove();
|
|
|
128 |
});
|
|
|
129 |
};
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Determine whether list should get a collapse button.
|
|
|
133 |
*
|
|
|
134 |
* List should get a collapse button if it's the topmost list only - or if
|
|
|
135 |
* its on the 2nd leven and the parent list has a VerticalTabs widget.
|
|
|
136 |
* @returns {boolean} True if list should get a collapse button. Else false.
|
|
|
137 |
*/
|
|
|
138 |
shouldListGetCollapseButtonMain = (list) => {
|
|
|
139 |
const closestParentList = findClosestParentList(list);
|
|
|
140 |
if (!closestParentList) {
|
|
|
141 |
return true; // Is topmost list
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/*
|
|
|
145 |
* Note: Currently, the only widget that changes the list editor
|
|
|
146 |
* appearance to not make the collapse button suitable is the
|
|
|
147 |
* VerticalTabs widget. In the future, this might change as other list
|
|
|
148 |
* widgets get developed so the following exception may not suffice then.
|
|
|
149 |
* There's no good way to determine this automatically, however.
|
|
|
150 |
*/
|
|
|
151 |
return (
|
|
|
152 |
// Second level list, but VerticalTabs widget
|
|
|
153 |
!findClosestParentList(closestParentList) &&
|
|
|
154 |
H5PEditor.VerticalTabs &&
|
|
|
155 |
closestParentList.widget instanceof H5PEditor.VerticalTabs
|
|
|
156 |
);
|
|
|
157 |
};
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Determine whether widget has expand/collapse capabilities.
|
|
|
161 |
* @returns {boolean} True if widget has collapse capabilities. Else false.
|
|
|
162 |
*/
|
|
|
163 |
self.hasCollapseCapabilities = () => {
|
|
|
164 |
return (
|
|
|
165 |
this.container?.parentNode.firstChild.querySelector(
|
|
|
166 |
'.h5p-editor-flex-wrapper .h5peditor-button-collapse'
|
|
|
167 |
) instanceof HTMLElement ||
|
|
|
168 |
this.container?.parentNode.firstChild.querySelector(
|
|
|
169 |
'.h5peditor-label-button'
|
|
|
170 |
) instanceof HTMLElement
|
|
|
171 |
);
|
|
|
172 |
};
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Toggle collapse button main label visibility.
|
|
|
176 |
* @param {boolean} visible True to show label. False to hide.
|
|
|
177 |
*/
|
|
|
178 |
self.toggleCollapseButtonMainLabel = (visible) => {
|
|
|
179 |
if (typeof visible !== 'boolean') {
|
|
|
180 |
return;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if (!visible) {
|
|
|
184 |
this.collapseButtonMain.style.width = '';
|
|
|
185 |
}
|
|
|
186 |
else {
|
|
|
187 |
setcollapseButtonMainWidth();
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
this.collapseButtonMain.classList.toggle('no-label', !visible);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Resize handler.
|
|
|
195 |
*/
|
|
|
196 |
self.handleResize = () => {
|
|
|
197 |
/*
|
|
|
198 |
* When the two buttons for collapsing/expanding groups are in the same
|
|
|
199 |
* container and the horizontal space does not suffice, first the main
|
|
|
200 |
* button should loose its label. If there's still not enough space, the
|
|
|
201 |
* list button label will wrap.
|
|
|
202 |
* Can't be done in CSS alone, unfortunately, because the main button
|
|
|
203 |
* needs a fixed width.
|
|
|
204 |
*/
|
|
|
205 |
const wrapperRect = this.collapseButtonsWrapper.getBoundingClientRect();
|
|
|
206 |
if (wrapperRect.width === 0) {
|
|
|
207 |
return; // Not visible
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
this.collapseButtonsGap = this.collapseButtonsGap ?? parseFloat(
|
|
|
211 |
window.getComputedStyle(this.collapseButtonsWrapper).gap ?? 0
|
|
|
212 |
);
|
|
|
213 |
|
|
|
214 |
const listButtonRect = this.collapseButtonList.getBoundingClientRect();
|
|
|
215 |
|
|
|
216 |
const hasSpaceForBothButtons =
|
|
|
217 |
wrapperRect.width - listButtonRect.width - this.collapseButtonsGap >=
|
|
|
218 |
this.fixedMainButtonWidth;
|
|
|
219 |
|
|
|
220 |
this.toggleCollapseButtonMainLabel(hasSpaceForBothButtons);
|
|
|
221 |
};
|
|
|
222 |
self.handleResize = self.handleResize.bind(self);
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Set toggle button collapsed state.
|
|
|
226 |
* @param {boolean} shouldBeCollapsed True for collapsed state.
|
|
|
227 |
*/
|
|
|
228 |
self.setButtonsCollapsed = (shouldBeCollapsed) => {
|
|
|
229 |
if (typeof shouldBeCollapsed !== 'boolean') {
|
|
|
230 |
return; // Invalid type
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
const ariaActionText = shouldBeCollapsed ?
|
|
|
234 |
H5PEditor.t('core', 'expandAllContent') :
|
|
|
235 |
H5PEditor.t('core', 'collapseAllContent');
|
|
|
236 |
|
|
|
237 |
if (this.collapseButtonList) {
|
|
|
238 |
this.collapseButtonList.classList.toggle(
|
|
|
239 |
'collapsed', shouldBeCollapsed
|
|
|
240 |
);
|
|
|
241 |
|
|
|
242 |
this.collapseButtonList.setAttribute(
|
|
|
243 |
'aria-label',
|
|
|
244 |
`${this.collapseButtonList.innerText}. ${ariaActionText}`
|
|
|
245 |
);
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
if (this.collapseButtonMain) {
|
|
|
249 |
this.collapseButtonMain.classList.toggle(
|
|
|
250 |
'collapsed', shouldBeCollapsed
|
|
|
251 |
);
|
|
|
252 |
|
|
|
253 |
this.collapseButtonMainLabel.innerText = ariaActionText;
|
|
|
254 |
}
|
|
|
255 |
};
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Add group collapse listener.
|
|
|
259 |
*/
|
|
|
260 |
self.addGroupCollapseListener = () => {
|
|
|
261 |
if (this.hasCollapseCapabilities()) {
|
|
|
262 |
return; // Don't add extra listener
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
list.on('groupCollapsedStateChanged', (event) => {
|
|
|
266 |
this.setButtonsCollapsed(event.data.allGroupsCollapsed);
|
|
|
267 |
});
|
|
|
268 |
|
|
|
269 |
/*
|
|
|
270 |
* Note: This is a workaround. It determines the element to focus
|
|
|
271 |
* by finding the first contained error message and then choosing the
|
|
|
272 |
* first element with the `.error` class that is commonly used by H5P
|
|
|
273 |
* editor widgets. This may fail if an editor widget does not put the
|
|
|
274 |
* `.error` class on the element however. If no such element is found,
|
|
|
275 |
* the error message will at least be scrolled into view.
|
|
|
276 |
* Ideally, every widget would have a method to return fields that do
|
|
|
277 |
* not validate, but that would require to change every widget and should
|
|
|
278 |
* be documented in the H5P core API.
|
|
|
279 |
*/
|
|
|
280 |
list.on('cannotCollapseAll', () => {
|
|
|
281 |
const errorMessageDOM =
|
|
|
282 |
[... this.container.querySelectorAll('.h5p-errors')]
|
|
|
283 |
.filter((error) => error.innerHTML.length > 0)
|
|
|
284 |
.shift();
|
|
|
285 |
|
|
|
286 |
if (!errorMessageDOM) {
|
|
|
287 |
return;
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
let errorDOM;
|
|
|
291 |
let parentNode = errorMessageDOM.parentNode;
|
|
|
292 |
|
|
|
293 |
while (!errorDOM && parentNode) {
|
|
|
294 |
errorDOM = parentNode.querySelector('.error');
|
|
|
295 |
parentNode = parentNode.parentNode;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
if (errorDOM) {
|
|
|
299 |
errorDOM?.focus();
|
|
|
300 |
}
|
|
|
301 |
else {
|
|
|
302 |
errorMessageDOM.scrollIntoView();
|
|
|
303 |
}
|
|
|
304 |
});
|
|
|
305 |
};
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* Add toggle buttons for collapsing/expanding groups to container.
|
|
|
309 |
*
|
|
|
310 |
* There's a main button for the topmost list with groups and a button that
|
|
|
311 |
* replaces the original list title for all other lists.
|
|
|
312 |
*/
|
|
|
313 |
self.addCollapseButtons = () => {
|
|
|
314 |
if (this.hasCollapseCapabilities()) {
|
|
|
315 |
return; // Don't add extra buttons
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/*
|
|
|
319 |
* Adding the same flex-wrapper approach that's used for the content title
|
|
|
320 |
* label and the metadata button, so the "collapse/expand" button can be
|
|
|
321 |
* aligned as required.
|
|
|
322 |
*/
|
|
|
323 |
this.collapseButtonsWrapper = document.createElement('div');
|
|
|
324 |
this.collapseButtonsWrapper.classList.add(
|
|
|
325 |
'h5p-editor-flex-wrapper', 'has-button-collapse'
|
|
|
326 |
);
|
|
|
327 |
|
|
|
328 |
/*
|
|
|
329 |
* Move original label offsite, because it is used as a <label> for screen
|
|
|
330 |
* readers and display list title collapse button instead.
|
|
|
331 |
*/
|
|
|
332 |
this.originalLabel =
|
|
|
333 |
this.container.parentNode?.querySelector('.h5peditor-label');
|
|
|
334 |
this.originalLabel.classList.add('offsite');
|
|
|
335 |
|
|
|
336 |
this.collapseButtonList = document.createElement('button');
|
|
|
337 |
this.collapseButtonList.classList.add('h5peditor-label-button');
|
|
|
338 |
|
|
|
339 |
const icon = document.createElement('div');
|
|
|
340 |
icon.classList.add('icon');
|
|
|
341 |
this.collapseButtonList.append(icon);
|
|
|
342 |
|
|
|
343 |
const label = document.createElement('div');
|
|
|
344 |
label.classList.add('label', 'h5peditor-required');
|
|
|
345 |
label.innerText = this.originalLabel.innerText;
|
|
|
346 |
this.collapseButtonList.append(label);
|
|
|
347 |
|
|
|
348 |
this.collapseButtonList.setAttribute(
|
|
|
349 |
'aria-label',
|
|
|
350 |
`${this.collapseButtonList.innerText}. ${H5PEditor.t('core', 'collapseAllContent')}`
|
|
|
351 |
);
|
|
|
352 |
|
|
|
353 |
this.collapseButtonList.addEventListener('click', () => {
|
|
|
354 |
list.toggleItemCollapsed();
|
|
|
355 |
});
|
|
|
356 |
|
|
|
357 |
/*
|
|
|
358 |
* If label is directly before the list editor container, put it next to
|
|
|
359 |
* the button. Otherwise, e. g. when there are list widgets, use button
|
|
|
360 |
* alone on top of those and leave the "label" where it was.
|
|
|
361 |
*/
|
|
|
362 |
const bothsButtonsInSameContainer =
|
|
|
363 |
self.container.previousSibling === this.originalLabel;
|
|
|
364 |
|
|
|
365 |
if (bothsButtonsInSameContainer) {
|
|
|
366 |
this.collapseButtonsWrapper.classList.add('has-label');
|
|
|
367 |
this.collapseButtonsWrapper.append(this.collapseButtonList);
|
|
|
368 |
}
|
|
|
369 |
else {
|
|
|
370 |
self.container.previousSibling.parentNode?.insertBefore(
|
|
|
371 |
this.collapseButtonList, self.container.previousSibling
|
|
|
372 |
);
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
if (shouldListGetCollapseButtonMain(list)) {
|
|
|
376 |
this.collapseButtonMain = document.createElement('button');
|
|
|
377 |
this.collapseButtonMain.classList.add(
|
|
|
378 |
'h5peditor-button',
|
|
|
379 |
'h5peditor-button-textual',
|
|
|
380 |
'h5peditor-button-collapse'
|
|
|
381 |
);
|
|
|
382 |
|
|
|
383 |
// Icon fixed left aligned
|
|
|
384 |
const icon = document.createElement('div');
|
|
|
385 |
icon.classList.add('icon');
|
|
|
386 |
this.collapseButtonMain.append(icon);
|
|
|
387 |
|
|
|
388 |
// Label centered in remaining space
|
|
|
389 |
this.collapseButtonMainLabel = document.createElement('div');
|
|
|
390 |
this.collapseButtonMainLabel.classList.add('label');
|
|
|
391 |
this.collapseButtonMain.append(this.collapseButtonMainLabel);
|
|
|
392 |
|
|
|
393 |
this.collapseButtonMainLabel.innerText =
|
|
|
394 |
H5PEditor.t('core', 'collapseAllContent');
|
|
|
395 |
|
|
|
396 |
// Longest label should fit inside button
|
|
|
397 |
setcollapseButtonMainWidth();
|
|
|
398 |
|
|
|
399 |
this.collapseButtonMain.addEventListener('click', () => {
|
|
|
400 |
list.toggleItemCollapsed();
|
|
|
401 |
});
|
|
|
402 |
|
|
|
403 |
this.collapseButtonsWrapper.append(this.collapseButtonMain);
|
|
|
404 |
|
|
|
405 |
if (bothsButtonsInSameContainer) {
|
|
|
406 |
// We may need to hide the main button's label
|
|
|
407 |
H5P.$window.get(0).addEventListener('resize', self.handleResize);
|
|
|
408 |
}
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
self.container.parentNode?.prepend(this.collapseButtonsWrapper);
|
|
|
412 |
};
|
|
|
413 |
|
|
|
414 |
// Create add button
|
|
|
415 |
var $button = ns.createButton(
|
|
|
416 |
list.getImportance(),
|
|
|
417 |
H5PEditor.t('core', 'addEntity', { ':entity': entity }),
|
|
|
418 |
() => {
|
|
|
419 |
list.addItem();
|
|
|
420 |
|
|
|
421 |
if (!this.hasCollapseCapabilities()) {
|
|
|
422 |
addGroupCollapseFunctionality();
|
|
|
423 |
}
|
|
|
424 |
},
|
|
|
425 |
true
|
|
|
426 |
);
|
|
|
427 |
|
|
|
428 |
// Used when dragging items around
|
|
|
429 |
var adjustX, adjustY, marginTop, formOffset;
|
|
|
430 |
|
|
|
431 |
/**
|
|
|
432 |
* @private
|
|
|
433 |
* @param {jQuery} $item
|
|
|
434 |
* @param {jQuery} $placeholder
|
|
|
435 |
* @param {Number} x
|
|
|
436 |
* @param {Number} y
|
|
|
437 |
*/
|
|
|
438 |
var moveItem = function ($item, $placeholder, x, y) {
|
|
|
439 |
var currentIndex;
|
|
|
440 |
|
|
|
441 |
// Adjust so the mouse is placed on top of the icon.
|
|
|
442 |
x = x - adjustX;
|
|
|
443 |
y = y - adjustY;
|
|
|
444 |
$item.css({
|
|
|
445 |
top: y - marginTop - formOffset.top,
|
|
|
446 |
left: x - formOffset.left
|
|
|
447 |
});
|
|
|
448 |
|
|
|
449 |
// Try to move up.
|
|
|
450 |
var $prev = $item.prev().prev();
|
|
|
451 |
if ($prev.length && y < $prev.offset().top + ($prev.height() / 2)) {
|
|
|
452 |
$prev.insertAfter($item);
|
|
|
453 |
|
|
|
454 |
currentIndex = $item.index();
|
|
|
455 |
list.moveItem(currentIndex, currentIndex - 1);
|
|
|
456 |
|
|
|
457 |
return;
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
// Try to move down.
|
|
|
461 |
var $next = $item.next();
|
|
|
462 |
if (
|
|
|
463 |
$next.length && y + $item.height() >
|
|
|
464 |
$next.offset().top + ($next.height() / 2)
|
|
|
465 |
) {
|
|
|
466 |
$next.insertBefore($placeholder);
|
|
|
467 |
|
|
|
468 |
currentIndex = $item.index() - 2;
|
|
|
469 |
list.moveItem(currentIndex, currentIndex + 1);
|
|
|
470 |
}
|
|
|
471 |
};
|
|
|
472 |
|
|
|
473 |
/**
|
|
|
474 |
* Default confirm handler.
|
|
|
475 |
*
|
|
|
476 |
* @param {Object} item Content parameters
|
|
|
477 |
* @param {number} id Index of element being removed
|
|
|
478 |
* @param {Object} buttonOffset Delete button offset, useful for positioning dialog
|
|
|
479 |
* @param {function} confirm Run to confirm delete
|
|
|
480 |
*/
|
|
|
481 |
self.defaultConfirmHandler = function (item, id, buttonOffset, confirm) {
|
|
|
482 |
// Create default confirmation dialog for removing list item
|
|
|
483 |
const confirmRemovalDialog = new H5P.ConfirmationDialog({
|
|
|
484 |
dialogText: H5PEditor.t('core', 'confirmRemoval', { ':type': entity })
|
|
|
485 |
}).appendTo(document.body);
|
|
|
486 |
|
|
|
487 |
// Remove list item on confirmation
|
|
|
488 |
confirmRemovalDialog.on('confirmed', confirm);
|
|
|
489 |
confirmRemovalDialog.show(buttonOffset.top);
|
|
|
490 |
};
|
|
|
491 |
|
|
|
492 |
// Use the default confirmation handler by default
|
|
|
493 |
let confirmHandler = self.defaultConfirmHandler;
|
|
|
494 |
|
|
|
495 |
/**
|
|
|
496 |
* Set custom confirmation handler callback (instead of the default dialog)
|
|
|
497 |
*
|
|
|
498 |
* @public
|
|
|
499 |
* @param {function} confirmHandler
|
|
|
500 |
*/
|
|
|
501 |
self.setConfirmHandler = function (handler) {
|
|
|
502 |
confirmHandler = handler;
|
|
|
503 |
};
|
|
|
504 |
|
|
|
505 |
/**
|
|
|
506 |
* Adds UI items to the widget.
|
|
|
507 |
*
|
|
|
508 |
* @public
|
|
|
509 |
* @param {Object} item
|
|
|
510 |
*/
|
|
|
511 |
self.addItem = function (item) {
|
|
|
512 |
var $placeholder, mouseDownAt;
|
|
|
513 |
var $item = $('<li/>', {
|
|
|
514 |
'class' : 'h5p-li',
|
|
|
515 |
});
|
|
|
516 |
|
|
|
517 |
/**
|
|
|
518 |
* Mouse move callback
|
|
|
519 |
*
|
|
|
520 |
* @private
|
|
|
521 |
* @param {Object} event
|
|
|
522 |
*/
|
|
|
523 |
var move = function (event) {
|
|
|
524 |
if (mouseDownAt) {
|
|
|
525 |
// Have not started moving yet
|
|
|
526 |
|
|
|
527 |
if (! (
|
|
|
528 |
event.pageX > mouseDownAt.x + 5 ||
|
|
|
529 |
event.pageX < mouseDownAt.x - 5 ||
|
|
|
530 |
event.pageY > mouseDownAt.y + 5 ||
|
|
|
531 |
event.pageY < mouseDownAt.y - 5
|
|
|
532 |
)) {
|
|
|
533 |
return; // Not ready to start moving
|
|
|
534 |
}
|
|
|
535 |
|
|
|
536 |
// Prevent wysiwyg becoming unresponsive
|
|
|
537 |
H5PEditor.Html.removeWysiwyg();
|
|
|
538 |
|
|
|
539 |
// Prepare to start moving
|
|
|
540 |
mouseDownAt = null;
|
|
|
541 |
|
|
|
542 |
var offset = $item.offset();
|
|
|
543 |
adjustX = event.pageX - offset.left;
|
|
|
544 |
adjustY = event.pageY - offset.top;
|
|
|
545 |
marginTop = parseInt($item.css('marginTop'));
|
|
|
546 |
formOffset = $list.offsetParent().offset();
|
|
|
547 |
// TODO: Couldn't formOffset and margin be added?
|
|
|
548 |
|
|
|
549 |
var width = $item.width();
|
|
|
550 |
var height = $item.height();
|
|
|
551 |
|
|
|
552 |
$item.addClass('moving').css({
|
|
|
553 |
width: width,
|
|
|
554 |
height: height
|
|
|
555 |
});
|
|
|
556 |
$placeholder = $('<li/>', {
|
|
|
557 |
'class': 'placeholder h5p-li',
|
|
|
558 |
css: {
|
|
|
559 |
width: width,
|
|
|
560 |
height: height
|
|
|
561 |
}
|
|
|
562 |
}).insertBefore($item);
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
moveItem($item, $placeholder, event.pageX, event.pageY);
|
|
|
566 |
};
|
|
|
567 |
|
|
|
568 |
/**
|
|
|
569 |
* Mouse button release callback
|
|
|
570 |
*
|
|
|
571 |
* @private
|
|
|
572 |
*/
|
|
|
573 |
var up = function () {
|
|
|
574 |
|
|
|
575 |
// Stop listening for mouse move events
|
|
|
576 |
H5P.$window
|
|
|
577 |
.unbind('mousemove', move)
|
|
|
578 |
.unbind('mouseup', up);
|
|
|
579 |
|
|
|
580 |
// Enable text select again
|
|
|
581 |
H5P.$body
|
|
|
582 |
.css({
|
|
|
583 |
'-moz-user-select': '',
|
|
|
584 |
'-webkit-user-select': '',
|
|
|
585 |
'user-select': '',
|
|
|
586 |
'-ms-user-select': ''
|
|
|
587 |
})
|
|
|
588 |
.attr('unselectable', 'off')[0]
|
|
|
589 |
.onselectstart = H5P.$body[0].ondragstart = null;
|
|
|
590 |
|
|
|
591 |
if (!mouseDownAt) {
|
|
|
592 |
// Not your regular click, we have been moving
|
|
|
593 |
$item.removeClass('moving').css({
|
|
|
594 |
width: 'auto',
|
|
|
595 |
height: 'auto'
|
|
|
596 |
});
|
|
|
597 |
$placeholder.remove();
|
|
|
598 |
|
|
|
599 |
if (item instanceof H5PEditor.Group) {
|
|
|
600 |
// Avoid groups expand/collapse toggling
|
|
|
601 |
item.preventToggle = true;
|
|
|
602 |
}
|
|
|
603 |
}
|
|
|
604 |
};
|
|
|
605 |
|
|
|
606 |
/**
|
|
|
607 |
* Mouse button down callback
|
|
|
608 |
*
|
|
|
609 |
* @private
|
|
|
610 |
*/
|
|
|
611 |
var down = function (event) {
|
|
|
612 |
if (event.which !== 1) {
|
|
|
613 |
return; // Only allow left mouse button
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
mouseDownAt = {
|
|
|
617 |
x: event.pageX,
|
|
|
618 |
y: event.pageY
|
|
|
619 |
};
|
|
|
620 |
|
|
|
621 |
// Start listening for mouse move events
|
|
|
622 |
H5P.$window
|
|
|
623 |
.mousemove(move)
|
|
|
624 |
.mouseup(up);
|
|
|
625 |
|
|
|
626 |
// Prevent text select
|
|
|
627 |
H5P.$body
|
|
|
628 |
.css({
|
|
|
629 |
'-moz-user-select': 'none',
|
|
|
630 |
'-webkit-user-select': 'none',
|
|
|
631 |
'user-select': 'none',
|
|
|
632 |
'-ms-user-select': 'none'
|
|
|
633 |
})
|
|
|
634 |
.attr('unselectable', 'on')[0]
|
|
|
635 |
.onselectstart = H5P.$body[0].ondragstart = () => {
|
|
|
636 |
return false;
|
|
|
637 |
};
|
|
|
638 |
};
|
|
|
639 |
|
|
|
640 |
/**
|
|
|
641 |
* Order current list item up
|
|
|
642 |
*
|
|
|
643 |
* @private
|
|
|
644 |
*/
|
|
|
645 |
var moveItemUp = function () {
|
|
|
646 |
var $prev = $item.prev();
|
|
|
647 |
if (!$prev.length) {
|
|
|
648 |
return; // Cannot move item further up
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
// Prevent wysiwyg becoming unresponsive
|
|
|
652 |
H5PEditor.Html.removeWysiwyg();
|
|
|
653 |
|
|
|
654 |
var currentIndex = $item.index();
|
|
|
655 |
$prev.insertAfter($item);
|
|
|
656 |
list.moveItem(currentIndex, currentIndex - 1);
|
|
|
657 |
};
|
|
|
658 |
|
|
|
659 |
/**
|
|
|
660 |
* Order current ist item down
|
|
|
661 |
*
|
|
|
662 |
* @private
|
|
|
663 |
*/
|
|
|
664 |
var moveItemDown = function () {
|
|
|
665 |
var $next = $item.next();
|
|
|
666 |
if (!$next.length) {
|
|
|
667 |
return; // Cannot move item further down
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
// Prevent wysiwyg becoming unresponsive
|
|
|
671 |
H5PEditor.Html.removeWysiwyg();
|
|
|
672 |
|
|
|
673 |
var currentIndex = $item.index();
|
|
|
674 |
$next.insertBefore($item);
|
|
|
675 |
list.moveItem(currentIndex, currentIndex + 1);
|
|
|
676 |
};
|
|
|
677 |
|
|
|
678 |
// List item title bar
|
|
|
679 |
var $titleBar = $('<div/>', {
|
|
|
680 |
'class': 'list-item-title-bar',
|
|
|
681 |
appendTo: $item
|
|
|
682 |
});
|
|
|
683 |
|
|
|
684 |
// Container for list actions
|
|
|
685 |
var $listActions = $('<div/>', {
|
|
|
686 |
class: 'list-actions',
|
|
|
687 |
appendTo: $titleBar
|
|
|
688 |
});
|
|
|
689 |
|
|
|
690 |
// Append order button
|
|
|
691 |
var $orderGroup = $('<div/>', {
|
|
|
692 |
class : 'order-group',
|
|
|
693 |
appendTo: $listActions
|
|
|
694 |
});
|
|
|
695 |
|
|
|
696 |
H5PEditor.createButton(
|
|
|
697 |
'order-up', H5PEditor.t('core', 'orderItemUp'), moveItemUp
|
|
|
698 |
).appendTo($orderGroup);
|
|
|
699 |
H5PEditor.createButton(
|
|
|
700 |
'order-down', H5PEditor.t('core', 'orderItemDown'), moveItemDown
|
|
|
701 |
).appendTo($orderGroup);
|
|
|
702 |
|
|
|
703 |
H5PEditor.createButton(
|
|
|
704 |
'remove', H5PEditor.t('core', 'removeItem'), function () {
|
|
|
705 |
confirmHandler(item, $item.index(), $(this).offset(), function () {
|
|
|
706 |
list.removeItem($item.index());
|
|
|
707 |
$item.remove();
|
|
|
708 |
|
|
|
709 |
if (!(list.getValue() ?? []).length) {
|
|
|
710 |
self.removeCollapseButtons();
|
|
|
711 |
}
|
|
|
712 |
});
|
|
|
713 |
}
|
|
|
714 |
).appendTo($listActions);
|
|
|
715 |
|
|
|
716 |
// Append new field item to content wrapper
|
|
|
717 |
if (item instanceof H5PEditor.Group) {
|
|
|
718 |
// Append to item
|
|
|
719 |
item.appendTo($item);
|
|
|
720 |
$item.addClass('listgroup');
|
|
|
721 |
$titleBar.addClass(list.getImportance());
|
|
|
722 |
|
|
|
723 |
// Move label
|
|
|
724 |
$item
|
|
|
725 |
.children('.field').children('.title')
|
|
|
726 |
.appendTo($titleBar).addClass('h5peditor-label');
|
|
|
727 |
|
|
|
728 |
// Handle expand and collapse
|
|
|
729 |
item.on('expanded', function () {
|
|
|
730 |
$item.addClass('expanded').removeClass('collapsed');
|
|
|
731 |
});
|
|
|
732 |
item.on('collapsed', function () {
|
|
|
733 |
$item.removeClass('expanded').addClass('collapsed');
|
|
|
734 |
});
|
|
|
735 |
}
|
|
|
736 |
else {
|
|
|
737 |
// Append content wrapper
|
|
|
738 |
var $content = $('<div/>', {
|
|
|
739 |
'class' : 'content'
|
|
|
740 |
}).appendTo($item);
|
|
|
741 |
|
|
|
742 |
// Add importance to items not in groups
|
|
|
743 |
$titleBar.addClass(list.getImportance());
|
|
|
744 |
|
|
|
745 |
// Append field
|
|
|
746 |
item.appendTo($content);
|
|
|
747 |
|
|
|
748 |
if (item.field.label !== 0) {
|
|
|
749 |
// Try to find and move the label to the title bar
|
|
|
750 |
const $label =
|
|
|
751 |
$content.children('.field').find('.h5peditor-label:first');
|
|
|
752 |
|
|
|
753 |
if ($label.length !== 0) {
|
|
|
754 |
$titleBar.append($('<label/>', {
|
|
|
755 |
'class': 'h5peditor-label',
|
|
|
756 |
'for': $label.parent().attr('for'),
|
|
|
757 |
html: $label.html()
|
|
|
758 |
}));
|
|
|
759 |
|
|
|
760 |
$label.hide();
|
|
|
761 |
}
|
|
|
762 |
}
|
|
|
763 |
}
|
|
|
764 |
|
|
|
765 |
// Append item to list
|
|
|
766 |
$item.appendTo($list);
|
|
|
767 |
|
|
|
768 |
if (item instanceof H5PEditor.Group && item.field.expanded !== false) {
|
|
|
769 |
/*
|
|
|
770 |
* Good UX: automatically expand groups if not explicitly disabled by
|
|
|
771 |
* semantics
|
|
|
772 |
*/
|
|
|
773 |
item.expand();
|
|
|
774 |
}
|
|
|
775 |
|
|
|
776 |
$titleBar.children('.h5peditor-label').mousedown(down);
|
|
|
777 |
};
|
|
|
778 |
|
|
|
779 |
/**
|
|
|
780 |
* Determine if child is a text field
|
|
|
781 |
*
|
|
|
782 |
* @param {Object} child
|
|
|
783 |
* @returns {boolean} True if child is a text field
|
|
|
784 |
*/
|
|
|
785 |
self.isTextField = function (child) {
|
|
|
786 |
var widget = ns.getWidgetName(child.field);
|
|
|
787 |
return widget === 'html' || widget === 'text';
|
|
|
788 |
};
|
|
|
789 |
|
|
|
790 |
/**
|
|
|
791 |
* Puts this widget at the end of the given container.
|
|
|
792 |
*
|
|
|
793 |
* @public
|
|
|
794 |
* @param {jQuery} $container
|
|
|
795 |
*/
|
|
|
796 |
self.appendTo = function ($container) {
|
|
|
797 |
self.container = $container[0];
|
|
|
798 |
|
|
|
799 |
addGroupCollapseFunctionality();
|
|
|
800 |
|
|
|
801 |
$list.appendTo($container);
|
|
|
802 |
$button.appendTo($container);
|
|
|
803 |
};
|
|
|
804 |
|
|
|
805 |
/**
|
|
|
806 |
* Remove this widget from the editor DOM.
|
|
|
807 |
*
|
|
|
808 |
* @public
|
|
|
809 |
*/
|
|
|
810 |
self.remove = function () {
|
|
|
811 |
this.removeCollapseButtons();
|
|
|
812 |
$list.remove();
|
|
|
813 |
$button.remove();
|
|
|
814 |
};
|
|
|
815 |
|
|
|
816 |
/**
|
|
|
817 |
* Remove collapse buttons from container.
|
|
|
818 |
*/
|
|
|
819 |
self.removeCollapseButtons = () => {
|
|
|
820 |
this.originalLabel?.classList.remove('offsite');
|
|
|
821 |
this.collapseButtonList?.remove();
|
|
|
822 |
this.collapseButtonMain?.remove();
|
|
|
823 |
this.collapseButtonsWrapper?.remove();
|
|
|
824 |
H5P.$window.get(0).removeEventListener('resize', self.handleResize);
|
|
|
825 |
};
|
|
|
826 |
}
|
|
|
827 |
|
|
|
828 |
return ListEditor;
|
|
|
829 |
})(H5P.jQuery);
|