-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathh5peditor-init.js
126 lines (104 loc) · 3.83 KB
/
h5peditor-init.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
(function ($, ns) {
H5PEditor.init = function ($form, $type, $upload, $create, $editor, $library, $params, $maxScore, $title, cancelSubmitCallback) {
H5PEditor.$ = H5P.jQuery;
H5PEditor.basePath = H5PIntegration.editor.libraryUrl;
H5PEditor.fileIcon = H5PIntegration.editor.fileIcon ?? {
path: `${H5PEditor.basePath}/images/binary-file.png`,
width: 50,
height:50
};
H5PEditor.ajaxPath = H5PIntegration.editor.ajaxPath;
H5PEditor.filesPath = H5PIntegration.editor.filesPath;
H5PEditor.apiVersion = H5PIntegration.editor.apiVersion;
H5PEditor.contentLanguage = H5PIntegration.editor.language;
// Semantics describing what copyright information can be stored for media.
H5PEditor.copyrightSemantics = H5PIntegration.editor.copyrightSemantics;
H5PEditor.metadataSemantics = H5PIntegration.editor.metadataSemantics;
// Required styles and scripts for the editor
H5PEditor.assets = H5PIntegration.editor.assets;
// Required for assets
H5PEditor.baseUrl = '';
H5PEditor.enableContentHub = H5PIntegration.editor.enableContentHub;
if (H5PIntegration.editor.nodeVersionId !== undefined) {
H5PEditor.contentId = H5PIntegration.editor.nodeVersionId;
}
if (H5PIntegration.editor.hub !== undefined) {
H5PIntegration.Hub = {
contentSearchUrl: H5PIntegration.editor.hub.contentSearchUrl
};
}
var h5peditor;
$create.hide();
var library = $library.val();
$type.change(function () {
if ($type.filter(':checked').val() === 'upload') {
$create.hide();
$upload.show();
}
else {
$upload.hide();
if (h5peditor === undefined) {
h5peditor = new ns.Editor(library, $params.val(), $editor[0]);
}
$create.show();
}
});
if ($type.filter(':checked').val() === 'upload') {
$type.change();
}
else {
$type.filter('input[value="create"]').attr('checked', true).change();
}
// Duplicate the submit button input because it is not posted when calling $form.submit()
const $submitters = $form.find('input[type="submit"]');
let isCanceling = false;
$submitters.click(function () {
// Create hidden input and give it the value
const name = $(this).prop('name');
const value = $(this).prop('value');
$('<input type="hidden" name="' + name + '" value="' + value + '" />').appendTo($form);
// Allow caller to cancel validation and submission of form on button click
if (cancelSubmitCallback) {
isCanceling = cancelSubmitCallback($(this));
}
});
let formIsUpdated = false;
$form.submit(function (event) {
if ($type.length && $type.filter(':checked').val() === 'upload') {
return; // Old file upload
}
if (isCanceling) {
return;
}
if (h5peditor !== undefined && !formIsUpdated) {
// Get content from editor
h5peditor.getContent(function (content) {
// Set the title field to the metadata title if the field exists
$title.val(content.title);
// Set main library
$library.val(content.library);
// Set params
$params.val(content.params);
// Submit form data
formIsUpdated = true;
$form.submit();
});
// Stop default submit
event.preventDefault();
}
});
};
H5PEditor.getAjaxUrl = function (action, parameters) {
var url = H5PIntegration.editor.ajaxPath + action;
if (parameters !== undefined) {
var separator = url.indexOf('?') === -1 ? '?' : '&';
for (var property in parameters) {
if (parameters.hasOwnProperty(property)) {
url += separator + property + '=' + parameters[property];
separator = '&';
}
}
}
return url;
};
})(H5P.jQuery, H5PEditor);