forked from h5p/h5p-documentation-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumentation-tool.js
619 lines (531 loc) · 19.3 KB
/
documentation-tool.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
var H5P = H5P || {};
/**
* Documentation tool module
* @external {jQuery} $ H5P.jQuery
*/
H5P.DocumentationTool = (function ($, NavigationMenu, JoubelUI, EventDispatcher) {
// CSS Classes:
var MAIN_CONTAINER = 'h5p-documentation-tool';
var PAGES_CONTAINER = 'h5p-documentation-tool-page-container';
var PAGE_INSTANCE = 'h5p-documentation-tool-page';
var FOOTER = 'h5p-documentation-tool-footer';
/**
* Initialize module.
* @param {Object} params Behavior settings
* @param {Number} id Content identification
* @returns {Object} DocumentationTool DocumentationTool instance
*/
function DocumentationTool(params, id) {
var self = this;
this.$ = $(this);
this.id = id;
// Set default behavior.
this.params = $.extend({}, {
taskDescription: 'Documentation Tool',
pagesList: [],
l10n: {
nextLabel: 'Next',
previousLabel: 'Previous',
closeLabel: 'Close'
}
}, params);
if (params.taskDescription === undefined && params.navMenuLabel !== undefined) {
this.params.taskDescription = params.navMenuLabel;
}
EventDispatcher.call(this);
this.on('resize', self.resize, self);
}
DocumentationTool.prototype = Object.create(EventDispatcher.prototype);
DocumentationTool.prototype.constructor = DocumentationTool;
/**
* Make a non-button element behave as a button. I.e handle enter and space
* keydowns as click
*
* @param {H5P.jQuery} $element The "button" element
* @param {Function} callback
*/
DocumentationTool.handleButtonClick = function ($element, callback) {
$element.click(function (event) {
callback.call($(this), event);
});
$element.keydown(function (event) {
// 32 - space, 13 - enter
if ([32, 13].indexOf(event.which) !== -1) {
event.preventDefault();
callback.call($(this), event);
}
});
};
/**
* Attach function called by H5P framework to insert H5P content into page.
*
* @param {jQuery} $container The container which will be appended to.
*/
DocumentationTool.prototype.attach = function ($container) {
var self = this;
this.pageInstances = [];
this.currentPageIndex = 0;
this.$inner = $container.addClass(MAIN_CONTAINER);
this.$mainContent = $('<div/>', {
'class': 'h5p-documentation-tool-main-content'
}).appendTo(this.$inner);
// Create pages
var $pagesContainer = self.createPages().appendTo(this.$mainContent);
self.$pagesArray = $pagesContainer.children();
// Create navigation menu
var navigationMenu = new NavigationMenu(self, this.params.taskDescription);
navigationMenu.attach(this.$mainContent);
if (this.$inner.children().length) {
self.$pagesArray.eq(self.currentPageIndex).addClass('current');
}
this.navigationMenu = navigationMenu;
self.resize();
};
/**
* Creates the footer.
* @returns {jQuery} $footer Footer element
*/
DocumentationTool.prototype.createFooter = function (enablePrevious, enableNext) {
var $footer = $('<div>', {
'class': FOOTER
});
// Next page button
this.createNavigationButton(1, enableNext).appendTo($footer);
// Previous page button
this.createNavigationButton(-1, enablePrevious).appendTo($footer);
return $footer;
};
/**
* Create navigation button
* @param {Number} moveDirection An integer for how many pages the button will move, and in which direction
* @returns {*}
*/
DocumentationTool.prototype.createNavigationButton = function (moveDirection, enabled) {
var self = this;
var type = 'next';
var navigationLabel = this.params.l10n.nextLabel;
if (moveDirection === -1) {
type = 'prev';
navigationLabel = this.params.l10n.previousLabel;
}
var $navButton = $('<div>', {
'class': 'joubel-simple-rounded-button h5p-documentation-tool-nav-button ' + type,
'title': navigationLabel,
'aria-disabled': !enabled,
'tabindex': enabled ? 0 : undefined,
'html': '<span class="joubel-simple-rounded-button-text"></span>'
});
DocumentationTool.handleButtonClick($navButton, function (event) {
self.movePage(self.currentPageIndex + moveDirection, event);
});
return $navButton;
};
/**
* Populate container and array with page instances.
* @returns {jQuery} Container
*/
DocumentationTool.prototype.createPages = function () {
var self = this;
var $pagesContainer = $('<div>', {
'class': PAGES_CONTAINER
});
var numPages = this.params.pagesList.length;
for (var i = 0; i < numPages; i++) {
var page = this.params.pagesList[i];
var $pageInstance = $('<div>', {
'class': PAGE_INSTANCE
}).appendTo($pagesContainer);
var singlePage = H5P.newRunnable(page, self.id, undefined, undefined, {
parent: self // Set the parent for xapi context
});
if (singlePage.libraryInfo.machineName === 'H5P.DocumentExportPage') {
singlePage.setExportTitle(self.params.taskDescription);
singlePage.setSumbitEnabled(H5PIntegration.reportingIsEnabled);
}
singlePage.attach($pageInstance);
self.createFooter(i !== 0, i < (numPages - 1)).appendTo($pageInstance);
self.pageInstances.push(singlePage);
singlePage.on('resize', function () {
self.trigger('resize');
});
singlePage.on('export-page-opened', self.hide, self);
singlePage.on('export-page-closed', self.show, self);
singlePage.on('open-help-dialog', self.showHelpDialog, self);
singlePage.on('submitted', function() {
self.triggerAnsweredEvents();
self.triggerXAPI('completed');
});
}
return $pagesContainer;
};
/**
* Remove tabindex for main content.
*/
DocumentationTool.prototype.untabalize = function () {
// Make all other elements in container not tabbable. When dialog is open,
// it's like the elements behind does not exist.
this.$tabbables = this.$mainContent.find('a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]').each(function () {
var $tabbable = $(this);
// Store current tabindex, so we can set it back when dialog closes
$tabbable.data('tabindex', $tabbable.attr('tabindex'));
// Make it non tabbable
$tabbable.attr('tabindex', '-1');
});
// Make sure container and all content within it is not seen by screenreaders
this.$mainContent.attr('aria-hidden', true);
};
/**
* Set back tabindex for main container.
*/
DocumentationTool.prototype.tabalize = function () {
if (this.$tabbables) {
this.$tabbables.each(function () {
var $element = $(this);
var tabindex = $element.data('tabindex');
if (tabindex !== undefined) {
$element.attr('tabindex', tabindex);
$element.removeData('tabindex');
}
else {
$element.removeAttr('tabindex');
}
});
}
this.$mainContent.removeAttr('aria-hidden');
};
/**
* Show help dialog
*
* @param {Object} event
*/
DocumentationTool.prototype.showHelpDialog = function (event) {
var self = this;
self.untabalize();
var helpTextDialog = new H5P.JoubelUI.createHelpTextDialog(event.data.title, event.data.helpText, self.params.l10n.closeLabel);
// Handle closing of the dialog
helpTextDialog.on('closed', function () {
// Set focus back on the page
self.tabalize();
self.getCurrentPage().$helpButton.focus();
});
this.$inner.append(helpTextDialog.getElement());
helpTextDialog.focus();
};
/**
* Hide me
*/
DocumentationTool.prototype.hide = function () {
this.$mainContent.addClass('hidden');
};
/**
* Show me
*/
DocumentationTool.prototype.show = function () {
this.$mainContent.removeClass('hidden');
};
/**
* Moves the documentation tool to the specified page
* @param {Number} toPageIndex Move to this page index
*/
DocumentationTool.prototype.movePage = function (toPageIndex, event) {
var self = this;
// Invalid value
if ((toPageIndex + 1 > this.$pagesArray.length) || (toPageIndex < 0)) {
return;
}
var assessmentGoals = self.getGoalAssessments(self.pageInstances);
var newGoals = self.getGoals(self.pageInstances);
assessmentGoals.forEach(function (assessmentPage) {
newGoals = self.mergeGoals(newGoals, assessmentPage);
});
// Update page depending on what page type it is
self.updatePage(toPageIndex, newGoals);
this.$pagesArray.eq(this.currentPageIndex).removeClass('current');
this.currentPageIndex = toPageIndex;
this.$pagesArray.eq(this.currentPageIndex).addClass('current');
// Update navigation menu
this.navigationMenu.updateNavigationMenu(this.currentPageIndex);
// Scroll to top
this.scrollToTop();
// Set focus on the new page after navigating to it
var pageInstance = self.pageInstances[toPageIndex];
if (pageInstance.focus) {
// Trigger focus on text tick
setTimeout(function () {
pageInstance.focus();
}, 0);
}
// Trigger xAPI event
var progressedEvent = self.createXAPIEventTemplate('progressed');
progressedEvent.data.statement.object.definition.extensions['http://id.tincanapi.com/extension/ending-point'] = toPageIndex;
self.trigger(progressedEvent);
self.trigger('resize');
};
/**
* Get current page instance
* @return {Object} The page instance
*/
DocumentationTool.prototype.getCurrentPage = function () {
return this.pageInstances[this.currentPageIndex];
};
/**
* Scroll to top if changing page and below y position is above threshold
*/
DocumentationTool.prototype.scrollToTop = function () {
var staticScrollToTopPadding = 90;
var yPositionThreshold = 75;
// Scroll to top of content type if above y threshold
if ($(window).scrollTop() - $(this.$inner).offset().top > yPositionThreshold) {
$(window).scrollTop(this.$inner.offset().top - staticScrollToTopPadding);
}
};
/**
* Update page depending on what type of page it is
* @param {Object} toPageIndex Page object that will be updated
* @param {Array} newGoals Array containing updated goals
*/
DocumentationTool.prototype.updatePage = function (toPageIndex, newGoals) {
var self = this;
var pageInstance = self.pageInstances[toPageIndex];
if (pageInstance.libraryInfo.machineName === 'H5P.GoalsAssessmentPage') {
self.setGoals(self.pageInstances, newGoals);
} else if (pageInstance.libraryInfo.machineName === 'H5P.DocumentExportPage') {
// Check if all required input fields are filled
var allRequiredInputsAreFilled = self.checkIfAllRequiredInputsAreFilled(self.pageInstances);
self.setRequiredInputsFilled(self.pageInstances, allRequiredInputsAreFilled);
// Get all input fields, goals and goal assessments
var allInputs = self.getDocumentExportInputs(self.pageInstances);
self.setDocumentExportOutputs(self.pageInstances, allInputs);
self.setDocumentExportGoals(self.pageInstances, newGoals);
}
};
/**
* Merge assessment goals and newly created goals
*
* @returns {Array} newGoals Merged goals list with updated assessments
*/
DocumentationTool.prototype.mergeGoals = function (newGoals, assessmentGoals) {
// Not an assessment page
if (!assessmentGoals.length) {
return newGoals;
}
newGoals.forEach(function (goalPage, pageIndex) {
goalPage.forEach(function (goalInstance) {
var result = $.grep(assessmentGoals[pageIndex], function (assessmentInstance) {
return assessmentInstance.getUniqueId() === goalInstance.getUniqueId();
});
if (result.length) {
goalInstance.goalAnswer(result[0].goalAnswer());
}
});
});
return newGoals;
};
/**
* Gets goals assessments from all goals assessment pages and returns update goals list.
*
* @param {Array} pageInstances Array of pages contained within the documentation tool
* @returns {Array} goals Updated goals list
*/
DocumentationTool.prototype.getGoalAssessments = function (pageInstances) {
var goals = [];
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.GoalsAssessmentPage') {
goals.push(page.getAssessedGoals());
}
});
return goals;
};
/**
* Retrieves all input fields from the documentation tool
* @returns {Array} inputArray Array containing all inputs of the documentation tool
*/
DocumentationTool.prototype.getDocumentExportInputs = function (pageInstances) {
var inputArray = [];
pageInstances.forEach(function (page) {
var pageInstanceInput = [];
var title = '';
if (page.libraryInfo.machineName === 'H5P.StandardPage') {
pageInstanceInput = page.getInputArray();
title = page.getTitle();
}
inputArray.push({inputArray: pageInstanceInput, title: title});
});
return inputArray;
};
/**
* Checks if all required inputs are filled
* @returns {boolean} True if all required inputs are filled
*/
DocumentationTool.prototype.checkIfAllRequiredInputsAreFilled = function (pageInstances) {
var allRequiredInputsAreFilled = true;
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.StandardPage') {
if (!page.requiredInputsIsFilled()) {
allRequiredInputsAreFilled = false;
}
}
});
return allRequiredInputsAreFilled;
};
/**
* Gets goals from all goal pages and returns updated goals list.
*
* @param {Array} pageInstances Array containing all pages.
* @returns {Array} goals Updated goals list.
*/
DocumentationTool.prototype.getGoals = function (pageInstances) {
var goals = [];
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.GoalsPage') {
goals.push(page.getGoals());
}
});
return goals;
};
/**
* Insert goals to all goal assessment pages.
* @param {Array} pageInstances Page instances
* @param {Array} goals Array of goals.
*/
DocumentationTool.prototype.setGoals = function (pageInstances, goals) {
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.GoalsAssessmentPage') {
page.updateAssessmentGoals(goals);
}
});
};
/**
* Sets the output for all document export pages
* @param {Array} inputs Array of input strings
*/
DocumentationTool.prototype.setDocumentExportOutputs = function (pageInstances, inputs) {
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.DocumentExportPage') {
page.updateOutputFields(inputs);
}
});
};
/**
* Sets the output for all document export pages
* @param {Array} inputs Array of input strings
*/
DocumentationTool.prototype.setDocumentExportGoals = function (pageInstances, newGoals) {
var assessmentPageTitle = '';
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.GoalsAssessmentPage') {
assessmentPageTitle = page.getTitle();
}
});
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.DocumentExportPage') {
page.updateExportableGoals({inputArray: newGoals, title: assessmentPageTitle});
}
});
};
/**
* Sets the required inputs filled boolean in all document export pages
* @param {boolean} isRequiredInputsFilled True if all required inputs are filled
*/
DocumentationTool.prototype.setRequiredInputsFilled = function (pageInstances, isRequiredInputsFilled) {
pageInstances.forEach(function (page) {
if (page.libraryInfo.machineName === 'H5P.DocumentExportPage') {
page.updateRequiredInputsFilled(isRequiredInputsFilled);
}
});
};
/**
* Resize function for responsiveness.
*/
DocumentationTool.prototype.resize = function () {
// Width calculations
this.adjustDocumentationToolWidth();
this.adjustNavBarHeight();
};
/**
* Adjusts navigation menu minimum height
*/
DocumentationTool.prototype.adjustNavBarHeight = function () {
var headerHeight = this.navigationMenu.$navigationMenuHeader.get(0).getBoundingClientRect().height +
parseFloat(this.navigationMenu.$navigationMenuHeader.css('margin-top')) +
parseFloat(this.navigationMenu.$navigationMenuHeader.css('margin-bottom'));
var entriesHeight = this.navigationMenu.$navigationMenuEntries.get(0).getBoundingClientRect().height;
var minHeight = headerHeight + entriesHeight;
this.$mainContent.css('min-height', minHeight + 'px');
};
/**
* Resizes navigation menu depending on task width
*/
DocumentationTool.prototype.adjustDocumentationToolWidth = function () {
// Show responsive design when width relative to font size is less than static threshold
var staticResponsiveLayoutThreshold = 40;
var relativeWidthOfContainer = this.$inner.width() / parseInt(this.$inner.css('font-size'), 10);
var responsiveLayoutRequirement = relativeWidthOfContainer < staticResponsiveLayoutThreshold;
this.navigationMenu.setResponsiveLayout(responsiveLayoutRequirement);
};
/**
* Triggers an 'answered' xAPI event for all inputs
* We do this because we can only trigger answered events once per submission
* therefore, we have to trigger all of them simultaneously with one function.
*/
DocumentationTool.prototype.triggerAnsweredEvents = function () {
this.pageInstances.forEach(function(page) {
if (page.triggerAnsweredEvents) {
page.triggerAnsweredEvents();
}
});
};
/**
* Generate xAPI object definition used in xAPI statements.
* @return {Object}
*/
DocumentationTool.prototype.getxAPIDefinition = function () {
var definition = {};
definition.interactionType = 'compound';
definition.type = 'http://adlnet.gov/expapi/activities/cmi.interaction';
definition.description = {
'en-US': ''
};
definition.extensions = {
'https://h5p.org/x-api/h5p-machine-name': 'H5P.DocumentationTool'
};
return definition;
};
/**
* Add the question itself to the definition part of an xAPIEvent
*/
DocumentationTool.prototype.addQuestionToXAPI = function (xAPIEvent) {
var definition = xAPIEvent.getVerifiedStatementValue(['object', 'definition']);
$.extend(definition, this.getxAPIDefinition());
};
/**
* Get xAPI data from sub content types
*
* @param {Object} metaContentType
* @returns {array}
*/
DocumentationTool.prototype.getXAPIDataFromChildren = function () {
var children = [];
this.pageInstances.forEach(function(page) {
if (page.getXAPIData) {
children.push(page.getXAPIData());
}
});
return children;
};
/**
* Get xAPI data.
* Contract used by report rendering engine.
*
* @see contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-6}
*/
DocumentationTool.prototype.getXAPIData = function () {
var xAPIEvent = this.createXAPIEventTemplate('answered');
this.addQuestionToXAPI(xAPIEvent);
return {
statement: xAPIEvent.data.statement,
children: this.getXAPIDataFromChildren()
};
};
return DocumentationTool;
}(H5P.jQuery, H5P.DocumentationTool.NavigationMenu, H5P.JoubelUI, H5P.EventDispatcher));