uploader_bootstrap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Bootstrap multiple file uploader
  4. *
  5. * Demo JS code for uploader_bootstrap.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var FileUpload = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Bootstrap file upload
  15. var _componentFileUpload = function() {
  16. if (!$().fileinput) {
  17. console.warn('Warning - fileinput.min.js is not loaded.');
  18. return;
  19. }
  20. //
  21. // Define variables
  22. //
  23. // Modal template
  24. var modalTemplate = '<div class="modal-dialog modal-lg" role="document">\n' +
  25. ' <div class="modal-content">\n' +
  26. ' <div class="modal-header align-items-center">\n' +
  27. ' <h6 class="modal-title">{heading} <small><span class="kv-zoom-title"></span></small></h6>\n' +
  28. ' <div class="kv-zoom-actions btn-group">{toggleheader}{fullscreen}{borderless}{close}</div>\n' +
  29. ' </div>\n' +
  30. ' <div class="modal-body">\n' +
  31. ' <div class="floating-buttons btn-group"></div>\n' +
  32. ' <div class="kv-zoom-body file-zoom-content"></div>\n' + '{prev} {next}\n' +
  33. ' </div>\n' +
  34. ' </div>\n' +
  35. '</div>\n';
  36. // Buttons inside zoom modal
  37. var previewZoomButtonClasses = {
  38. toggleheader: 'btn btn-light btn-icon btn-header-toggle btn-sm',
  39. fullscreen: 'btn btn-light btn-icon btn-sm',
  40. borderless: 'btn btn-light btn-icon btn-sm',
  41. close: 'btn btn-light btn-icon btn-sm'
  42. };
  43. // Icons inside zoom modal classes
  44. var previewZoomButtonIcons = {
  45. prev: '<i class="icon-arrow-left32"></i>',
  46. next: '<i class="icon-arrow-right32"></i>',
  47. toggleheader: '<i class="icon-menu-open"></i>',
  48. fullscreen: '<i class="icon-screen-full"></i>',
  49. borderless: '<i class="icon-alignment-unalign"></i>',
  50. close: '<i class="icon-cross2 font-size-base"></i>'
  51. };
  52. // File actions
  53. var fileActionSettings = {
  54. zoomClass: '',
  55. zoomIcon: '<i class="icon-zoomin3"></i>',
  56. dragClass: 'p-2',
  57. dragIcon: '<i class="icon-three-bars"></i>',
  58. removeClass: '',
  59. removeErrorClass: 'text-danger',
  60. removeIcon: '<i class="icon-bin"></i>',
  61. indicatorNew: '<i class="icon-file-plus text-success"></i>',
  62. indicatorSuccess: '<i class="icon-checkmark3 file-icon-large text-success"></i>',
  63. indicatorError: '<i class="icon-cross2 text-danger"></i>',
  64. indicatorLoading: '<i class="icon-spinner2 spinner text-muted"></i>'
  65. };
  66. //
  67. // Basic example
  68. //
  69. $('.file-input').fileinput({
  70. browseLabel: 'Browse',
  71. browseIcon: '<i class="icon-file-plus mr-2"></i>',
  72. uploadIcon: '<i class="icon-file-upload2 mr-2"></i>',
  73. removeIcon: '<i class="icon-cross2 font-size-base mr-2"></i>',
  74. layoutTemplates: {
  75. icon: '<i class="icon-file-check"></i>',
  76. modal: modalTemplate
  77. },
  78. initialCaption: "No file selected",
  79. previewZoomButtonClasses: previewZoomButtonClasses,
  80. previewZoomButtonIcons: previewZoomButtonIcons,
  81. fileActionSettings: fileActionSettings
  82. });
  83. //
  84. // Custom layout
  85. //
  86. $('.file-input-custom').fileinput({
  87. previewFileType: 'image',
  88. browseLabel: 'Select',
  89. browseClass: 'btn bg-slate-700',
  90. browseIcon: '<i class="icon-image2 mr-2"></i>',
  91. removeLabel: 'Remove',
  92. removeClass: 'btn btn-danger',
  93. removeIcon: '<i class="icon-cancel-square mr-2"></i>',
  94. uploadClass: 'btn bg-teal-400',
  95. uploadIcon: '<i class="icon-file-upload mr-2"></i>',
  96. layoutTemplates: {
  97. icon: '<i class="icon-file-check"></i>',
  98. modal: modalTemplate
  99. },
  100. initialCaption: "Please select image",
  101. mainClass: 'input-group',
  102. previewZoomButtonClasses: previewZoomButtonClasses,
  103. previewZoomButtonIcons: previewZoomButtonIcons,
  104. fileActionSettings: fileActionSettings
  105. });
  106. //
  107. // Template modifications
  108. //
  109. $('.file-input-advanced').fileinput({
  110. browseLabel: 'Browse',
  111. browseClass: 'btn btn-light',
  112. removeClass: 'btn btn-light',
  113. uploadClass: 'btn bg-success-400',
  114. browseIcon: '<i class="icon-file-plus mr-2"></i>',
  115. uploadIcon: '<i class="icon-file-upload2 mr-2"></i>',
  116. removeIcon: '<i class="icon-cross2 font-size-base mr-2"></i>',
  117. layoutTemplates: {
  118. icon: '<i class="icon-file-check"></i>',
  119. main1: "{preview}\n" +
  120. "<div class='input-group {class}'>\n" +
  121. " <div class='input-group-prepend'>\n" +
  122. " {browse}\n" +
  123. " </div>\n" +
  124. " {caption}\n" +
  125. " <div class='input-group-append'>\n" +
  126. " {upload}\n" +
  127. " {remove}\n" +
  128. " </div>\n" +
  129. "</div>",
  130. modal: modalTemplate
  131. },
  132. initialCaption: "No file selected",
  133. previewZoomButtonClasses: previewZoomButtonClasses,
  134. previewZoomButtonIcons: previewZoomButtonIcons,
  135. fileActionSettings: fileActionSettings
  136. });
  137. //
  138. // Custom file extensions
  139. //
  140. $('.file-input-extensions').fileinput({
  141. browseLabel: 'Browse',
  142. browseIcon: '<i class="icon-file-plus mr-2"></i>',
  143. uploadIcon: '<i class="icon-file-upload2 mr-2"></i>',
  144. removeIcon: '<i class="icon-cross2 font-size-base mr-2"></i>',
  145. layoutTemplates: {
  146. icon: '<i class="icon-file-check"></i>',
  147. modal: modalTemplate
  148. },
  149. maxFilesNum: 10,
  150. allowedFileExtensions: ["jpg", "gif", "png", "txt"],
  151. initialCaption: "No file selected",
  152. previewZoomButtonClasses: previewZoomButtonClasses,
  153. previewZoomButtonIcons: previewZoomButtonIcons,
  154. fileActionSettings: fileActionSettings
  155. });
  156. //
  157. // Always display preview
  158. //
  159. $('.file-input-preview').fileinput({
  160. browseLabel: 'Browse',
  161. browseIcon: '<i class="icon-file-plus mr-2"></i>',
  162. uploadIcon: '<i class="icon-file-upload2 mr-2"></i>',
  163. removeIcon: '<i class="icon-cross2 font-size-base mr-2"></i>',
  164. layoutTemplates: {
  165. icon: '<i class="icon-file-check"></i>',
  166. modal: modalTemplate
  167. },
  168. initialPreview: [
  169. '../../../../global_assets/images/placeholders/placeholder.jpg',
  170. '../../../../global_assets/images/placeholders/placeholder.jpg',
  171. ],
  172. initialPreviewConfig: [
  173. {caption: 'Jane.jpg', size: 930321, key: 1, url: '{$url}', showDrag: false},
  174. {caption: 'Anna.jpg', size: 1218822, key: 2, url: '{$url}', showDrag: false}
  175. ],
  176. initialPreviewAsData: true,
  177. overwriteInitial: false,
  178. maxFileSize: 100,
  179. previewZoomButtonClasses: previewZoomButtonClasses,
  180. previewZoomButtonIcons: previewZoomButtonIcons,
  181. fileActionSettings: fileActionSettings
  182. });
  183. //
  184. // Display preview on load
  185. //
  186. $('.file-input-overwrite').fileinput({
  187. browseLabel: 'Browse',
  188. browseIcon: '<i class="icon-file-plus mr-2"></i>',
  189. uploadIcon: '<i class="icon-file-upload2 mr-2"></i>',
  190. removeIcon: '<i class="icon-cross2 font-size-base mr-2"></i>',
  191. layoutTemplates: {
  192. icon: '<i class="icon-file-check"></i>',
  193. modal: modalTemplate
  194. },
  195. initialPreview: [
  196. '../../../../global_assets/images/placeholders/placeholder.jpg',
  197. '../../../../global_assets/images/placeholders/placeholder.jpg'
  198. ],
  199. initialPreviewConfig: [
  200. {caption: 'Jane.jpg', size: 930321, key: 1, url: '{$url}'},
  201. {caption: 'Anna.jpg', size: 1218822, key: 2, url: '{$url}'}
  202. ],
  203. initialPreviewAsData: true,
  204. overwriteInitial: true,
  205. previewZoomButtonClasses: previewZoomButtonClasses,
  206. previewZoomButtonIcons: previewZoomButtonIcons,
  207. fileActionSettings: fileActionSettings
  208. });
  209. //
  210. // AJAX upload
  211. //
  212. $('.file-input-ajax').fileinput({
  213. browseLabel: 'Browse',
  214. uploadUrl: "http://localhost", // server upload action
  215. uploadAsync: true,
  216. maxFileCount: 5,
  217. initialPreview: [],
  218. browseIcon: '<i class="icon-file-plus mr-2"></i>',
  219. uploadIcon: '<i class="icon-file-upload2 mr-2"></i>',
  220. removeIcon: '<i class="icon-cross2 font-size-base mr-2"></i>',
  221. fileActionSettings: {
  222. removeIcon: '<i class="icon-bin"></i>',
  223. uploadIcon: '<i class="icon-upload"></i>',
  224. uploadClass: '',
  225. zoomIcon: '<i class="icon-zoomin3"></i>',
  226. zoomClass: '',
  227. indicatorNew: '<i class="icon-file-plus text-success"></i>',
  228. indicatorSuccess: '<i class="icon-checkmark3 file-icon-large text-success"></i>',
  229. indicatorError: '<i class="icon-cross2 text-danger"></i>',
  230. indicatorLoading: '<i class="icon-spinner2 spinner text-muted"></i>',
  231. },
  232. layoutTemplates: {
  233. icon: '<i class="icon-file-check"></i>',
  234. modal: modalTemplate
  235. },
  236. initialCaption: 'No file selected',
  237. previewZoomButtonClasses: previewZoomButtonClasses,
  238. previewZoomButtonIcons: previewZoomButtonIcons
  239. });
  240. //
  241. // Misc
  242. //
  243. // Disable/enable button
  244. $('#btn-modify').on('click', function() {
  245. $btn = $(this);
  246. if ($btn.text() == 'Disable file input') {
  247. $('#file-input-methods').fileinput('disable');
  248. $btn.html('Enable file input');
  249. alert('Hurray! I have disabled the input and hidden the upload button.');
  250. }
  251. else {
  252. $('#file-input-methods').fileinput('enable');
  253. $btn.html('Disable file input');
  254. alert('Hurray! I have reverted back the input to enabled with the upload button.');
  255. }
  256. });
  257. };
  258. //
  259. // Return objects assigned to module
  260. //
  261. return {
  262. init: function() {
  263. _componentFileUpload();
  264. }
  265. }
  266. }();
  267. // Initialize module
  268. // ------------------------------
  269. document.addEventListener('DOMContentLoaded', function() {
  270. FileUpload.init();
  271. });