2015-02-13 08:33:19 +00:00
define ( [
'common' ,
'backbone' ,
'jquery.fileupload-ui'
] , function ( Common , Backbone , FileUpload ) {
window . locale = {
"fileupload" : {
"errors" : {
"maxFileSize" : gettext ( "File is too big" ) ,
"minFileSize" : gettext ( "File is too small" ) ,
"acceptFileTypes" : gettext ( "Filetype not allowed" ) ,
"maxNumberOfFiles" : gettext ( "Max number of files exceeded" ) ,
"uploadedBytes" : gettext ( "Uploaded bytes exceed file size" ) ,
"emptyResult" : gettext ( "Empty file upload result" )
} ,
"error" : gettext ( "Error" ) ,
"uploaded" : gettext ( "uploaded" ) ,
"canceled" : gettext ( "canceled" ) ,
"start" : gettext ( "Start" ) ,
"cancel" : gettext ( "Cancel" ) ,
"destroy" : gettext ( "Delete" )
}
} ;
var FileUploadView = Backbone . View . extend ( {
el : $ ( '#upload-file-dialog' ) ,
initialize : function ( options ) {
var dirView = this . dirView = options . dirView ;
var dirents = dirView . dir ;
var popup = this . $el . addClass ( 'fixed-upload-file-dialog' ) ;
this . popup _height = '200px' ;
var $fu _status = $ ( '.status' , popup ) ,
$total _progress = $ ( '.total-progress' , popup ) ,
cancel _all _btn = $ ( '.fileupload-buttonbar .cancel' , popup ) ,
close _icon = $ ( '.close' , popup ) ,
saving _tip = $ ( '.saving-tip' , popup ) ;
var fu _status = {
'uploading' : gettext ( "File Uploading..." ) ,
'complete' : gettext ( "File Upload complete" ) ,
'canceled' : gettext ( "File Upload canceled" ) ,
'failed' : gettext ( "File Upload failed" )
} ;
var uploaded _files = [ ] ;
2015-03-26 03:48:02 +00:00
var updated _files = [ ] ;
2015-02-13 08:33:19 +00:00
var enable _upload _folder = app . pageOptions . enable _upload _folder ;
var new _dir _names = [ ] ;
var dirs _to _update = [ ] ;
popup . fileupload ( {
paramName : 'file' ,
// customize it for 'done'
getFilesFromResponse : function ( data ) {
if ( data . result ) {
return data . result ;
}
} ,
autoUpload : true ,
maxNumberOfFiles : 500 ,
sequentialUploads : true
} )
. bind ( 'fileuploadadd' , function ( e , data ) {
// for drag & drop
if ( ! dirView . $el . is ( ':visible' ) ) {
return false ;
}
if ( dirents . user _perm && dirents . user _perm != 'rw' ) {
return false ;
}
popup . removeClass ( 'hide' ) ;
cancel _all _btn . removeClass ( 'hide' ) ;
close _icon . addClass ( 'hide' ) ;
var path = dirents . path ;
popup . fileupload ( 'option' , 'formData' , {
'parent_dir' : path == '/' ? path : path + '/'
} ) ;
if ( ! enable _upload _folder ) {
return ;
}
// hide the upload menu
var menu = dirView . $ ( '#upload-menu' ) ;
if ( ! menu . hasClass ( 'hide' ) ) {
menu . find ( '.item' ) . removeAttr ( 'style' )
. end ( ) . addClass ( 'hide' ) ;
}
2015-02-26 08:36:20 +00:00
2015-02-13 08:33:19 +00:00
var file = data . files [ 0 ] ;
2015-02-26 08:36:20 +00:00
// add folder by clicking 'Upload Folder'
if ( file . name == '.' ) { // a subdirectory will be shown as '.'
2015-02-13 08:33:19 +00:00
data . files . shift ( ) ;
2015-02-26 08:36:20 +00:00
return ;
2015-02-13 08:33:19 +00:00
}
2015-02-26 08:36:20 +00:00
if ( file . webkitRelativePath ) {
2015-02-13 08:33:19 +00:00
file . relative _path = file . webkitRelativePath ;
}
2015-02-26 08:36:20 +00:00
// add folder by drag & drop
if ( file . relativePath ) {
2015-02-13 08:33:19 +00:00
file . relative _path = file . relativePath + file . name ;
}
} )
. bind ( 'fileuploadstart' , function ( ) {
$fu _status . html ( fu _status . uploading ) ;
} )
. bind ( 'fileuploadsubmit' , function ( e , data ) {
2015-02-26 08:36:20 +00:00
if ( data . files . length == 0 ) {
return false ;
}
2015-02-13 08:33:19 +00:00
var file = data . files [ 0 ] ;
2015-03-26 03:48:02 +00:00
if ( file . error ) {
return false ;
}
var upload _file = function ( ) {
2015-02-13 08:33:19 +00:00
$ . ajax ( {
url : Common . getUrl ( {
name : 'get_file_op_url' ,
repo _id : dirents . repo _id
2015-03-26 03:48:02 +00:00
} ) ,
data : {
'op_type' : 'upload' ,
'path' : dirents . path
} ,
2015-02-13 08:33:19 +00:00
cache : false ,
dataType : 'json' ,
success : function ( ret ) {
if ( enable _upload _folder ) {
var file _path = file . relative _path ,
r _path ;
if ( file _path ) { // 'add folder'
r _path = file _path . substring ( 0 , file _path . lastIndexOf ( '/' ) + 1 ) ;
}
var formData = popup . fileupload ( 'option' , 'formData' ) ;
formData . relative _path = r _path || '' ;
popup . fileupload ( 'option' , 'formData' , formData ) ;
}
data . url = ret [ 'url' ] ;
data . jqXHR = popup . fileupload ( 'send' , data ) ;
} ,
error : function ( ) {
file . error = gettext ( "Failed to get upload url" ) ;
}
} ) ;
2015-03-26 03:48:02 +00:00
} ;
if ( file . relative _path || data . originalFiles . length > 1 ) { // 'add folder' or upload more than 1 file once
upload _file ( ) ;
2015-02-13 08:33:19 +00:00
return false ;
}
2015-03-26 03:48:02 +00:00
var update _file = function ( ) {
$ . ajax ( {
url : Common . getUrl ( {
name : 'get_file_op_url' ,
repo _id : dirents . repo _id
} ) ,
data : {
'op_type' : 'update' ,
'path' : dirents . path
} ,
cache : false ,
dataType : 'json' ,
success : function ( ret ) {
var formData = popup . fileupload ( 'option' , 'formData' ) ;
formData . target _file = formData . parent _dir + file . name ;
popup . fileupload ( 'option' , 'formData' , formData ) ;
file . to _update = true ;
data . url = ret [ 'url' ] ;
data . jqXHR = popup . fileupload ( 'send' , data ) ;
} ,
error : function ( ) {
file . error = gettext ( "Failed to get update url" ) ;
}
} ) ;
} ;
var files = dirents . where ( { 'is_file' : true } ) ,
file _names = [ ] ;
$ ( files ) . each ( function ( ) {
file _names . push ( this . get ( 'obj_name' ) ) ;
} ) ;
if ( file _names . indexOf ( file . name ) != - 1 ) { // file with the same name already exists in the dir
2015-03-30 07:26:23 +00:00
var confirm _popup = $ ( '#confirm-popup' ) ;
confirm _popup . modal ( {
2015-03-26 03:48:02 +00:00
onClose : function ( ) {
$ . modal . close ( ) ;
if ( file . choose _to _update ) {
update _file ( ) ;
2015-03-30 07:26:23 +00:00
} else if ( file . choose _to _upload ) {
2015-03-26 03:48:02 +00:00
upload _file ( ) ;
2015-03-30 07:26:23 +00:00
} else {
data . jqXHR = popup . fileupload ( 'send' , data ) ;
data . jqXHR . abort ( ) ;
2015-03-26 03:48:02 +00:00
}
}
} ) ;
2015-03-30 07:26:23 +00:00
$ ( '#simplemodal-container' ) . css ( { 'width' : 'auto' , 'height' : 'auto' } ) ;
var confirm _msg = gettext ( "Replace file {filename}?" )
. replace ( '{filename}' , '<span class="op-target">' + Common . HTMLescape ( file . name ) + '</span>' ) ;
var confirm _msg _detail = gettext ( "A file with the same name already exists in this folder." ) + '<br />' + gettext ( "Replacing it will overwrite its content." ) ;
var upload _btn = $ ( '<button id="not-replace" class="btn">' + gettext ( "Don't replace" ) + '</button>' ) . css ( { 'margin-left' : '8px' } ) ;
$ ( '#confirm-con' ) . html ( '<h3>' + confirm _msg + '</h3><p>' + confirm _msg _detail + '</p>' ) ;
$ ( '#confirm-yes' ) . html ( gettext ( "Replace" ) ) . after ( upload _btn ) ;
$ ( '.simplemodal-close' , confirm _popup ) . html ( gettext ( "Cancel" ) ) ;
2015-03-26 03:48:02 +00:00
$ ( '#confirm-yes' ) . click ( function ( ) {
file . choose _to _update = true ;
$ . modal . close ( ) ;
} ) ;
2015-03-30 07:26:23 +00:00
upload _btn . click ( function ( ) {
file . choose _to _upload = true ;
$ . modal . close ( ) ;
} ) ;
2015-03-26 03:48:02 +00:00
} else {
upload _file ( ) ;
}
return false ;
2015-02-13 08:33:19 +00:00
} )
. bind ( 'fileuploadprogressall' , function ( e , data ) {
$total _progress . html ( parseInt ( data . loaded / data . total * 100 , 10 ) + '% ' +
'<span style="font-size:14px;color:#555;">(' +
$ ( this ) . data ( 'blueimp-fileupload' ) . _formatBitrate ( data . bitrate ) +
')</span>' ) . removeClass ( 'hide' ) ;
if ( data . loaded > 0 && data . loaded == data . total ) {
saving _tip . show ( ) ;
}
} )
. bind ( 'fileuploaddone' , function ( e , data ) {
if ( data . textStatus != 'success' ) {
return ;
}
var file = data . files [ 0 ] ;
var file _path = file . relative _path ;
var file _uploaded = data . result [ 0 ] ; // 'id', 'name', 'size'
// for 'template_download' render
file _uploaded . uploaded = true ;
if ( file _path ) {
file _uploaded . relative _path = file _path . substring ( 0 , file _path . lastIndexOf ( '/' ) + 1 ) + file _uploaded . name ;
}
var path = dirents . path ;
path = path == '/' ? path : path + '/' ;
if ( data . formData . parent _dir != path ) {
return ;
}
if ( ! file _path ) {
2015-03-26 03:48:02 +00:00
if ( ! file . to _update ) {
uploaded _files . push ( file _uploaded ) ;
} else {
updated _files . push ( file _uploaded ) ;
}
2015-02-13 08:33:19 +00:00
return ;
}
if ( ! enable _upload _folder ) {
return ;
}
// for 'add folder'
var dir _name = file _path . substring ( 0 , file _path . indexOf ( '/' ) ) ;
var dir = dirents . where ( { 'is_dir' : true , 'obj_name' : dir _name } ) ;
if ( dir . length > 0 ) { // 0 or 1
if ( dirs _to _update . indexOf ( dir _name ) == - 1 ) {
dirs _to _update . push ( dir _name ) ;
}
} else {
if ( new _dir _names . indexOf ( dir _name ) == - 1 ) {
new _dir _names . push ( dir _name ) ;
}
}
} )
. bind ( 'fileuploadstop' , function ( ) {
cancel _all _btn . addClass ( 'hide' ) ;
close _icon . removeClass ( 'hide' ) ;
var path = dirents . path ;
path = path == '/' ? path : path + '/' ;
if ( popup . fileupload ( 'option' , 'formData' ) . parent _dir != path ) {
return ;
}
var now = parseInt ( new Date ( ) . getTime ( ) / 1000 ) ;
if ( uploaded _files . length > 0 ) {
$ ( uploaded _files ) . each ( function ( index , file ) {
var new _dirent = dirents . add ( {
'is_file' : true ,
'obj_name' : file . name ,
'last_modified' : now ,
'file_size' : Common . fileSizeFormat ( file . size , 1 ) ,
'obj_id' : file . id ,
'file_icon' : 'file.png' ,
'last_update' : gettext ( "Just now" ) ,
2015-03-26 03:48:02 +00:00
'starred' : false
2015-02-13 08:33:19 +00:00
} , { silent : true } ) ;
dirView . addNewFile ( new _dirent ) ;
} ) ;
uploaded _files = [ ] ;
}
if ( new _dir _names . length > 0 ) {
$ ( new _dir _names ) . each ( function ( index , new _name ) {
var new _dirent = dirents . add ( {
'is_dir' : true ,
'obj_name' : new _name ,
'last_modified' : now ,
'last_update' : gettext ( "Just now" ) ,
'p_dpath' : path + new _name
} , { silent : true } ) ;
dirView . addNewDir ( new _dirent ) ;
} ) ;
new _dir _names = [ ] ;
}
if ( dirs _to _update . length > 0 ) {
$ ( dirs _to _update ) . each ( function ( index , dir _name ) {
var dir _to _update = dirents . where ( { 'is_dir' : true , 'obj_name' : dir _name } ) ;
dir _to _update [ 0 ] . set ( {
'last_modified' : now ,
'last_update' : gettext ( "Just now" )
} ) ;
} ) ;
dirs _to _update = [ ] ;
}
2015-03-26 03:48:02 +00:00
if ( updated _files . length > 0 ) {
$ ( updated _files ) . each ( function ( index , item ) {
var file _to _update = dirents . where ( { 'is_file' : true , 'obj_name' : item . name } ) ;
file _to _update [ 0 ] . set ( {
'obj_id' : item . id ,
'file_size' : Common . fileSizeFormat ( item . size , 1 ) ,
'last_modified' : now ,
'last_update' : gettext ( "Just now" )
} ) ;
} ) ;
updated _files = [ ] ;
}
2015-02-13 08:33:19 +00:00
} )
// after tpl has rendered
. bind ( 'fileuploadcompleted' , function ( ) { // 'done'
if ( $ ( '.files .cancel' , popup ) . length == 0 ) {
saving _tip . hide ( ) ;
$total _progress . addClass ( 'hide' ) ;
$fu _status . html ( fu _status . complete ) ;
}
} )
. bind ( 'fileuploadfailed' , function ( e , data ) { // 'fail'
if ( $ ( '.files .cancel' , popup ) . length == 0 ) {
cancel _all _btn . addClass ( 'hide' ) ;
close _icon . removeClass ( 'hide' ) ;
$total _progress . addClass ( 'hide' ) ;
saving _tip . hide ( ) ;
if ( data . errorThrown == 'abort' ) { // 'cancel'
$fu _status . html ( fu _status . canceled ) ;
} else { // 'error'
$fu _status . html ( fu _status . failed ) ;
}
}
} ) ;
var max _upload _file _size = app . pageOptions . max _upload _file _size ;
if ( max _upload _file _size ) {
popup . fileupload (
'option' ,
'maxFileSize' ,
max _upload _file _size ) ;
}
// Enable iframe cross-domain access via redirect option:
popup . fileupload (
'option' ,
'redirect' ,
window . location . href . replace ( /\/repo\/[-a-z0-9]{36}\/.*/ , app . config . mediaUrl + 'cors/result.html?%s' )
) ;
$ ( document ) . click ( function ( e ) {
var target = e . target || event . srcElement ;
var closePopup = function ( popup , popup _switch ) {
if ( ! popup . hasClass ( 'hide' ) && ! popup . is ( target ) && ! popup . find ( '*' ) . is ( target ) && ! popup _switch . is ( target ) && ! popup _switch . find ( '*' ) . is ( target ) ) {
popup . addClass ( 'hide' ) ;
}
} ;
closePopup ( dirView . $ ( '#upload-menu' ) , dirView . $ ( '#upload-file' ) ) ;
} ) ;
} ,
events : {
'click .fold-switch' : 'foldAndUnfoldPopup' ,
'click .close' : 'closePopup'
} ,
foldAndUnfoldPopup : function ( ) {
var popup = this . $el ;
var full _ht = parseInt ( this . popup _height ) ;
var main _con = $ ( '.fileupload-buttonbar, .table' , popup ) ;
if ( popup . height ( ) == full _ht ) {
popup . height ( $ ( '.hd' , popup ) . outerHeight ( true ) ) ;
main _con . addClass ( 'hide' ) ;
} else {
popup . height ( full _ht ) ;
main _con . removeClass ( 'hide' ) ;
}
} ,
closePopup : function ( ) {
var popup = this . $el ;
popup . addClass ( 'hide' ) ;
$ ( '.files' , popup ) . empty ( ) ;
} ,
setFileInput : function ( ) {
var dirView = this . dirView ,
dir = dirView . dir ;
var popup = this . $el ;
if ( dir . user _perm && dir . user _perm == 'rw' ) {
popup . fileupload (
'option' ,
'fileInput' ,
dirView . $ ( '#upload-file input' ) ) ;
}
if ( ! app . pageOptions . enable _upload _folder ) {
return ;
}
var upload _btn = dirView . $ ( '#upload-file' ) ,
upload _menu = dirView . $ ( '#upload-menu' ) ;
if ( dir . user _perm && dir . user _perm == 'rw' &&
'webkitdirectory' in $ ( 'input[type="file"]' , upload _btn ) [ 0 ] ) {
upload _btn . find ( 'input' ) . remove ( ) . end ( ) . addClass ( 'cspt' ) ;
$ ( '.item' , upload _menu ) . click ( function ( ) {
popup . fileupload (
'option' ,
'fileInput' ,
$ ( 'input[type="file"]' , $ ( this ) )
) ;
} )
. hover (
function ( ) {
$ ( this ) . css ( { 'background' : '#f3f3f3' } ) ;
} ,
function ( ) {
$ ( this ) . css ( { 'background' : 'transparent' } ) ;
}
) ;
dirView . $ ( '.repo-op' ) . css ( { 'position' : 'relative' } ) ;
upload _menu . css ( {
'left' : upload _btn . position ( ) . left ,
'top' : parseInt ( dirView . $ ( '.repo-op' ) . css ( 'padding-top' ) ) + upload _btn . outerHeight ( true )
} ) ;
upload _btn . click ( function ( ) {
upload _menu . toggleClass ( 'hide' ) ;
} ) ;
}
}
} ) ;
return FileUploadView ;
} ) ;