/* ------------------------------------------------------------------------------ * * # Bootstrap multiple file uploader * * Demo JS code for uploader_bootstrap.html page * * ---------------------------------------------------------------------------- */ // Setup module // ------------------------------ var FileUpload = function() { // // Setup module components // // Bootstrap file upload var _componentFileUpload = function() { if (!$().fileinput) { console.warn('Warning - fileinput.min.js is not loaded.'); return; } // // Define variables // // Modal template var modalTemplate = '\n'; // Buttons inside zoom modal var previewZoomButtonClasses = { toggleheader: 'btn btn-light btn-icon btn-header-toggle btn-sm', fullscreen: 'btn btn-light btn-icon btn-sm', borderless: 'btn btn-light btn-icon btn-sm', close: 'btn btn-light btn-icon btn-sm' }; // Icons inside zoom modal classes var previewZoomButtonIcons = { prev: '', next: '', toggleheader: '', fullscreen: '', borderless: '', close: '' }; // File actions var fileActionSettings = { zoomClass: '', zoomIcon: '', dragClass: 'p-2', dragIcon: '', removeClass: '', removeErrorClass: 'text-danger', removeIcon: '', indicatorNew: '', indicatorSuccess: '', indicatorError: '', indicatorLoading: '' }; // // Basic example // $('.file-input').fileinput({ browseLabel: 'Browse', browseIcon: '', uploadIcon: '', removeIcon: '', layoutTemplates: { icon: '', modal: modalTemplate }, initialCaption: "No file selected", previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons, fileActionSettings: fileActionSettings }); // // Custom layout // $('.file-input-custom').fileinput({ previewFileType: 'image', browseLabel: 'Select', browseClass: 'btn bg-slate-700', browseIcon: '', removeLabel: 'Remove', removeClass: 'btn btn-danger', removeIcon: '', uploadClass: 'btn bg-teal-400', uploadIcon: '', layoutTemplates: { icon: '', modal: modalTemplate }, initialCaption: "Please select image", mainClass: 'input-group', previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons, fileActionSettings: fileActionSettings }); // // Template modifications // $('.file-input-advanced').fileinput({ browseLabel: 'Browse', browseClass: 'btn btn-light', removeClass: 'btn btn-light', uploadClass: 'btn bg-success-400', browseIcon: '', uploadIcon: '', removeIcon: '', layoutTemplates: { icon: '', main1: "{preview}\n" + "
\n" + "
\n" + " {browse}\n" + "
\n" + " {caption}\n" + "
\n" + " {upload}\n" + " {remove}\n" + "
\n" + "
", modal: modalTemplate }, initialCaption: "No file selected", previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons, fileActionSettings: fileActionSettings }); // // Custom file extensions // $('.file-input-extensions').fileinput({ browseLabel: 'Browse', browseIcon: '', uploadIcon: '', removeIcon: '', layoutTemplates: { icon: '', modal: modalTemplate }, maxFilesNum: 10, allowedFileExtensions: ["jpg", "gif", "png", "txt"], initialCaption: "No file selected", previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons, fileActionSettings: fileActionSettings }); // // Always display preview // $('.file-input-preview').fileinput({ browseLabel: 'Browse', browseIcon: '', uploadIcon: '', removeIcon: '', layoutTemplates: { icon: '', modal: modalTemplate }, initialPreview: [ '../../../../global_assets/images/placeholders/placeholder.jpg', '../../../../global_assets/images/placeholders/placeholder.jpg', ], initialPreviewConfig: [ {caption: 'Jane.jpg', size: 930321, key: 1, url: '{$url}', showDrag: false}, {caption: 'Anna.jpg', size: 1218822, key: 2, url: '{$url}', showDrag: false} ], initialPreviewAsData: true, overwriteInitial: false, maxFileSize: 100, previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons, fileActionSettings: fileActionSettings }); // // Display preview on load // $('.file-input-overwrite').fileinput({ browseLabel: 'Browse', browseIcon: '', uploadIcon: '', removeIcon: '', layoutTemplates: { icon: '', modal: modalTemplate }, initialPreview: [ '../../../../global_assets/images/placeholders/placeholder.jpg', '../../../../global_assets/images/placeholders/placeholder.jpg' ], initialPreviewConfig: [ {caption: 'Jane.jpg', size: 930321, key: 1, url: '{$url}'}, {caption: 'Anna.jpg', size: 1218822, key: 2, url: '{$url}'} ], initialPreviewAsData: true, overwriteInitial: true, previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons, fileActionSettings: fileActionSettings }); // // AJAX upload // $('.file-input-ajax').fileinput({ browseLabel: 'Browse', uploadUrl: "http://localhost", // server upload action uploadAsync: true, maxFileCount: 5, initialPreview: [], browseIcon: '', uploadIcon: '', removeIcon: '', fileActionSettings: { removeIcon: '', uploadIcon: '', uploadClass: '', zoomIcon: '', zoomClass: '', indicatorNew: '', indicatorSuccess: '', indicatorError: '', indicatorLoading: '', }, layoutTemplates: { icon: '', modal: modalTemplate }, initialCaption: 'No file selected', previewZoomButtonClasses: previewZoomButtonClasses, previewZoomButtonIcons: previewZoomButtonIcons }); // // Misc // // Disable/enable button $('#btn-modify').on('click', function() { $btn = $(this); if ($btn.text() == 'Disable file input') { $('#file-input-methods').fileinput('disable'); $btn.html('Enable file input'); alert('Hurray! I have disabled the input and hidden the upload button.'); } else { $('#file-input-methods').fileinput('enable'); $btn.html('Disable file input'); alert('Hurray! I have reverted back the input to enabled with the upload button.'); } }); }; // // Return objects assigned to module // return { init: function() { _componentFileUpload(); } } }(); // Initialize module // ------------------------------ document.addEventListener('DOMContentLoaded', function() { FileUpload.init(); });