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
2015-06-16 21:13:40 +08:00
function postUpMsg ( attachmentId ) {
2014-10-23 18:49:14 +08:00
$ . 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-11-11 16:02:37 +08:00
function addFile _board ( inputEl , file , eagerUpload , id , btnId ) {
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 ++ ;
2015-06-16 21:13:40 +08:00
var fileSpan = $ ( '<span>' , {
'id' : 'attachments_' + attachmentId ,
'class' : 'attachment'
} ) ;
2015-05-15 10:07:52 +08:00
fileSpan . append (
2015-06-16 21:13:40 +08:00
$ ( '<input>' , {
'type' : 'text' ,
2015-11-03 10:28:13 +08:00
'class' : 'upload_filename readonly' ,
2015-06-16 21:13:40 +08:00
'name' : 'attachments[' + attachmentId + '][filename]' ,
'readonly' : 'readonly'
} ) . val ( file . name ) ,
2015-11-03 10:28:13 +08:00
// $('<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-06-16 21:13:40 +08:00
$ ( '<a> </a>' ) . attr ( {
'href' : "#" ,
'class' : 'remove-upload' ,
'data-containerid' : id
} ) . click ( function ( ) {
if ( confirm ( $ ( inputEl ) . data ( 'areYouSure' ) ) ) {
2015-05-15 10:07:52 +08:00
removeFile ( ) ;
2015-06-16 21:13:40 +08:00
if ( ! eagerUpload ) {
( function ( e ) {
2015-05-15 10:07:52 +08:00
reload ( e ) ;
} ) ( fileSpan ) ;
}
2015-03-23 18:06:53 +08:00
}
2015-05-15 10:07:52 +08:00
} ) . toggle ( ! eagerUpload ) ,
2015-06-16 21:13:40 +08:00
$ ( '<div>' , {
'class' : 'div_attachments' ,
'name' : 'div_' + 'attachments_' + attachmentId
} )
2015-05-15 10:07:52 +08:00
) . appendTo ( attachments _frame ) ;
2014-10-23 18:49:14 +08:00
2015-06-16 21:13:40 +08:00
if ( eagerUpload ) {
2015-11-11 16:02:37 +08:00
ajaxUpload ( file , attachmentId , fileSpan , inputEl , btnId ) ;
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-06-16 21:13:40 +08:00
2015-11-11 16:02:37 +08:00
function addFile ( inputEl , file , eagerUpload , btnId ) {
2015-05-12 17:14:07 +08:00
var attachments _frame = '#attachments_fields' ;
2016-11-10 17:16:54 +08:00
// $("#issue_confirm").attr('href', '#');
$ ( '#issue_confirm' ) . attr ( "disabled" , true ) ;
$ ( '#issue_confirm' ) . css ( 'background-color' , '#c1c1c1' ) ;
$ ( '#issue_confirm' ) . css ( 'cursor' , 'default' ) ;
2016-02-20 11:06:21 +08:00
if ( true ) {
2015-05-12 17:14:07 +08:00
deleteallfiles = $ ( inputEl ) . data ( 'deleteAllFiles' ) ;
var attachmentId = addFile . nextAttachmentId ++ ;
2015-06-16 21:13:40 +08:00
var fileSpan = $ ( '<span>' , {
'id' : 'attachments_' + attachmentId ,
'class' : 'attachment'
} ) ;
2015-10-20 10:30:09 +08:00
fileSpan . append (
2016-11-07 17:08:24 +08:00
$ ( '<input>' , {
2015-10-20 10:30:09 +08:00
'type' : 'text' ,
2016-11-07 15:21:00 +08:00
'class' : 'upload_filename readonly hidden' ,
2015-10-20 10:30:09 +08:00
'name' : 'attachments[' + attachmentId + '][filename]' ,
2016-10-25 15:37:49 +08:00
'readonly' : 'readonly' ,
2016-11-11 19:19:26 +08:00
'style' : 'border:none; max-width:270px;'
2016-11-07 17:08:24 +08:00
} ) . val ( file . name ) ,
2015-11-03 10:28:13 +08:00
// $('<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-10-20 10:30:09 +08:00
$ ( '<a> </a>' ) . attr ( {
'href' : "#" ,
2016-11-07 15:21:00 +08:00
'class' : 'remove-upload' ,
'style' : 'vertical-align:top;'
2015-10-20 10:30:09 +08:00
} ) . click ( function ( ) {
if ( confirm ( $ ( inputEl ) . data ( 'areYouSure' ) ) ) {
removeFile ( ) ;
if ( ! eagerUpload ) {
( function ( e ) {
reload ( e ) ;
} ) ( fileSpan ) ;
2015-05-12 17:14:07 +08:00
}
2015-10-20 10:30:09 +08:00
}
2014-10-23 18:49:14 +08:00
2015-10-20 10:30:09 +08:00
} ) . toggle ( ! eagerUpload ) ,
$ ( '<div>' , {
'class' : 'div_attachments' ,
'name' : 'div_' + 'attachments_' + attachmentId
} )
) . appendTo ( '#attachments_fields' ) ;
2015-05-12 17:14:07 +08:00
2015-06-16 21:13:40 +08:00
if ( eagerUpload ) {
2015-11-11 16:02:37 +08:00
ajaxUpload ( file , attachmentId , fileSpan , inputEl , btnId ) ;
2015-05-12 17:14:07 +08:00
}
return attachmentId ;
}
return null ;
}
2014-10-23 18:49:14 +08:00
addFile . nextAttachmentId = 1 ;
2015-12-04 16:21:11 +08:00
function addReviseFile ( inputEl , file , eagerUpload , btnId ) {
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' : 'upload_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' ) ) ) {
removeReviseFile ( ) ;
if ( ! eagerUpload ) {
( function ( e ) {
reload ( e ) ;
} ) ( fileSpan ) ;
}
}
} ) . toggle ( ! eagerUpload ) ,
$ ( '<div>' , {
'class' : 'div_attachments' ,
'name' : 'div_' + 'attachments_' + attachmentId
} )
) . appendTo ( '#attachments_fields' ) ;
$ ( "#uploadReviseBox" ) . addClass ( 'disable_link' ) ;
$ ( "#choose_revise_attach" ) . attr ( "onclick" , "return false;" ) ;
if ( eagerUpload ) {
ajaxUpload ( file , attachmentId , fileSpan , inputEl , btnId ) ;
}
return attachmentId ;
}
return null ;
}
2015-11-11 16:02:37 +08:00
function ajaxUpload ( file , attachmentId , fileSpan , inputEl , btnId ) {
2015-11-12 16:35:13 +08:00
//上传开始调用函数
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-11-12 16:35:13 +08:00
//更改progressbar的值
2015-05-15 10:07:52 +08:00
function onProgress ( e ) {
2015-06-16 21:13:40 +08:00
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
}
2015-11-11 16:02:37 +08:00
function actualUpload ( file , attachmentId , fileSpan , inputEl , btnId ) {
2015-05-15 10:07:52 +08:00
ajaxUpload . uploading ++ ;
uploadBlob ( file , $ ( inputEl ) . data ( 'upload-path' ) , attachmentId , {
2015-10-20 10:30:09 +08:00
loadstartEventHandler : onLoadstart . bind ( progressSpan ) ,
progressEventHandler : onProgress . bind ( progressSpan )
} )
2015-05-15 10:07:52 +08:00
. done ( function ( result ) {
2015-11-12 16:35:13 +08:00
progressSpan . progressbar ( 'value' , 100 ) . remove ( ) ; //上传完成就去掉该文件名后的进度条
2015-05-15 10:07:52 +08:00
fileSpan . find ( 'input.description, a' ) . css ( 'display' , 'inline-block' ) ;
fileSpan . find ( 'input.is_public_checkbox, a' ) . css ( 'display' , 'inline-block' ) ;
} )
. fail ( function ( result ) {
2015-11-12 16:35:13 +08:00
progressSpan . text ( "网络错误" ) ;
if ( $ ( "#network_issue" ) ) { //在上传处都要给出一个network_issue的div用来显示错误
2015-05-15 10:07:52 +08:00
$ ( "#network_issue" ) . show ( ) ;
}
} ) . always ( function ( ) {
ajaxUpload . uploading -- ;
fileSpan . removeClass ( 'ajax-loading' ) ;
2015-11-12 16:35:13 +08:00
var containerid = $ ( inputEl ) . data ( 'containerid' ) ; //多个上传控件需要的容器id
if ( containerid == undefined ) {
var count = 1 ; //同步上传参数为1, 所以不需要去获取正在上传的文件就知道是1
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'lebelFileUploding' ) ) ;
}
else {
var count = 1 ;
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'lebelFileUploding' ) ) ;
}
2015-05-15 10:07:52 +08:00
var form = fileSpan . parents ( 'form' ) ;
2015-11-12 16:35:13 +08:00
if ( form . queue ( 'upload' ) . length == 0 && ajaxUpload . uploading == 0 ) { //所有文件上传完毕,更改状态
2015-05-15 10:07:52 +08:00
$ ( 'input:submit' , form ) . removeAttr ( 'disabled' ) ;
2015-11-12 16:35:13 +08:00
$ ( '#' + btnId ) . removeAttr ( "disabled" ) ;
$ ( '#' + btnId ) . val ( '确定' )
$ ( '#' + btnId ) . css ( 'background-color' , '#269ac9' )
if ( containerid == undefined ) {
var count = $ ( '#attachments_fields>span' ) . length ;
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
} else {
var count = $ ( '#attachments_fields' + containerid + '>span' ) . length ;
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
}
2015-05-15 10:07:52 +08:00
}
2015-11-12 16:35:13 +08:00
form . dequeue ( 'upload' ) ; //上传完成一个就退出一个
2015-05-15 10:07:52 +08:00
} ) ;
//gcm files count and add delete_all link
2015-10-19 18:30:28 +08:00
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ start
2015-06-16 21:13:40 +08:00
var containerid = $ ( inputEl ) . data ( 'containerid' ) ;
if ( containerid == undefined ) {
var count = $ ( '#attachments_fields>span' ) . length ;
2015-11-11 16:02:37 +08:00
$ ( '#' + btnId ) . attr ( "disabled" , true ) ;
$ ( '#' + btnId ) . val ( '提交中' ) ;
$ ( '#' + btnId ) . css ( 'background-color' , '#c1c1c1' )
2015-11-12 16:35:13 +08:00
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">1</span>" + $ ( inputEl ) . data ( 'lebelFileUploding' ) ) ; //目前肯定是穿一个文件
2015-06-16 21:13:40 +08:00
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 11:45:15 +08:00
}
2015-05-15 10:07:52 +08:00
}
2015-06-16 21:13:40 +08:00
} else {
var count = $ ( '#attachments_fields' + containerid + '>span' ) . length ;
2015-11-11 16:02:37 +08:00
$ ( '#' + btnId ) . attr ( "disabled" , true ) ;
$ ( '#' + btnId ) . val ( '提交中' ) ;
$ ( '#' + btnId ) . css ( 'background-color' , '#c1c1c1' )
2015-11-12 16:35:13 +08:00
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">1</span>" + $ ( inputEl ) . data ( 'lebelFileUploding' ) ) ; //目前肯定是穿一个文件
2015-06-16 21:13:40 +08:00
if ( count >= 1 ) {
var add _attachs = $ ( '.add_attachment' ) . filter ( function ( index ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
2015-05-15 11:45:15 +08:00
} ) ;
2015-06-16 21:13:40 +08:00
var delete _all = $ ( '.remove_all' ) . filter ( function ( index ) {
return $ ( this ) . data ( 'containerid' ) == containerid ;
2015-05-15 11:45:15 +08:00
} ) ;
2015-06-16 21:13:40 +08:00
if ( delete _all . length < 1 ) {
2015-05-15 11:45:15 +08:00
add _attachs . append ( $ ( "<a> </a>" ) . attr ( {
2015-06-16 21:13:40 +08:00
"href" : "javascript:void(0)" ,
2015-05-15 11:45:15 +08:00
'class' : 'remove_all' ,
'data-containerid' : containerid ,
2015-06-16 21:13:40 +08:00
"onclick" : "removeAll('" + containerid + "')"
2015-05-15 11:45:15 +08:00
} ) ) ;
}
}
2015-10-19 18:30:28 +08:00
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <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
}
2015-11-12 16:35:13 +08:00
//在插入文件名的后边插入div
2015-11-11 11:19:10 +08:00
var progressSpan = $ ( '<div>' ) . insertAfter ( fileSpan . find ( 'input.upload_filename' ) ) ;
2015-11-12 16:35:13 +08:00
progressSpan . progressbar ( ) ; //将div处理成progress bar
fileSpan . addClass ( 'ajax-waiting' ) ; //添加文件正在上传的状态
var containerid = $ ( inputEl ) . data ( 'containerid' ) ; //多个上传控件需要的容器id
if ( containerid == undefined ) { //选择了多少个文件也要显示,因为有时候文件过大,一时半会传不完,但还是显示文件没有选择
var count = $ ( '#attachments_fields>span' ) . length ;
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + '个文件被选择' ) ;
} else {
var count = $ ( '#attachments_fields' + containerid + '>span' ) . length ;
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">" + count + "</span>" + '个文件被选择' ) ;
}
//最大的同步上传参数
var maxSyncUpload = 1 //$(inputEl).data('max-concurrent-uploads');
//如果没有指定同步上传参数 或者参数不对,或者需要上传的文件已经小于同步上传的个数,那么直接上传
2015-06-16 21:13:40 +08:00
if ( maxSyncUpload == null || maxSyncUpload <= 0 || ajaxUpload . uploading < maxSyncUpload )
2015-11-11 16:02:37 +08:00
actualUpload ( file , attachmentId , fileSpan , inputEl , btnId ) ;
2015-11-12 16:35:13 +08:00
else //否则就进行队列上传
$ ( inputEl ) . parents ( 'form' ) . queue ( 'upload' , actualUpload . bind ( this , file , attachmentId , fileSpan , inputEl , btnId ) ) ;
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
}
2015-12-04 16:21:11 +08:00
function removeReviseFile ( ) {
$ ( this ) . parent ( 'span' ) . remove ( ) ;
$ ( "#uploadReviseBox" ) . removeClass ( 'disable_link' ) ;
$ ( "#choose_revise_attach" ) . attr ( "onclick" , "_file.click();" ) ;
return false ;
}
2014-10-23 18:49:14 +08:00
//gcm delete all file
2015-10-19 18:30:28 +08:00
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ start
2015-06-16 21:13:40 +08:00
function removeAll ( containerid ) {
2015-10-20 10:30:09 +08:00
if ( confirm ( deleteallfiles ) ) {
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 11:45:15 +08:00
}
2015-05-15 10:07:52 +08:00
}
2015-10-20 10:30:09 +08:00
// return false;
}
//modify by yutao 2015-5-14 <20> <> 1<EFBFBD> <31> ҳ<EFBFBD> <D2B3> <EFBFBD> <EFBFBD> ڶ<EFBFBD> <DAB6> <EFBFBD> ϴ<EFBFBD> <CFB4> ؼ<EFBFBD> ʱ<EFBFBD> ˿<EFBFBD> <CBBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> bug <20> ʸ<EFBFBD> ֮ end
//gcm
2014-10-23 18:49:14 +08:00
function uploadBlob ( blob , uploadUrl , attachmentId , options ) {
2015-05-15 10:07:52 +08:00
var actualOptions = $ . extend ( {
loadstartEventHandler : $ . noop ,
progressEventHandler : $ . noop
} , options ) ;
2015-06-16 21:13:40 +08:00
if ( uploadUrl . indexOf ( '?' ) > 0 ) {
2015-05-15 10:07:52 +08:00
uploadUrl = uploadUrl + '&attachment_id=' + attachmentId ;
2015-06-16 21:13:40 +08:00
} else {
2015-05-15 10:07:52 +08:00
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
}
2015-12-04 16:21:11 +08:00
function addReviseInputFiles ( inputEl , btnId ) {
// var clearedFileInput = $(inputEl).clone().val('');
if ( inputEl . files ) {
uploadAndAttachReviseFiles ( inputEl . files , inputEl , btnId ) ;
// $(inputEl).remove();
} else {
// browser not supporting the file API, upload on form submission
var attachmentId ;
var aFilename = inputEl . value . split ( /\/|\\/ ) ;
var count = $ ( '#attachments_fields>span' ) . length ;
attachmentId = addReviseFile ( inputEl , {
name : aFilename [ aFilename . length - 1 ]
} , false ) ;
if ( attachmentId ) {
$ ( inputEl ) . attr ( {
name : 'attachments[' + attachmentId + '][file]'
} ) . hide ( ) ;
if ( count <= 0 ) count = 1 ;
}
}
//clearedFileInput.insertAfter('#attachments_fields');
}
2015-11-11 16:02:37 +08:00
function addInputFiles ( inputEl , btnId ) {
2015-05-15 10:07:52 +08:00
// var clearedFileInput = $(inputEl).clone().val('');
2014-10-23 18:49:14 +08:00
if ( inputEl . files ) {
2015-11-12 16:35:13 +08:00
// if(inputEl.files.length >= 5){
// alert('一次选择的文件不能超过5个')
// return;
// }
2014-10-23 18:49:14 +08:00
// upload files using ajax
2015-11-11 16:02:37 +08:00
uploadAndAttachFiles ( inputEl . files , inputEl , btnId ) ;
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 ( /\/|\\/ ) ;
2015-06-16 21:13:40 +08:00
var count = $ ( '#attachments_fields>span' ) . length ;
attachmentId = addFile ( inputEl , {
name : aFilename [ aFilename . length - 1 ]
} , false ) ;
2014-10-23 18:49:14 +08:00
if ( attachmentId ) {
2015-06-16 21:13:40 +08:00
$ ( inputEl ) . attr ( {
name : 'attachments[' + attachmentId + '][file]'
} ) . hide ( ) ;
if ( count <= 0 ) count = 1 ;
$ ( '#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-06-16 21:13:40 +08:00
2015-11-11 16:02:37 +08:00
function addInputFiles _board ( inputEl , id , btnId ) {
2015-05-12 17:14:07 +08:00
// var clearedFileInput = $(inputEl).clone().val('');
if ( inputEl . files ) {
2015-11-12 16:35:13 +08:00
// if(inputEl.files.length >= 5){
// alert('一次选择的文件不能超过5个')
// return;
// }
2015-05-12 17:14:07 +08:00
// upload files using ajax
2015-11-11 16:02:37 +08:00
uploadAndAttachFiles _board ( inputEl . files , inputEl , id , btnId ) ;
2015-05-12 17:14:07 +08:00
// $(inputEl).remove();
} else {
// browser not supporting the file API, upload on form submission
var attachmentId ;
var aFilename = inputEl . value . split ( /\/|\\/ ) ;
2015-06-16 21:13:40 +08:00
attachmentId = addFile _board ( inputEl , {
name : aFilename [ aFilename . length - 1 ]
} , false , id ) ;
2015-05-12 17:14:07 +08:00
if ( attachmentId ) {
2015-06-16 21:13:40 +08:00
$ ( inputEl ) . attr ( {
name : 'attachments[' + attachmentId + '][file]'
} ) . hide ( ) ;
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
2015-05-12 17:14:07 +08:00
}
}
2014-10-23 18:49:14 +08:00
2015-05-12 17:14:07 +08:00
//clearedFileInput.insertAfter('#attachments_fields');
}
2015-06-16 21:13:40 +08:00
2015-12-04 16:21:11 +08:00
function uploadAndAttachReviseFiles ( files , inputEl , btnId ) {
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 ( ) {
addReviseFile ( inputEl , this , true , btnId ) ;
} ) ;
}
}
2015-11-11 16:02:37 +08:00
function uploadAndAttachFiles ( files , inputEl , btnId ) {
2014-10-23 18:49:14 +08:00
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 ( ) {
2015-06-16 21:13:40 +08:00
if ( this . size && maxFileSize != null && this . size > parseInt ( maxFileSize ) ) {
sizeExceeded = true ;
}
2015-05-15 10:07:52 +08:00
} ) ;
if ( sizeExceeded ) {
window . alert ( maxFileSizeExceeded ) ;
} else {
2015-06-16 21:13:40 +08:00
$ . each ( files , function ( ) {
2015-11-11 16:02:37 +08:00
addFile ( inputEl , this , true , btnId ) ;
2015-06-16 21:13:40 +08:00
} ) ;
2015-05-15 10:07:52 +08:00
}
2014-10-23 18:49:14 +08:00
}
2015-06-16 21:13:40 +08:00
2015-11-11 16:02:37 +08:00
function uploadAndAttachFiles _board ( files , inputEl , id , btnId ) {
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 ( ) {
2015-06-16 21:13:40 +08:00
if ( this . size && maxFileSize != null && this . size > parseInt ( maxFileSize ) ) {
sizeExceeded = true ;
}
2015-05-12 17:14:07 +08:00
} ) ;
if ( sizeExceeded ) {
window . alert ( maxFileSizeExceeded ) ;
} else {
2015-06-16 21:13:40 +08:00
$ . each ( files , function ( ) {
2015-11-11 16:02:37 +08:00
addFile _board ( inputEl , this , true , id , btnId ) ;
2015-06-16 21:13:40 +08:00
} ) ;
2015-05-12 17:14:07 +08:00
}
}
2015-06-16 21:13:40 +08:00
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-06-16 21:13:40 +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-06-16 21:13:40 +08:00
$ ( function ( ) {
2015-05-15 11:45:15 +08:00
$ ( ".file_selector" ) . each ( function ( ) {
deleteallfiles = $ ( this ) . data ( 'deleteAllFiles' ) ;
var containerid = $ ( this ) . data ( 'containerid' ) ;
2015-06-16 21:13:40 +08:00
if ( containerid == undefined ) containerid = '' ;
2015-05-15 11:45:15 +08:00
var count = $ ( '#attachments_fields' + containerid + '>span' ) . length ;
if ( count >= 1 ) {
$ ( '#upload_file_count' + containerid ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( this ) . data ( 'fileCount' ) ) ;
2015-06-16 21:13:40 +08:00
var add _attachs = $ ( '.add_attachment' ) . filter ( function ( index ) {
2015-05-15 11:45:15 +08:00
return $ ( this ) . data ( 'containerid' ) == containerid ;
} ) ;
2015-06-16 21:13:40 +08:00
var delete _all = $ ( '.remove_all' ) . filter ( function ( index ) {
2015-05-15 11:45:15 +08:00
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 + "')"
} ) ) ;
}
}
} ) ;
2015-06-16 21:13:40 +08:00
} ) ;
2015-10-20 10:30:09 +08:00
//课程课件
function addInputFilesCourseSource ( inputEl ) {
checkBox = arguments [ 1 ] == 'public' ? false : true ;
2015-11-11 16:02:37 +08:00
btnId = arguments [ 2 ] ;
2015-10-20 10:30:09 +08:00
// var clearedFileInput = $(inputEl).clone().val('');
if ( inputEl . files ) {
2015-11-12 16:35:13 +08:00
// if(inputEl.files.length >= 5){
// alert('一次选择的文件不能超过5个')
// return;
// }
2015-10-20 10:30:09 +08:00
// upload files using ajax
2015-11-11 16:02:37 +08:00
uploadAndAttachFilesCourseSource ( inputEl . files , inputEl , checkBox , btnId ) ;
2015-10-20 10:30:09 +08:00
// $(inputEl).remove();
} else {
// browser not supporting the file API, upload on form submission
var attachmentId ;
var aFilename = inputEl . value . split ( /\/|\\/ ) ;
var count = $ ( '#attachments_fields>span' ) . length ;
attachmentId = addFile ( inputEl , {
name : aFilename [ aFilename . length - 1 ]
} , false ) ;
if ( attachmentId ) {
$ ( inputEl ) . attr ( {
name : 'attachments[' + attachmentId + '][file]'
} ) . hide ( ) ;
if ( count <= 0 ) count = 1 ;
$ ( '#upload_file_count' ) . html ( "<span id=\"count\">" + count + "</span>" + $ ( inputEl ) . data ( 'fileCount' ) ) ;
}
}
//clearedFileInput.insertAfter('#attachments_fields');
}
2015-11-11 16:02:37 +08:00
function uploadAndAttachFilesCourseSource ( files , inputEl , checkBox , btnId ) {
2015-10-20 10:30:09 +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 ( ) {
2015-11-11 16:02:37 +08:00
addFileCourseSource ( inputEl , this , true , checkBox , btnId ) ;
2015-10-20 10:30:09 +08:00
} ) ;
}
}
2015-11-11 16:02:37 +08:00
function addFileCourseSource ( inputEl , file , eagerUpload , checkBox , btnId ) {
2015-10-20 10:30:09 +08:00
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'
} ) ;
//alert(checkBox);
if ( checkBox ) {
fileSpan . append (
$ ( '<input>' , {
'type' : 'text' ,
2015-11-03 10:28:13 +08:00
'class' : 'upload_filename readonly' ,
2015-10-20 10:30:09 +08:00
'name' : 'attachments[' + attachmentId + '][filename]' ,
'readonly' : 'readonly'
} ) . val ( file . name ) ,
2015-11-03 10:28:13 +08:00
// $('<input>', {
// 'type': 'text',
// 'class': 'description',
// 'name': 'attachments[' + attachmentId + '][description]',
// 'maxlength': 254,
// 'placeholder': $(inputEl).data('descriptionPlaceholder')
// }).toggle(!eagerUpload),
2015-10-20 10:30:09 +08:00
$ ( '<div>' , {
'class' : 'div_attachments' ,
'name' : 'div_' + 'attachments_' + attachmentId
} )
) . appendTo ( '#attachments_fields' ) ;
} else {
fileSpan . append (
$ ( '<input>' , {
'type' : 'text' ,
2015-11-03 14:33:59 +08:00
'class' : 'upload_filename readonly' ,
2015-10-20 10:30:09 +08:00
'name' : 'attachments[' + attachmentId + '][filename]' ,
'readonly' : 'readonly'
} ) . val ( file . name ) ,
2015-11-03 14:33:59 +08:00
// $('<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-10-20 10:30:09 +08:00
$ ( '<a> </a>' ) . attr ( {
'href' : "#" ,
'class' : 'remove-upload'
} ) . click ( function ( ) {
if ( confirm ( $ ( inputEl ) . data ( 'areYouSure' ) ) ) {
removeFile ( ) ;
if ( ! eagerUpload ) {
( function ( e ) {
reload ( e ) ;
} ) ( fileSpan ) ;
}
}
} ) . toggle ( ! eagerUpload ) ,
$ ( '<div>' , {
'class' : 'div_attachments' ,
'name' : 'div_' + 'attachments_' + attachmentId
} )
) . appendTo ( '#attachments_fields' ) ;
}
if ( eagerUpload ) {
2015-11-11 16:02:37 +08:00
ajaxUpload ( file , attachmentId , fileSpan , inputEl , btnId ) ;
2015-10-20 10:30:09 +08:00
}
return attachmentId ;
}
return null ;
}
addFileCourseSource . nextAttachmentId = 1 ;