forked from h5p/h5p-course-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrades.js
339 lines (297 loc) · 11.7 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
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
var H5PUpgrades = H5PUpgrades || {};
H5PUpgrades['H5P.CoursePresentation'] = (function () {
return {
1: {
2: function (parameters, finished) {
// Allows overriding of buttons for subcontent.
parameters.override = {
overrideButtons: true,
overrideShowSolutionButton: parameters.showSolutions === undefined ? true : parameters.showSolutions,
overrideRetry: true
};
delete parameters.showSolutions;
var i, j, slide;
// Determine if keywords has been removed
var keywordsRemoved = true;
for (i = 0; i < parameters.slides.length; i++) {
slide = parameters.slides[i];
if (keywordsRemoved && slide.keywords !== undefined) {
keywordsRemoved = false;
break;
}
}
if (!keywordsRemoved) {
// Move and resize elements
for (i = 0; i < parameters.slides.length; i++) {
slide = parameters.slides[i];
if (slide.elements !== undefined) {
for (j = 0; j < slide.elements.length; j++) {
var element = slide.elements[j];
element.x += 31.25;
element.width *= 0.6875;
}
}
}
}
// Move slides inside presentation wrapper.
parameters.presentation = {
slides: parameters.slides,
keywordListEnabled: !keywordsRemoved,
keywordListAlwaysShow: true,
keywordListAutoHide: false,
keywordListOpacity: 90
};
delete parameters.slides;
finished(null, parameters);
},
3: function (parameters, finished) {
delete parameters.l10n.goHome;
delete parameters.l10n.scrollLeft;
delete parameters.l10n.jumpToSlide;
delete parameters.l10n.scrollRight;
delete parameters.l10n.goToSlide;
parameters.l10n.fullscreen = 'fullscreen';
parameters.l10n.exitFullscreen = 'Exit fullscreen';
parameters.l10n.prevSlide = 'Previous slide';
parameters.l10n.nextSlide = 'Next slide';
parameters.l10n.currentSlide = 'Current slide';
parameters.l10n.lastSlide = 'Last slide';
parameters.l10n.solutionModeTitle = 'Exit solution mode';
parameters.l10n.solutionModeText = 'Solution Mode:';
parameters.l10n.solutionModeUnderlined = 'Close';
parameters.l10n.summaryMultipleTaskText = 'Text when multiple tasks on a page';
parameters.l10n.scoreMessage = 'You achieved:';
parameters.l10n.shareFacebook = 'Share on Facebook';
parameters.l10n.shareTwitter = 'Share on Twitter';
finished(null, parameters);
},
4: function (parameters, finished) {
var slides = parameters.presentation.slides;
for (var i = 0; i < slides.length; i++) {
if (slides[i].elements !== undefined) {
for (var j = 0; j < slides[i].elements.length; j++) {
if (slides[i].elements[j].action && slides[i].elements[j].action.subContentId === undefined) {
// NOTE: We avoid using H5P.createUUID since this is an upgrade script and H5P function may change in the
// future
slides[i].elements[j].action.subContentId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
var random = Math.random()*16|0, newChar = char === 'x' ? random : (random&0x3|0x8);
return newChar.toString(16);
});
}
}
}
}
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support CP 1.5.
*
* Converts H5P.Text elements into H5P.AdvancedText. This is to support
* more styling options for text.
*
* @params {Object} parameters
* @params {function} finished
*/
5: function (parameters, finished) {
var slides = parameters.presentation.slides;
// Go through slides and elements
for (var i = 0; i < slides.length; i++) {
if (slides[i].elements !== undefined) {
for (var j = 0; j < slides[i].elements.length; j++) {
var element = slides[i].elements[j];
// Check if element type is text
if (element.action && element.action.library &&
element.action.library.split(' ')[0] === 'H5P.Text') {
element.action.library = 'H5P.AdvancedText 1.0';
}
}
}
}
// Done
finished(null, parameters);
},
/**
* Increases width of H5P.AdvancedText to make old content keep proper dimensions.
*
* @param parameters
* @param finished
*/
6: function (parameters, finished) {
var slides = parameters.presentation.slides;
// Go through slides and elements
for (var i = 0; i < slides.length; i++) {
if (slides[i].elements !== undefined) {
for (var j = 0; j < slides[i].elements.length; j++) {
var element = slides[i].elements[j];
// Check if element type is text
if (element.action && element.action.library &&
(element.action.library.split(' ')[0] === 'H5P.AdvancedText' ||
element.action.library.split(' ')[0] === 'H5P.ContinuousText'
)) {
element.width += 1.7;
element.height += 3;
}
}
}
}
// Done
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support CP 1.10.
*
* Converts overriding checkboxes into select fields with three
* options each.
*
* @params {Object} parameters
* @params {function} finished
*/
10: function (parameters, finished) {
if (parameters.override) {
if (parameters.override.overrideButtons) {
// Set new variables
parameters.override.showSolutionButton =
(parameters.override.overrideShowSolutionButton ? 'on' : 'off');
parameters.override.retryButton =
(parameters.override.overrideRetry ? 'on' : 'off');
}
// Remove old field variables
delete parameters.override.overrideButtons;
delete parameters.override.overrideShowSolutionButton;
delete parameters.override.overrideRetry;
}
// Done
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support CP 1.11.
*
* Set overriding checkbox `enablePrintButton` to true for
* old versions of CP.
*
* @param {Object} parameters
* @param {function} finished
*/
11: function (parameters, finished) {
if (parameters.override) {
parameters.override.enablePrintButton = true;
}
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support CP 1.12.
*
* Fixes color selector widget upgrade
*
* @param {Object} parameters
* @param {function} finished
*/
12: function (parameters, finished) {
if (parameters.presentation) {
if (parameters.presentation.globalBackgroundSelector &&
parameters.presentation.globalBackgroundSelector.fillGlobalBackground) {
parameters.presentation.globalBackgroundSelector.fillGlobalBackground =
'#' + parameters.presentation.globalBackgroundSelector.fillGlobalBackground;
}
if (parameters.presentation.slides) {
parameters.presentation.slides.forEach(function (slide) {
if (slide.slideBackgroundSelector &&
slide.slideBackgroundSelector.fillSlideBackground) {
slide.slideBackgroundSelector.fillSlideBackground =
'#' + slide.slideBackgroundSelector.fillSlideBackground;
}
});
}
}
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support CP 1.17.
*
* - Adds small as default for button size
* - Converts H5P.AppearIn to H5P.AdvancedText
*
* @param {Object} parameters
* @param {function} finished
*/
17: function (parameters, finished) {
// Adds small as default for button size
if (parameters.presentation) {
if (parameters.presentation.slides) {
parameters.presentation.slides.forEach(function (slide) {
if (slide.elements) {
slide.elements.forEach(function (element) {
if (element.displayAsButton) {
element.buttonSize = 'small';
}
});
}
});
}
}
// Converts H5P.AppearIn to H5P.AdvancedText
if (parameters && parameters.presentation && parameters.presentation.slides) {
var slides = parameters.presentation.slides;
// Go through slides and elements
for (var i = 0; i < slides.length; i++) {
if (slides[i].elements !== undefined) {
for (var j = 0; j < slides[i].elements.length; j++) {
var element = slides[i].elements[j];
// Check if element type is text
if (element.action && element.action.library &&
element.action.library.split(' ')[0] === 'H5P.AppearIn') {
element.action.library = 'H5P.AdvancedText 1.1';
var roomName = '';
element.action.params = element.action.params || {};
if (element.action.params.appearRoom) {
roomName = element.action.params.appearRoom;
}
var userMessage = '<p>AppearIn support for embedded rooms has been deprecated and is no longer maintained. Access your room in a new tab with the following <a target="_blank" href="https://appear.in/' + roomName + '">link.</a></p>';
element.action.params.text = userMessage;
}
}
}
}
}
// Done
finished(null, parameters);
},
/**
* Asynchronous content upgrade hook.
* Upgrades content parameters to support CP 1.21.
*
* Set bgcolor hard to old default bgcolor if not set and no global background set
*
* @param {Object} parameters
* @param {function} finished
*/
21: function (parameters, finished) {
if (parameters && parameters.presentation) {
const presentation = parameters.presentation;
// Check for global background
const globalBackground = presentation && presentation.globalBackgroundSelector &&
(presentation.globalBackgroundSelector.fillGlobalBackground || presentation.globalBackgroundSelector.imageGlobalBackground);
if (globalBackground === undefined && presentation.slides) {
const slides = presentation.slides;
slides.forEach(function (slide) {
if (slide.slideBackgroundSelector) {
// Old CPs should keep the previous default bgcolor
const bg = slide.slideBackgroundSelector;
if (!bg.fillSlideBackground && !bg.imageSlideBackground) {
bg.fillSlideBackground = '#e8e6e7';
}
}
});
}
}
// Done
finished(null, parameters);
}
}
};
})();