imageUpload.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //https://css-tricks.com/drag-and-drop-file-uploading/
  2. function imageInit(){
  3. // feature detection for drag&drop upload
  4. var isAdvancedUpload = function()
  5. {
  6. var div = document.createElement( 'div' );
  7. return ( ( 'draggable' in div ) || ( 'ondragstart' in div && 'ondrop' in div ) ) && 'FormData' in window && 'FileReader' in window;
  8. }();
  9. // applying the effect for every form
  10. var forms = document.querySelectorAll( '.box' );
  11. Array.prototype.forEach.call( forms, function( form )
  12. {
  13. var input = form.querySelector( 'input[type="file"]' ),
  14. label = form.querySelector( 'label' ),
  15. errorMsg = form.querySelector( '.box__error span' ),
  16. restart = form.querySelectorAll( '.box__restart' ),
  17. droppedFiles = false,
  18. showFiles = function( files )
  19. {
  20. label.textContent = files.length > 1 ? ( input.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', files.length ) : files[ 0 ].name;
  21. },
  22. triggerFormSubmit = function()
  23. {
  24. var event = document.createEvent( 'HTMLEvents' );
  25. event.initEvent( 'submit', true, false );
  26. form.dispatchEvent( event );
  27. };
  28. // letting the server side to know we are going to make an Ajax request
  29. var ajaxFlag = document.createElement( 'input' );
  30. ajaxFlag.setAttribute( 'type', 'hidden' );
  31. ajaxFlag.setAttribute( 'name', 'ajax' );
  32. ajaxFlag.setAttribute( 'value', 1 );
  33. form.appendChild( ajaxFlag );
  34. // automatically submit the form on file select
  35. input.addEventListener( 'change', function( e )
  36. {
  37. showFiles( e.target.files );
  38. triggerFormSubmit();
  39. });
  40. // drag&drop files if the feature is available
  41. if( isAdvancedUpload )
  42. {
  43. form.classList.add( 'has-advanced-upload' ); // letting the CSS part to know drag&drop is supported by the browser
  44. [ 'drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop' ].forEach( function( event )
  45. {
  46. form.addEventListener( event, function( e )
  47. {
  48. // preventing the unwanted behaviours
  49. e.preventDefault();
  50. e.stopPropagation();
  51. });
  52. });
  53. [ 'dragover', 'dragenter' ].forEach( function( event )
  54. {
  55. form.addEventListener( event, function()
  56. {
  57. form.classList.add( 'is-dragover' );
  58. });
  59. });
  60. [ 'dragleave', 'dragend', 'drop' ].forEach( function( event )
  61. {
  62. form.addEventListener( event, function()
  63. {
  64. form.classList.remove( 'is-dragover' );
  65. });
  66. });
  67. form.addEventListener( 'drop', function( e )
  68. {
  69. droppedFiles = e.dataTransfer.files; // the files that were dropped
  70. showFiles( droppedFiles );
  71. triggerFormSubmit();
  72. });
  73. }
  74. // if the form was submitted
  75. form.addEventListener( 'submit', function( e )
  76. {
  77. // preventing the duplicate submissions if the current one is in progress
  78. if( form.classList.contains( 'is-uploading' ) ) return false;
  79. form.classList.add( 'is-uploading' );
  80. form.classList.remove( 'is-error' );
  81. var station = $('#pattern_station').text();
  82. var pattern = $('#pattern_ID').text();
  83. var curCount = $('#image_count').text();
  84. var language = $('#image_language option:selected').text();
  85. if( isAdvancedUpload ) // ajax file upload for modern browsers
  86. {
  87. e.preventDefault();
  88. // gathering the form data
  89. var ajaxData;
  90. if( droppedFiles )
  91. {
  92. ajaxData = new FormData();
  93. Array.prototype.forEach.call( droppedFiles, function( file )
  94. {
  95. ajaxData.append( input.getAttribute( 'name' ), file );
  96. });
  97. }
  98. else{
  99. ajaxData = new FormData( form );
  100. }
  101. ajaxData.append("language", language)
  102. // ajax request
  103. var ajax = new XMLHttpRequest();
  104. ajax.open( form.getAttribute( 'method' ), form.getAttribute( 'action' ), true );
  105. ajax.onload = function()
  106. {
  107. form.classList.remove( 'is-uploading' );
  108. if( ajax.status >= 200 && ajax.status < 400 )
  109. {
  110. getPattern(station, pattern)
  111. input.value = "";
  112. //form.classList.add('is-success');
  113. //old upload php: saveImages(station, pattern, curCount, language, ajax.responseText);
  114. /*
  115. var data = JSON.parse( ajax.responseText );
  116. form.classList.add( data.success == true ? 'is-success' : 'is-error' );
  117. if( !data.success ) errorMsg.textContent = data.error;
  118. */
  119. }
  120. else alert( 'Error. Please, contact the webmaster!' );
  121. };
  122. ajax.onerror = function()
  123. {
  124. form.classList.remove( 'is-uploading' );
  125. alert( 'Error. Please, try again!' );
  126. };
  127. ajax.send( ajaxData );
  128. imageInit();
  129. }
  130. else // fallback Ajax solution upload for older browsers
  131. {
  132. var iframeName = 'uploadiframe' + new Date().getTime(),
  133. iframe = document.createElement( 'iframe' );
  134. $iframe = $( '<iframe name="' + iframeName + '" style="display: none;"></iframe>' );
  135. iframe.setAttribute( 'name', iframeName );
  136. iframe.style.display = 'none';
  137. document.body.appendChild( iframe );
  138. form.setAttribute( 'target', iframeName );
  139. iframe.addEventListener( 'load', function()
  140. {
  141. var data = JSON.parse( iframe.contentDocument.body.innerHTML );
  142. form.classList.remove( 'is-uploading' )
  143. form.classList.add( data.success == true ? 'is-success' : 'is-error' )
  144. form.removeAttribute( 'target' );
  145. if( !data.success ) errorMsg.textContent = data.error;
  146. iframe.parentNode.removeChild( iframe );
  147. });
  148. }
  149. });
  150. // restart the form if has a state of error/success
  151. Array.prototype.forEach.call( restart, function( entry )
  152. {
  153. entry.addEventListener( 'click', function( e )
  154. {
  155. e.preventDefault();
  156. form.classList.remove( 'is-error', 'is-success' );
  157. input.click();
  158. });
  159. });
  160. // Firefox focus bug fix for file input
  161. input.addEventListener( 'focus', function(){ input.classList.add( 'has-focus' ); });
  162. input.addEventListener( 'blur', function(){ input.classList.remove( 'has-focus' ); });
  163. });
  164. }
  165. $(document).ready(function() {
  166. imageInit();
  167. });