257 lines
8.0 KiB
JavaScript
257 lines
8.0 KiB
JavaScript
/* Redmine - project management software
|
|
Copyright (C) 2006-2013 Jean-Philippe Lang */
|
|
|
|
function postUpMsg(attachmentId)
|
|
{
|
|
$.ajax({
|
|
url: '/attachments/renderTag',
|
|
type: "GET",
|
|
data: {
|
|
attachmentId: attachmentId
|
|
}
|
|
|
|
})
|
|
}
|
|
|
|
function reload(fileSpan) {
|
|
fileSpan.remove();
|
|
$('#upload_file_count').html("未上传文件");
|
|
$old_file = $("#_file");
|
|
$new_file = $old_file.clone(true);
|
|
$old_file.replaceWith($new_file);
|
|
$new_file.show();
|
|
|
|
}
|
|
|
|
function addFile(inputEl, file, eagerUpload) {
|
|
|
|
if ($('#attachments_fields').children().length < 10) {
|
|
|
|
var attachmentId = addFile.nextAttachmentId++;
|
|
|
|
var fileSpan = $('<span>', { 'id': 'attachments_' + attachmentId, 'class':'attachment' });
|
|
|
|
fileSpan.append(
|
|
$('<input>', { 'type': 'text', 'class': 'filename readonly', 'name': 'attachments[' + attachmentId + '][filename]', 'readonly': 'readonly'} ).val(file.name),
|
|
$('<input>', { 'type': 'text', 'class': 'description', 'name': 'attachments[' + attachmentId + '][description]', 'maxlength': 254, 'placeholder': $(inputEl).data('descriptionPlaceholder') } ).toggle(!eagerUpload),
|
|
$('<span >'+$(inputEl).data('fieldIsPublic')+':</span>').attr({ 'class': 'ispublic-label' }) ,
|
|
$('<input>', { 'type': 'checkbox', 'class': 'is_public_checkbox','value':1, 'name': 'attachments[' + attachmentId + '][is_public_checkbox]', checked:'checked' } ).toggle(!eagerUpload),
|
|
$('<a> </a>').attr({ 'href': "#", 'class': 'remove-upload' }).click(function(){
|
|
if(confirm('您确定要删除吗?')){
|
|
removeFile();
|
|
if(!eagerUpload){
|
|
(function(e){
|
|
reload(e);
|
|
})(fileSpan);
|
|
}
|
|
}
|
|
|
|
}).toggle(!eagerUpload),
|
|
$('<div>', { 'class': 'div_attachments', 'name': 'div_'+'attachments_' + attachmentId} )
|
|
).appendTo('#attachments_fields');
|
|
|
|
if(eagerUpload) {
|
|
ajaxUpload(file, attachmentId, fileSpan, inputEl);
|
|
|
|
}
|
|
return attachmentId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
addFile.nextAttachmentId = 1;
|
|
|
|
function ajaxUpload(file, attachmentId, fileSpan, inputEl) {
|
|
|
|
function onLoadstart(e) {
|
|
fileSpan.removeClass('ajax-waiting');
|
|
fileSpan.addClass('ajax-loading');
|
|
$('input:submit', $(this).parents('form')).attr('disabled', 'disabled');
|
|
}
|
|
|
|
function onProgress(e) {
|
|
if(e.lengthComputable) {
|
|
this.progressbar( 'value', e.loaded * 100 / e.total );
|
|
}
|
|
}
|
|
|
|
function actualUpload(file, attachmentId, fileSpan, inputEl) {
|
|
|
|
ajaxUpload.uploading++;
|
|
|
|
uploadBlob(file, $(inputEl).data('upload-path'), attachmentId, {
|
|
loadstartEventHandler: onLoadstart.bind(progressSpan),
|
|
progressEventHandler: onProgress.bind(progressSpan)
|
|
})
|
|
.done(function(result) {
|
|
progressSpan.progressbar( 'value', 100 ).remove();
|
|
fileSpan.find('input.description, a').css('display', 'inline-block');
|
|
fileSpan.find('input.is_public_checkbox, a').css('display', 'inline-block');
|
|
})
|
|
.fail(function(result) {
|
|
progressSpan.text(result.statusText);
|
|
if($("#network_issue"))
|
|
{
|
|
$("#network_issue").show();
|
|
}
|
|
}).always(function() {
|
|
ajaxUpload.uploading--;
|
|
fileSpan.removeClass('ajax-loading');
|
|
var form = fileSpan.parents('form');
|
|
if (form.queue('upload').length == 0 && ajaxUpload.uploading == 0) {
|
|
$('input:submit', form).removeAttr('disabled');
|
|
}
|
|
form.dequeue('upload');
|
|
});
|
|
|
|
//gcm files count and add delete_all link
|
|
|
|
var count=$('#attachments_fields>span').length;
|
|
$('#upload_file_count').html("<span id=\"count\">"+count+"</span>"+"个文件已上传");
|
|
if(count>=1){
|
|
var add_attachs=$('.add_attachment');
|
|
var delete_all=$('.remove_all');
|
|
if(delete_all.length<1){
|
|
add_attachs.append($("<a> </a>").attr({"href":"javascript:void(0)", 'class': 'remove_all',"onclick": "removeAll()"}));
|
|
}
|
|
}
|
|
|
|
//gcm
|
|
|
|
}
|
|
|
|
var progressSpan = $('<div>').insertAfter(fileSpan.find('input.filename'));
|
|
progressSpan.progressbar();
|
|
fileSpan.addClass('ajax-waiting');
|
|
|
|
var maxSyncUpload = $(inputEl).data('max-concurrent-uploads');
|
|
|
|
if(maxSyncUpload == null || maxSyncUpload <= 0 || ajaxUpload.uploading < maxSyncUpload)
|
|
actualUpload(file, attachmentId, fileSpan, inputEl);
|
|
else
|
|
$(inputEl).parents('form').queue('upload', actualUpload.bind(this, file, attachmentId, fileSpan, inputEl));
|
|
}
|
|
|
|
ajaxUpload.uploading = 0;
|
|
|
|
function removeFile() {
|
|
$(this).parent('span').remove();
|
|
return false;
|
|
}
|
|
|
|
//gcm delete all file
|
|
function removeAll(){
|
|
if(confirm("您确定要删除所有文件吗")){
|
|
$(".remove-upload").removeAttr("data-confirm");
|
|
$(".remove-upload").click();
|
|
}
|
|
// return false;
|
|
}
|
|
//gcm
|
|
|
|
function uploadBlob(blob, uploadUrl, attachmentId, options) {
|
|
|
|
var actualOptions = $.extend({
|
|
loadstartEventHandler: $.noop,
|
|
progressEventHandler: $.noop
|
|
}, options);
|
|
if(uploadUrl.indexOf('?') > 0){
|
|
uploadUrl = uploadUrl + '&attachment_id=' + attachmentId;
|
|
}else{
|
|
uploadUrl = uploadUrl + '?attachment_id=' + attachmentId;
|
|
}
|
|
if (blob instanceof window.File) {
|
|
uploadUrl += '&filename=' + encodeURIComponent(blob.name);
|
|
}
|
|
|
|
return $.ajax(uploadUrl, {
|
|
type: 'POST',
|
|
contentType: 'application/octet-stream',
|
|
beforeSend: function(jqXhr) {
|
|
jqXhr.setRequestHeader('Accept', 'application/js');
|
|
},
|
|
xhr: function() {
|
|
var xhr = $.ajaxSettings.xhr();
|
|
xhr.upload.onloadstart = actualOptions.loadstartEventHandler;
|
|
xhr.upload.onprogress = actualOptions.progressEventHandler;
|
|
return xhr;
|
|
},
|
|
data: blob,
|
|
cache: false,
|
|
processData: false
|
|
});
|
|
}
|
|
|
|
function addInputFiles(inputEl) {
|
|
// var clearedFileInput = $(inputEl).clone().val('');
|
|
|
|
if (inputEl.files) {
|
|
// upload files using ajax
|
|
uploadAndAttachFiles(inputEl.files, inputEl);
|
|
// $(inputEl).remove();
|
|
} else {
|
|
// browser not supporting the file API, upload on form submission
|
|
var attachmentId;
|
|
var aFilename = inputEl.value.split(/\/|\\/);
|
|
attachmentId = addFile(inputEl, { name: aFilename[ aFilename.length - 1 ] }, false);
|
|
if (attachmentId) {
|
|
$(inputEl).attr({ name: 'attachments[' + attachmentId + '][file]', style: 'display:none;' }).appendTo('#attachments_' + attachmentId);
|
|
}
|
|
}
|
|
|
|
//clearedFileInput.insertAfter('#attachments_fields');
|
|
}
|
|
|
|
function uploadAndAttachFiles(files, inputEl) {
|
|
|
|
var maxFileSize = $(inputEl).data('max-file-size');
|
|
var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
|
|
|
|
var sizeExceeded = false;
|
|
$.each(files, function() {
|
|
if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {sizeExceeded=true;}
|
|
});
|
|
if (sizeExceeded) {
|
|
window.alert(maxFileSizeExceeded);
|
|
} else {
|
|
$.each(files, function() {addFile(inputEl, this, true);});
|
|
}
|
|
}
|
|
|
|
function handleFileDropEvent(e) {
|
|
|
|
$(this).removeClass('fileover');
|
|
blockEventPropagation(e);
|
|
|
|
if ($.inArray('Files', e.dataTransfer.types) > -1) {
|
|
uploadAndAttachFiles(e.dataTransfer.files, $('input:file.file_selector'));
|
|
}
|
|
}
|
|
|
|
function dragOverHandler(e) {
|
|
$(this).addClass('fileover');
|
|
blockEventPropagation(e);
|
|
}
|
|
|
|
function dragOutHandler(e) {
|
|
$(this).removeClass('fileover');
|
|
blockEventPropagation(e);
|
|
}
|
|
|
|
function setupFileDrop() {
|
|
if (window.File && window.FileList && window.ProgressEvent && window.FormData) {
|
|
|
|
$.event.fixHooks.drop = { props: [ 'dataTransfer' ] };
|
|
|
|
$('form div.box').has('input:file').each(function() {
|
|
$(this).on({
|
|
dragover: dragOverHandler,
|
|
dragleave: dragOutHandler,
|
|
drop: handleFileDropEvent
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
$(document).ready(setupFileDrop);
|