Upload.js 6.3 KB

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