imageuploadify.min.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ;
  2. (function($, window, document, undefined) {
  3. window.addEventListener("dragover", function(e) {
  4. e = e || event;
  5. e.preventDefault();
  6. }, false);
  7. window.addEventListener("drop", function(e) {
  8. e = e || event;
  9. e.preventDefault();
  10. }, false);
  11. const compareMimeType = (mimeTypes, fileType, formatFile) => {
  12. if (mimeTypes.length < 2 && mimeTypes[0] === "*") {
  13. return true;
  14. }
  15. for (let index = 1; index < mimeTypes.length; index += 3) {
  16. if (mimeTypes[index + 1] === "*" && fileType.search(new RegExp(mimeTypes[index])) != -1) {
  17. return true;
  18. } else if (mimeTypes[index + 1] && mimeTypes[index + 1] != "*" && fileType.search(new RegExp("\\*" + mimeTypes[index + 1] + "\\*")) != -1) {
  19. return true;
  20. } else if (mimeTypes[index + 1] && mimeTypes[index + 1] != "*" && fileType.search(new RegExp(mimeTypes[index + 1])) != -1) {
  21. return true;
  22. } else if (mimeTypes[index + 1] === "" && (fileType.search(new RegExp(mimeTypes[index])) != -1 || formatFile.search(new RegExp(mimeTypes[index])) != -1)) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. $.fn.imageuploadify = function(opts) {
  29. const settings = $.extend({}, $.fn.imageuploadify.defaults, opts);
  30. this.each(function() {
  31. const self = this;
  32. if (!$(self).attr("multiple")) {
  33. return;
  34. }
  35. let accept = $(self).attr("accept") ? $(self).attr("accept").replace(/\s/g, "").split(",") : null;
  36. let result = [];
  37. accept.forEach((item) => {
  38. let regexp;
  39. if (item.search(/\//) != -1) {
  40. regexp = new RegExp("([A-Za-z-.]*)\/([A-Za-z-*.]*)", "g");
  41. } else {
  42. regexp = new RegExp("\.([A-Za-z-]*)()", "g");
  43. }
  44. const r = regexp.exec(item);
  45. result = result.concat(r);
  46. });
  47. let totalFiles = [];
  48. let counter = 0;
  49. let dragbox = $(`<div class="imageuploadify well"><div class="imageuploadify-overlay"><i class="fa fa-picture-o"></i></div><div class="imageuploadify-images-list text-center"><i class="bx bxs-cloud-upload"></i><span class='imageuploadify-message'>Drag&Drop Your File(s)Here To Upload</span><button type="button"class="btn btn-default">or select file to upload</button></div></div>`);
  50. let overlay = dragbox.find(".imageuploadify-overlay");
  51. let uploadIcon = dragbox.find(".imageuploadify-overlay i");
  52. let imagesList = dragbox.find(".imageuploadify-images-list");
  53. let addIcon = dragbox.find(".imageuploadify-images-list i");
  54. let addMsg = dragbox.find(".imageuploadify-images-list span");
  55. let button = dragbox.find(".imageuploadify-images-list button");
  56. const retrieveFiles = (files) => {
  57. for (let index = 0; index < files.length; ++index) {
  58. if (!accept || compareMimeType(result, files[index].type, /(?:\.([^.]+))?$/.exec(files[index].name)[1])) {
  59. const id = Math.random().toString(36).substr(2, 9);
  60. readingFile(id, files[index]);
  61. totalFiles.push({
  62. id: id,
  63. file: files[index]
  64. });
  65. }
  66. }
  67. }
  68. const readingFile = (id, file) => {
  69. const fReader = new FileReader();
  70. const width = dragbox.width();
  71. const boxesNb = Math.floor(width / 100);
  72. const marginSize = Math.floor((width - (boxesNb * 100)) / (boxesNb + 1));
  73. let container = $(`<div class='imageuploadify-container'><button type='button'class='btn btn-danger bx bx-x'></button><div class='imageuploadify-details'><span>${file.name}</span><span>${file.type}</span><span>${file.size}</span></div></div>`);
  74. let details = container.find(".imageuploadify-details");
  75. let deleteBtn = container.find("button");
  76. container.css("margin-left", marginSize + "px");
  77. details.hover(function() {
  78. $(this).css("opacity", "1");
  79. }).mouseleave(function() {
  80. $(this).css("opacity", "0");
  81. });
  82. if (file.type && file.type.search(/image/) != -1) {
  83. fReader.onloadend = function(e) {
  84. let image = $("<img>");
  85. image.attr("src", e.target.result);
  86. container.append(image);
  87. imagesList.append(container);
  88. imagesList.find(".imageuploadify-container:nth-child(" + boxesNb + "n+4)").css("margin-left", marginSize + "px");
  89. imagesList.find(".imageuploadify-container:nth-child(" + boxesNb + "n+3)").css("margin-right", marginSize + "px");
  90. };
  91. } else if (file.type) {
  92. let type = "<i class='fa fa-file'></i>";
  93. if (file.type.search(/audio/) != -1) {
  94. type = "<i class='fa fa-file-audio-o'></i>";
  95. } else if (file.type.search(/video/) != -1) {
  96. type = "<i class='fa fa-file-video-o'></i>";
  97. }
  98. fReader.onloadend = function(e) {
  99. let span = $("<span>" + type + "</span>");
  100. span.css("font-size", "5em");
  101. container.append(span);
  102. imagesList.append(container);
  103. imagesList.find(".imageuploadify-container:nth-child(" + boxesNb + "n+4)").css("margin-left", marginSize + "px");
  104. imagesList.find(".imageuploadify-container:nth-child(" + boxesNb + "n+3)").css("margin-right", marginSize + "px");
  105. };
  106. }
  107. deleteBtn.on("click", function() {
  108. $(this.parentElement).remove();
  109. for (let index = 0; totalFiles.length > index; ++index) {
  110. if (totalFiles[index].id === id) {
  111. totalFiles.splice(index, 1);
  112. break;
  113. }
  114. }
  115. });
  116. fReader.readAsDataURL(file);
  117. };
  118. const disableMouseEvents = () => {
  119. overlay.css("display", "flex");
  120. dragbox.css("border-color", "#3AA0FF");
  121. button.css("pointer-events", "none");
  122. addMsg.css("pointer-events", "none");
  123. addIcon.css("pointer-events", "none");
  124. imagesList.css("pointer-events", "none");
  125. }
  126. const enableMouseEvents = () => {
  127. overlay.css("display", "none");
  128. dragbox.css("border-color", "rgb(210, 210, 210)");
  129. button.css("pointer-events", "initial");
  130. addMsg.css("pointer-events", "initial");
  131. addIcon.css("pointer-events", "initial");
  132. imagesList.css("pointer-events", "initial");
  133. }
  134. button.mouseenter(function onMouseEnter(event) {
  135. button.css("background", "#3AA0FF").css("color", "white");
  136. }).mouseleave(function onMouseLeave() {
  137. button.css("background", "white").css("color", "#3AA0FF");
  138. });
  139. button.on("click", function onClick(event) {
  140. event.stopPropagation();
  141. event.preventDefault();
  142. $(self).click();
  143. });
  144. dragbox.on("dragenter", function onDragenter(event) {
  145. event.stopPropagation();
  146. event.preventDefault();
  147. counter++;
  148. disableMouseEvents();
  149. });
  150. dragbox.on("dragleave", function onDragLeave(event) {
  151. event.stopPropagation();
  152. event.preventDefault();
  153. counter--;
  154. if (counter === 0) {
  155. enableMouseEvents();
  156. }
  157. });
  158. dragbox.on("drop", function onDrop(event) {
  159. event.stopPropagation();
  160. event.preventDefault();
  161. enableMouseEvents();
  162. const files = event.originalEvent.dataTransfer.files;
  163. retrieveFiles(files);
  164. });
  165. $(window).bind("resize", function(e) {
  166. window.resizeEvt;
  167. $(window).resize(function() {
  168. clearTimeout(window.resizeEvt);
  169. window.resizeEvt = setTimeout(function() {
  170. const width = dragbox.width();
  171. const boxesNb = Math.floor(width / 100);
  172. const marginSize = Math.floor((width - (boxesNb * 100)) / (boxesNb + 1));
  173. let containers = imagesList.find(".imageuploadify-container");
  174. for (let index = 0; index < containers.length; ++index) {
  175. $(containers[index]).css("margin-right", "0px");
  176. $(containers[index]).css("margin-left", marginSize + "px");
  177. }
  178. imagesList.find(".imageuploadify-container:nth-child(" + boxesNb + "n+4)").css("margin-left", marginSize + "px");
  179. imagesList.find(".imageuploadify-container:nth-child(" + boxesNb + "n+3)").css("margin-right", marginSize + "px");
  180. }, 500);
  181. });
  182. })
  183. $(self).on("change", function onChange() {
  184. const files = this.files;
  185. retrieveFiles(files);
  186. });
  187. $(self).closest("form").on("submit", function(event) {
  188. event.stopPropagation();
  189. event.preventDefault(event);
  190. const inputs = this.querySelectorAll("input, textarea, select, button");
  191. const formData = new FormData();
  192. for (let index = 0; index < inputs.length; ++index) {
  193. if (inputs[index].tagName === "SELECT" && inputs[index].hasAttribute("multiple")) {
  194. const options = inputs[index].options;
  195. for (let i = 0; options.length > i; ++i) {
  196. if (options[i].selected) {
  197. formData.append(inputs[index].getAttribute("name"), options[i].value);
  198. }
  199. }
  200. } else if (!inputs[index].getAttribute("type") || ((inputs[index].getAttribute("type").toLowerCase()) !== "checkbox" && (inputs[index].getAttribute("type").toLowerCase()) !== "radio") || inputs[index].checked) {
  201. formData.append(inputs[index].name, inputs[index].value);
  202. } else if ($(inputs[index]).getAttribute("type")  != "file") {
  203. formData.append(inputs[index].name, inputs[index].value);
  204. }
  205. }
  206. for (var i = 0; i < totalFiles.length; i++) {
  207. formData.append(self.name, totalFiles[i].file);
  208. }
  209. var xhr = new XMLHttpRequest();
  210. xhr.onreadystatechange = function(e) {
  211. if (xhr.status == 200 && xhr.readyState === XMLHttpRequest.DONE) {
  212. window.location.replace(xhr.responseURL);
  213. }
  214. }
  215. xhr.open("POST", $(this).attr("action"), true);
  216. xhr.send(formData);
  217. return false;
  218. });
  219. $(self).hide();
  220. dragbox.insertAfter(this);
  221. });
  222. return this;
  223. };
  224. $.fn.imageuploadify.defaults = {};
  225. }(jQuery, window, document));