-
Notifications
You must be signed in to change notification settings - Fork 32
/
standard-page.js
272 lines (233 loc) · 7.36 KB
/
standard-page.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
var H5P = H5P || {};
/**
* Standard Page module
* @external {jQuery} $ H5P.jQuery
*/
H5P.StandardPage = (function ($, EventDispatcher) {
"use strict";
// CSS Classes:
var MAIN_CONTAINER = 'h5p-standard-page';
/**
* Initialize module.
* @param {Object} params Behavior settings
* @param {Number} id Content identification
* @param {object} [extras] Saved state, metadata, etc.
* @returns {Object} StandardPage StandardPage instance
*/
function StandardPage(params, id, extras) {
EventDispatcher.call(this);
this.$ = $(this);
this.id = id;
this.extras = extras;
// Set default behavior.
this.params = $.extend({
title: this.getTitle(),
a11yFriendlyTitle: this.getTitle(false),
elementList: [],
helpTextLabel: 'Read more',
helpText: 'Help text'
}, params);
if (extras !== undefined && extras.previousState !== null && typeof extras.previousState === 'object' && Object.keys(extras.previousState).length) {
this.previousState = extras.previousState;
}
}
// Setting up inheritance
StandardPage.prototype = Object.create(EventDispatcher.prototype);
StandardPage.prototype.constructor = StandardPage;
/**
* Attach function called by H5P framework to insert H5P content into page.
*
* @param {jQuery} $container The container which will be appended to.
*/
StandardPage.prototype.attach = function ($container) {
var self = this;
this.$inner = $('<div>', {
'class': MAIN_CONTAINER
}).appendTo($container);
self.$pageTitle = $('<div>', {
'class': 'page-header',
role: 'heading',
tabindex: -1,
'aria-label': self.params.a11yFriendlyTitle,
append: $('<div>', {
class: 'page-title',
html: self.params.title
}),
appendTo: self.$inner
});
if (self.params.helpText !== undefined && self.params.helpText.length !== 0) {
self.$helpButton = $('<button>', {
'class': 'page-help-text',
html: self.params.helpTextLabel,
click: function () {
self.trigger('open-help-dialog', {
title: self.params.title,
helpText: self.params.helpText
});
},
appendTo: self.$pageTitle
});
}
this.pageInstances = [];
this.params.elementList.forEach(function (element, index) {
var $elementContainer = $('<div>', {
'class': 'h5p-standard-page-element'
}).appendTo(self.$inner);
const childExtras = {}
if (self.previousState && self.previousState.childrenStates[index]) {
childExtras.previousState = self.previousState.childrenStates[index];
}
var elementInstance = H5P.newRunnable(
element,
self.id,
undefined,
true,
childExtras
);
elementInstance.on('loaded', function () {
self.trigger('resize');
});
elementInstance.on('resize', function () {
self.parent.trigger('resize');
});
elementInstance.attach($elementContainer);
self.pageInstances.push(elementInstance);
});
};
/**
* Retrieves input array.
*/
StandardPage.prototype.getInputArray = function () {
var inputArray = [];
this.pageInstances.forEach(function (elementInstance) {
if (elementInstance.libraryInfo.machineName === 'H5P.TextInputField') {
inputArray.push(elementInstance.getInput());
}
});
return inputArray;
};
/**
* Returns True if all required inputs are filled.
* @returns {boolean} True if all required inputs are filled.
*/
StandardPage.prototype.requiredInputsIsFilled = function () {
var requiredInputsIsFilled = true;
this.pageInstances.forEach(function (elementInstance) {
if (elementInstance.libraryInfo.machineName === 'H5P.TextInputField') {
if (!elementInstance.isRequiredInputFilled()) {
requiredInputsIsFilled = false;
}
}
});
return requiredInputsIsFilled;
};
/**
* Mark required input fields.
*/
StandardPage.prototype.markRequiredInputFields = function () {
this.pageInstances.forEach(function (elementInstance) {
if (elementInstance.libraryInfo.machineName === 'H5P.TextInputField') {
if (!elementInstance.isRequiredInputFilled()) {
elementInstance.markEmptyField();
}
}
});
};
/**
* Sets focus on page
*/
StandardPage.prototype.focus = function () {
this.$pageTitle.focus();
};
/**
* Get page title
* @param {boolean} turncatedTitle turncate title flag
* @returns {String} page title
*/
StandardPage.prototype.getTitle = function (turncatedTitle = true) {
const pageTitle = (this.extras && this.extras.metadata && this.extras.metadata.title) ? this.extras.metadata.title : 'Instructions';
return turncatedTitle ? H5P.createTitle(pageTitle) : pageTitle;
};
/**
* Triggers an 'answered' xAPI event for all inputs
*/
StandardPage.prototype.triggerAnsweredEvents = function () {
this.pageInstances.forEach(function (elementInstance) {
if (elementInstance.triggerAnsweredEvent) {
elementInstance.triggerAnsweredEvent();
}
});
};
/**
* Helper function to return all xAPI data
* @returns {Array}
*/
StandardPage.prototype.getXAPIDataFromChildren = function () {
var children = [];
this.pageInstances.forEach(function (elementInstance) {
if (elementInstance.getXAPIData) {
children.push(elementInstance.getXAPIData());
}
});
return children;
};
/**
* Generate xAPI object definition used in xAPI statements.
* @return {Object}
*/
StandardPage.prototype.getxAPIDefinition = function () {
var definition = {};
var self = this;
definition.interactionType = 'compound';
definition.type = 'http://adlnet.gov/expapi/activities/cmi.interaction';
definition.description = {
'en-US': self.params.title
};
definition.extensions = {
'https://h5p.org/x-api/h5p-machine-name': 'H5P.StandardPage'
};
return definition;
};
/**
* Add the question itself to the definition part of an xAPIEvent
*/
StandardPage.prototype.addQuestionToXAPI = function (xAPIEvent) {
var definition = xAPIEvent.getVerifiedStatementValue(['object', 'definition']);
$.extend(definition, this.getxAPIDefinition());
};
/**
* Get xAPI data.
* Contract used by report rendering engine.
*
* @see contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-6}
*/
StandardPage.prototype.getXAPIData = function () {
var xAPIEvent = this.createXAPIEventTemplate('answered');
this.addQuestionToXAPI(xAPIEvent);
return {
statement: xAPIEvent.data.statement,
children: this.getXAPIDataFromChildren()
};
};
/**
* Answer call to return the current state.
*
* @return {object} Current state.
*/
StandardPage.prototype.getCurrentState = function () {
const childrenStates = this.pageInstances.map(function (instance) {
return (typeof instance.getCurrentState === 'function') ?
instance.getCurrentState() :
undefined;
});
return {
childrenStates: childrenStates
};
};
StandardPage.prototype.resetTask = function() {
this.pageInstances.map(function (instance) {
typeof instance.resetTask === 'function' && instance.resetTask();
});
};
return StandardPage;
}(H5P.jQuery, H5P.EventDispatcher));