/* * file-selector.js - Add support for drag and drop file selection, and paste from clipboard on supported browsers. * * Usage: * $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/ajax.js'; * $config['additional_javascript'][] = 'js/file-selector.js'; */ function init_file_selector(max_images) { // Temporarily block iOS if (/iPad|iPhone|iPod/.test(navigator.userAgent)) { return; } $(document).ready(function () { // add options panel item if (window.Options && Options.get_tab('general')) { Options.extend_tab('general', ''); $('#file-drag-drop>input').on('click', function() { if ($('#file-drag-drop>input').is(':checked')) { localStorage.file_dragdrop = 'true'; } else { localStorage.file_dragdrop = 'false'; } }); if (typeof localStorage.file_dragdrop === 'undefined') localStorage.file_dragdrop = 'true'; if (localStorage.file_dragdrop === 'true') $('#file-drag-drop>input').prop('checked', true); } }); // disabled by user, or incompatible browser. if (localStorage.file_dragdrop == 'false' || !(window.URL.createObjectURL && window.File)) return; // multipost not enabled if (typeof max_images == 'undefined') { var max_images = 1; } $(''+ '').prependTo('#upload td'); var files = []; $('#upload_file').remove(); // remove the original file selector $('.dropzone-wrap').css('user-select', 'none').show(); // let jquery add browser specific prefix function addFile(file) { if (files.length == max_images) return; files.push(file); addThumb(file); } function removeFile(file) { files.splice(files.indexOf(file), 1); } function getThumbElement(file) { return $('.tmb-container').filter(function(){return($(this).data('file-ref')==file);}); } function addThumb(file) { var fileName = (file.name.length < 24) ? file.name : file.name.substr(0, 22) + '…'; var fileType = file.type.split('/')[0]; var fileExt = file.type.split('/')[1]; var $container = $('
') .addClass('tmb-container') .data('file-ref', file) .append( $('
').addClass('remove-btn').html('✖'), $('
').addClass('file-tmb'), $('
').addClass('tmb-filename').html(fileName) ) .appendTo('.file-thumbs'); var $fileThumb = $container.find('.file-tmb'); if (fileType == 'image') { // if image file, generate thumbnail var objURL = window.URL.createObjectURL(file); $fileThumb.css('background-image', 'url('+ objURL +')'); } else { $fileThumb.html('' + fileExt.toUpperCase() + ''); } } $(document).on('ajax_before_post', function (e, formData) { for (var i=0; i 0) key += i + 1; if (typeof files[i] === 'undefined') break; formData.append(key, files[i]); } }); // clear file queue and UI on success $(document).on('ajax_after_post', function () { files = []; $('.file-thumbs').empty(); }); var dragCounter = 0; var dropHandlers = { dragenter: function (e) { e.stopPropagation(); e.preventDefault(); if (dragCounter === 0) $('.dropzone').addClass('dragover'); dragCounter++; }, dragover: function (e) { // needed for webkit to work e.stopPropagation(); e.preventDefault(); }, dragleave: function (e) { e.stopPropagation(); e.preventDefault(); dragCounter--; if (dragCounter === 0) $('.dropzone').removeClass('dragover'); }, drop: function (e) { e.stopPropagation(); e.preventDefault(); $('.dropzone').removeClass('dragover'); dragCounter = 0; var fileList = e.originalEvent.dataTransfer.files; for (var i=0; i'); $fileSelector.on('change', function (e) { if (this.files.length > 0) { for (var i=0; i