Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slideshow functionality #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions image-slider.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
margin-bottom: 2.5em;
}

.h5p-image-slider-slides-holder.h5p-image-slider-no-navigation {
margin-bottom: 0;
}

.h5p-image-slider-button {
position: absolute;
top: 0;
Expand Down Expand Up @@ -59,9 +63,13 @@
-moz-osx-font-smoothing: grayscale;
}

.h5p-image-slider-button-text.h5p-image-slider-no-navigation {
display: none;
}

.h5p-image-slider-left-button .h5p-image-slider-button-text {
left: calc(20% - 0.2em);

}
.h5p-image-slider-left-button .h5p-image-slider-button-text:before {
content: "\e901";
Expand All @@ -84,7 +92,7 @@
transition: opacity 0.3s;
}

.h5p-image-slider-button:hover .h5p-image-slider-button-background {
.h5p-image-slider-button:hover .h5p-image-slider-button-background {
opacity: 1;
}

Expand Down
92 changes: 77 additions & 15 deletions image-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ H5P.ImageSlider = (function ($) {
prevSlide: 'Previous Image',
gotoSlide: 'Go to image %slide'
},
aspectRatioMode: 'auto',
aspectRatio: {
aspectWidth: 4,
aspectHeight: 3
behaviour: {
aspectRatioMode: 'auto',
aspectRatio: {
aspectWidth: 4,
aspectHeight: 3
},
loop: false,
progressionTime: 0,
hideNavigation: false
}
}, options);

Expand Down Expand Up @@ -61,7 +66,8 @@ H5P.ImageSlider = (function ($) {
var fullScreenOn = self.$container.hasClass('h5p-fullscreen') || self.$container.hasClass('h5p-semi-fullscreen');
if (fullScreenOn) {
self.$slides.css('height', '');
var newAspectRatio = window.innerWidth / (window.innerHeight - self.$progressBar.outerHeight());
const heightProgressBar = self.$progressBar ? self.$progressBar.outerHeight() : 0;
var newAspectRatio = window.innerWidth / (window.innerHeight - heightProgressBar);
for (var i = 0; i < self.imageSlides.length; i++) {
self.imageSlides[i].setAspectRatio(newAspectRatio);
}
Expand All @@ -87,7 +93,7 @@ H5P.ImageSlider = (function ($) {
this.aspectRatio = 4/3;

// Try to identify aspectRatio according to settings
switch (this.options.aspectRatioMode) {
switch (this.options.behaviour.aspectRatioMode) {
case 'auto':
var imageRatios = [];
for (var i = 0; i < this.options.imageSlides.length; i++) {
Expand All @@ -102,8 +108,8 @@ H5P.ImageSlider = (function ($) {
break;

case 'custom':
if (this.options.aspectRatio.aspectWidth && this.options.aspectRatio.aspectHeight) {
this.aspectRatio = this.options.aspectRatio.aspectWidth / this.options.aspectRatio.aspectHeight;
if (this.options.behaviour.aspectRatio.aspectWidth && this.options.behaviour.aspectRatio.aspectHeight) {
this.aspectRatio = this.options.behaviour.aspectRatio.aspectWidth / this.options.behaviour.aspectRatio.aspectHeight;
}
break;

Expand Down Expand Up @@ -139,6 +145,10 @@ H5P.ImageSlider = (function ($) {
class: 'h5p-image-slider-slides-holder'
}).appendTo($container);

if (this.options.behaviour.hideNavigation) {
this.$slidesHolder.addClass('h5p-image-slider-no-navigation');
}

this.$screenReaderAnnouncement = $('<div>', {
class: 'h5p-image-slider-sr-only',
'aria-atomic': 'true',
Expand All @@ -154,6 +164,37 @@ H5P.ImageSlider = (function ($) {
this.$currentSlide = this.imageSlideHolders[0].addClass('h5p-image-slider-current');

this.attachControls();

// Auto progression
this.options.behaviour.progressionTime = this.options.behaviour.autoProgress ? this.options.behaviour.progressionTime : 0;
this.autoProgress(this.options.behaviour.progressionTime);
};

/**
* Auto progress after a certain time period.
*
* @param {number} seconds Time period in seconds, at least 1.
*/
C.prototype.autoProgress = function(seconds) {
if (!(seconds > 0)) {
return;
}

const self = this;

// Reset timer if running already
if (this.progressionTimer) {
clearTimeout(this.progressionTimer);
}

this.progressionTimer = setTimeout(function() {
const success = self.gotoSlide(self.currentSlideId + 1);

// Stop progression if we can't continue.
if (success) {
self.autoProgress(seconds);
}
}, seconds * 1000);
};

/**
Expand Down Expand Up @@ -213,8 +254,8 @@ H5P.ImageSlider = (function ($) {
*/
C.prototype.attachControls = function() {
var self = this;
this.$leftButton = this.createControlButton(this.options.a11y.prevSlide, 'left');
this.$rightButton = this.createControlButton(this.options.a11y.nextSlide, 'right');
this.$leftButton = this.createControlButton(this.options.a11y.prevSlide, 'left', this.options.behaviour.hideNavigation);
this.$rightButton = this.createControlButton(this.options.a11y.nextSlide, 'right', this.options.behaviour.hideNavigation);
C.handleButtonClick(this.$leftButton, function () {
if (!self.dragging) {
self.gotoSlide(self.currentSlideId - 1);
Expand All @@ -230,7 +271,9 @@ H5P.ImageSlider = (function ($) {
this.$slidesHolder.append(this.$leftButton);
this.$slidesHolder.append(this.$rightButton);
this.updateNavButtons();
this.attachProgressBar();
if (!this.options.behaviour.hideNavigation) {
this.attachProgressBar();
}
this.initDragging();
this.initKeyEvents();
};
Expand Down Expand Up @@ -278,9 +321,10 @@ H5P.ImageSlider = (function ($) {
*
* @param {string} text - label for the button
* @param {string} dir - next or prev
* @param {boolean} hidden - hide text
* @return {jQuery} control button
*/
C.prototype.createControlButton = function(text, dir) {
C.prototype.createControlButton = function(text, dir, hidden) {
var $controlButton = $('<div>', {
class: 'h5p-image-slider-button ' + 'h5p-image-slider-' + dir + '-button',
});
Expand All @@ -296,6 +340,11 @@ H5P.ImageSlider = (function ($) {
'role': 'button',
'tabindex': 0
});

if (hidden) {
$controlText.addClass('h5p-image-slider-no-navigation');
}

$controlButton.append($controlText);

return $controlButton;
Expand All @@ -308,9 +357,18 @@ H5P.ImageSlider = (function ($) {
* @return {Boolean} false if failed(typically the slide didn't exist), true if not
*/
C.prototype.gotoSlide = function(slideId) {
if (slideId < 0 || slideId >= this.imageSlideHolders.length) {
if ((slideId < 0 || slideId >= this.imageSlideHolders.length) && !this.options.behaviour.loop) {
return false;
}

// Loop to first/last slide
if (slideId < 0) {
slideId = this.imageSlideHolders.length - 1;
}
else if (slideId >= this.imageSlideHolders.length) {
slideId = 0;
}

$('.h5p-image-slider-removing', this.$container).removeClass('.h5p-image-slider-removing');
var nextSlideDirection = (this.currentSlideId < slideId) ? 'future' : 'past';
var prevSlideDirection = nextSlideDirection === 'past' ? 'future' : 'past';
Expand Down Expand Up @@ -339,6 +397,10 @@ H5P.ImageSlider = (function ($) {
this.announceCurrentSlide();
this.updateNavButtons();
this.updateProgressBar();

// Reset auto progress timer
this.autoProgress(this.options.behaviour.progressionTime);

return true;
};

Expand All @@ -359,13 +421,13 @@ H5P.ImageSlider = (function ($) {
* Updates all navigation buttons, typically toggling and positioning
*/
C.prototype.updateNavButtons = function() {
if (this.currentSlideId >= this.imageSlides.length - 1) {
if (this.currentSlideId >= this.imageSlides.length - 1 && !this.options.behaviour.loop) {
this.$rightButton.hide();
}
else {
this.$rightButton.show();
}
if (this.currentSlideId <= 0) {
if (this.currentSlideId <= 0 && !this.options.behaviour.loop) {
this.$leftButton.hide();
}
else {
Expand Down
67 changes: 50 additions & 17 deletions language/.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,63 @@
}
},
{
"label": "Aspect ratio",
"description": "Automatic means fixed aspect ratio automatically determined based on the images",
"options": [
"label": "Behavioural settings",
"description": "These options will let you control how the task behaves.",
"fields": [
{
"label": "Automatic"
"label": "Aspect ratio",
"description": "Automatic means fixed aspect ratio automatically determined based on the images",
"default": "auto",
"options": [
{
"label": "Automatic"
},
{
"label": "Custom"
},
{
"label": "Not fixed"
}
]
},
{
"label": "Custom"
"label": "Aspect Ratio Settings",
"showWhen": {
"rules": [
{}
]
},
"fields": [
{
"label": "Aspect ratio width",
"description": "If you use 4 here, and 3 for the height the aspect ratio will be 4:3"
},
{
"label": "Aspect ratio height",
"description": "If you use 3 here, and 4 for the width the aspect ratio will be 4:3"
}
]
},
{
"label": "Not fixed"
}
]
},
{
"label": "Aspect Ratio Settings",
"fields": [
"label": "Loop",
"description": "If set, slider will progress from last image to first image and vice versa"
},
{
"label": "Aspect ratio width",
"description": "If you use 4 here, and 3 for the height the aspect ratio will be 4:3"
"label": "Automatic progression",
"description": "If set, slider will progress to next slide automatically after a configurable time period"
},
{
"label": "Progression time",
"description": "If a time period is set (in seconds), the slider will progress to the next slide automatically after that time period.",
"showWhen": {
"rules": [
{}
]
}
},
{
"label": "Aspect ratio height",
"description": "If you use 3 here, and 4 for the width the aspect ratio will be 4:3"
"label": "Hide navigation",
"description": "If set, navigation will work, but items used for navigation are hidden."
}
]
},
Expand All @@ -60,4 +93,4 @@
]
}
]
}
}
65 changes: 49 additions & 16 deletions language/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,63 @@
}
},
{
"label": "Aspect ratio",
"description": "Automatic means fixed aspect ratio automatically determined based on the images",
"options": [
"label": "Behavioural settings",
"description": "These options will let you control how the task behaves.",
"fields": [
{
"label": "Automatic"
"label": "Aspect ratio",
"description": "Automatic means fixed aspect ratio automatically determined based on the images",
"default": "auto",
"options": [
{
"label": "Automatic"
},
{
"label": "Custom"
},
{
"label": "Not fixed"
}
]
},
{
"label": "Custom"
"label": "Aspect Ratio Settings",
"showWhen": {
"rules": [
{}
]
},
"fields": [
{
"label": "Aspect ratio width",
"description": "If you use 4 here, and 3 for the height the aspect ratio will be 4:3"
},
{
"label": "Aspect ratio height",
"description": "If you use 3 here, and 4 for the width the aspect ratio will be 4:3"
}
]
},
{
"label": "Not fixed"
}
]
},
{
"label": "Aspect Ratio Settings",
"fields": [
"label": "Loop",
"description": "If set, slider will progress from last image to first image and vice versa"
},
{
"label": "Aspect ratio width",
"description": "If you use 4 here, and 3 for the height the aspect ratio will be 4:3"
"label": "Automatic progression",
"description": "If set, slider will progress to next slide automatically after a configurable time period"
},
{
"label": "Progression time",
"description": "If a time period is set (in seconds), the slider will progress to the next slide automatically after that time period.",
"showWhen": {
"rules": [
{}
]
}
},
{
"label": "Aspect ratio height",
"description": "If you use 3 here, and 4 for the width the aspect ratio will be 4:3"
"label": "Hide navigation",
"description": "If set, navigation will work, but items used for navigation are hidden."
}
]
},
Expand Down
Loading