galeria.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Array with al the photos in the currently opened album.
  3. * It is populated in {@link populatePhotos()} and read
  4. * in {@link showPhotoByPath(path)}, {@link scrollPhoto(inc)}
  5. * and {@link keyDown(ev)}.
  6. */
  7. var photo = [];
  8. /**
  9. * ID of the photo currently being viewed.
  10. * Set and read from {@link showPhotoByPath(path)},
  11. * {@link scrollPhoto(inc)} and {@link keyDown(ev)}.
  12. */
  13. var cid;
  14. /**
  15. * Populates the photo array {@link photo} with the IDs of the photos
  16. * of the current album.
  17. */
  18. function populatePhotos(){
  19. var photos = document.getElementsByClassName('photo_img');
  20. for (var i = 0; i < photos.length; i ++){
  21. var path = photos.item(i).src.substring(photos.item(i).src.lastIndexOf('/') + 1);
  22. photo.push(path);
  23. }
  24. }
  25. /**
  26. * Displays the photo viewer and loads a photo from it's URL.
  27. * It also sets {@link cid} to be able to use the next/previous features.
  28. * TODO Show a spinning wheel or something before real content is loaded.
  29. * @param path Photo URL.
  30. */
  31. function showPhotoByPath(path){
  32. // Load content
  33. if(XMLHttpRequest){
  34. var x = new XMLHttpRequest();
  35. }
  36. else{ //IE v<7
  37. var x = new ActiveXObject("Microsoft.XMLHTTP");
  38. }
  39. x.open("GET", "/galeria/load_photo.php?path=" + path, true);
  40. x.send();
  41. x.onreadystatechange = function(){
  42. if(x.readyState == 4){
  43. if(x.status == 200)
  44. document.getElementById('photo_viewer').innerHTML = x.responseText;
  45. }
  46. else
  47. document.getElementById('photo_viewer').innerHTML = "";
  48. }
  49. //Show
  50. document.getElementById('screen_cover').style.display = 'block';
  51. document.getElementById('screen_cover').style.opacity = 0.7;
  52. document.getElementById('photo_viewer').style.display = 'block';
  53. document.getElementById('photo_viewer').style.opacity = 1;
  54. //Searh for path in photo array and set cid
  55. for (var i = 0; i < photo.length; i ++){
  56. if (photo[i] == path){
  57. cid = i;
  58. }
  59. }
  60. }
  61. /**
  62. * Closes the photo viewer.
  63. */
  64. function closeViewer(){
  65. document.getElementById('screen_cover').style.display = 'none';
  66. document.getElementById('screen_cover').style.opacity = 0;
  67. document.getElementById('photo_viewer').style.display = 'none';
  68. document.getElementById('photo_viewer').style.opacity = 0;
  69. document.getElementById('photo_viewer').innerHTML = "";
  70. }
  71. /**
  72. * Reads a keypress and acts accordingly if the viewer is shown:
  73. * ESC: Closes the viewer calling {@link closeViewer()}
  74. * LEFT/RIGHT ARROW: Shows previous/next photo
  75. * @param ev Key event.
  76. */
  77. function keyDown(ev){
  78. var code = ev.keyCode;
  79. switch (code){
  80. case 27: //ESC
  81. closeViewer();
  82. break;
  83. case 37: //Left arrow
  84. if (document.getElementById('photo_viewer').innerHTML != ""){
  85. if (cid == 0){
  86. showPhotoByPath(photo[photo.length - 1]);
  87. }
  88. else{
  89. showPhotoByPath(photo[cid - 1]);
  90. }
  91. }
  92. break;
  93. case 39: //Right arow
  94. if (document.getElementById('photo_viewer').innerHTML != ""){
  95. if (cid == photo.length - 1){
  96. showPhotoByPath(photo[0]);
  97. }
  98. else{
  99. showPhotoByPath(photo[cid + 1]);
  100. }
  101. }
  102. break;
  103. }
  104. }
  105. /**
  106. * Shows the previous or next photo in the viewer.
  107. * Does nothing if there are no more photos.
  108. * @param inc -1 for the previous photo, 1 for the next one.
  109. */
  110. function scrollPhoto(inc){
  111. switch(inc){
  112. case -1: //Previous
  113. if (document.getElementById('photo_viewer').innerHTML != ""){
  114. if (cid == 0){
  115. showPhotoByPath(photo[photo.length - 1]);
  116. }
  117. else{
  118. showPhotoByPath(photo[cid - 1]);
  119. }
  120. }
  121. break;
  122. case 1: //Next
  123. if (document.getElementById('photo_viewer').innerHTML != ""){
  124. if (cid == photo.length - 1){
  125. showPhotoByPath(photo[0]);
  126. }
  127. else{
  128. showPhotoByPath(photo[cid + 1]);
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. /**
  135. * Returns an input element to it's default borders once an error
  136. * has been corrected.
  137. * WARNING: If changed in css, change here.
  138. * @param id The element whose border must be altered.
  139. */
  140. function defaultInputBorder(id){
  141. id.style.border = '0.2em solid #0078ff'
  142. }
  143. /**
  144. * Posts a comment in a photo. Checks required fields and
  145. * makes an AJAX request. Also it inserts the comment in
  146. * the page without needingo to reload.
  147. * @param photo Photo ID.
  148. * @param lang User language.
  149. */
  150. function postComment(photo, lang){
  151. //Get field values
  152. var text = document.getElementById('new_comment_text').value;
  153. var user = document.getElementById('new_comment_user').value;
  154. //Check fields
  155. if (text.length == 0){
  156. defaultBorderStyle = document.getElementById('new_comment_text').style.border;
  157. document.getElementById('new_comment_text').style.border = '0.4em solid #fc5359';
  158. document.getElementById('new_comment_text').focus();
  159. return false;
  160. }
  161. if (user.length == 0){
  162. document.getElementById('new_comment_user').style.border = '0.4em solid #fc5359';
  163. document.getElementById('new_comment_user').focus();
  164. return false;
  165. }
  166. //Load page
  167. if(XMLHttpRequest){
  168. var x = new XMLHttpRequest();
  169. }
  170. else{ //IE v<7
  171. var x = new ActiveXObject("Microsoft.XMLHTTP");
  172. }
  173. x.open("POST", "/galeria/comment.php", true);
  174. var params = "photo=" + photo + "&user=" + user + "&text=" + text + "&lang=" + lang;
  175. x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  176. x.onreadystatechange = function(){
  177. if(x.readyState == 4){
  178. if(x.status == 200){
  179. document.getElementById("comment_list").innerHTML = x.responseText;
  180. document.getElementById('new_comment_text').value = '';
  181. document.getElementById('new_comment_user').value = '';
  182. }
  183. }
  184. }
  185. x.send(params);
  186. return false;
  187. }