2015-03-15 15:18:58 +01:00
|
|
|
/*
|
|
|
|
* file-selector.js - Add support for drag and drop file selection, and paste from clipbboard on supported browsers.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/file-selector.js';
|
|
|
|
*/
|
2015-03-20 04:22:17 +01:00
|
|
|
function init_file_selector(max_images) {
|
2015-03-15 15:18:58 +01:00
|
|
|
|
2015-03-15 16:32:53 +01:00
|
|
|
$(document).ready(function () {
|
2015-03-18 14:35:58 +01:00
|
|
|
// add options panel item
|
|
|
|
if (window.Options && Options.get_tab('general')) {
|
|
|
|
Options.extend_tab('general', '<label id="file-drag-drop"><input type="checkbox">' + _('Drag and drop file selection') + '</label>');
|
|
|
|
|
|
|
|
$('#file-drag-drop>input').on('click', function() {
|
|
|
|
if ($('#file-drag-drop>input').is(':checked')) {
|
|
|
|
localStorage.file_dragdrop = 'true';
|
|
|
|
} else {
|
|
|
|
localStorage.file_dragdrop = 'false';
|
|
|
|
}
|
|
|
|
});
|
2015-03-15 15:18:58 +01:00
|
|
|
|
2015-03-20 04:22:17 +01:00
|
|
|
if (typeof localStorage.file_dragdrop === 'undefined') localStorage.file_dragdrop = 'true';
|
2015-03-18 14:35:58 +01:00
|
|
|
if (localStorage.file_dragdrop === 'true') $('#file-drag-drop>input').prop('checked', true);
|
|
|
|
}
|
|
|
|
});
|
2015-03-15 15:18:58 +01:00
|
|
|
|
|
|
|
// disabled by user, or incompatible browser.
|
2015-03-19 12:29:48 +01:00
|
|
|
if (localStorage.file_dragdrop == 'false' || !(window.URL.createObjectURL && window.File))
|
2015-03-15 15:18:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// multipost not enabled
|
|
|
|
if (typeof max_images == 'undefined') {
|
|
|
|
var max_images = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var files = [];
|
2015-03-18 14:35:58 +01:00
|
|
|
$('#upload_file').hide(); // hide the original file selector
|
|
|
|
$('.dropzone-wrap').css('user-select', 'none').show(); // let jquery add browser specific prefix
|
2015-03-15 15:18:58 +01:00
|
|
|
|
|
|
|
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];
|
2015-03-20 05:48:32 +01:00
|
|
|
var $container = $('<div>')
|
2015-03-15 15:18:58 +01:00
|
|
|
.addClass('tmb-container')
|
|
|
|
.data('file-ref', file)
|
|
|
|
.append(
|
|
|
|
$('<div>').addClass('remove-btn').html('✖'),
|
|
|
|
$('<div>').addClass('file-tmb'),
|
|
|
|
$('<div>').addClass('tmb-filename').html(fileName)
|
|
|
|
)
|
2015-03-20 05:48:32 +01:00
|
|
|
.appendTo('.file-thumbs');
|
2015-03-15 15:18:58 +01:00
|
|
|
|
2015-03-19 12:29:48 +01:00
|
|
|
var $fileThumb = $container.find('.file-tmb');
|
2015-03-15 15:18:58 +01:00
|
|
|
if (fileType == 'image') {
|
|
|
|
// if image file, generate thumbnail
|
2015-03-19 12:29:48 +01:00
|
|
|
var objURL = window.URL.createObjectURL(file);
|
|
|
|
$fileThumb.css('background-image', 'url('+ objURL +')');
|
2015-03-15 15:18:58 +01:00
|
|
|
} else {
|
|
|
|
$fileThumb.html('<span>' + fileExt.toUpperCase() + '</span>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).on('ajax_before_post', function (e, formData) {
|
2015-03-15 16:32:53 +01:00
|
|
|
for (var i=0; i<max_images; i++) {
|
2015-03-15 15:18:58 +01:00
|
|
|
var key = 'file';
|
|
|
|
if (i > 0) key += i + 1;
|
|
|
|
formData.append(key, files[i]);
|
|
|
|
}
|
2015-03-15 16:32:53 +01:00
|
|
|
});
|
2015-03-15 15:18:58 +01:00
|
|
|
|
2015-03-15 16:32:53 +01:00
|
|
|
// clear file queue and UI on success
|
|
|
|
$(document).on('ajax_after_post', function () {
|
2015-03-15 15:18:58 +01:00
|
|
|
files = [];
|
|
|
|
$('.file-thumbs').empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
var dragCounter = 0;
|
|
|
|
var dropHandlers = {
|
|
|
|
dragenter: function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (dragCounter === 0) $(this).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) $(this).removeClass('dragover');
|
|
|
|
},
|
|
|
|
drop: function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
$(this).removeClass('dragover');
|
|
|
|
dragCounter = 0;
|
|
|
|
|
|
|
|
var fileList = e.originalEvent.dataTransfer.files;
|
|
|
|
for (var i=0; i<fileList.length; i++) {
|
|
|
|
addFile(fileList[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// attach handlers
|
|
|
|
$(document).on(dropHandlers, '.dropzone');
|
|
|
|
|
|
|
|
$(document).on('click', '.dropzone .remove-btn', function (e) {
|
2015-03-20 01:53:57 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
|
2015-03-15 15:18:58 +01:00
|
|
|
var file = $(e.target).parent().data('file-ref');
|
|
|
|
|
2015-03-19 12:29:48 +01:00
|
|
|
getThumbElement(file).remove();
|
2015-03-15 15:18:58 +01:00
|
|
|
removeFile(file);
|
|
|
|
});
|
|
|
|
|
2015-03-22 15:15:43 +01:00
|
|
|
$(document).on('keypress click', '.dropzone', function (e) {
|
2015-03-20 01:53:57 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
|
2015-03-22 15:15:43 +01:00
|
|
|
// accept mouse click or Enter
|
|
|
|
if ((e.which != 1 || e.target.className != 'file-hint') &&
|
|
|
|
e.which != 13)
|
2015-03-20 01:53:57 +01:00
|
|
|
return;
|
|
|
|
|
2015-03-15 15:18:58 +01:00
|
|
|
var $fileSelector = $('<input type="file" multiple>');
|
|
|
|
|
|
|
|
$fileSelector.on('change', function (e) {
|
|
|
|
if (this.files.length > 0) {
|
|
|
|
for (var i=0; i<this.files.length; i++) {
|
|
|
|
addFile(this.files[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
$fileSelector.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('paste', function (e) {
|
|
|
|
var clipboard = e.originalEvent.clipboardData;
|
|
|
|
if (typeof clipboard.items != 'undefined' && clipboard.items.length != 0) {
|
|
|
|
|
|
|
|
//Webkit
|
|
|
|
for (var i=0; i<clipboard.items.length; i++) {
|
|
|
|
if (clipboard.items[i].kind != 'file')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//convert blob to file
|
|
|
|
var file = new File([clipboard.items[i].getAsFile()], 'ClipboardImage.png', {type: 'image/png'});
|
|
|
|
addFile(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-15 16:32:53 +01:00
|
|
|
}
|