forked from h5p/h5p-standard-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard-page.js
219 lines (183 loc) · 5.69 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
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
* @returns {Object} StandardPage StandardPage instance
*/
function StandardPage(params, id) {
EventDispatcher.call(this);
this.$ = $(this);
this.id = id;
// Set default behavior.
this.params = $.extend({}, {
title: 'Title',
elementList: [],
helpTextLabel: 'Read more',
helpText: 'Help text'
}, params);
}
// 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);
var standardPageTemplate =
'<div class="page-header" role="heading" tabindex="-1">' +
' <div class="page-title">{{{title}}}</div>' +
' <button class="page-help-text">{{{helpTextLabel}}}</button>' +
'</div>';
/*global Mustache */
self.$inner.append(Mustache.render(standardPageTemplate, self.params));
self.$pageTitle = self.$inner.find('.page-header');
self.$helpButton = self.$inner.find('.page-help-text');
self.createHelpTextButton();
this.pageInstances = [];
this.params.elementList.forEach(function (element) {
var $elementContainer = $('<div>', {
'class': 'h5p-standard-page-element'
}).appendTo(self.$inner);
var elementInstance = H5P.newRunnable(element, self.id);
elementInstance.on('loaded', function () {
self.trigger('resize');
});
elementInstance.attach($elementContainer);
self.pageInstances.push(elementInstance);
});
};
/**
* Create help text functionality for reading more about the task
*/
StandardPage.prototype.createHelpTextButton = function () {
var self = this;
if (this.params.helpText !== undefined && this.params.helpText.length) {
self.$helpButton.on('click', function () {
self.trigger('open-help-dialog', {
title: self.params.title,
helpText: self.params.helpText
});
});
}
else {
self.$helpButton.remove();
}
};
/**
* 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;
};
/**
* Sets focus on page
*/
StandardPage.prototype.focus = function () {
this.$pageTitle.focus();
};
/**
* Get page title
* @returns {String} page title
*/
StandardPage.prototype.getTitle = function () {
return this.params.title;
};
/**
* 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('compound');
this.addQuestionToXAPI(xAPIEvent);
return {
statement: xAPIEvent.data.statement,
children: this.getXAPIDataFromChildren()
};
};
return StandardPage;
}(H5P.jQuery, H5P.EventDispatcher));