Upload.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $('#imgUpload').submit();
  25. //var event = document.createEvent( 'HTMLEvents' );
  26. //event.initEvent( 'submit', true, false );
  27. //form.dispatchEvent( event );
  28. };
  29. // letting the server side to know we are going to make an Ajax request
  30. var ajaxFlag = document.createElement( 'input' );
  31. ajaxFlag.setAttribute( 'type', 'hidden' );
  32. ajaxFlag.setAttribute( 'name', 'ajax' );
  33. ajaxFlag.setAttribute( 'value', 1 );
  34. form.appendChild( ajaxFlag );
  35. // automatically submit the form on file select
  36. input.addEventListener( 'change', function( e )
  37. {
  38. showFiles( e.target.files );
  39. triggerFormSubmit();
  40. });
  41. // drag&drop files if the feature is available
  42. if( isAdvancedUpload )
  43. {
  44. form.classList.add( 'has-advanced-upload' ); // letting the CSS part to know drag&drop is supported by the browser
  45. [ 'drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop' ].forEach( function( event )
  46. {
  47. form.addEventListener( event, function( e )
  48. {
  49. // preventing the unwanted behaviours
  50. e.preventDefault();
  51. e.stopPropagation();
  52. });
  53. });
  54. [ 'dragover', 'dragenter' ].forEach( function( event )
  55. {
  56. form.addEventListener( event, function()
  57. {
  58. form.classList.add( 'is-dragover' );
  59. });
  60. });
  61. [ 'dragleave', 'dragend', 'drop' ].forEach( function( event )
  62. {
  63. form.addEventListener( event, function()
  64. {
  65. form.classList.remove( 'is-dragover' );
  66. });
  67. });
  68. form.addEventListener( 'drop', function( e )
  69. {
  70. droppedFiles = e.dataTransfer.files; // the files that were dropped
  71. showFiles( droppedFiles );
  72. triggerFormSubmit();
  73. e.dataTransfer.files = [];
  74. });
  75. }
  76. // if the form was submitted
  77. $('#imgUpload').submit(function(e){
  78. // preventing the duplicate submissions if the current one is in progress
  79. if( form.classList.contains( 'is-uploading' ) ) return false;
  80. form.classList.add( 'is-uploading' );
  81. form.classList.remove( 'is-error' );
  82. var station = $('#pattern_station').text();
  83. var pattern = $('#pattern_ID').text();
  84. var curCount = $('#image_count').text();
  85. var language = $('#image_language option:selected').text();
  86. if( isAdvancedUpload ) // ajax file upload for modern browsers
  87. {
  88. e.preventDefault();
  89. // gathering the form data
  90. var ajaxData;
  91. if( droppedFiles )
  92. {
  93. ajaxData = new FormData();
  94. Array.prototype.forEach.call( droppedFiles, function( file )
  95. {
  96. ajaxData.append( input.getAttribute( 'name' ), file );
  97. });
  98. }
  99. else{
  100. ajaxData = new FormData( form );
  101. }
  102. // ajax request
  103. var ajax = new XMLHttpRequest();
  104. ajax.open( form.getAttribute( 'method' ), form.getAttribute( 'action' ), false );
  105. ajax.onload = function()
  106. {
  107. form.classList.remove( 'is-uploading' );
  108. if( ajax.status >= 200 && ajax.status < 400 )
  109. {
  110. listData($("#selectfolders").val())
  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. form.classList.remove( 'is-uploading' );
  129. form.classList.remove( 'is-error', 'is-success' );
  130. e.target.files = [];
  131. input.value = "";
  132. droppedFiles = false;
  133. }
  134. else // fallback Ajax solution upload for older browsers
  135. {
  136. var iframeName = 'uploadiframe' + new Date().getTime(),
  137. iframe = document.createElement( 'iframe' );
  138. $iframe = $( '<iframe name="' + iframeName + '" style="display: none;"></iframe>' );
  139. iframe.setAttribute( 'name', iframeName );
  140. iframe.style.display = 'none';
  141. document.body.appendChild( iframe );
  142. form.setAttribute( 'target', iframeName );
  143. iframe.addEventListener( 'load', function()
  144. {
  145. var data = JSON.parse( iframe.contentDocument.body.innerHTML );
  146. form.classList.remove( 'is-uploading' )
  147. form.classList.add( data.success == true ? 'is-success' : 'is-error' )
  148. form.removeAttribute( 'target' );
  149. if( !data.success ) errorMsg.textContent = data.error;
  150. iframe.parentNode.removeChild( iframe );
  151. });
  152. }
  153. });
  154. // restart the form if has a state of error/success
  155. Array.prototype.forEach.call( restart, function( entry )
  156. {
  157. entry.addEventListener( 'click', function( e )
  158. {
  159. e.preventDefault();
  160. form.classList.remove( 'is-error', 'is-success' );
  161. input.click();
  162. });
  163. });
  164. // Firefox focus bug fix for file input
  165. input.addEventListener( 'focus', function(){ input.classList.add( 'has-focus' ); });
  166. input.addEventListener( 'blur', function(){ input.classList.remove( 'has-focus' ); });
  167. });
  168. }
  169. $(document).ready(function() {
  170. imageInit();
  171. });