site-accueil-insa/assets/scripts/photosScript.js

157 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-03-25 13:22:28 +02:00
var showcase = $("#img_big");
2018-04-21 15:20:18 +02:00
var showcaseButton = $("#photo_buttons");
2018-03-25 13:22:28 +02:00
var showcaseLink = $("#img_big_link");
var showcaseDownload = $("#img_big_download");
2018-04-21 15:20:18 +02:00
var photoOverlay = $("#photo_overlay");
var headerTop = $("#header_top");
var sideNav = $("#menuSidenav");
2018-04-23 12:04:40 +02:00
var loading = $("#loading");
2018-03-25 14:19:53 +02:00
/*
* Display selected image in showcase
* When clicked, display image in full size
*/
2018-03-25 14:19:53 +02:00
function displayBig(elem) {
changeImage($(elem).attr('src'));
hideTopBar();
2018-04-21 15:20:18 +02:00
disableFullscreen();
photoOverlay.fadeIn(500);
2018-03-25 13:22:28 +02:00
}
2018-03-25 14:19:53 +02:00
function getSourceFromThumbnail(source) {
return source.replace("photos_thumb/", "photos/");
}
/*
* Hide showcase image
*/
2018-03-25 13:22:28 +02:00
function closeBig() {
showTopBar();
2018-04-21 15:20:18 +02:00
disableFullscreen();
photoOverlay.fadeOut(500);
2018-03-25 13:22:28 +02:00
}
2018-03-25 14:19:53 +02:00
2018-04-21 15:20:18 +02:00
/*
* Toggle display of buttons/header
*/
function toggleFullscreen() {
if (showcaseButton.css("display") == "none")
disableFullscreen();
else
enableFullscreen();
}
function enableFullscreen() {
showcaseButton.fadeOut(500);
}
function disableFullscreen() {
showcaseButton.fadeIn(500);
}
function hideTopBar() {
headerTop.fadeOut(500);
sideNav.fadeOut(500);
}
function showTopBar() {
2018-04-21 15:20:18 +02:00
headerTop.fadeIn(500);
sideNav.fadeIn(500);
}
$(document).ready(function() {
document.getElementById("photos_title").scrollIntoView();
});
2018-04-21 15:20:18 +02:00
/*
* Control images with keyboard arrows
*/
$(document).keydown(function(e) {
switch(e.which) {
case 37: // left
displayNext(-1);
break;
case 39: // right
displayNext(1);
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
/*
* Control images with swipes
*/
var img = document.querySelector('#photo_overlay');
// Create a manager to manager the element
var manager = new Hammer.Manager(img);
// Create a recognizer
var Swipe = new Hammer.Swipe();
// Add the recognizer to the manager
manager.add(Swipe);
// Subscribe to the swipe event
manager.on('swipe', function(e) {
var direction = e.offsetDirection;
if (direction === 4) { // right
displayNext(-1);
} else if (direction === 2){ // left
displayNext(1);
}
});
2018-04-21 15:20:18 +02:00
/*
* Display next/last image in showcase. When reaching end/start, loop back to start/end
*/
2018-03-25 13:22:28 +02:00
function displayNext(direction) {
var currentSrc = showcase.attr('src');
var photos = document.getElementsByClassName("photo");
var current = 0;
for (var i = 0; i < photos.length; i++) {
if (getSourceFromThumbnail($(photos[i]).attr('src')) === currentSrc) {
2018-03-25 13:22:28 +02:00
current = i;
}
}
var next = current + direction;
var nextId = "";
if (direction > 0) {
nextId = "#photo-0";
} else {
nextId = "#photo-" + (photos.length - 1);
}
2018-03-25 14:19:53 +02:00
if (document.getElementById("photo-" + next) != null) {
2018-03-25 13:22:28 +02:00
nextId = "#photo-" + next;
}
var nextSrc = $(nextId).attr('src');
changeImage(nextSrc);
}
/*
* Change image source, link and download
*/
function changeImage(thumb) {
2018-04-23 12:04:40 +02:00
displayLoading();
showcase.on('load', function() {
hideLoading();
});
var source = getSourceFromThumbnail(thumb);
showcase.attr("src", source);
showcaseLink.attr("href", source);
showcaseDownload.attr("href", source);
2018-03-25 13:22:28 +02:00
}
2018-04-23 12:04:40 +02:00
function displayLoading() {
loading.show();
}
function hideLoading() {
loading.fadeOut(200);
}