galeria.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. if(XMLHttpRequest)
  17. var x = new XMLHttpRequest();
  18. else
  19. var x = new ActiveXObject("Microsoft.XMLHTTP");
  20. x.open("GET", "/galeria/load_photo.php?path=" + path, true);
  21. x.send();
  22. x.onreadystatechange = function(){
  23. if(x.readyState == 4){
  24. if(x.status == 200)
  25. document.getElementById('photo_viewer').innerHTML = x.responseText;
  26. }
  27. else
  28. document.getElementById('photo_viewer').innerHTML = "";
  29. }
  30. //Show
  31. document.getElementById('screen_cover').style.display = 'block';
  32. document.getElementById('screen_cover').style.opacity = 0.7;
  33. document.getElementById('photo_viewer').style.display = 'block';
  34. document.getElementById('photo_viewer').style.opacity = 1;
  35. //Searh for path in photo array and set cid
  36. for (var i = 0; i < photo.length; i ++){
  37. if (photo[i] == path){
  38. cid = i;
  39. }
  40. }
  41. }
  42. function closeViewer(){
  43. document.getElementById('screen_cover').style.display = 'none';
  44. document.getElementById('screen_cover').style.opacity = 0;
  45. document.getElementById('photo_viewer').style.display = 'none';
  46. document.getElementById('photo_viewer').style.opacity = 0;
  47. document.getElementById('photo_viewer').innerHTML = "";
  48. }
  49. function keyDown(ev){
  50. var code = ev.keyCode; //('charCode' in event) ? event.charCode : event.keyCode;
  51. switch (code){
  52. case 27: //ESC
  53. closeViewer();
  54. break;
  55. case 37: //Left arrow
  56. if (document.getElementById('photo_viewer').innerHTML != ""){
  57. if (cid == 0){
  58. showPhotoByPath(photo[photo.length - 1]);
  59. }
  60. else{
  61. showPhotoByPath(photo[cid - 1]);
  62. }
  63. }
  64. break;
  65. case 39: //Right arow
  66. if (document.getElementById('photo_viewer').innerHTML != ""){
  67. if (cid == photo.length - 1){
  68. showPhotoByPath(photo[0]);
  69. }
  70. else{
  71. showPhotoByPath(photo[cid + 1]);
  72. }
  73. }
  74. break;
  75. }
  76. }
  77. function scrollPhoto(inc){
  78. switch(inc){
  79. case -1: //Previous
  80. if (document.getElementById('photo_viewer').innerHTML != ""){
  81. if (cid == 0){
  82. showPhotoByPath(photo[photo.length - 1]);
  83. }
  84. else{
  85. showPhotoByPath(photo[cid - 1]);
  86. }
  87. }
  88. break;
  89. case 1: //Next
  90. if (document.getElementById('photo_viewer').innerHTML != ""){
  91. if (cid == photo.length - 1){
  92. showPhotoByPath(photo[0]);
  93. }
  94. else{
  95. showPhotoByPath(photo[cid + 1]);
  96. }
  97. }
  98. break;
  99. }
  100. }
  101. //WARNING: If changed in css, change here.
  102. function defaultInputBorder(id){
  103. id.style.border = '0.2em solid #0078ff'
  104. }
  105. function postComment(photo, lang){
  106. //Get field values
  107. var text = document.getElementById('new_comment_text').value;
  108. var user = document.getElementById('new_comment_user').value;
  109. //Check fields
  110. if (text.length == 0){
  111. defaultBorderStyle = document.getElementById('new_comment_text').style.border;
  112. document.getElementById('new_comment_text').style.border = '0.4em solid #fc5359';
  113. document.getElementById('new_comment_text').focus();
  114. return false;
  115. }
  116. if (user.length == 0){
  117. document.getElementById('new_comment_user').style.border = '0.4em solid #fc5359';
  118. document.getElementById('new_comment_user').focus();
  119. return false;
  120. }
  121. //Load page
  122. if(XMLHttpRequest){
  123. var x = new XMLHttpRequest();
  124. }
  125. else {
  126. var x = new ActiveXObject("Microsoft.XMLHTTP");
  127. }
  128. x.open("POST", "/galeria/comment.php", true);
  129. var params = "photo=" + photo + "&user=" + user + "&text=" + text + "&lang=" + lang;
  130. x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  131. x.onreadystatechange = function(){
  132. if(x.readyState == 4){
  133. if(x.status == 200){
  134. document.getElementById("comment_list").innerHTML = x.responseText;
  135. document.getElementById('new_comment_text').value = '';
  136. document.getElementById('new_comment_user').value = '';
  137. }
  138. }
  139. }
  140. x.send(params);
  141. return false;
  142. }
  143. //Upload functions
  144. function dropFile(ev, title, description){
  145. ev.preventDefault();
  146. var totalFiles = ev.dataTransfer.files.length;
  147. var entry = "";
  148. var reader;
  149. for (var i = 0; i < totalFiles; i++) {
  150. console.log(ev.dataTransfer.files[i]);
  151. if (ev.dataTransfer.files[i].type.substring(0, 5) == 'image'){
  152. reader = new FileReader();
  153. reader.onload = function(event) {
  154. //imgtag.src = event.target.result;
  155. entry = "<div class='form_photo entry'>";
  156. entry = entry + "<img src='" + event.target.result + "'/><br/>";
  157. entry = entry + "<input type='text' placeholder='" + title + "'/><br/>";
  158. entry = entry + "<textarea placeholder='" + description + "'></textarea><br/>";
  159. entry = entry + "</div>";
  160. document.getElementById("file_list").innerHTML = document.getElementById("file_list").innerHTML + entry;
  161. }
  162. reader.readAsDataURL(ev.dataTransfer.files[i]);
  163. }
  164. }
  165. //Show submit button
  166. document.getElementById('photo_submit').style.display = 'initial';
  167. }
  168. function selectFile(ev, inp, title, description){
  169. var totalFiles = inp.files.length;
  170. var entry = "";
  171. var reader;
  172. for (var i = 0; i < inp.files.length; ++i) {
  173. //console.log(inp.files[i]);
  174. if (inp.files[i].type.substring(0, 5) == 'image'){
  175. reader = new FileReader();
  176. reader.onload = function(event) {
  177. //imgtag.src = event.target.result;
  178. entry = "<div class='form_photo entry'>";
  179. entry = entry + "<img src='" + event.target.result + "'/><br/>";
  180. entry = entry + "<input type='text' placeholder='" + title + "'/><br/>";
  181. entry = entry + "<textarea placeholder='" + description + "'></textarea><br/>";
  182. entry = entry + "</div>";
  183. document.getElementById("file_list").innerHTML = document.getElementById("file_list").innerHTML + entry;
  184. }
  185. reader.readAsDataURL(inp.files[i]);
  186. }
  187. }
  188. //Show submit button
  189. document.getElementById('photo_submit').style.display = 'initial';
  190. }
  191. function isInteger(n) {
  192. return n % 1 === 0;
  193. }
  194. function submitPhotos(){
  195. var photoList = [];
  196. var photoData;
  197. var path;
  198. var img;
  199. var album;
  200. var username;
  201. //Check that there is an album selected
  202. album = document.getElementById('album').value;
  203. if (album == -1 || isInteger(album) == false){
  204. //TODO: Notify
  205. console.log('album');
  206. return;
  207. }
  208. //Check that username is filled
  209. username = document.getElementById('username').value;
  210. if (username.length <= 0){
  211. //TODO: Notify
  212. console.log('user');
  213. return;
  214. }
  215. //var url;
  216. var title;
  217. var description;
  218. //Loop section with class form_photo, getting data
  219. var ph = document.getElementsByClassName('form_photo');
  220. for (var i = 0; i < ph.length; i ++){
  221. //Get image
  222. path = ph.item(i).getElementsByTagName('img')[0].src;
  223. //url = ph.item(i).getAsDataURL();
  224. title = ph.item(i).getElementsByTagName('input')[0].value;
  225. description = ph.item(i).getElementsByTagName('textarea')[0].value;
  226. //path = ph.item(i).src.substring(ph.item(i).src.lastIndexOf('/') + 1);
  227. photoList.push([encodeURIComponent(path), encodeURIComponent(title), encodeURIComponent(description)]);
  228. //console.log(photoList[i]);
  229. }
  230. //Load page
  231. if(XMLHttpRequest){
  232. var x = new XMLHttpRequest();
  233. }
  234. else {
  235. var x = new ActiveXObject("Microsoft.XMLHTTP");
  236. }
  237. x.open("POST", "/galeria/save.php", true);
  238. //Set paramas
  239. //TODO: paramas, add user and album
  240. var params = "username=" + username + "&album=" + album;
  241. for (var i = 0; i < photoList.length; i ++){
  242. var params = params + "&file_" + i + "=" + photoList[i][0] + "&title_" + i + "=" + photoList[i][1] + "&description_" + i + "=" + photoList[i][2];
  243. }
  244. console.log(params);
  245. x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  246. x.onreadystatechange = function(){
  247. if(x.readyState == 4){
  248. if(x.status == 200){
  249. console.log("RESPONSE: " + x.responseText);
  250. window.location = "/galeria/";
  251. }
  252. }
  253. }
  254. x.send(params);
  255. }
  256. function launchFileSelector(){
  257. document.getElementById("file_selector").click();
  258. }