Upload.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // ajax request
  102. var ajax = new XMLHttpRequest();
  103. ajax.open( form.getAttribute( 'method' ), form.getAttribute( 'action' ), true );
  104. ajax.onload = function()
  105. {
  106. form.classList.remove( 'is-uploading' );
  107. if( ajax.status >= 200 && ajax.status < 400 )
  108. {
  109. listData($("#selectfolders").val())
  110. input.value = "";
  111. //form.classList.add('is-success');
  112. //old upload php: saveImages(station, pattern, curCount, language, ajax.responseText);
  113. /*
  114. var data = JSON.parse( ajax.responseText );
  115. form.classList.add( data.success == true ? 'is-success' : 'is-error' );
  116. if( !data.success ) errorMsg.textContent = data.error;
  117. */
  118. }
  119. else alert( 'Error. Please, contact the webmaster!' );
  120. };
  121. ajax.onerror = function()
  122. {
  123. form.classList.remove( 'is-uploading' );
  124. alert( 'Error. Please, try again!' );
  125. };
  126. ajax.send( ajaxData );
  127. imageInit();
  128. }
  129. else // fallback Ajax solution upload for older browsers
  130. {
  131. var iframeName = 'uploadiframe' + new Date().getTime(),
  132. iframe = document.createElement( 'iframe' );
  133. $iframe = $( '<iframe name="' + iframeName + '" style="display: none;"></iframe>' );
  134. iframe.setAttribute( 'name', iframeName );
  135. iframe.style.display = 'none';
  136. document.body.appendChild( iframe );
  137. form.setAttribute( 'target', iframeName );
  138. iframe.addEventListener( 'load', function()
  139. {
  140. var data = JSON.parse( iframe.contentDocument.body.innerHTML );
  141. form.classList.remove( 'is-uploading' )
  142. form.classList.add( data.success == true ? 'is-success' : 'is-error' )
  143. form.removeAttribute( 'target' );
  144. if( !data.success ) errorMsg.textContent = data.error;
  145. iframe.parentNode.removeChild( iframe );
  146. });
  147. }
  148. });
  149. // restart the form if has a state of error/success
  150. Array.prototype.forEach.call( restart, function( entry )
  151. {
  152. entry.addEventListener( 'click', function( e )
  153. {
  154. e.preventDefault();
  155. form.classList.remove( 'is-error', 'is-success' );
  156. input.click();
  157. });
  158. });
  159. // Firefox focus bug fix for file input
  160. input.addEventListener( 'focus', function(){ input.classList.add( 'has-focus' ); });
  161. input.addEventListener( 'blur', function(){ input.classList.remove( 'has-focus' ); });
  162. });
  163. }
  164. $(document).ready(function() {
  165. imageInit();
  166. });