Skip to content

Commit

Permalink
Merge pull request #36 from otacke/save-content-state
Browse files Browse the repository at this point in the history
Implement save-content-state-feature
  • Loading branch information
fnoks authored Mar 9, 2022
2 parents d11bf6e + aae5b82 commit d4e5498
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions standard-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ H5P.StandardPage = (function ($, EventDispatcher) {
* 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) {
Expand All @@ -30,6 +31,10 @@ H5P.StandardPage = (function ($, EventDispatcher) {
helpTextLabel: 'Read more',
helpText: 'Help text'
}, params);

if (extras !== undefined && typeof extras.previousState === 'object' && Object.keys(extras.previousState).length) {
this.previousState = extras.previousState;
}
}

// Setting up inheritance
Expand Down Expand Up @@ -64,12 +69,23 @@ H5P.StandardPage = (function ($, EventDispatcher) {

this.pageInstances = [];

this.params.elementList.forEach(function (element) {
this.params.elementList.forEach(function (element, index) {
var $elementContainer = $('<div>', {
'class': 'h5p-standard-page-element'
}).appendTo(self.$inner);

var elementInstance = H5P.newRunnable(element, self.id);
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');
Expand Down Expand Up @@ -229,5 +245,22 @@ H5P.StandardPage = (function ($, EventDispatcher) {
};
};

/**
* 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
};
};

return StandardPage;
}(H5P.jQuery, H5P.EventDispatcher));

0 comments on commit d4e5498

Please sign in to comment.