gallery.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var photo = [];
  2. var maxId;
  3. var cid; //Current photo ID;
  4. function populatePhotos(){
  5. var photos = document.getElementsByClassName('photo_img');
  6. for (var i = 0; i < photos.length; i ++){
  7. var path = photos.item(i).src.substring(photos.item(i).src.lastIndexOf('/') + 1);
  8. photo.push(path);
  9. }
  10. maxId = photos.length;
  11. console.log("photo 0: " + photo[0]);
  12. }
  13. function showPhotoByPath(path){
  14. //TODO Change content to a spinning wheel or something before real content is loaded
  15. // Load content
  16. var x = new XMLHttpRequest();
  17. x.open("GET", "/galeria/foto.php?path=" + path, true);
  18. x.send();
  19. x.onreadystatechange = function(){
  20. if(x.readyState == 4){
  21. if(x.status == 200)
  22. document.getElementById('photo_viewer').innerHTML = x.responseText;
  23. }
  24. else
  25. document.getElementById('photo_viewer').innerHTML = "";
  26. }
  27. //Show
  28. document.getElementById('screen_cover').style.display = 'block';
  29. document.getElementById('screen_cover').style.opacity = 0.7;
  30. document.getElementById('photo_viewer').style.display = 'block';
  31. document.getElementById('photo_viewer').style.opacity = 1;
  32. //Searh for path in photo array and set cid
  33. for (var i = 0; i < photo.length; i ++){
  34. if (photo[i] == path){
  35. cid = i;
  36. }
  37. }
  38. }
  39. function closeViewer(){
  40. document.getElementById('screen_cover').style.display = 'none';
  41. document.getElementById('screen_cover').style.opacity = 0;
  42. document.getElementById('photo_viewer').style.display = 'none';
  43. document.getElementById('photo_viewer').style.opacity = 0;
  44. document.getElementById('photo_viewer').innerHTML = "";
  45. }
  46. function keyDown(ev){
  47. var code = ev.keyCode; //('charCode' in event) ? event.charCode : event.keyCode;
  48. switch (code){
  49. case 27: //ESC
  50. closeViewer();
  51. break;
  52. case 37: //Left arrow
  53. if (document.getElementById('photo_viewer').innerHTML != ""){
  54. if (cid == 0){
  55. showPhotoByPath(photo[photo.length - 1]);
  56. }
  57. else{
  58. showPhotoByPath(photo[cid - 1]);
  59. }
  60. }
  61. break;
  62. case 39: //Right arow
  63. if (document.getElementById('photo_viewer').innerHTML != ""){
  64. if (cid == photo.length - 1){
  65. showPhotoByPath(photo[0]);
  66. }
  67. else{
  68. showPhotoByPath(photo[cid + 1]);
  69. }
  70. }
  71. break;
  72. }
  73. }
  74. function scrollPhoto(inc){
  75. switch(inc){
  76. case -1: //Previous
  77. if (document.getElementById('photo_viewer').innerHTML != ""){
  78. if (cid == 0){
  79. showPhotoByPath(photo[photo.length - 1]);
  80. }
  81. else{
  82. showPhotoByPath(photo[cid - 1]);
  83. }
  84. }
  85. break;
  86. case 1: //Next
  87. if (document.getElementById('photo_viewer').innerHTML != ""){
  88. if (cid == photo.length - 1){
  89. showPhotoByPath(photo[0]);
  90. }
  91. else{
  92. showPhotoByPath(photo[cid + 1]);
  93. }
  94. }
  95. break;
  96. }
  97. }