2014-10-23 18:49:14 +08:00
/ * R e d m i n e - p r o j e c t m a n a g e m e n t s o f t w a r e
2015-05-15 10:07:52 +08:00
Copyright ( C ) 2006 - 2013 Jean - Philippe Lang * /
2014-10-23 18:49:14 +08:00
function postUpMsg ( attachmentId )
{
$ . ajax ( {
2015-05-15 10:07:52 +08:00
url : '/attachments/renderTag' ,
2014-10-23 18:49:14 +08:00
type : "GET" ,
data : {
attachmentId : attachmentId
}
} )
}
2015-03-23 18:06:53 +08:00
function reload ( fileSpan ) {
fileSpan . remove ( ) ;
2015-04-10 16:59:45 +08:00
$ ( '#upload_file_count' ) . html ( "<%= l(:label_no_file_uploaded)%>" ) ;
2015-03-23 18:06:53 +08:00
$old _file = $ ( "#_file" ) ;
$new _file = $old _file . clone ( true ) ;
$old _file . replaceWith ( $new _file ) ;
$new _file . show ( ) ;
}
2015-05-12 17:14:07 +08:00
function addFile _board ( inputEl , file , eagerUpload , id ) {
2014-10-23 18:49:14 +08:00
2015-05-15 10:07:52 +08:00
var attachments _frame = '#attachments_fields' + id ;
if ( $ ( attachments _frame ) . children ( ) . length < 30 ) {
deleteallfiles = $ ( inputEl ) . data ( 'deleteAllFiles' ) ;
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 ) ,
2015-05-15 11:45:15 +08:00
$ ( '<a> </a>' ) . attr ( { 'href' : "#" , 'class' : 'remove-upload' , 'data-containerid' : id } ) . click ( function ( ) {
2015-05-15 10:07:52 +08:00
if ( confirm ( $ ( inputEl ) . data ( 'areYouSure' ) ) ) {
removeFile ( ) ;
if ( ! eagerUpload ) {
( function ( e ) {
reload ( e ) ;
} ) ( fileSpan ) ;
}
2015-03-23 18:06:53 +08:00
}
2015-05-15 10:07:52 +08:00
} ) . toggle ( ! eagerUpload ) ,
$ ( '<div>' , { 'class' : 'div_attachments' , 'name' : 'div_' + 'attachments_' + attachmentId } )
) . appendTo ( attachments _frame ) ;
2014-10-23 18:49:14 +08:00
2015-05-15 10:07:52 +08:00
if ( eagerUpload ) {
ajaxUpload ( file , attachmentId , fileSpan , inputEl ) ;
2014-10-23 18:49:14 +08:00
2015-05-15 10:07:52 +08:00
}
return attachmentId ;
2014-10-23 18:49:14 +08:00
}
2015-05-15 10:07:52 +08:00
return null ;
2014-10-23 18:49:14 +08:00
}
2015-05-12 17:14:07 +08:00
function addFile ( inputEl , file , eagerUpload ) {
var attachments _frame = '#attachments_fields' ;
if ( $ ( attachments _frame ) . children ( ) . length < 30 ) {
deleteallfiles = $ ( inputEl ) . data ( 'deleteAllFiles' ) ;
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 ( $ ( inputEl ) . data ( 'areYouSure' ) ) ) {
removeFile ( ) ;
if ( ! eagerUpload ) {
( function ( e ) {
reload ( e ) ;
} ) ( fileSpan ) ;
}
}
2014-10-23 18:49:14 +08:00
2015-05-12 17:14:07 +08:00
} ) . toggle ( ! eagerUpload ) ,
$ ( '<div>' , { 'class' : 'div_attachments' , 'name' : 'div_' + 'attachments_' + attachmentId } )
) . appendTo ( '#attachments_fields' ) ;
if ( eagerUpload ) {
ajaxUpload ( file , attachmentId , fileSpan , inputEl ) ;
}
return attachmentId ;
}
return null ;
}
2014-10-23 18:49:14 +08:00
addFile . nextAttachmentId = 1 ;
function ajaxUpload ( file , attachmentId , fileSpan , inputEl ) {
2015-05-15 10:07:52 +08:00
function onLoadstart ( e ) {
fileSpan . removeClass ( 'ajax-waiting' ) ;
fileSpan . addClass ( 'ajax-loading' ) ;
$ ( 'input:submit' , $ ( this ) . parents ( 'form' ) ) . attr ( 'disabled' , 'disabled' ) ;
2014-10-23 18:49:14 +08:00
}
2015-05-15 10:07:52 +08:00
function onProgress ( e ) {
if ( e . lengthComputable ) {
this . progressbar ( 'value' , e . loaded * 100 / e . total ) ;
2014-10-23 18:49:14 +08:00
}
2015-05-15 10:07:52 +08:00
}
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
2015-05-15 11:45:15 +08:00
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ start
var containerid = $ ( inputEl ) . data ( 'containerid' ) ;
if ( containerid == undefined ) {
var count = $ ( '#attachments_fields>span' ) . length ;
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
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()" } ) ) ;
}
2015-05-15 10:07:52 +08:00
}
2015-05-15 11:45:15 +08:00
} else {
var count = $ ( '#attachments_fields' + containerid + '>span' ) . length ;
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
if ( count >= 1 ) {
var add _attachs = $ ( '.add_attachment' ) . filter ( function ( index ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
} ) ;
var delete _all = $ ( '.remove_all' ) . filter ( function ( index ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
} ) ;
if ( delete _all . length < 1 ) {
add _attachs . append ( $ ( "<a> </a>" ) . attr ( {
"href" : "javascript:void(0)" ,
'class' : 'remove_all' ,
'data-containerid' : containerid ,
"onclick" : "removeAll('" + containerid + "')"
} ) ) ;
}
}
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ end
2014-10-23 18:49:14 +08:00
}
2015-05-15 10:07:52 +08:00
//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 ) ) ;
2014-10-23 18:49:14 +08:00
}
ajaxUpload . uploading = 0 ;
function removeFile ( ) {
2015-05-15 10:07:52 +08:00
$ ( this ) . parent ( 'span' ) . remove ( ) ;
return false ;
2014-10-23 18:49:14 +08:00
}
//gcm delete all file
2015-05-15 11:45:15 +08:00
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ start
function removeAll ( containerid ) {
2015-05-15 10:07:52 +08:00
if ( confirm ( deleteallfiles ) ) {
2015-05-15 11:45:15 +08:00
if ( containerid == undefined ) {
$ ( ".remove-upload" ) . removeAttr ( "data-confirm" ) ;
$ ( ".remove-upload" ) . click ( ) ;
} else {
var arr = $ ( ".remove-upload" ) . filter ( function ( ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
} ) ;
arr . removeAttr ( "data-confirm" ) ;
arr . click ( ) ;
}
2015-05-15 10:07:52 +08:00
}
2014-10-23 18:49:14 +08:00
// return false;
}
2015-05-15 11:45:15 +08:00
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ end
2014-10-23 18:49:14 +08:00
//gcm
function uploadBlob ( blob , uploadUrl , attachmentId , options ) {
2015-05-15 10:07:52 +08:00
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
} ) ;
2014-10-23 18:49:14 +08:00
}
function addInputFiles ( inputEl ) {
2015-05-15 10:07:52 +08:00
// var clearedFileInput = $(inputEl).clone().val('');
2014-10-23 18:49:14 +08:00
if ( inputEl . files ) {
// upload files using ajax
uploadAndAttachFiles ( inputEl . files , inputEl ) ;
2015-05-15 10:07:52 +08:00
// $(inputEl).remove();
2014-10-23 18:49:14 +08:00
} 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 ) {
2015-04-08 13:52:52 +08:00
$ ( inputEl ) . attr ( { name : 'attachments[' + attachmentId + '][file]' } ) . hide ( ) ;
2015-04-10 17:58:59 +08:00
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
2014-10-23 18:49:14 +08:00
}
}
//clearedFileInput.insertAfter('#attachments_fields');
}
2015-05-12 17:14:07 +08:00
function addInputFiles _board ( inputEl , id ) {
// var clearedFileInput = $(inputEl).clone().val('');
if ( inputEl . files ) {
// upload files using ajax
uploadAndAttachFiles _board ( inputEl . files , inputEl , id ) ;
// $(inputEl).remove();
} else {
// browser not supporting the file API, upload on form submission
var attachmentId ;
var aFilename = inputEl . value . split ( /\/|\\/ ) ;
attachmentId = addFile _board ( inputEl , { name : aFilename [ aFilename . length - 1 ] } , false , id ) ;
if ( attachmentId ) {
$ ( inputEl ) . attr ( { name : 'attachments[' + attachmentId + '][file]' } ) . hide ( ) ;
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
}
}
2014-10-23 18:49:14 +08:00
2015-05-12 17:14:07 +08:00
//clearedFileInput.insertAfter('#attachments_fields');
}
2014-10-23 18:49:14 +08:00
function uploadAndAttachFiles ( files , inputEl ) {
2015-05-15 10:07:52 +08:00
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 ) ; } ) ;
}
2014-10-23 18:49:14 +08:00
}
2015-05-12 17:14:07 +08:00
function uploadAndAttachFiles _board ( files , inputEl , id ) {
2014-10-23 18:49:14 +08:00
2015-05-12 17:14:07 +08:00
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 _board ( inputEl , this , true , id ) ; } ) ;
}
}
2014-10-23 18:49:14 +08:00
function handleFileDropEvent ( e ) {
2015-05-15 10:07:52 +08:00
$ ( this ) . removeClass ( 'fileover' ) ;
blockEventPropagation ( e ) ;
2014-10-23 18:49:14 +08:00
2015-05-15 10:07:52 +08:00
if ( $ . inArray ( 'Files' , e . dataTransfer . types ) > - 1 ) {
uploadAndAttachFiles ( e . dataTransfer . files , $ ( 'input:file.file_selector' ) ) ;
}
2014-10-23 18:49:14 +08:00
}
function dragOverHandler ( e ) {
2015-05-15 10:07:52 +08:00
$ ( this ) . addClass ( 'fileover' ) ;
blockEventPropagation ( e ) ;
2014-10-23 18:49:14 +08:00
}
function dragOutHandler ( e ) {
2015-05-15 10:07:52 +08:00
$ ( this ) . removeClass ( 'fileover' ) ;
blockEventPropagation ( e ) ;
2014-10-23 18:49:14 +08:00
}
function setupFileDrop ( ) {
2015-05-15 10:07:52 +08:00
if ( window . File && window . FileList && window . ProgressEvent && window . FormData ) {
2014-10-23 18:49:14 +08:00
2015-05-15 10:07:52 +08:00
$ . event . fixHooks . drop = { props : [ 'dataTransfer' ] } ;
2014-10-23 18:49:14 +08:00
2015-05-15 10:07:52 +08:00
$ ( 'form div.box' ) . has ( 'input:file' ) . each ( function ( ) {
$ ( this ) . on ( {
dragover : dragOverHandler ,
dragleave : dragOutHandler ,
drop : handleFileDropEvent
} ) ;
} ) ;
}
2014-10-23 18:49:14 +08:00
}
$ ( document ) . ready ( setupFileDrop ) ;
2015-05-15 11:45:15 +08:00
$ ( function ( ) {
$ ( ".file_selector" ) . each ( function ( ) {
deleteallfiles = $ ( this ) . data ( 'deleteAllFiles' ) ;
var containerid = $ ( this ) . data ( 'containerid' ) ;
if ( containerid == undefined ) containerid = '' ;
var count = $ ( '#attachments_fields' + containerid + '>span' ) . length ;
if ( count >= 1 ) {
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( this ) . data ( 'fileCount' ) ) ;
var add _attachs = $ ( '.add_attachment' ) . filter ( function ( index ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
} ) ;
var delete _all = $ ( '.remove_all' ) . filter ( function ( index ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
} ) ;
if ( delete _all . length < 1 ) {
add _attachs . append ( $ ( "<a> </a>" ) . attr ( {
"href" : "javascript:void(0)" ,
'class' : 'remove_all' ,
'data-containerid' : containerid ,
"onclick" : "removeAll('" + containerid + "')"
} ) ) ;
}
}
} ) ;
} ) ;