-
Notifications
You must be signed in to change notification settings - Fork 53
/
interactive-video-editor.js
1828 lines (1592 loc) · 54.2 KB
/
interactive-video-editor.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global H5PEditor, H5P, H5PIntegration*/
H5PEditor.widgets.interactiveVideo = H5PEditor.InteractiveVideo = (function ($) {
var counter = 0;
/**
* Initialize interactive video editor.
*
* @class H5PEditor.InteractiveVideo
* @param {Object} parent
* @param {Object} field
* @param {Object} params
* @param {function} setValue
*/
function InteractiveVideoEditor(parent, field, params, setValue) {
var that = this;
H5P.DragNBar.FormManager.call(this, parent.parent, {
doneButtonLabel: t('done'),
deleteButtonLabel: t('remove'),
expandBreadcrumbButtonLabel: t('expandBreadcrumbButtonLabel'),
collapseBreadcrumbButtonLabel: t('collapseBreadcrumbButtonLabel')
}, 'interactivevideo');
this.parent = parent;
this.field = field;
this.resizeId = 'resize.iveditor-' + (counter++);
this.findField(this.field.video, function (field) {
if (field.field.type !== 'video') {
throw t('notVideoField', {':path': that.field.video});
}
if (field.params !== undefined) {
that.setVideo(field.params);
}
field.changes.push(function () {
that.setVideo(field.params);
});
});
this.findField(this.field.poster, function (field) {
if (field.field.type !== 'image') {
throw t('notImageField', {':path': that.field.poster});
}
if (field.params !== undefined) {
that.setPoster(field.params);
}
field.changes.push(function () {
that.setPoster(field.params);
});
});
// Will be true only on first load of IV or if there's no video file
this.freshVideo = (params === undefined || !parent.params.video.files);
this.params = $.extend({
interactions: [],
bookmarks: [],
endscreens: []
}, params);
setValue(field, this.params);
this.children = [];
this.passReadies = true;
parent.ready(function () {
that.passReadies = false;
// Set active right away to generate common fields for interactions.
that.setActive();
});
H5P.$window.on('resize', function () {
if (that.IV) {
that.IV.trigger('resize');
}
});
// Tour de editor
this.currentTabIndex = 0;
// When wizard changes step
parent.on('stepChanged', function (event) {
that.currentTabIndex = event.data.id;
that.startGuidedTour(H5PEditor.InteractiveVideo.GuidedTours.isOpen());
});
// Update paste button
H5P.externalDispatcher.on('datainclipboard', function (event) {
if (!that.libraries) {
return;
}
var canPaste = !event.data.reset;
if (canPaste) {
// Check if content type is supported here
canPaste = that.canPaste(H5P.getClipboard());
}
that.dnb.setCanPaste(canPaste);
});
}
InteractiveVideoEditor.prototype = Object.create(H5P.DragNBar.FormManager.prototype);
InteractiveVideoEditor.prototype.constructor = InteractiveVideoEditor;
/**
* Check if the clipboard can be pasted into IV.
*
* @param {Object} [clipboard] Clipboard data.
* @return {boolean} True, if clipboard can be pasted.
*/
InteractiveVideoEditor.prototype.canPaste = function (clipboard) {
if (clipboard) {
if (clipboard.from === InteractiveVideoEditor.clipboardKey &&
(!clipboard.generic || this.supported(clipboard.generic.library))) {
// Content comes from the same version of IV
// Non generic part = must be custom content from ourselves
return true;
}
else if (clipboard.generic && this.supported(clipboard.generic.library)) {
// Supported library from another content type
return true;
}
}
return false;
};
/**
* Must be changed if the semantics for the elements changes.
* @private
* @type {string}
*/
InteractiveVideoEditor.clipboardKey = 'H5PEditor.InteractiveVideo';
/**
* Find a field, then run the callback.
*
* @param {function} callback
*/
InteractiveVideoEditor.prototype.findField = function (path, callback) {
var that = this;
// Find field when tree is ready.
this.parent.ready(function () {
var field = H5PEditor.findField(path, that.parent);
if (!field) {
throw H5PEditor.t('core', 'unknownFieldPath', {':path': path});
}
callback(field);
});
};
/**
* Add "add item" items to chooser menu.
*
* @param {jQuery} $chooser - Chooser menu to attach the item to.
* @param {string} textIndex - Index of text to be translated or text if not found.
* @param {string} selector - Class name.
* @param {function} action - Action for click.
* @return {jQuery} Item for adding items.
*/
InteractiveVideoEditor.prototype.setChooserItem = function ($chooser, textIndex, selector, action) {
// Best effort for the item text
var itemText = t(textIndex);
if (itemText === '[Missing translation ' + textIndex + ']') {
itemText = textIndex;
}
var self = this;
var $item = $('<div/>', {
'class': selector,
html: itemText,
role: 'button',
tabindex: 0,
on: {
click: function () {
action.call(self);
}
}
});
$item.data('default', itemText);
$chooser.append($item);
return $item;
};
/**
* Attach a custom tooltip to a jQuery element.
*
* @param {jQuery} $element - Element to attach the tooltip to.
* @param {string} textIndex - Index of text to be translated or text if not found.
* @return {jQuery} Tooltip element.
*/
InteractiveVideoEditor.prototype.setTooltip = function ($element, textIndex) {
var that = this;
// Best effort for the tooltip text
var tooltipText = t(textIndex);
if (tooltipText === '[Missing translation ' + textIndex + ']') {
tooltipText = textIndex;
}
var $tooltip = $('<span>', {
class: 'h5p-interactive-video-tooltip',
html: tooltipText
});
$element
.append($tooltip)
// Hide hover on click
.hover(undefined, function () {
if (that.IV.$controls.find('.h5p-show').length === 0) {
$(this).removeClass('h5p-no-tooltip');
}
})
.click(function () {
$(this).addClass('h5p-no-tooltip');
});
return $tooltip;
};
/**
* Reposition/resize the tooltips.
*/
InteractiveVideoEditor.prototype.resizeTooltips = function () {
var that = this;
/*
* The tooltips should look like the dnb tooltips, but they lie within
* the controls bar which overrides the dynamic size of $container. We
* therefore get the first dnb tooltip and copy the current properties
* to be set for each tooltip.
*/
var $base = this.$bar.find('.h5p-dragnbar-tooltip').first();
var tooltips = this.IV.$container.find('.h5p-interactive-video-tooltip');
tooltips.each(function () {
$(this).css('font-size', $base.css('font-size'));
$(this).css('line-height', $base.css('line-height'));
$(this).css('padding', $base.css('padding'));
// Compute horizontal position (left aligned, centered, or right aligned to element)
var left = $(this).parent().offset().left - $(this).outerWidth() / 2 < 0;
var right = $(this).parent().offset().left + $(this).outerWidth() / 2 > that.IV.$container.width();
var pos;
if (left === right) {
pos = -($(this).outerWidth() / 2) + $(this).parent().outerWidth() / 2;
}
else if (left) {
pos = 0;
}
else {
pos = -$(this).outerWidth() + $(this).parent().outerWidth();
}
$(this).css('left', pos + 'px');
});
};
/**
* Our tab has been set active. Create a new player if necessary.
*/
InteractiveVideoEditor.prototype.setActive = function () {
if (this.IV !== undefined) {
// A video has been loaded, no need to recreate.
// (but we can do some resizing :D)
this.IV.trigger('resize');
return;
}
// Reset css
this.$editor.css({
width: '',
height: '',
fontSize: ''
});
if (this.video === undefined) {
this.$editor.html(this.noVideoSourceMessage(this.parent)).removeClass('h5p-interactive-video');
return;
}
var that = this;
// Create new player.
this.IV = new H5P.InteractiveVideo({
interactiveVideo: {
video: {
files: this.video,
startScreenOptions: {
poster: this.poster
}
},
assets: this.params
}
}, H5PEditor.contentId);
this.IV.editor = this;
$(window).on(this.resizeId, function () {
if (that.dnb) {
that.dnb.resize();
that.resizeTooltips();
}
});
for (var i = 0; i < this.IV.interactions.length; i++) {
this.processInteraction(this.IV.interactions[i], this.params.interactions[i]);
}
this.IV.on('controls', function () {
if (!that.IV) {
return; // Video source or poster may have changed – abort!
}
// Add DragNBar.
that.$bar = $('<div class="h5p-interactive-video-dragnbar">' + t('loading') + '</div>').prependTo(that.$editor);
var interactions = findField('interactions', that.field.fields);
var action = findField('action', interactions.field.fields);
H5PEditor.LibraryListCache.getLibraries(
action.options,
function (libraries) {
// Validate the extra reporting availabe before showing the Free text Question
// Allow for the org users
const allowedLibraries = libraries.filter(function (library) {
return (library.name !== 'H5P.FreeTextQuestion' || H5PEditor.reportingIsAvailable === undefined) ? true : H5PEditor.reportingIsAvailable;
});
this.createDragNBar(allowedLibraries);
this.setInteractionTitles();
this.startGuidedTour();
this.IV.trigger('dnbEditorReady');
},
that
);
// Add "add item" items
that.setChooserItem(that.IV.controls.$bookmarksChooser, 'addBookmark', 'h5p-add-bookmark', that.addBookmark);
that.setChooserItem(that.IV.controls.$endscreensChooser, 'addEndscreen', 'h5p-add-endscreen', that.addEndscreen);
// Add tooltips
that.$bookmarksTooltip = that.setTooltip(that.IV.controls.$bookmarksButton, 'tooltipBookmarks');
that.$endscreensTooltip = that.setTooltip(that.IV.controls.$endscreensButton.parent(), 'tooltipEndscreens');
});
this.IV.on('bookmarkAdded', that.bookmarkAdded, that);
this.IV.on('endscreenAdded', that.endscreenAdded, that);
this.IV.on('dnbEditorReady', that.resizeTooltips, that);
this.IV.attach(this.$editor);
// Create a focus handler
this.$focusHandler = $('<div>', {
'class': 'h5peditor-iv-focus-handler'
}).click(function () {
if (!that.dnb.focusedElement || !that.dnb.focusedElement.$element.is(':focus')) {
// No focused element, remove overlay
that.$focusHandler.removeClass('show');
that.IV.$overlay.removeClass('h5p-visible');
}
}).appendTo(this.IV.$videoWrapper);
this.pToEm = (this.IV.width / this.IV.fontSize) / 100;
};
/**
* Set custom interaction titles when libraries are registered.
*/
InteractiveVideoEditor.prototype.setInteractionTitles = function () {
var self = this;
this.IV.interactions.forEach(function (interaction) {
// Try to figure out a title for the dialog
var title = self.findLibraryTitle(interaction.getLibraryName());
if (!title) {
// Couldn't find anything, use default
title = self.IV.l10n.interaction;
}
interaction.setTitle(title);
});
// Create title element
this.$interactionTitle = $('<div>', {
'class': 'h5p-interaction-button-title'
}).appendTo(this.$editor);
};
InteractiveVideoEditor.prototype.showInteractionTitle = function (title, $interaction) {
if (!this.$interactionTitle) {
return;
}
// Set static margin
var fontSize = parseInt(this.IV.$videoWrapper.css('font-size'), 10);
var staticMargin = 0.3 * fontSize;
var videoOffsetX = $interaction.position().left;
var videoOffsetY = $interaction.position().top;
var dnbOffsetY = this.$bar.height();
this.$interactionTitle.html(title);
// center title
var totalOffsetX = videoOffsetX - (this.$interactionTitle.outerWidth(true) / 2) + ($interaction.width() / 2);
if (totalOffsetX < 0) {
totalOffsetX = 0;
}
else if (totalOffsetX + this.$interactionTitle.outerWidth(true) > this.IV.$videoWrapper.width()) {
totalOffsetX = this.IV.$videoWrapper.width() - this.$interactionTitle.outerWidth(true);
}
var totalOffsetY = videoOffsetY + dnbOffsetY - this.$interactionTitle.height() - 1;
this.$interactionTitle.css({
'left': totalOffsetX,
'top': totalOffsetY - staticMargin
}).addClass('show');
};
InteractiveVideoEditor.prototype.hideInteractionTitle = function () {
if (!this.$interactionTitle) {
return;
}
this.$interactionTitle.removeClass('show');
};
/**
* Add bookmark
*/
InteractiveVideoEditor.prototype.addBookmark = function () {
var time = this.IV.video.getCurrentTime();
// Find out where to place the bookmark
for (var i = 0; i < this.params.bookmarks.length; i++) {
if (this.params.bookmarks[i].time > time) {
// Insert before this.
break;
}
}
var tenth = Math.floor(time * 10) / 10;
if (this.checkMarkerSpace(tenth) === false) {
return; // Not space for another bookmark
}
// Hide dialog
if (this.IV.controls.$bookmarksChooser.hasClass('h5p-show')) {
this.IV.toggleBookmarksChooser(false, {keepStopped: true});
}
else if (this.IV.controls.$bookmarks) {
this.IV.controls.$bookmarks.click();
}
// Move other increament other ids.
this.IV.trigger('bookmarksChanged', {'index': i, 'number': 1});
this.params.bookmarks.splice(i, 0, {
time: time,
label: t('newBookmark')
});
var $bookmark = this.IV.addBookmark(i, tenth);
$bookmark.addClass('h5p-show');
$bookmark.find('.h5p-bookmark-text').click();
};
/**
* Add endscreen
* @param {number} time - Time in s to put endscreen at.
* @param {boolean} freshOnly - If true, the endscreen label will not pop up and only be included for fresh videos.
*/
InteractiveVideoEditor.prototype.addEndscreen = function (time, freshOnly) {
if (!this.freshVideo && freshOnly === true) {
return;
}
time = time || this.IV.video.getCurrentTime();
// Find out where to place the endscreen
for (var i = 0; i < this.params.endscreens.length; i++) {
if (this.params.endscreens[i].time > time) {
// Insert before this.
break;
}
}
var tenth = Math.floor(time * 10) / 10;
if (this.checkMarkerSpace(tenth) === false) {
return; // Not space for another endscreen
}
// Hide dialog
if (this.IV.controls.$endscreensChooser.hasClass('h5p-show')) {
this.IV.toggleEndscreensChooser(false, {keepStopped: true});
}
else if (this.IV.controls.$bookmarks) {
this.IV.controls.$bookmarks.click();
}
// Move other increament other ids.
this.IV.trigger('endscreensChanged', {'index': i, 'number': 1});
this.params.endscreens.splice(i, 0, {
time: time,
label: H5P.InteractiveVideo.humanizeTime(time) + ' ' + t('endscreen')
});
var $endscreen = this.IV.addEndscreen(i, tenth);
if (!freshOnly) {
$endscreen.addClass('h5p-show');
}
};
/**
* Check for blocked marker position.
*
* @param {number} tenth - Position to check in tenth.
* @return {boolean} True if position was free.
*/
InteractiveVideoEditor.prototype.checkMarkerSpace = function (tenth) {
if (this.IV.bookmarksMap[tenth] !== undefined) {
this.displayMessage(t('bookmarkAlreadyExists'));
return false;
}
if (this.IV.endscreensMap[tenth] !== undefined) {
this.displayMessage(t('endscreenAlreadyExists'));
return false;
}
return true;
};
/**
* Display a popup containing a message.
*
* @param {string} message
*/
InteractiveVideoEditor.prototype.displayMessage = function (message) {
var timeout;
var $warning = $('<div/>', {
'class': 'h5p-iv-message-popup',
text: message,
click: function () {
clearTimeout(timeout);
$warning.remove();
}
}).appendTo(this.$editor);
timeout = setTimeout(function () {
$warning.remove();
}, 3000);
};
/**
* Gets called whenever a bookmark is added to the UI.
*
* @param {H5P.Event} event
*/
InteractiveVideoEditor.prototype.bookmarkAdded = function (event) {
var self = this;
var $bookmark = event.data.bookmark;
$('<a class="h5p-remove-bookmark" href="#"></a>')
.appendTo($bookmark.find('.h5p-bookmark-label'))
.click(function () {
var id = $bookmark.data('id');
self.params.bookmarks.splice(id, 1);
self.IV.trigger('bookmarksChanged', {'index': id, 'number': -1});
$bookmark.remove();
self.IV.resumeVideo();
return false;
});
// Click to edit label.
$bookmark.find('.h5p-bookmark-text').click(function () {
if ($bookmark.hasClass('h5p-force-show')) {
return; // Double click
}
$bookmark.addClass('h5p-force-show');
var $text = $(this);
// This is a IE-fix. Without this, text is not shown when editing
$text.css({overflow: 'visible'});
var $input = $text.html('<input type="text" class="h5p-bookmark-input" style="width:' + ($text.width() - 19) + 'px" maxlength="255" value="' + $text.text() + '"/>')
.children()
.blur(function () {
var newText = $input.val();
if (H5P.trim(newText) === '') {
newText = t('newBookmark');
}
$text.text(newText);
$bookmark.removeClass('h5p-force-show').mouseover().mouseout();
$text.css({overflow: 'hidden'});
var id = $bookmark.data('id');
self.params.bookmarks[id].label = newText;
self.IV.controls.$bookmarksChooser.find('li:eq(' + id + ')').text(newText);
self.IV.resumeVideo();
})
.keydown(function (event) {
if (event.which === 13) {
$input.blur();
}
})
.focus();
if ($input.val() === t('newBookmark')) {
// Delete default value when editing
$input.val('');
}
});
};
/**
* Gets called whenever a endscreen is added to the UI.
*
* @param {H5P.Event} event
*/
InteractiveVideoEditor.prototype.endscreenAdded = function (event) {
var self = this;
var $endscreen = event.data.endscreen;
$('<a class="h5p-remove-endscreen" href="#"></a>')
.appendTo($endscreen.find('.h5p-endscreen-label'))
.click(function () {
var id = $endscreen.data('id');
self.params.endscreens.splice(id, 1);
self.IV.trigger('endscreensChanged', {'index': id, 'number': -1});
$endscreen.remove();
return false;
});
self.IV.resumeVideo();
};
/**
* Initialize the toolbar for creating interactivties.
*
* @param {Array} libraries
*/
InteractiveVideoEditor.prototype.createDragNBar = function (libraries) {
var that = this;
this.libraries = libraries;
this.dnb = new H5P.DragNBar(this.getButtons(libraries), this.IV.$videoWrapper, this.IV.$container, {libraries: libraries});
this.dnb.overflowThreshold = 15;
/**
* @private
* @param {string} lib uber name
* @returns {boolean}
*/
this.supported = function (lib) {
for (var i = 0; i < libraries.length; i++) {
if (libraries[i].restricted !== true && libraries[i].uberName === lib) {
return true; // Library is supported and allowed
}
}
return false;
};
this.dnb.on('paste', function (event) {
var pasted = event.data;
var options = {
width: pasted.width,
height: pasted.height,
pasted: true
};
if (pasted.from === InteractiveVideoEditor.clipboardKey) {
// Pasted content comes from the same version of IV
if (!pasted.generic) {
// Non generic part, must be a something not created yet
that.dnb.focus(that.addInteraction(pasted.specific, options));
}
else if (that.supported(pasted.generic.library)) {
// Has generic part and the generic libray is supported
that.dnb.focus(that.addInteraction(pasted.specific, options));
}
else {
that.showConfirmationDialog({
headerText: H5PEditor.t('core', 'pasteError'),
dialogText: H5PEditor.t('H5P.DragNBar', 'unableToPaste'),
cancelText: ' ',
confirmText: t('ok'),
});
}
}
else if (pasted.generic) {
if (that.supported(pasted.generic.library)) {
// Supported library from another content type
if (pasted.specific.displayAsButton) {
// Make sure buttons from CP still are buttons.
options.displayType = 'button';
}
options.action = pasted.generic;
that.dnb.focus(that.addInteraction(pasted.generic.library, options));
}
else {
that.showConfirmationDialog({
headerText: H5PEditor.t('core', 'pasteError'),
dialogText: H5PEditor.t('H5P.DragNBar', 'unableToPaste'),
cancelText: ' ',
confirmText: t('ok'),
});
}
}
});
that.dnb.dnr.on('stoppedResizing', function (event) {
// Set size in em
that.interaction.setSize(event.data.width, event.data.height);
if (event.data.left !== undefined && event.data.top !== undefined) {
// Set pos in %
var containerStyle = window.getComputedStyle(that.dnb.$container[0]);
that.interaction.setPosition(event.data.left / (parseFloat(containerStyle.width) / 100), event.data.top / (parseFloat(containerStyle.height) / 100));
}
});
// Make sure that dialog can't be closed without validation
that.dnb.dialog.on('open', function () {
that.dnb.dialog.disableOverlay = true;
});
this.dnb.dnd.startMovingCallback = function () {
that.dnb.dnd.min = {x: 0, y: 0};
that.dnb.dnd.max = {
x: that.dnb.$container.width() - that.dnb.$element.outerWidth(),
y: that.dnb.$container.height() - that.dnb.$element.outerHeight()
};
if (that.dnb.newElement) {
that.dnb.dnd.adjust.x = 10;
that.dnb.dnd.adjust.y = 10;
that.dnb.dnd.min.y -= that.dnb.$list.height();
}
return true;
};
// Update params when the element is dropped.
this.dnb.stopMovingCallback = function (x, y) {
that.interaction.positionLabel(that.IV.$videoWrapper.width());
that.interaction.setPosition(x, y);
};
this.dnb.dnd.releaseCallback = function () {
// Edit element when it is dropped.
if (that.dnb.newElement) {
that.dnb.dnd.$element.dblclick();
that.dnb.blurAll();
}
};
that.IV.interactions.forEach(function (interaction) {
// Add drag functionality if interaction has element
if (interaction.getElement()) {
var libraryName = interaction.getLibraryName();
var options = {
cornerLock: (libraryName === 'H5P.Image'),
disableResize: (libraryName === 'H5P.Link') || interaction.isButton()
};
that.addInteractionToDnb(interaction, interaction.getElement(), options);
}
});
if (that.IV.scaledFontSize) {
// Set the container em since the resizing is useless without it
that.dnb.dnr.setContainerEm(that.IV.scaledFontSize);
}
this.dnb.attach(this.$bar);
// Set paste button
this.dnb.setCanPaste(this.canPaste(H5P.getClipboard()));
};
/**
* Create form for interaction.
*
* @param {H5P.InteractiveVideoInteraction} interaction
* @param {Object} parameters
*/
InteractiveVideoEditor.prototype.createInteractionForm = function (interaction, parameters) {
var self = this;
var $semanticFields = $('<div>', {
'class': 'h5p-dialog-inner-semantics'
});
// Create form
interaction.$form = $semanticFields;
var interactions = findField('interactions', this.field.fields);
// Clone semantics to avoid changing them for all interactions
var interactionFields = H5PEditor.$.extend(true, [], interactions.field.fields);
// Hide some fields for some interaction types
var type = interaction.getLibraryName();
if (InteractiveVideoEditor.XAPI_QUESTION_TYPES.indexOf(type) === -1) {
hideFields(interactionFields, ['adaptivity']);
}
if (type === 'H5P.Nil') {
hideFields(interactionFields, ['displayType']);
}
if (type !== 'H5P.Text' && type !== 'H5P.Image') {
hideFields(interactionFields, ['goto']);
}
if (['H5P.Text', 'H5P.Image', 'H5P.Link', 'H5P.Table'].indexOf(type) === -1) {
hideFields(interactionFields, ['visuals']);
}
if (type === 'H5P.Summary') {
var adaptivityFields = findField('adaptivity', interactionFields);
hideFields(adaptivityFields.fields, ['requireCompletion']);
}
if (parameters.visuals === undefined) {
// Make Image background transparent by default
if (type === 'H5P.Image') {
parameters.visuals = {
backgroundColor: 'rgba(0,0,0,0)',
boxShadow: true
};
}
// Set default link visuals
else if (type === 'H5P.Link') {
parameters.visuals = {
backgroundColor: 'rgba(0,0,0,0.5)',
boxShadow: true
};
}
}
// Always show link as poster
if (type === 'H5P.Link' || type === 'H5P.GoToQuestion' || type === 'H5P.IVHotspot') {
var field = findField('displayType', interactionFields);
// Must set default to false and hide
field.default = 'poster';
field.widget = 'none';
// Hide label field
var labelField = findField('label', interactionFields);
labelField.widget = 'none';
if (type === 'H5P.GoToQuestion' && parameters.pause === undefined) {
parameters.pause = true;
}
}
// Set default displayType of images to poster
if (type === 'H5P.Image') {
const field = findField('displayType', interactionFields);
field.default = 'poster';
}
H5PEditor.processSemanticsChunk(interactionFields, parameters, $semanticFields, self);
// Remove library selector and copy button and paste button
var pos = interactionFields.map(function (field) {
return field.type;
}).indexOf('library');
if (pos > -1) {
self.children[pos].hide();
}
self.setLibraryName(interaction.$form, type);
};
/**
* Process interaction.
*
* @param {H5P.InteractiveVideoInteraction} interaction
* @param {Object} parameters
*/
InteractiveVideoEditor.prototype.processInteraction = function (interaction, parameters) {
var self = this;
var type = interaction.getLibraryName();
this.createInteractionForm(interaction, parameters);
// Keep track of form elements
interaction.children = this.children;
this.children = undefined;
// Interaction fields object generated from interaction children
var interactionFields = self.getInteractionFields(interaction);
// Add classes to form elements if they exist
if (interactionFields.duration.$item) {
interactionFields.duration.$item.addClass('h5peditor-interaction-duration');
}
if (interactionFields.pause.$item) {
interactionFields.pause.$item.addClass('h5peditor-interaction-pause');
}
if (interactionFields.label.$item) {
interactionFields.label.$item.addClass('h5peditor-interaction-label');
// Remove label when displayType is poster
var $displayTypeRadios = $('.h5p-image-radio-button-group input:radio', interaction.$form);
var $labelWrapper = interactionFields.label.$item;
$displayTypeRadios.change(function () {
$labelWrapper.toggleClass('hide', !interaction.isButton());
if (!interaction.isButton() && interactionFields.pause.$item) {
interactionFields.pause.$input[0].checked = true;
interactionFields.pause.$input.trigger('change');
}
});
$labelWrapper.toggleClass('hide', !interaction.isButton());
}
if (interactionFields.buttonOnMobile.$item) {
var $buttonOnMobile = interactionFields.buttonOnMobile.$item;
if (type == 'H5P.Image') {
$displayTypeRadios.change(function () {
$buttonOnMobile.toggleClass('hide', interaction.isButton());
});
$buttonOnMobile.addClass((interaction.isButton() ? 'hide' : ''));
}
else {
$buttonOnMobile.remove();
}
}
if (interactionFields.visuals.$group) {
$('.h5p-image-radio-button-group input:radio', interaction.$form).change(function () {
interactionFields.visuals.$group.toggleClass('hide', $(this).val() !== 'poster');
});
interactionFields.visuals.$group.toggleClass('hide', parameters.displayType !== 'poster');
}
// Create require completion instances for content types with scores.
// Summary is filtered out because it can't be retried.
var eligibleForRequireCompletion = InteractiveVideoEditor.XAPI_QUESTION_TYPES
.filter(function (questionType) {
return questionType !== 'H5P.Summary';
}).indexOf(interaction.getLibraryName()) >= 0;
if (eligibleForRequireCompletion) {
new H5PEditor.InteractiveVideo.RequireCompletion(self, interaction);
}
interaction.on('display', function (event) {
var $interaction = event.data;
// Customize rendering of interaction
self.newInteraction(interaction, $interaction);
});
// Find library field instance
var libraryFieldInstance;
for (var i = 0; i < interaction.children.length; i++) {
if (interaction.children[i] instanceof H5PEditor.Library) {
libraryFieldInstance = interaction.children[i];
}
}
if (libraryFieldInstance) {
/**
* Callback for when library changes.
*
* @private
*/
var libraryChange = function () {
var lib = libraryFieldInstance.currentLibrary.split(' ')[0];
if (lib !== 'H5P.Image') {
return;
}
/**
* Callback for when image changes.
*
* @private
* @param {Object} newParams
*/
var imageChange = function (newParams) {
if (newParams !== undefined && newParams.width !== undefined && newParams.height !== undefined) {
self.setImageSize(parameters, newParams);
}
};
// Add callback to the correct field
libraryFieldInstance.forEachChild(function (child) {
if (child.field.name === 'file') {
child.changes.push(imageChange);
return true;
}
});
};
// Add callback