Skip to content

Commit a21389b

Browse files
committed
Add progress display
1 parent 9c418a2 commit a21389b

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

scripts/single-choice-set.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
206206
this.lastAnswerIsCorrect = event.data.correct;
207207

208208
if (!this.options.behaviour.autoContinue) {
209-
this.updateDirectionButtons();
209+
this.updateNavigation();
210210
}
211211

212212
// Keep track of num correct/wrong answers
@@ -547,6 +547,10 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
547547
}
548548
}
549549

550+
const navigation = document.createElement('div');
551+
navigation.setAttribute('class', 'h5p-scs-navigation');
552+
self.$container.get(0).append(navigation);
553+
550554
this.directionButtons.previous = UI.createButton({
551555
'class': 'h5p-ssc-direction-button previous',
552556
'aria-label': self.l10n.previousButtonLabel, // TODO: Add l10n
@@ -561,9 +565,15 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
561565
event.preventDefault();
562566
}
563567
},
564-
appendTo: self.$container
568+
appendTo: navigation
565569
}).get(0);
566570

571+
this.progressDisplay = document.createElement('div');
572+
this.progressDisplay.setAttribute('class', 'h5p-scs-progress-display');
573+
// Progress announcement is already handled by the progressbar
574+
this.progressDisplay.setAttribute('aria-hidden', 'true');
575+
navigation.append(this.progressDisplay);
576+
567577
this.directionButtons.next = UI.createButton({
568578
'class': 'h5p-ssc-direction-button next',
569579
'aria-label': self.l10n.nextButtonLabel,
@@ -578,10 +588,10 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
578588
event.preventDefault();
579589
}
580590
},
581-
appendTo: self.$container
591+
appendTo: navigation
582592
}).get(0);
583593

584-
this.updateDirectionButtons();
594+
this.updateNavigation();
585595
}
586596

587597
if (self.options.behaviour.soundEffectsEnabled) {
@@ -646,13 +656,19 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
646656
};
647657

648658
/**
649-
* Update direction buttons.
659+
* Update navigation.
650660
*/
651-
SingleChoiceSet.prototype.updateDirectionButtons = function () {
652-
const shouldShowPreviousButton = this.options.behaviour.backwardsNavigationIsAllowed && this.currentIndex > 0;
661+
SingleChoiceSet.prototype.updateNavigation = function () {
662+
const shouldShowPreviousButton = this.options.behaviour.backwardsNavigationIsAllowed &&
663+
this.currentIndex > 0 && this.currentIndex < this.choices.length;
653664

654665
this.toggleDirectionButton('previous', shouldShowPreviousButton);
655666
this.toggleDirectionButton('next', this.choices[this.currentIndex]?.getAnswerGiven());
667+
668+
if (this.progressDisplay) {
669+
this.progressDisplay.innerText = `${this.currentIndex + 1} / ${this.options.choices.length}`;
670+
this.progressDisplay?.classList.toggle('display-none', this.currentIndex >= this.choices.length);
671+
}
656672
};
657673

658674
/**
@@ -718,7 +734,7 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
718734

719735
self.currentIndex = index;
720736

721-
this.updateDirectionButtons();
737+
this.updateNavigation();
722738
};
723739

724740
/**
@@ -897,6 +913,8 @@ H5P.SingleChoiceSet = (function ($, UI, Question, SingleChoice, SolutionView, Re
897913
H5P.Transition.onTransitionEnd(this.$choices, function () {
898914
self.removeFeedback();
899915
}, 600);
916+
917+
this.updateNavigation();
900918
};
901919

902920
/**

styles/single-choice-set.css

+24-18
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
height: 100%;
1313
}
1414

15-
.h5p-sc-set-wrapper:has(.h5p-ssc-direction-button) {
16-
margin-bottom: 2.5em;
15+
.h5p-sc-set-wrapper:has(.h5p-scs-navigation) {
16+
margin-bottom: 0.5em;
1717
}
1818

1919
.h5p-sc-set {
@@ -55,27 +55,26 @@
5555
display: block;
5656
}
5757

58+
.h5p-single-choice-set .h5p-scs-navigation {
59+
display: grid;
60+
grid-template-columns: 2.1875em auto 2.1875em;
61+
height: 2em;
62+
margin: auto;
63+
position: relative;
64+
top: -0.5em; /* Account for old layout that probably should not use that much absolute positioning. */
65+
width: calc(100% - 2 * 1em);
66+
}
67+
5868
.h5p-ssc-direction-button {
5969
display: none;
60-
position: absolute;
61-
right: 0.5em;
62-
bottom: 0;
6370
width: 2.1875em;
71+
margin: 0;
6472
padding: 0.5em 0;
6573
transition: transform 0.2s;
6674
font-family: H5PFontAwesome4;
6775
text-align: center;
6876
}
6977

70-
.h5p-ssc-direction-button.previous {
71-
left: 0.5em;
72-
}
73-
74-
.h5p-ssc-direction-button.next {
75-
right: 0.5em;
76-
}
77-
78-
7978
.h5p-ssc-direction-button:before {
8079
content: "\f054";
8180
position: relative;
@@ -92,18 +91,25 @@
9291
left: 2px;
9392
}
9493

95-
.h5p-ssc-direction-button:active {
96-
/* Overriding JoubelUI, which sets it to relative positioning */
97-
position: absolute;
98-
}
9994
.navigatable .h5p-ssc-direction-button {
10095
display: inline-block;
96+
margin: 0;
10197
}
10298
.h5p-ssc-direction-button[aria-disabled="true"] {
10399
transform: scale(0.01);
104100
visibility: hidden;
105101
}
106102

103+
.h5p-single-choice-set .h5p-scs-progress-display {
104+
align-self: center;
105+
justify-self: center;
106+
text-align: center;
107+
}
108+
109+
.h5p-single-choice-set .h5p-scs-progress-display.display-none {
110+
display: none;
111+
}
112+
107113
ul.h5p-sc-alternatives {
108114
margin: 0;
109115
padding: 0;

0 commit comments

Comments
 (0)