1 |
efrain |
1 |
/* global ns */
|
|
|
2 |
/**
|
|
|
3 |
* @class
|
|
|
4 |
* @alias H5PEditor.SelectorHub
|
|
|
5 |
*/
|
|
|
6 |
ns.SelectorHub = function (libraries, selectedLibrary, changeLibraryDialog) {
|
|
|
7 |
var self = this;
|
|
|
8 |
|
|
|
9 |
H5P.EventDispatcher.call(this);
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Looks up content type object
|
|
|
13 |
*
|
|
|
14 |
* @param {string} machineName
|
|
|
15 |
* @return {object}
|
|
|
16 |
*/
|
|
|
17 |
this.getContentType = function (machineName) {
|
|
|
18 |
for (var i = 0; i < libraries.libraries.length; i++) {
|
|
|
19 |
var contentType = libraries.libraries[i];
|
|
|
20 |
|
|
|
21 |
if (contentType.machineName === machineName) {
|
|
|
22 |
return contentType;
|
|
|
23 |
}
|
|
|
24 |
}
|
|
|
25 |
};
|
|
|
26 |
|
|
|
27 |
var state = {
|
|
|
28 |
contentId: H5PEditor.contentId || 0,
|
|
|
29 |
contentTypes: libraries,
|
|
|
30 |
getAjaxUrl: H5PEditor.getAjaxUrl,
|
|
|
31 |
expanded: true,
|
|
|
32 |
canPaste: false,
|
|
|
33 |
enableContentHub: H5PEditor.enableContentHub || false,
|
|
|
34 |
};
|
|
|
35 |
|
|
|
36 |
if (selectedLibrary) {
|
|
|
37 |
var contentType = this.getContentType(selectedLibrary.split(' ')[0]);
|
|
|
38 |
state.title = contentType ? contentType.title || contentType.machineName : selectedLibrary.split(' ')[0];
|
|
|
39 |
state.expanded = false;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// Initialize hub client
|
|
|
43 |
this.client = new H5P.HubClient(state, H5PEditor.language.core);
|
|
|
44 |
|
|
|
45 |
// Default to nothing selected and empty params
|
|
|
46 |
this.currentLibrary = selectedLibrary;
|
|
|
47 |
|
|
|
48 |
// Listen for content type selection
|
|
|
49 |
this.client.on('select', function (event) {
|
|
|
50 |
var contentType = event;
|
|
|
51 |
|
|
|
52 |
// Already selected library
|
|
|
53 |
if (contentType.machineName === self.currentLibrary.split(' ')[0]) {
|
|
|
54 |
return;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if (!self.currentLibrary) {
|
|
|
58 |
self.currentLibrary = self.createContentTypeId(contentType, true);
|
|
|
59 |
self.trigger('selected');
|
|
|
60 |
return;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
self.currentLibrary = self.createContentTypeId(contentType, true);
|
|
|
64 |
delete self.currentParams;
|
|
|
65 |
delete self.currentMetadata;
|
|
|
66 |
changeLibraryDialog.show(ns.$(self.getElement()).offset().top);
|
|
|
67 |
}, this);
|
|
|
68 |
|
|
|
69 |
// Listen for uploads
|
|
|
70 |
this.client.on('upload', function (event) {
|
|
|
71 |
libraries = event.contentTypes;
|
|
|
72 |
var previousLibrary = self.currentLibrary;
|
|
|
73 |
|
|
|
74 |
// Use version from event data
|
|
|
75 |
const uploadedVersion = event.h5p.preloadedDependencies
|
|
|
76 |
.filter(function (dependency) {
|
|
|
77 |
return dependency.machineName === event.h5p.mainLibrary;
|
|
|
78 |
})[0];
|
|
|
79 |
self.currentLibrary = self.createContentTypeId(uploadedVersion);
|
|
|
80 |
self.currentParams = event.content;
|
|
|
81 |
self.currentMetadata = {
|
|
|
82 |
title: event.h5p.title,
|
|
|
83 |
authors: event.h5p.authors,
|
|
|
84 |
license: event.h5p.license,
|
|
|
85 |
licenseVersion: event.h5p.licenseVersion,
|
|
|
86 |
licenseExtras: event.h5p.licenseExtras,
|
|
|
87 |
yearFrom: event.h5p.yearFrom,
|
|
|
88 |
yearTo: event.h5p.yearTo,
|
|
|
89 |
source: event.h5p.source,
|
|
|
90 |
changes: event.h5p.changes,
|
|
|
91 |
authorComments: event.h5p.authorComments,
|
|
|
92 |
defaultLanguage: event.h5p.defaultLanguage
|
|
|
93 |
};
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Change library immediately or show confirmation dialog
|
|
|
97 |
* @private
|
|
|
98 |
*/
|
|
|
99 |
const selectLibrary = function () {
|
|
|
100 |
if (!previousLibrary) {
|
|
|
101 |
self.trigger('selected');
|
|
|
102 |
}
|
|
|
103 |
else {
|
|
|
104 |
changeLibraryDialog.show(ns.$(self.getElement()).offset().top);
|
|
|
105 |
}
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Use backend to filter parameter values according to semantics
|
|
|
110 |
*
|
|
|
111 |
* @private
|
|
|
112 |
* @param {Object} library
|
|
|
113 |
*/
|
|
|
114 |
const filterParameters = function (library) {
|
|
|
115 |
const libraryString = ns.ContentType.getNameVersionString(library);
|
|
|
116 |
|
|
|
117 |
var formData = new FormData();
|
|
|
118 |
formData.append('libraryParameters', JSON.stringify({
|
|
|
119 |
library: libraryString,
|
|
|
120 |
params: self.currentParams,
|
|
|
121 |
metadata: self.currentMetadata
|
|
|
122 |
}));
|
|
|
123 |
var request = new XMLHttpRequest();
|
|
|
124 |
request.onload = function () {
|
|
|
125 |
try {
|
|
|
126 |
result = JSON.parse(request.responseText);
|
|
|
127 |
self.currentLibrary = result.data.library;
|
|
|
128 |
self.currentParams = result.data.params;
|
|
|
129 |
self.currentMetadata = result.data.metadata;
|
|
|
130 |
selectLibrary();
|
|
|
131 |
}
|
|
|
132 |
catch (err) {
|
|
|
133 |
H5P.error(err);
|
|
|
134 |
}
|
|
|
135 |
};
|
|
|
136 |
request.open('POST', H5PEditor.getAjaxUrl('filter'), true);
|
|
|
137 |
request.send(formData);
|
|
|
138 |
};
|
|
|
139 |
|
|
|
140 |
// Check if we have any newer versions
|
|
|
141 |
const upgradeLibrary = ns.ContentType.getPossibleUpgrade(uploadedVersion, libraries.libraries);
|
|
|
142 |
if (upgradeLibrary) {
|
|
|
143 |
// We need to run content upgrade before showing the editor
|
|
|
144 |
ns.upgradeContent(uploadedVersion, upgradeLibrary, {params: self.currentParams, metadata: self.currentMetadata}, function (err, result) {
|
|
|
145 |
if (err) {
|
|
|
146 |
// Reset the Hub
|
|
|
147 |
var contentType = self.getContentType(self.currentLibrary.split(' ')[0]);
|
|
|
148 |
self.client.setPanelTitle(contentType.title || contentType.machineName, true);
|
|
|
149 |
}
|
|
|
150 |
else {
|
|
|
151 |
const content = JSON.parse(result);
|
|
|
152 |
self.currentParams = content.params;
|
|
|
153 |
self.currentMetadata = content.metadata;
|
|
|
154 |
self.currentLibrary = self.createContentTypeId(upgradeLibrary, true);
|
|
|
155 |
filterParameters(upgradeLibrary);
|
|
|
156 |
}
|
|
|
157 |
})
|
|
|
158 |
}
|
|
|
159 |
else {
|
|
|
160 |
filterParameters(uploadedVersion);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
}, this);
|
|
|
164 |
|
|
|
165 |
this.client.on('update', function (event) {
|
|
|
166 |
// Handle update to the content type cache
|
|
|
167 |
libraries = event;
|
|
|
168 |
});
|
|
|
169 |
|
|
|
170 |
this.client.on('resize', function () {
|
|
|
171 |
self.trigger('resize');
|
|
|
172 |
});
|
|
|
173 |
|
|
|
174 |
this.client.on('paste', function () {
|
|
|
175 |
self.trigger('paste');
|
|
|
176 |
});
|
|
|
177 |
};
|
|
|
178 |
|
|
|
179 |
// Extends the event dispatcher
|
|
|
180 |
ns.SelectorHub.prototype = Object.create(H5P.EventDispatcher.prototype);
|
|
|
181 |
ns.SelectorHub.prototype.constructor = ns.SelectorHub;
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Reset current library to the provided library.
|
|
|
185 |
*
|
|
|
186 |
* @param {string} library Full library name
|
|
|
187 |
* @param {Object} params Library parameters
|
|
|
188 |
* @param {Object} metadata Library metadata
|
|
|
189 |
* @param {boolean} expanded Selector open
|
|
|
190 |
*/
|
|
|
191 |
ns.SelectorHub.prototype.resetSelection = function (library, params, metadata, expanded) {
|
|
|
192 |
this.currentLibrary = library;
|
|
|
193 |
this.currentParams = params;
|
|
|
194 |
this.currentMetadata = metadata;
|
|
|
195 |
|
|
|
196 |
var contentType = this.getContentType(library.split(' ')[0]);
|
|
|
197 |
this.client.setPanelTitle(contentType.title || contentType.machineName, expanded);
|
|
|
198 |
};
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* Reset current library to the provided library.
|
|
|
202 |
*
|
|
|
203 |
* @param {boolean} canPaste
|
|
|
204 |
* @param {string} [title]
|
|
|
205 |
*/
|
|
|
206 |
ns.SelectorHub.prototype.setCanPaste = function (canPaste, title) {
|
|
|
207 |
this.client.setCanPaste(canPaste, title);
|
|
|
208 |
};
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Get currently selected library
|
|
|
212 |
*
|
|
|
213 |
* @param {function} next Callback
|
|
|
214 |
*/
|
|
|
215 |
ns.SelectorHub.prototype.getSelectedLibrary = function (next) {
|
|
|
216 |
var selected = {
|
|
|
217 |
uberName: this.currentLibrary
|
|
|
218 |
};
|
|
|
219 |
|
|
|
220 |
var contentType = this.getContentType(this.currentLibrary.split(' ')[0]);
|
|
|
221 |
if (contentType) {
|
|
|
222 |
selected.tutorialUrl = contentType.tutorial;
|
|
|
223 |
selected.exampleUrl = contentType.example;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
return next(selected);
|
|
|
227 |
};
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Get params connected with the currently selected library
|
|
|
231 |
*
|
|
|
232 |
* @returns {object} Parameters connected to the selected library
|
|
|
233 |
*/
|
|
|
234 |
ns.SelectorHub.prototype.getParams = function () {
|
|
|
235 |
return this.currentParams;
|
|
|
236 |
};
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Get metadata connected with the currently selected library
|
|
|
240 |
*
|
|
|
241 |
* @returns {object} Metadata connected to the selected library
|
|
|
242 |
*/
|
|
|
243 |
ns.SelectorHub.prototype.getMetadata = function () {
|
|
|
244 |
return this.currentMetadata;
|
|
|
245 |
};
|
|
|
246 |
|
|
|
247 |
/**
|
|
|
248 |
* Returns the html element for the hub
|
|
|
249 |
*
|
|
|
250 |
* @public
|
|
|
251 |
* @return {HTMLElement}
|
|
|
252 |
*/
|
|
|
253 |
ns.SelectorHub.prototype.getElement = function () {
|
|
|
254 |
return this.client.getElement();
|
|
|
255 |
};
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Takes a content type, and extracts the full id (ubername)
|
|
|
259 |
*
|
|
|
260 |
* @param {ContentType} contentType
|
|
|
261 |
* @param {boolean} [useLocalVersion] Decides if we should use local version or cached version
|
|
|
262 |
*
|
|
|
263 |
* @private
|
|
|
264 |
* @return {string}
|
|
|
265 |
*/
|
|
|
266 |
ns.SelectorHub.prototype.createContentTypeId = function (contentType, useLocalVersion) {
|
|
|
267 |
var id = contentType.machineName;
|
|
|
268 |
if (useLocalVersion) {
|
|
|
269 |
id += ' ' + contentType.localMajorVersion + '.' + contentType.localMinorVersion;
|
|
|
270 |
}
|
|
|
271 |
else {
|
|
|
272 |
id += ' ' + contentType.majorVersion + '.' + contentType.minorVersion;
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return id;
|
|
|
276 |
};
|