-
Notifications
You must be signed in to change notification settings - Fork 3
/
upgrades.js
55 lines (50 loc) · 1.77 KB
/
upgrades.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
/** @namespace H5PUpgrades */
var H5PUpgrades = H5PUpgrades || {};
H5PUpgrades['H5P.Portfolio'] = (function () {
return {
0: {
/**
* Asynchronous content upgrade hook.
* Fix potentially duplicated subContentIds
* @param {object} parameters Parameters.
* @param {function} finished Callback.
* @param {object} extras Extra parameters.
*/
8: function (parameters, finished, extras) {
/**
* Replace all subcontent ids in H5P parameters object.
* @param {object} params Parameters.
* @returns {object} Parameters with fresh subcontent ids.
*/
const replaceSubContentIDs = function (params) {
if (Array.isArray(params)) {
params = params.map((param) => {
return replaceSubContentIDs(param);
});
}
else if (typeof params === 'object' && params !== null) {
if (params.library && params.subContentId) {
/*
* NOTE: We avoid using H5P.createUUID since this is an upgrade
* script and H5P function may change in the future
*/
params.subContentId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
const random = Math.random() * 16 | 0, newChar = char === 'x' ? random : (random & 0x3 | 0x8);
return newChar.toString(16);
});
}
for (let param in params) {
param = replaceSubContentIDs(params[param]);
}
}
return params;
};
if (parameters) {
parameters = replaceSubContentIDs(parameters);
}
// Done
finished(null, parameters, extras);
}
}
};
})();