-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathguess-the-answer.js
153 lines (137 loc) · 4.33 KB
/
guess-the-answer.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
var H5P = H5P || {};
/**
* Guess the answer module
*/
H5P.GuessTheAnswer = (function () {
/**
* Triggers 'resize' event on an instance. Stops infinite loops
* by not re-triggering the event, when it comes from a sibling
*
* @param {object} siblingInstance
* @return {Function}
*/
function triggerResize(siblingInstance) {
return function (event) {
var fromSibling = event.data && (event.data.fromSibling === true);
if (!fromSibling) {
siblingInstance.trigger('resize', { fromSibling: true });
}
};
}
/**
* Create the media element
*
* @param {object} params
* @param {number} contentId
* @param {object} instance
* @return {Element}
*/
function createMediaElement(params, contentId, instance) {
var element = document.createElement('div');
var mediaInstance = H5P.newRunnable(params, contentId, H5P.jQuery(element), true);
// Resize this instance, on video resize, and vise versa
instance.on('resize', triggerResize(mediaInstance));
mediaInstance.on('resize', triggerResize(instance));
return element;
}
/**
* Initializes the image
*
* @param {Element} imageElement
* @param {object} instance
*/
function initImage(imageElement, instance) {
// if has image, resize on load
if (imageElement) {
imageElement.style.width = null;
imageElement.style.height = null;
imageElement.addEventListener('load', function () {
instance.trigger('resize');
}, false);
}
}
/**
* Simple recusive function the helps set default values without
* destroying object references.
*
* Note: Can be removed if 'babel-plugin-transform-object-assign' is added
*
* @param {object} params values
* @param {object} values default values
*/
var setDefaults = function (params, values) {
for (var prop in values) {
if (values.hasOwnProperty(prop)) {
if (params[prop] === undefined) {
params[prop] = values[prop];
}
else if (params[prop] instanceof Object && !(params[prop] instanceof Array)) {
setDefaults(params[prop], values[prop]);
}
}
}
};
/**
* Initialize module.
*
* @class
* @alias H5P.GuessTheAnswer
* @param {object} params
* @param {number} contentId
*/
function C(params, contentId) {
const self = this;
// Set default behavior.
setDefaults(params, {
taskDescription: '',
solutionLabel: 'Click to see the answer.',
solutionText: ''
});
// get element references
var rootElement = this.rootElement = this.createRootElement(params);
var mediaElement = rootElement.querySelector('.media');
var buttonElement = rootElement.querySelector('.show-solution-button');
var solutionElement = rootElement.querySelector('.solution-text');
// add media
if (params.media && Object.keys(params.media?.params).length > 0) {
var el = createMediaElement(params.media, contentId, this);
initImage(el.querySelector('img'), this);
mediaElement.appendChild(el);
}
// add show solution text on button click
buttonElement.addEventListener('click', function() {
buttonElement.classList.add('hidden');
solutionElement.classList.remove('hidden');
setTimeout(() => {
solutionElement.innerHTML = params.solutionText;
self.trigger('resize');
}, 0)
});
}
/**
* Creates the root element with the markup for the content type
*
* @param {object} params
* @return {Element}
*/
C.prototype.createRootElement = function (params) {
var element = document.createElement('div');
element.classList.add('h5p-guess-answer');
element.innerHTML = '<div class="h5p-guess-answer-title">' + params.taskDescription +'</div>' +
'<div class="media"></div>' +
'<button class="show-solution-button">' + params.solutionLabel + '</button>' +
'<span class="empty-text-for-nvda"> </span>' +
'<div class="solution-text hidden" aria-live="polite"></div>';
return element;
};
/**
* Attach function called by H5P framework to insert H5P content into page.
*
* @param {jQuery} $container The container which will be appended to.
*/
C.prototype.attach = function ($container) {
this.setActivityStarted();
$container.get(0).appendChild(this.rootElement);
};
return C;
})();