-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvertical-tabs.js
534 lines (460 loc) · 14.3 KB
/
vertical-tabs.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/** @namespace H5PEditor */
var H5PEditor = H5PEditor || {};
H5PEditor.VerticalTabs = (function ($) {
/**
* Utility for checking if any of the parent fields is using the VerticalTabs widget
*
* @param {Object} field
* @returns bool
*/
const hasVerticalTabAsParent = function (field) {
if (!field) {
return false;
}
if (field.widget && field.widget instanceof H5PEditor.VerticalTabs) {
return true;
}
if (field.parent) {
return hasVerticalTabAsParent(field.parent);
}
return false;
}
/**
* Draws the list.
*
* @class
* @param {List} list
*/
function VerticalTabs(list) {
var self = this;
if (hasVerticalTabAsParent(list.parent)) {
self.genericList = new H5PEditor.ListEditor(list);
}
else {
var entity = list.getEntity();
// Make first letter upper case.
entity = entity.substr(0,1).toUpperCase() + entity.substr(1);
// Create DOM elements
var $wrapper = $('<div/>', {
'class': 'h5p-vtab-wrapper'
});
var $inner = $('<div/>', {
'class': 'h5p-vtabs'
}).appendTo($wrapper);
var $tabs = $('<ol/>', {
id: list.getId(),
'aria-describedby': list.getDescriptionId(),
'role': 'tablist',
'class': 'h5p-ul'
}).appendTo($inner);
H5PEditor.createButton('add-entity', H5PEditor.t('core', 'addEntity', {':entity': entity}), function () {
if (list.addItem()) {
$tabs.children(':last').trigger('open');
toggleOrderButtonsState();
}
}, true).appendTo($inner);
var $forms = $('<div/>', {
'class': 'h5p-vtab-forms'
}).appendTo($wrapper);
// Once all items have been added we toggle the state of the order buttons
list.once('changeWidget', function () {
toggleOrderButtonsState();
});
}
// Used when dragging items around
var adjustX, adjustY, marginTop, formOffset, $currentTab;
/**
* @private
* @param {jQuery} $item
* @param {jQuery} $placeholder
* @param {Number} x
* @param {Number} y
*/
var moveItem = function ($item, $placeholder, x, y) {
var currentIndex;
// Adjust so the mouse is placed on top of the icon.
x = x - adjustX;
y = y - adjustY;
$item.css({
top: y - marginTop - formOffset.top,
left: x - formOffset.left
});
// Try to move up.
var $prev = $item.prev().prev();
if ($prev.length && y < $prev.offset().top + ($prev.height() / 2)) {
$prev.insertAfter($item);
currentIndex = $item.index();
list.moveItem(currentIndex, currentIndex - 1);
return;
}
// Try to move down.
var $next = $item.next();
if ($next.length && y + $item.height() > $next.offset().top + ($next.height() / 2)) {
$next.insertBefore($placeholder);
currentIndex = $item.index() - 2;
list.moveItem(currentIndex, currentIndex + 1);
}
};
/**
* Re-index labels. Necessary after tabs are sorted or removed.
*
* @private
*/
var reindexLabels = function () {
$tabs.find('.h5p-index-label').each(function (index, element) {
$(element).text(index + 1);
});
toggleOrderButtonsState();
};
/**
* Decode HTML entities
* @param {String}
* @return {String}
*/
var decodeEntity = function(inputStr) {
var textarea = document.createElement("textarea");
textarea.innerHTML = inputStr;
return textarea.value;
}
/**
* Always run after reordering, adding or removing to ensure correct
* state of the order buttons.
*
* @private
*/
var toggleOrderButtonsState = function () {
$tabs.children().each(function (index, element) {
var $tab = $(element);
var isTopTab = !$tab.prev().length;
$tab.find('.order-up').attr('aria-disabled', isTopTab).attr('tabindex', isTopTab ? '-1' : '0');
var isBottomTab = !$tab.next().length;
$tab.find('.order-down').attr('aria-disabled', isBottomTab).attr('tabindex', isBottomTab ? '-1' : '0');
});
};
/**
* Opens the given tab.
*
* @private
* @param {jQuery} $newTab
*/
var openTab = function ($newTab) {
if ($currentTab !== undefined) {
H5PEditor.Html.removeWysiwyg();
$currentTab.removeClass('h5p-current');
}
$newTab.addClass('h5p-current');
$currentTab = $newTab;
};
/**
* Adds UI items to the widget.
*
* @public
* @param {Object} item
*/
self.addItem = function (item) {
if (self.genericList) {
self.genericList.addItem(item);
return;
}
var $placeholder;
var $tab = $('<li/>', {
'class': 'h5p-vtab-li'
}).appendTo($tabs);
/**
* Mouse move callback
*
* @private
* @param {Object} event
*/
var move = function (event) {
moveItem($tab, $placeholder, event.pageX, event.pageY);
};
/**
* Mouse button release callback
*
* @private
*/
var up = function () {
H5P.$window
.unbind('mousemove', move)
.unbind('mouseup', up);
H5P.$body
.attr('unselectable', 'off')
.css({
'-moz-user-select': '',
'-webkit-user-select': '',
'user-select': '',
'-ms-user-select': '',
'overflow': '',
})[0].onselectstart = H5P.$body[0].ondragstart = null;
$tab.removeClass('h5p-moving').css({
width: 'auto',
height: 'auto',
top: '',
left: ''
});
$placeholder.remove();
reindexLabels();
};
/**
* Mouse button down callback
*
* @private
*/
var down = function (event) {
if (event.which !== 1) {
return; // Only allow left mouse button
}
H5P.$window
.mousemove(move)
.mouseup(up);
// Start tracking mouse
H5P.$body
.attr('unselectable', 'on')
.css({
'-moz-user-select': 'none',
'-webkit-user-select': 'none',
'user-select': 'none',
'-ms-user-select': 'none',
'overflow': 'hidden'
})[0].onselectstart = H5P.$body[0].ondragstart = function () {
return false;
};
var offset = $tab.offset();
adjustX = event.pageX - offset.left;
adjustY = event.pageY - offset.top;
marginTop = parseInt($tab.css('marginTop'));
formOffset = $tabs.offsetParent().offset();
// TODO: Couldn't formOffset and margin be added?
var width = $tab.width();
var height = $tab.height();
$tab.addClass('h5p-moving').css({
width: width,
height: height
});
$placeholder = $('<li/>', {
'class': 'h5p-placeholder'
}).insertBefore($tab);
$('<div/>', {
class: 'h5p-vtab-a',
}).appendTo($placeholder);
move(event);
};
/**
* Order current list item up
*
* @private
*/
var moveItemUp = function () {
var $prev = $tab.prev();
if (!$prev.length) {
return; // Cannot move item further up
}
var currentIndex = $tab.index();
$prev.insertAfter($tab);
list.moveItem(currentIndex, currentIndex - 1);
reindexLabels();
};
/**
* Order current ist item down
*
* @private
*/
var moveItemDown = function () {
var $next = $tab.next();
if (!$next.length) {
return; // Cannot move item further down
}
var currentIndex = $tab.index();
$next.insertBefore($tab);
list.moveItem(currentIndex, currentIndex + 1);
reindexLabels();
};
// Handle opening of the tab
$tab.on('open', function () {
openTab($tab.add($form));
});
var mouseDownPos;
// Add clickable label
$('<div/>', {
'class' : 'h5p-vtab-a',
html: '<span class="h5p-index-label">' + ($tab.index() + 1) + '</span>. <span class="h5p-label" title="' + entity + '">' + entity + '</span>',
role: 'tab',
tabIndex: 0,
on: {
mouseup: function (e) {
if (!mouseDownPos) {
return;
}
// Determine movement
var xDiff = Math.abs(mouseDownPos.x - e.pageX);
var yDiff = Math.abs(mouseDownPos.y - e.pageY);
var moveThreshold = 20;
// Open tab if moved less than threshold
if (xDiff < moveThreshold && yDiff < moveThreshold) {
$tab.trigger('open');
}
},
mousedown: function (e) {
// Start position
mouseDownPos = {
x: e.pageX,
y: e.pageY
};
// Order element
down(e);
},
keypress: function (e) {
if (e.which === 32) {
e.preventDefault();
$tab.trigger('open');
}
}
}
}).appendTo($tab);
var $tabLabel = $tab.find('.h5p-label');
var setTabLabel = function (label) {
$tabLabel.text(label).attr('title', label);
};
// Add buttons for ordering
var $orderWrapper = $('<div/>', {
'class': 'vtab-order-wrapper',
appendTo: $tab
});
H5PEditor.createButton('order-up', H5PEditor.t('core', 'orderItemUp'), moveItemUp).appendTo($orderWrapper);
H5PEditor.createButton('order-down', H5PEditor.t('core', 'orderItemDown'), moveItemDown).appendTo($orderWrapper);
// Add remove button
var $removeWrapper = $('<div/>', {
'class': 'vtab-remove-wrapper',
appendTo: $tab
});
H5PEditor.createButton('remove', H5PEditor.t('core', 'removeItem'), function () {
confirmRemovalDialog.show($(this).offset().top);
}).appendTo($removeWrapper);
// Create confirmation dialog for removing list item
var confirmRemovalDialog = new H5P.ConfirmationDialog({
dialogText: H5PEditor.t('core', 'confirmRemoval', {':type': entity.toLocaleLowerCase()})
}).appendTo(document.body);
// Remove list item on confirmation
confirmRemovalDialog.on('confirmed', function () {
var $next, index = $tab.index();
if ($tab.hasClass('h5p-current')) {
if (index) {
// Go to previous tab
$next = $tab.prev().add($form.prev());
}
else {
// Go to next tab
$next = $tab.next().add($form.next());
}
if ($next.length) {
// Open another tab
$next.trigger('open');
}
}
list.removeItem(index);
$tab.remove();
$form.remove();
reindexLabels();
});
// Create form wrapper
var $form = $('<fieldset/>', {
'role': 'tabpanel',
'class': 'h5p-vtab-form'
});
if (item instanceof H5PEditor.Group) {
item.on('summary', function (event) {
if (event.data) {
// Update tab with summary
setTabLabel(event.data.substr(0, 32));
}
});
}
// Append new field item to forms wrapper
item.appendTo($form);
// Append form wrapper to forms list
$form.appendTo($forms);
// Good UX: automatically expand groups
if (item instanceof H5PEditor.Group) {
item.expand();
// Remove group title
item.$group.children('.title').remove();
}
else if (item instanceof H5PEditor.Library) {
$form.addClass('content');
let summary = item.$select.children(':selected').text();
if (item.params.metadata && item.params.metadata.title) {
// The given title usually makes more sense than the type name
summary = decodeEntity(item.params.metadata.title) + (!item.libraries || (item.libraries.length > 1 && item.params.metadata.title.indexOf(summary) === -1) ? ' (' + summary + ')' : '');
}
setTabLabel(summary);
// Use selected library as title
item.changes.push(function (library) {
setTabLabel(library ? library.title : '');
});
let lastLib;
const setSummary = function () {
if (item.params && item.params.metadata && item.params.metadata.title) {
// The given title usually makes more sense than the type name
setTabLabel(decodeEntity(item.params.metadata.title) + (item.libraries.length > 1 && item.params.metadata.title.indexOf(lastLib.title) === -1 ? ' (' + lastLib.title + ')' : ''));
}
else {
setTabLabel(lastLib ? lastLib.title : '');
}
};
if (item.metadataForm) {
item.metadataForm.on('titlechange', setSummary);
}
item.change(function (library) {
lastLib = library;
setSummary();
if (item.metadataForm) {
// Update summary when metadata title changes
item.metadataForm.off('titlechange', setSummary);
item.metadataForm.on('titlechange', setSummary);
}
});
}
else if (item instanceof H5PEditor.Select) {
// Use selected value as title
var change = function () {
var value = item.$select.val();
setTabLabel(value === '-' ? entity : item.$select.children('option[value="' + value + '"]').text());
};
item.$select.change(change);
change();
}
if ($currentTab === undefined) {
// Open tab if there are none open
$tab.trigger('open');
}
$tabs.find('.h5p-vtab-li .h5p-vtab-a').first().focus();
};
/**
* Puts this widget at the end of the given container.
*
* @public
* @param {jQuery} $container
*/
self.appendTo = function ($container) {
if (self.genericList) {
self.genericList.appendTo($container);
return;
}
$wrapper.appendTo($container);
};
/**
* Remove this widget from the editor DOM.
*
* @public
*/
self.remove = function () {
if (self.genericList) {
self.genericList.remove();
return;
}
$wrapper.remove();
};
}
return VerticalTabs;
})(H5P.jQuery);