Skip to content

Commit

Permalink
Support undocumented use of enableCheckButton
Browse files Browse the repository at this point in the history
  • Loading branch information
otacke committed Jul 30, 2021
1 parent 2505f3d commit c1ad2e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 13 additions & 3 deletions scripts/essay.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ H5P.Essay = function ($, Question) {
behaviour: {
minimumLength: 0,
inputFieldSize: 10,
enableCheckButton: true,
enableRetry: true,
ignoreScoring: false,
pointsHost: 1
Expand All @@ -65,7 +66,6 @@ H5P.Essay = function ($, Question) {
this.languageTag = Essay.formatLanguageCode(defaultLanguage);

this.score = 0;
this.isAnswered = false;
this.internalShowSolutionsCall = false;

// Sanitize HTML encoding
Expand All @@ -76,6 +76,7 @@ H5P.Essay = function ($, Question) {
this.previousState = contentData.previousState;
}

this.isAnswered = this.previousState && this.previousState.inputField && this.previousState.inputField !== '';
/*
* this.params.behaviour.enableSolutionsButton and this.params.behaviour.enableRetry are used by
* contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-8} and
Expand Down Expand Up @@ -159,6 +160,9 @@ H5P.Essay = function ($, Question) {
}, {
onInteracted: (function () {
that.handleInteracted();
}),
onInput: (function () {
that.handleInput();
})
});

Expand Down Expand Up @@ -212,7 +216,7 @@ H5P.Essay = function ($, Question) {
that.showButton('show-solution');
}
that.hideButton('check-answer');
}, true, {
}, this.params.behaviour.enableCheckButton, {
'aria-label': this.params.ariaCheck
}, {});

Expand Down Expand Up @@ -242,6 +246,8 @@ H5P.Essay = function ($, Question) {
* Handle user interacted.
*/
Essay.prototype.handleInteracted = function () {
// Deliberately keeping the state once answered
this.isAnswered = this.isAnswered || this.inputField.getText().length > 0;
this.triggerXAPI('interacted');
};

Expand Down Expand Up @@ -317,7 +323,11 @@ H5P.Essay = function ($, Question) {

this.hideButton('show-solution');
this.hideButton('try-again');
this.showButton('check-answer');

// QuestionSet can control check button despite not in Question Type contract
if (this.params.behaviour.enableCheckButton) {
this.showButton('check-answer');
}

this.inputField.enable();
this.inputField.focus();
Expand Down
16 changes: 16 additions & 0 deletions scripts/inputfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var H5P = H5P || {};
this.setText(this.previousState);
this.oldValue = this.previousState;

this.containsText = this.oldValue.length > 0;

// Interacted listener
this.inputField.addEventListener('blur', function () {
if (that.oldValue !== that.getText()) {
Expand All @@ -63,6 +65,20 @@ var H5P = H5P || {};
that.oldValue = that.getText();
});

/*
* Extra listener required to be used in QuestionSet properly
*/
this.inputField.addEventListener('input', function () {
if (
that.containsText && that.getText().length === 0 ||
!that.containsText && that.getText().length > 0
) {
that.callbacks.onInteracted();
}

that.containsText = that.getText().length > 0;
});

this.content = document.createElement('div');
this.content.appendChild(this.inputField);

Expand Down

0 comments on commit c1ad2e4

Please sign in to comment.