2011-11-02 13:31:05 -04:00
|
|
|
$(function(){
|
|
|
|
var dropbox = $('#dropbox'),
|
2011-12-30 17:15:26 -05:00
|
|
|
message = $('.message', dropbox),
|
|
|
|
button = $('#file-input');
|
2011-11-02 13:31:05 -04:00
|
|
|
|
|
|
|
dropbox.filedrop({
|
|
|
|
// The name of the $_FILES entry:
|
|
|
|
paramname:'image',
|
|
|
|
|
|
|
|
maxfiles: 5,
|
2011-11-02 15:23:38 -04:00
|
|
|
maxfilesize: 2,
|
2011-11-05 12:35:17 -04:00
|
|
|
url: '/upload/process/',
|
2011-11-02 13:31:05 -04:00
|
|
|
|
|
|
|
uploadFinished:function(i,file,response){
|
2011-11-02 16:35:25 -04:00
|
|
|
if(response.type == 'error') {
|
|
|
|
alert('There was an error: ' + response.status);
|
|
|
|
$.data(file).addClass('error');
|
|
|
|
$.data(file).find('.linkBox').remove();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$.data(file).addClass('done');
|
|
|
|
$.data(file).find('.linkBox').val(response.file);
|
2011-11-07 22:39:43 -05:00
|
|
|
$.data(file).find('.linkBox').click(function() {
|
|
|
|
this.focus();
|
|
|
|
this.select();
|
|
|
|
});
|
2011-11-02 16:35:25 -04:00
|
|
|
}
|
2011-11-02 13:31:05 -04:00
|
|
|
// response is the JSON object that post_file.php returns
|
|
|
|
},
|
|
|
|
|
|
|
|
error: function(err, file) {
|
|
|
|
switch(err) {
|
|
|
|
case 'BrowserNotSupported':
|
|
|
|
showMessage('Your browser does not support HTML5 file uploads!');
|
|
|
|
break;
|
|
|
|
case 'TooManyFiles':
|
|
|
|
alert('Too many files! Please select 5 at most! (configurable)');
|
|
|
|
break;
|
|
|
|
case 'FileTooLarge':
|
|
|
|
alert(file.name+' is too large! Please upload files up to 2mb (configurable).');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called before each upload is started
|
|
|
|
beforeEach: function(file){
|
|
|
|
if(!file.type.match(/^image\//)){
|
|
|
|
alert('Only images are allowed!');
|
|
|
|
|
|
|
|
// Returning false will cause the
|
|
|
|
// file to be rejected
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadStarted:function(i, file, len){
|
|
|
|
createImage(file);
|
|
|
|
},
|
2011-11-02 10:15:55 -04:00
|
|
|
|
2011-11-02 13:31:05 -04:00
|
|
|
progressUpdated: function(i, file, progress) {
|
|
|
|
$.data(file).find('.progress').width(progress);
|
2011-11-02 12:45:37 -04:00
|
|
|
}
|
2011-11-02 13:31:05 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2011-11-02 14:32:42 -04:00
|
|
|
var template = '<div class="preview">' +
|
|
|
|
'<span class="imageHolder">' +
|
|
|
|
'<img />' +
|
|
|
|
'<span class="uploaded"></span>' +
|
|
|
|
'</span>' +
|
|
|
|
'<div class="progressHolder">' +
|
|
|
|
'<div class="progress"></div>' +
|
|
|
|
'</div>' +
|
2011-11-02 14:41:40 -04:00
|
|
|
'<div class="linkHolder">' +
|
|
|
|
'<input type="text" class="linkBox" />' +
|
|
|
|
'</div>' +
|
2011-11-02 13:31:05 -04:00
|
|
|
'</div>';
|
|
|
|
|
|
|
|
|
|
|
|
function createImage(file){
|
2011-11-02 12:45:37 -04:00
|
|
|
|
2011-11-02 13:31:05 -04:00
|
|
|
var preview = $(template),
|
2011-11-02 14:41:40 -04:00
|
|
|
image = $('img', preview);
|
2011-11-02 13:31:05 -04:00
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
|
|
|
image.width = 100;
|
|
|
|
image.height = 100;
|
|
|
|
|
|
|
|
reader.onload = function(e){
|
|
|
|
|
|
|
|
// e.target.result holds the DataURL which
|
|
|
|
// can be used as a source of the image:
|
|
|
|
|
|
|
|
image.attr('src',e.target.result);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reading the file as a DataURL. When finished,
|
|
|
|
// this will trigger the onload function above:
|
2011-11-02 12:45:37 -04:00
|
|
|
reader.readAsDataURL(file);
|
2011-11-02 13:31:05 -04:00
|
|
|
|
|
|
|
message.hide();
|
|
|
|
preview.appendTo(dropbox);
|
|
|
|
|
|
|
|
// Associating a preview container
|
|
|
|
// with the file, using jQuery's $.data():
|
|
|
|
|
|
|
|
$.data(file,preview);
|
|
|
|
}
|
2011-11-02 12:45:37 -04:00
|
|
|
|
2011-11-02 13:31:05 -04:00
|
|
|
function showMessage(msg){
|
|
|
|
message.html(msg);
|
|
|
|
}
|
2011-11-02 10:15:55 -04:00
|
|
|
|
2011-12-30 17:15:26 -05:00
|
|
|
// Button upload support
|
|
|
|
button.change(function(){
|
|
|
|
var fileList = this.files;
|
|
|
|
|
|
|
|
if (fileList.length > 0) {
|
|
|
|
// Send this as if it were dropped
|
|
|
|
var e = $.Event("drop", {
|
|
|
|
dataTransfer: {
|
|
|
|
files: fileList,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dropbox.trigger(e);
|
|
|
|
}
|
|
|
|
});
|
2012-07-20 21:44:51 -04:00
|
|
|
|
|
|
|
// Clipboard paste support
|
|
|
|
$(document).bind("paste", function(e) {
|
|
|
|
if (e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.items) {
|
|
|
|
var items = e.originalEvent.clipboardData.items;
|
|
|
|
for (var i = 0; i < items.length; i++) {
|
|
|
|
var file = items[i];
|
|
|
|
if (file.type == "image/png") {
|
|
|
|
// Send this as if it were dropped
|
|
|
|
file = file.getAsFile();
|
|
|
|
file.name = "paste_" + Math.random().toString(16).slice(2) + ".png";
|
|
|
|
var e = $.Event("drop", {
|
|
|
|
dataTransfer: {
|
|
|
|
files: [file],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dropbox.trigger(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-12-30 17:15:26 -05:00
|
|
|
});
|