2014-01-30 13:41:04 +04:00
/ *
* Copyright ( c ) 2014
*
* This file is licensed under the Affero General Public License version 3
* or later .
*
* See the COPYING - README file .
*
* /
/* global OC, t, n, FileList, FileActions, Files */
/* global procesSelection, dragOptions, SVGSupport, replaceSVG */
window . FileList = {
2014-02-20 18:16:45 +04:00
appName : t ( 'files' , 'Files' ) ,
2012-04-15 17:48:02 +04:00
useUndo : true ,
2013-10-22 20:11:03 +04:00
postProcessList : function ( ) {
$ ( '#fileList tr' ) . each ( function ( ) {
2013-08-17 15:07:18 +04:00
//little hack to set unescape filenames in attribute
$ ( this ) . attr ( 'data-file' , decodeURIComponent ( $ ( this ) . attr ( 'data-file' ) ) ) ;
} ) ;
} ,
2014-02-12 03:54:35 +04:00
/ * *
* Sets a new page title
* /
setPageTitle : function ( title ) {
2014-02-20 17:56:28 +04:00
if ( title ) {
title += ' - ' ;
}
else {
title = '' ;
}
2014-02-20 18:16:45 +04:00
title += FileList . appName ;
2014-02-12 03:54:35 +04:00
// Sets the page title with the " - ownCloud" suffix as in templates
2014-02-13 13:48:01 +04:00
window . document . title = title + ' - ' + oc _defaults . title ;
2014-02-20 18:36:52 +04:00
2014-02-12 03:54:35 +04:00
return true ;
} ,
2014-01-10 18:02:26 +04:00
/ * *
* Returns the tr element for a given file name
* /
findFileEl : function ( fileName ) {
// use filterAttr to avoid escaping issues
return $ ( '#fileList tr' ) . filterAttr ( 'data-file' , fileName ) ;
} ,
2011-06-04 20:44:14 +04:00
update : function ( fileListHtml ) {
2013-10-21 00:47:44 +04:00
var $fileList = $ ( '#fileList' ) ;
2013-08-17 15:07:18 +04:00
$fileList . empty ( ) . html ( fileListHtml ) ;
2013-10-21 00:47:44 +04:00
FileList . updateEmptyContent ( ) ;
2013-08-17 15:07:18 +04:00
$fileList . find ( 'tr' ) . each ( function ( ) {
FileActions . display ( $ ( this ) . children ( 'td.filename' ) ) ;
} ) ;
$fileList . trigger ( jQuery . Event ( "fileActionsReady" ) ) ;
FileList . postProcessList ( ) ;
// "Files" might not be loaded in extending apps
2013-10-22 20:11:03 +04:00
if ( window . Files ) {
2013-08-17 15:07:18 +04:00
Files . setupDragAndDrop ( ) ;
}
2013-08-29 03:17:04 +04:00
FileList . updateFileSummary ( ) ;
2014-01-17 20:49:00 +04:00
procesSelection ( ) ;
2014-02-04 14:14:00 +04:00
$ ( window ) . scrollTop ( 0 ) ;
2013-08-17 15:07:18 +04:00
$fileList . trigger ( jQuery . Event ( "updated" ) ) ;
2011-06-04 20:44:14 +04:00
} ,
2013-10-22 20:11:03 +04:00
createRow : function ( type , name , iconurl , linktarget , size , lastModified , permissions ) {
2013-02-09 17:42:03 +04:00
var td , simpleSize , basename , extension ;
//containing tr
var tr = $ ( '<tr></tr>' ) . attr ( {
"data-type" : type ,
"data-size" : size ,
"data-file" : name ,
"data-permissions" : permissions
} ) ;
// filename td
td = $ ( '<td></td>' ) . attr ( {
"class" : "filename" ,
2013-08-26 16:36:18 +04:00
"style" : 'background-image:url(' + iconurl + '); background-size: 32px;'
2013-02-09 17:42:03 +04:00
} ) ;
2013-08-26 18:33:51 +04:00
var rand = Math . random ( ) . toString ( 16 ) . slice ( 2 ) ;
td . append ( '<input id="select-' + rand + '" type="checkbox" /><label for="select-' + rand + '"></label>' ) ;
2013-02-09 17:42:03 +04:00
var link _elem = $ ( '<a></a>' ) . attr ( {
"class" : "name" ,
"href" : linktarget
} ) ;
//split extension from filename for non dirs
2013-10-22 20:11:03 +04:00
if ( type !== 'dir' && name . indexOf ( '.' ) !== - 1 ) {
2012-09-23 05:16:52 +04:00
basename = name . substr ( 0 , name . lastIndexOf ( '.' ) ) ;
extension = name . substr ( name . lastIndexOf ( '.' ) ) ;
2013-02-09 17:42:03 +04:00
} else {
2012-09-23 05:16:52 +04:00
basename = name ;
extension = false ;
2011-07-29 01:52:49 +04:00
}
2013-02-09 17:42:03 +04:00
var name _span = $ ( '<span></span>' ) . addClass ( 'nametext' ) . text ( basename ) ;
link _elem . append ( name _span ) ;
2013-10-22 20:11:03 +04:00
if ( extension ) {
2013-02-09 17:42:03 +04:00
name _span . append ( $ ( '<span></span>' ) . addClass ( 'extension' ) . text ( extension ) ) ;
}
2013-02-22 20:21:57 +04:00
//dirs can show the number of uploaded files
2013-10-22 20:11:03 +04:00
if ( type === 'dir' ) {
2013-02-09 17:42:03 +04:00
link _elem . append ( $ ( '<span></span>' ) . attr ( {
'class' : 'uploadtext' ,
'currentUploads' : 0
} ) ) ;
2011-07-29 01:52:49 +04:00
}
2013-02-09 17:42:03 +04:00
td . append ( link _elem ) ;
tr . append ( td ) ;
2013-02-22 20:21:57 +04:00
2013-02-09 17:42:03 +04:00
//size column
2013-10-22 20:11:03 +04:00
if ( size !== t ( 'files' , 'Pending' ) ) {
2013-07-19 00:15:26 +04:00
simpleSize = humanFileSize ( size ) ;
2013-10-22 20:11:03 +04:00
} else {
2013-02-09 17:42:03 +04:00
simpleSize = t ( 'files' , 'Pending' ) ;
2011-07-29 01:52:49 +04:00
}
2013-06-11 16:30:13 +04:00
var sizeColor = Math . round ( 160 - Math . pow ( ( size / ( 1024 * 1024 ) ) , 2 ) ) ;
2013-02-09 17:42:03 +04:00
var lastModifiedTime = Math . round ( lastModified . getTime ( ) / 1000 ) ;
td = $ ( '<td></td>' ) . attr ( {
"class" : "filesize" ,
"style" : 'color:rgb(' + sizeColor + ',' + sizeColor + ',' + sizeColor + ')'
} ) . text ( simpleSize ) ;
tr . append ( td ) ;
2013-02-22 20:21:57 +04:00
2013-02-09 17:42:03 +04:00
// date column
var modifiedColor = Math . round ( ( Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) - lastModifiedTime ) / 60 / 60 / 24 * 5 ) ;
td = $ ( '<td></td>' ) . attr ( { "class" : "date" } ) ;
td . append ( $ ( '<span></span>' ) . attr ( {
"class" : "modified" ,
"title" : formatDate ( lastModified ) ,
"style" : 'color:rgb(' + modifiedColor + ',' + modifiedColor + ',' + modifiedColor + ')'
} ) . text ( relative _modified _date ( lastModified . getTime ( ) / 1000 ) ) ) ;
tr . append ( td ) ;
return tr ;
} ,
2013-10-22 20:11:03 +04:00
addFile : function ( name , size , lastModified , loading , hidden , param ) {
2013-02-09 17:42:03 +04:00
var imgurl ;
2013-06-25 14:24:14 +04:00
if ( ! param ) {
param = { } ;
}
var download _url = null ;
if ( ! param . download _url ) {
download _url = OC . Router . generate ( 'download' , { file : $ ( '#dir' ) . val ( ) + '/' + name } ) ;
} else {
download _url = param . download _url ;
}
2013-02-09 17:42:03 +04:00
if ( loading ) {
imgurl = OC . imagePath ( 'core' , 'loading.gif' ) ;
} else {
imgurl = OC . imagePath ( 'core' , 'filetypes/file.png' ) ;
}
var tr = this . createRow (
'file' ,
name ,
imgurl ,
2013-06-25 14:24:14 +04:00
download _url ,
2013-02-09 17:42:03 +04:00
size ,
lastModified ,
$ ( '#permissions' ) . val ( )
) ;
2013-02-22 20:21:57 +04:00
2013-03-13 20:26:37 +04:00
FileList . insertElement ( name , 'file' , tr ) ;
2013-10-22 20:11:03 +04:00
if ( loading ) {
tr . data ( 'loading' , true ) ;
} else {
2013-03-13 20:26:37 +04:00
tr . find ( 'td.filename' ) . draggable ( dragOptions ) ;
2011-07-19 22:57:40 +04:00
}
2012-09-05 08:12:11 +04:00
if ( hidden ) {
2013-03-13 20:26:37 +04:00
tr . hide ( ) ;
2012-09-05 08:12:11 +04:00
}
2013-03-13 20:26:37 +04:00
return tr ;
2011-06-04 20:44:14 +04:00
} ,
2013-10-22 20:11:03 +04:00
addDir : function ( name , size , lastModified , hidden ) {
2013-02-22 20:21:57 +04:00
2013-02-09 17:42:03 +04:00
var tr = this . createRow (
'dir' ,
name ,
OC . imagePath ( 'core' , 'filetypes/folder.png' ) ,
OC . linkTo ( 'files' , 'index.php' ) + "?dir=" + encodeURIComponent ( $ ( '#dir' ) . val ( ) + '/' + name ) . replace ( /%2F/g , '/' ) ,
size ,
lastModified ,
$ ( '#permissions' ) . val ( )
) ;
2013-02-22 20:21:57 +04:00
2013-10-22 20:11:03 +04:00
FileList . insertElement ( name , 'dir' , tr ) ;
2013-03-13 20:26:37 +04:00
var td = tr . find ( 'td.filename' ) ;
td . draggable ( dragOptions ) ;
td . droppable ( folderDropOptions ) ;
2012-09-05 08:12:11 +04:00
if ( hidden ) {
2013-03-13 20:26:37 +04:00
tr . hide ( ) ;
2012-09-05 08:12:11 +04:00
}
2013-10-17 13:59:13 +04:00
FileActions . display ( tr . find ( 'td.filename' ) , true ) ;
2013-03-13 20:26:37 +04:00
return tr ;
2011-06-04 20:44:14 +04:00
} ,
2013-11-06 13:55:19 +04:00
getCurrentDirectory : function ( ) {
return $ ( '#dir' ) . val ( ) || '/' ;
} ,
2013-08-17 15:07:18 +04:00
/ * *
* @ brief Changes the current directory and reload the file list .
* @ param targetDir target directory ( non URL encoded )
* @ param changeUrl false if the URL must not be changed ( defaults to true )
2013-10-22 20:11:03 +04:00
* @ param { boolean } force set to true to force changing directory
2013-08-17 15:07:18 +04:00
* /
2013-10-22 20:11:03 +04:00
changeDirectory : function ( targetDir , changeUrl , force ) {
2013-08-17 15:07:18 +04:00
var $dir = $ ( '#dir' ) ,
url ,
2014-02-20 18:16:45 +04:00
currentDir = $dir . val ( ) || '/' ;
2013-08-17 15:07:18 +04:00
targetDir = targetDir || '/' ;
2013-10-22 20:11:03 +04:00
if ( ! force && currentDir === targetDir ) {
2013-08-17 15:07:18 +04:00
return ;
}
FileList . setCurrentDir ( targetDir , changeUrl ) ;
2013-11-08 16:15:48 +04:00
$ ( '#fileList' ) . trigger (
jQuery . Event ( 'changeDirectory' , {
dir : targetDir ,
previousDir : currentDir
}
) ) ;
2013-08-17 15:07:18 +04:00
FileList . reload ( ) ;
} ,
2013-10-22 20:11:03 +04:00
linkTo : function ( dir ) {
2013-08-29 23:56:14 +04:00
return OC . linkTo ( 'files' , 'index.php' ) + "?dir=" + encodeURIComponent ( dir ) . replace ( /%2F/g , '/' ) ;
} ,
2013-10-22 20:11:03 +04:00
setCurrentDir : function ( targetDir , changeUrl ) {
2014-02-20 18:16:45 +04:00
var url ,
baseDir = OC . basename ( targetDir ) ;
if ( baseDir !== '' ) {
FileList . setPageTitle ( baseDir ) ;
}
else {
FileList . setPageTitle ( ) ;
}
2013-08-17 15:07:18 +04:00
$ ( '#dir' ) . val ( targetDir ) ;
2013-10-22 20:11:03 +04:00
if ( changeUrl !== false ) {
if ( window . history . pushState && changeUrl !== false ) {
2013-08-29 23:56:14 +04:00
url = FileList . linkTo ( targetDir ) ;
window . history . pushState ( { dir : targetDir } , '' , url ) ;
}
// use URL hash for IE8
else {
window . location . hash = '?dir=' + encodeURIComponent ( targetDir ) . replace ( /%2F/g , '/' ) ;
}
2013-08-17 15:07:18 +04:00
}
} ,
/ * *
* @ brief Reloads the file list using ajax call
* /
2013-10-22 20:11:03 +04:00
reload : function ( ) {
2013-08-17 15:07:18 +04:00
FileList . showMask ( ) ;
2013-10-22 20:11:03 +04:00
if ( FileList . _reloadCall ) {
2013-08-17 15:07:18 +04:00
FileList . _reloadCall . abort ( ) ;
}
FileList . _reloadCall = $ . ajax ( {
url : OC . filePath ( 'files' , 'ajax' , 'list.php' ) ,
data : {
dir : $ ( '#dir' ) . val ( ) ,
breadcrumb : true
} ,
2013-10-22 20:11:03 +04:00
error : function ( result ) {
2013-08-17 15:07:18 +04:00
FileList . reloadCallback ( result ) ;
} ,
success : function ( result ) {
FileList . reloadCallback ( result ) ;
}
} ) ;
} ,
2013-10-22 20:11:03 +04:00
reloadCallback : function ( result ) {
2013-08-17 15:07:18 +04:00
var $controls = $ ( '#controls' ) ;
delete FileList . _reloadCall ;
FileList . hideMask ( ) ;
if ( ! result || result . status === 'error' ) {
OC . Notification . show ( result . data . message ) ;
return ;
}
2013-10-22 20:11:03 +04:00
if ( result . status === 404 ) {
2013-08-17 15:07:18 +04:00
// go back home
FileList . changeDirectory ( '/' ) ;
return ;
}
2013-11-06 13:55:19 +04:00
// TODO: should rather return upload file size through
// the files list ajax call
Files . updateStorageStatistics ( true ) ;
2013-10-22 20:11:03 +04:00
if ( result . data . permissions ) {
2013-08-30 01:45:02 +04:00
FileList . setDirectoryPermissions ( result . data . permissions ) ;
}
2013-10-22 20:11:03 +04:00
if ( typeof ( result . data . breadcrumb ) !== 'undefined' ) {
2013-08-17 15:07:18 +04:00
$controls . find ( '.crumb' ) . remove ( ) ;
$controls . prepend ( result . data . breadcrumb ) ;
2013-08-27 15:13:00 +04:00
var width = $ ( window ) . width ( ) ;
Files . initBreadCrumbs ( ) ;
Files . resizeBreadcrumbs ( width , true ) ;
2013-08-27 13:18:59 +04:00
// in case svg is not supported by the browser we need to execute the fallback mechanism
2013-10-22 20:11:03 +04:00
if ( ! SVGSupport ( ) ) {
2013-08-27 13:18:59 +04:00
replaceSVG ( ) ;
}
2011-06-04 20:44:14 +04:00
}
2013-08-27 13:18:59 +04:00
2011-06-04 20:44:14 +04:00
FileList . update ( result . data . files ) ;
} ,
2013-10-22 20:11:03 +04:00
setDirectoryPermissions : function ( permissions ) {
2013-08-30 01:45:02 +04:00
var isCreatable = ( permissions & OC . PERMISSION _CREATE ) !== 0 ;
$ ( '#permissions' ) . val ( permissions ) ;
$ ( '.creatable' ) . toggleClass ( 'hidden' , ! isCreatable ) ;
$ ( '.notCreatable' ) . toggleClass ( 'hidden' , isCreatable ) ;
2011-06-04 20:44:14 +04:00
} ,
2013-10-15 19:59:59 +04:00
/ * *
* Shows / hides action buttons
*
* @ param show true for enabling , false for disabling
* /
showActions : function ( show ) {
$ ( '.actions,#file_action_panel' ) . toggleClass ( 'hidden' , ! show ) ;
if ( show ) {
// make sure to display according to permissions
var permissions = $ ( '#permissions' ) . val ( ) ;
var isCreatable = ( permissions & OC . PERMISSION _CREATE ) !== 0 ;
$ ( '.creatable' ) . toggleClass ( 'hidden' , ! isCreatable ) ;
$ ( '.notCreatable' ) . toggleClass ( 'hidden' , isCreatable ) ;
}
2013-10-31 15:31:40 +04:00
else {
$ ( '.creatable, .notCreatable' ) . addClass ( 'hidden' ) ;
}
2013-10-15 19:59:59 +04:00
} ,
/ * *
* Enables / disables viewer mode .
* In viewer mode , apps can embed themselves under the controls bar .
* In viewer mode , the actions of the file list will be hidden .
* @ param show true for enabling , false for disabling
* /
setViewerMode : function ( show ) {
this . showActions ( ! show ) ;
$ ( '#filestable' ) . toggleClass ( 'hidden' , show ) ;
} ,
2011-06-04 20:44:14 +04:00
remove : function ( name ) {
2014-01-10 18:02:26 +04:00
var fileEl = FileList . findFileEl ( name ) ;
2013-12-09 19:03:23 +04:00
if ( fileEl . data ( 'permissions' ) & OC . PERMISSION _DELETE ) {
// file is only draggable when delete permissions are set
fileEl . find ( 'td.filename' ) . draggable ( 'destroy' ) ;
}
2014-01-10 18:02:26 +04:00
fileEl . remove ( ) ;
2013-07-03 21:50:03 +04:00
FileList . updateFileSummary ( ) ;
2013-10-22 20:11:03 +04:00
if ( ! $ ( 'tr[data-file]' ) . exists ( ) ) {
2013-08-17 15:07:18 +04:00
$ ( '#emptycontent' ) . removeClass ( 'hidden' ) ;
2013-10-12 19:28:20 +04:00
$ ( '#filescontent th' ) . addClass ( 'hidden' ) ;
2011-07-30 16:42:58 +04:00
}
2011-06-04 20:44:14 +04:00
} ,
2013-10-22 20:11:03 +04:00
insertElement : function ( name , type , element ) {
2011-06-04 20:44:14 +04:00
//find the correct spot to insert the file or folder
2012-09-23 05:16:52 +04:00
var pos , fileElements = $ ( 'tr[data-file][data-type="' + type + '"]:visible' ) ;
2013-10-22 20:11:03 +04:00
if ( name . localeCompare ( $ ( fileElements [ 0 ] ) . attr ( 'data-file' ) ) < 0 ) {
pos = - 1 ;
} else if ( name . localeCompare ( $ ( fileElements [ fileElements . length - 1 ] ) . attr ( 'data-file' ) ) > 0 ) {
pos = fileElements . length - 1 ;
} else {
for ( pos = 0 ; pos < fileElements . length - 1 ; pos ++ ) {
if ( name . localeCompare ( $ ( fileElements [ pos ] ) . attr ( 'data-file' ) ) > 0
&& name . localeCompare ( $ ( fileElements [ pos + 1 ] ) . attr ( 'data-file' ) ) < 0 )
{
2011-06-04 20:44:14 +04:00
break ;
}
}
}
2013-10-22 20:11:03 +04:00
if ( fileElements . exists ( ) ) {
if ( pos === - 1 ) {
2011-07-26 18:55:28 +04:00
$ ( fileElements [ 0 ] ) . before ( element ) ;
2013-10-22 20:11:03 +04:00
} else {
2011-07-26 18:55:28 +04:00
$ ( fileElements [ pos ] ) . after ( element ) ;
}
2013-10-22 20:11:03 +04:00
} else if ( type === 'dir' && $ ( 'tr[data-file]' ) . exists ( ) ) {
2011-07-26 18:55:28 +04:00
$ ( 'tr[data-file]' ) . first ( ) . before ( element ) ;
2013-10-22 20:11:03 +04:00
} else if ( type === 'file' && $ ( 'tr[data-file]' ) . exists ( ) ) {
2013-07-12 12:40:24 +04:00
$ ( 'tr[data-file]' ) . last ( ) . before ( element ) ;
2013-10-22 20:11:03 +04:00
} else {
2011-07-04 23:46:20 +04:00
$ ( '#fileList' ) . append ( element ) ;
}
2013-08-17 15:07:18 +04:00
$ ( '#emptycontent' ) . addClass ( 'hidden' ) ;
2013-10-12 19:28:20 +04:00
$ ( '#filestable th' ) . removeClass ( 'hidden' ) ;
2013-07-03 21:50:03 +04:00
FileList . updateFileSummary ( ) ;
2011-07-19 22:57:40 +04:00
} ,
2013-10-22 20:11:03 +04:00
loadingDone : function ( name , id ) {
2014-01-10 18:02:26 +04:00
var mime , tr = FileList . findFileEl ( name ) ;
2013-10-22 20:11:03 +04:00
tr . data ( 'loading' , false ) ;
mime = tr . data ( 'mime' ) ;
tr . attr ( 'data-mime' , mime ) ;
if ( id ) {
2012-10-08 19:47:02 +04:00
tr . attr ( 'data-id' , id ) ;
}
2013-08-24 01:19:21 +04:00
var path = getPathForPreview ( name ) ;
2013-10-28 14:22:34 +04:00
Files . lazyLoadPreview ( path , mime , function ( previewpath ) {
2013-07-02 13:13:22 +04:00
tr . find ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + previewpath + ')' ) ;
2013-10-28 14:22:34 +04:00
} , null , null , tr . attr ( 'data-etag' ) ) ;
2011-10-08 23:18:47 +04:00
tr . find ( 'td.filename' ) . draggable ( dragOptions ) ;
2011-07-19 22:57:40 +04:00
} ,
2014-01-10 18:02:26 +04:00
isLoading : function ( file ) {
return FileList . findFileEl ( file ) . data ( 'loading' ) ;
2011-07-29 01:04:34 +04:00
} ,
2013-10-22 20:11:03 +04:00
rename : function ( oldname ) {
2012-09-23 05:16:52 +04:00
var tr , td , input , form ;
2014-01-10 18:02:26 +04:00
tr = FileList . findFileEl ( oldname ) ;
2011-07-29 04:26:20 +04:00
tr . data ( 'renaming' , true ) ;
2013-10-22 20:11:03 +04:00
td = tr . children ( 'td.filename' ) ;
input = $ ( '<input type="text" class="filename"/>' ) . val ( oldname ) ;
form = $ ( '<form></form>' ) ;
2011-07-29 01:04:34 +04:00
form . append ( input ) ;
2012-10-18 16:16:59 +04:00
td . children ( 'a.name' ) . hide ( ) ;
td . append ( form ) ;
2011-07-29 01:04:34 +04:00
input . focus ( ) ;
2013-05-13 17:54:45 +04:00
//preselect input
var len = input . val ( ) . lastIndexOf ( '.' ) ;
if ( len === - 1 ) {
len = input . val ( ) . length ;
}
2013-10-22 20:11:03 +04:00
input . selectRange ( 0 , len ) ;
var checkInput = function ( ) {
var filename = input . val ( ) ;
if ( filename !== oldname ) {
if ( ! Files . isFileNameValid ( filename ) ) {
// Files.isFileNameValid(filename) throws an exception itself
} else if ( $ ( '#dir' ) . val ( ) === '/' && filename === 'Shared' ) {
2013-10-23 13:08:30 +04:00
throw t ( 'files' , 'In the home folder \'Shared\' is a reserved filename' ) ;
2013-10-22 20:11:03 +04:00
} else if ( FileList . inList ( filename ) ) {
throw t ( 'files' , '{new_name} already exists' , { new _name : filename } ) ;
}
}
return true ;
} ;
2014-02-04 14:14:00 +04:00
2013-10-22 20:11:03 +04:00
form . submit ( function ( event ) {
2011-07-29 01:04:34 +04:00
event . stopPropagation ( ) ;
event . preventDefault ( ) ;
2013-10-22 20:11:03 +04:00
try {
var newname = input . val ( ) ;
if ( newname !== oldname ) {
checkInput ( ) ;
2013-05-03 02:15:28 +04:00
// save background image, because it's replaced by a spinner while async request
var oldBackgroundImage = td . css ( 'background-image' ) ;
// mark as loading
td . css ( 'background-image' , 'url(' + OC . imagePath ( 'core' , 'loading.gif' ) + ')' ) ;
$ . ajax ( {
url : OC . filePath ( 'files' , 'ajax' , 'rename.php' ) ,
data : {
dir : $ ( '#dir' ) . val ( ) ,
newname : newname ,
2013-10-22 20:11:03 +04:00
file : oldname
2013-05-03 02:15:28 +04:00
} ,
success : function ( result ) {
if ( ! result || result . status === 'error' ) {
2013-10-22 20:11:03 +04:00
OC . dialogs . alert ( result . data . message , t ( 'core' , 'Could not rename file' ) ) ;
2013-05-03 02:15:28 +04:00
// revert changes
2013-10-22 20:11:03 +04:00
newname = oldname ;
2013-05-03 02:15:28 +04:00
tr . attr ( 'data-file' , newname ) ;
var path = td . children ( 'a.name' ) . attr ( 'href' ) ;
2013-10-22 20:11:03 +04:00
td . children ( 'a.name' ) . attr ( 'href' , path . replace ( encodeURIComponent ( oldname ) , encodeURIComponent ( newname ) ) ) ;
2014-02-07 20:47:42 +04:00
var basename = newname ;
2013-05-03 02:15:28 +04:00
if ( newname . indexOf ( '.' ) > 0 && tr . data ( 'type' ) !== 'dir' ) {
2014-02-07 20:47:42 +04:00
basename = newname . substr ( 0 , newname . lastIndexOf ( '.' ) ) ;
2013-05-03 02:15:28 +04:00
}
td . find ( 'a.name span.nametext' ) . text ( basename ) ;
if ( newname . indexOf ( '.' ) > 0 && tr . data ( 'type' ) !== 'dir' ) {
2013-10-22 20:11:03 +04:00
if ( ! td . find ( 'a.name span.extension' ) . exists ( ) ) {
2013-05-03 02:15:28 +04:00
td . find ( 'a.name span.nametext' ) . append ( '<span class="extension"></span>' ) ;
}
td . find ( 'a.name span.extension' ) . text ( newname . substr ( newname . lastIndexOf ( '.' ) ) ) ;
}
tr . find ( '.fileactions' ) . effect ( 'highlight' , { } , 5000 ) ;
tr . effect ( 'highlight' , { } , 5000 ) ;
2013-11-28 00:34:19 +04:00
// remove loading mark and recover old image
td . css ( 'background-image' , oldBackgroundImage ) ;
}
else {
var fileInfo = result . data ;
tr . attr ( 'data-mime' , fileInfo . mime ) ;
tr . attr ( 'data-etag' , fileInfo . etag ) ;
if ( fileInfo . isPreviewAvailable ) {
Files . lazyLoadPreview ( fileInfo . directory + '/' + fileInfo . name , result . data . mime , function ( previewpath ) {
tr . find ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + previewpath + ')' ) ;
} , null , null , result . data . etag ) ;
}
else {
tr . find ( 'td.filename' ) . removeClass ( 'preview' ) . attr ( 'style' , 'background-image:url(' + fileInfo . icon + ')' ) ;
}
2013-05-03 02:15:28 +04:00
}
2013-10-22 20:11:03 +04:00
// reinsert row
tr . detach ( ) ;
FileList . insertElement ( tr . attr ( 'data-file' ) , tr . attr ( 'data-type' ) , tr ) ;
2013-11-28 00:34:19 +04:00
// update file actions in case the extension changed
FileActions . display ( tr . find ( 'td.filename' ) , true ) ;
2012-07-30 20:21:58 +04:00
}
} ) ;
2012-09-19 16:05:09 +04:00
}
2013-10-22 20:11:03 +04:00
input . tipsy ( 'hide' ) ;
tr . data ( 'renaming' , false ) ;
tr . attr ( 'data-file' , newname ) ;
var path = td . children ( 'a.name' ) . attr ( 'href' ) ;
// FIXME this will fail if the path contains the filename.
td . children ( 'a.name' ) . attr ( 'href' , path . replace ( encodeURIComponent ( oldname ) , encodeURIComponent ( newname ) ) ) ;
var basename = newname ;
if ( newname . indexOf ( '.' ) > 0 && tr . data ( 'type' ) !== 'dir' ) {
basename = newname . substr ( 0 , newname . lastIndexOf ( '.' ) ) ;
2014-02-04 14:14:00 +04:00
}
2013-10-22 20:11:03 +04:00
td . find ( 'a.name span.nametext' ) . text ( basename ) ;
if ( newname . indexOf ( '.' ) > 0 && tr . data ( 'type' ) !== 'dir' ) {
if ( ! td . find ( 'a.name span.extension' ) . exists ( ) ) {
td . find ( 'a.name span.nametext' ) . append ( '<span class="extension"></span>' ) ;
}
td . find ( 'a.name span.extension' ) . text ( newname . substr ( newname . lastIndexOf ( '.' ) ) ) ;
2012-10-18 16:16:59 +04:00
}
2013-10-22 20:11:03 +04:00
form . remove ( ) ;
2013-10-02 23:09:03 +04:00
FileActions . display ( tr . find ( 'td.filename' ) , true ) ;
2013-10-22 20:11:03 +04:00
td . children ( 'a.name' ) . show ( ) ;
} catch ( error ) {
input . attr ( 'title' , error ) ;
input . tipsy ( { gravity : 'w' , trigger : 'manual' } ) ;
input . tipsy ( 'show' ) ;
input . addClass ( 'error' ) ;
2012-07-30 20:21:58 +04:00
}
2011-12-06 02:51:44 +04:00
return false ;
2011-07-29 01:04:34 +04:00
} ) ;
2013-10-22 20:11:03 +04:00
input . keyup ( function ( event ) {
// verify filename on typing
try {
checkInput ( ) ;
input . tipsy ( 'hide' ) ;
input . removeClass ( 'error' ) ;
} catch ( error ) {
input . attr ( 'title' , error ) ;
input . tipsy ( { gravity : 'w' , trigger : 'manual' } ) ;
input . tipsy ( 'show' ) ;
input . addClass ( 'error' ) ;
}
if ( event . keyCode === 27 ) {
input . tipsy ( 'hide' ) ;
2012-12-18 19:39:01 +04:00
tr . data ( 'renaming' , false ) ;
form . remove ( ) ;
td . children ( 'a.name' ) . show ( ) ;
}
} ) ;
2013-10-22 20:11:03 +04:00
input . click ( function ( event ) {
2011-07-29 01:04:34 +04:00
event . stopPropagation ( ) ;
event . preventDefault ( ) ;
} ) ;
2013-10-22 20:11:03 +04:00
input . blur ( function ( ) {
2011-07-29 19:51:17 +04:00
form . trigger ( 'submit' ) ;
2011-07-29 01:04:34 +04:00
} ) ;
2011-08-04 02:22:44 +04:00
} ,
2014-01-10 18:02:26 +04:00
inList : function ( file ) {
return FileList . findFileEl ( file ) . length ;
2012-09-05 08:12:11 +04:00
} ,
replace : function ( oldName , newName , isNewFile ) {
2012-07-30 20:21:58 +04:00
// Finish any existing actions
2014-01-10 18:02:26 +04:00
var oldFileEl = FileList . findFileEl ( oldName ) ;
var newFileEl = FileList . findFileEl ( newName ) ;
oldFileEl . hide ( ) ;
newFileEl . hide ( ) ;
var tr = oldFileEl . clone ( ) ;
2012-09-05 08:12:11 +04:00
tr . attr ( 'data-replace' , 'true' ) ;
tr . attr ( 'data-file' , newName ) ;
var td = tr . children ( 'td.filename' ) ;
td . children ( 'a.name .span' ) . text ( newName ) ;
var path = td . children ( 'a.name' ) . attr ( 'href' ) ;
td . children ( 'a.name' ) . attr ( 'href' , path . replace ( encodeURIComponent ( oldName ) , encodeURIComponent ( newName ) ) ) ;
2014-02-07 20:47:42 +04:00
var basename = newName ;
2012-09-05 08:12:11 +04:00
if ( newName . indexOf ( '.' ) > 0 ) {
2014-02-07 20:47:42 +04:00
basename = newName . substr ( 0 , newName . lastIndexOf ( '.' ) ) ;
2012-09-05 08:12:11 +04:00
}
td . children ( 'a.name' ) . empty ( ) ;
var span = $ ( '<span class="nametext"></span>' ) ;
span . text ( basename ) ;
td . children ( 'a.name' ) . append ( span ) ;
if ( newName . indexOf ( '.' ) > 0 ) {
span . append ( $ ( '<span class="extension">' + newName . substr ( newName . lastIndexOf ( '.' ) ) + '</span>' ) ) ;
}
FileList . insertElement ( newName , tr . data ( 'type' ) , tr ) ;
tr . show ( ) ;
2012-07-30 20:21:58 +04:00
FileList . replaceCanceled = false ;
FileList . replaceOldName = oldName ;
FileList . replaceNewName = newName ;
2012-09-05 08:12:11 +04:00
FileList . replaceIsNewFile = isNewFile ;
2012-08-20 19:24:10 +04:00
FileList . lastAction = function ( ) {
2012-07-30 20:21:58 +04:00
FileList . finishReplace ( ) ;
} ;
2013-02-09 20:20:08 +04:00
if ( ! isNewFile ) {
2013-10-22 20:11:03 +04:00
OC . Notification . showHtml ( t ( 'files' , 'replaced {new_name} with {old_name}' , { new _name : newName } , { old _name : oldName } ) + '<span class="undo">' + t ( 'files' , 'undo' ) + '</span>' ) ;
2012-09-05 08:12:11 +04:00
}
2012-07-30 20:21:58 +04:00
} ,
finishReplace : function ( ) {
if ( ! FileList . replaceCanceled && FileList . replaceOldName && FileList . replaceNewName ) {
2012-09-06 06:13:50 +04:00
$ . ajax ( { url : OC . filePath ( 'files' , 'ajax' , 'rename.php' ) , async : false , data : { dir : $ ( '#dir' ) . val ( ) , newname : FileList . replaceNewName , file : FileList . replaceOldName } , success : function ( result ) {
2013-10-22 20:11:03 +04:00
if ( result && result . status === 'success' ) {
$ ( 'tr[data-replace="true"' ) . removeAttr ( 'data-replace' ) ;
2012-09-06 06:13:50 +04:00
} else {
OC . dialogs . alert ( result . data . message , 'Error moving file' ) ;
}
FileList . replaceCanceled = true ;
FileList . replaceOldName = null ;
FileList . replaceNewName = null ;
FileList . lastAction = null ;
} } ) ;
2012-07-30 20:21:58 +04:00
}
} ,
2014-02-13 23:20:00 +04:00
do _delete : function ( files , dir ) {
var params ;
if ( files && files . substr ) {
2013-01-30 17:32:20 +04:00
files = [ files ] ;
2013-02-21 00:57:50 +04:00
}
2014-02-13 23:20:00 +04:00
if ( files ) {
for ( var i = 0 ; i < files . length ; i ++ ) {
var deleteAction = FileList . findFileEl ( files [ i ] ) . children ( "td.date" ) . children ( ".action.delete" ) ;
deleteAction . removeClass ( 'delete-icon' ) . addClass ( 'progress-icon' ) ;
}
2013-01-30 17:32:20 +04:00
}
2012-07-30 20:21:58 +04:00
// Finish any existing actions
2012-09-19 13:56:31 +04:00
if ( FileList . lastAction ) {
2012-07-30 20:21:58 +04:00
FileList . lastAction ( ) ;
2011-08-04 02:22:44 +04:00
}
2012-10-14 23:04:08 +04:00
2014-02-13 23:20:00 +04:00
var params = {
dir : dir || FileList . getCurrentDirectory ( )
} ;
if ( files ) {
params . files = JSON . stringify ( files ) ;
}
else {
// no files passed, delete all in current dir
params . allfiles = true ;
}
2013-01-22 20:43:46 +04:00
$ . post ( OC . filePath ( 'files' , 'ajax' , 'delete.php' ) ,
2014-02-13 23:20:00 +04:00
params ,
2013-10-22 20:11:03 +04:00
function ( result ) {
if ( result . status === 'success' ) {
2014-02-13 23:20:00 +04:00
if ( params . allfiles ) {
// clear whole list
$ ( '#fileList tr' ) . remove ( ) ;
}
else {
$ . each ( files , function ( index , file ) {
var files = FileList . findFileEl ( file ) ;
files . remove ( ) ;
files . find ( 'input[type="checkbox"]' ) . removeAttr ( 'checked' ) ;
files . removeClass ( 'selected' ) ;
} ) ;
}
2013-01-22 20:43:46 +04:00
procesSelection ( ) ;
2013-07-26 13:13:43 +04:00
checkTrashStatus ( ) ;
2013-07-03 21:50:03 +04:00
FileList . updateFileSummary ( ) ;
2013-10-21 00:47:44 +04:00
FileList . updateEmptyContent ( ) ;
2013-11-06 13:15:05 +04:00
Files . updateStorageStatistics ( ) ;
2013-01-30 17:32:20 +04:00
} else {
2013-11-14 19:48:04 +04:00
if ( result . status === 'error' && result . data . message ) {
OC . Notification . show ( result . data . message ) ;
}
else {
OC . Notification . show ( t ( 'files' , 'Error deleting file.' ) ) ;
}
// hide notification after 10 sec
setTimeout ( function ( ) {
OC . Notification . hide ( ) ;
} , 10000 ) ;
2014-02-13 23:20:00 +04:00
if ( params . allfiles ) {
// reload the page as we don't know what files were deleted
// and which ones remain
FileList . reload ( ) ;
}
else {
$ . each ( files , function ( index , file ) {
var deleteAction = FileList . findFileEl ( file ) . find ( '.action.delete' ) ;
deleteAction . removeClass ( 'progress-icon' ) . addClass ( 'delete-icon' ) ;
} ) ;
}
2013-02-22 20:21:57 +04:00
}
2013-01-22 20:43:46 +04:00
} ) ;
2013-07-03 21:50:03 +04:00
} ,
createFileSummary : function ( ) {
2013-10-17 19:18:56 +04:00
if ( $ ( '#fileList tr' ) . exists ( ) ) {
var summary = this . _calculateFileSummary ( ) ;
2013-07-03 21:50:03 +04:00
// Get translations
2013-10-17 19:18:56 +04:00
var directoryInfo = n ( 'files' , '%n folder' , '%n folders' , summary . totalDirs ) ;
var fileInfo = n ( 'files' , '%n file' , '%n files' , summary . totalFiles ) ;
2013-07-03 21:50:03 +04:00
var infoVars = {
dirs : '<span class="dirinfo">' + directoryInfo + '</span><span class="connector">' ,
files : '</span><span class="fileinfo">' + fileInfo + '</span>'
2013-10-22 20:11:03 +04:00
} ;
2013-07-03 21:50:03 +04:00
var info = t ( 'files' , '{dirs} and {files}' , infoVars ) ;
// don't show the filesize column, if filesize is NaN (e.g. in trashbin)
2013-10-17 19:18:56 +04:00
if ( isNaN ( summary . totalSize ) ) {
2013-07-03 21:50:03 +04:00
var fileSize = '' ;
} else {
2013-10-17 19:18:56 +04:00
var fileSize = '<td class="filesize">' + humanFileSize ( summary . totalSize ) + '</td>' ;
2013-07-03 21:50:03 +04:00
}
2013-11-27 22:09:49 +04:00
var $summary = $ ( '<tr class="summary" data-file="undefined"><td><span class="info">' + info + '</span></td>' + fileSize + '<td></td></tr>' ) ;
2013-10-30 19:14:08 +04:00
$ ( '#fileList' ) . append ( $summary ) ;
2013-07-03 21:50:03 +04:00
2013-10-30 19:14:08 +04:00
var $dirInfo = $summary . find ( '.dirinfo' ) ;
var $fileInfo = $summary . find ( '.fileinfo' ) ;
var $connector = $summary . find ( '.connector' ) ;
2013-07-03 21:50:03 +04:00
// Show only what's necessary, e.g.: no files: don't show "0 files"
2013-11-07 13:11:18 +04:00
if ( summary . totalDirs === 0 ) {
2013-07-03 21:50:03 +04:00
$dirInfo . hide ( ) ;
$connector . hide ( ) ;
}
2013-11-07 13:11:18 +04:00
if ( summary . totalFiles === 0 ) {
2013-07-03 21:50:03 +04:00
$fileInfo . hide ( ) ;
$connector . hide ( ) ;
}
}
} ,
2013-10-17 19:18:56 +04:00
_calculateFileSummary : function ( ) {
var result = {
totalDirs : 0 ,
totalFiles : 0 ,
totalSize : 0
} ;
$ . each ( $ ( 'tr[data-file]' ) , function ( index , value ) {
var $value = $ ( value ) ;
if ( $value . data ( 'type' ) === 'dir' ) {
result . totalDirs ++ ;
} else if ( $value . data ( 'type' ) === 'file' ) {
result . totalFiles ++ ;
}
if ( $value . data ( 'size' ) !== undefined && $value . data ( 'id' ) !== - 1 ) {
//Skip shared as it does not count toward quota
result . totalSize += parseInt ( $value . data ( 'size' ) ) ;
}
} ) ;
return result ;
} ,
2013-07-03 21:50:03 +04:00
updateFileSummary : function ( ) {
var $summary = $ ( '.summary' ) ;
// Check if we should remove the summary to show "Upload something"
if ( $ ( '#fileList tr' ) . length === 1 && $summary . length === 1 ) {
$summary . remove ( ) ;
}
// If there's no summary create one (createFileSummary checks if there's data)
else if ( $summary . length === 0 ) {
FileList . createFileSummary ( ) ;
}
// There's a summary and data -> Update the summary
else if ( $ ( '#fileList tr' ) . length > 1 && $summary . length === 1 ) {
2013-10-17 19:18:56 +04:00
var fileSummary = this . _calculateFileSummary ( ) ;
2013-07-03 21:50:03 +04:00
var $dirInfo = $ ( '.summary .dirinfo' ) ;
var $fileInfo = $ ( '.summary .fileinfo' ) ;
var $connector = $ ( '.summary .connector' ) ;
// Substitute old content with new translations
2013-10-17 19:18:56 +04:00
$dirInfo . html ( n ( 'files' , '%n folder' , '%n folders' , fileSummary . totalDirs ) ) ;
$fileInfo . html ( n ( 'files' , '%n file' , '%n files' , fileSummary . totalFiles ) ) ;
$ ( '.summary .filesize' ) . html ( humanFileSize ( fileSummary . totalSize ) ) ;
2013-07-03 21:50:03 +04:00
// Show only what's necessary (may be hidden)
2013-11-13 13:52:05 +04:00
if ( fileSummary . totalDirs === 0 ) {
2013-07-03 21:50:03 +04:00
$dirInfo . hide ( ) ;
$connector . hide ( ) ;
} else {
$dirInfo . show ( ) ;
}
2013-11-13 13:52:05 +04:00
if ( fileSummary . totalFiles === 0 ) {
2013-07-03 21:50:03 +04:00
$fileInfo . hide ( ) ;
$connector . hide ( ) ;
} else {
$fileInfo . show ( ) ;
}
2013-11-13 13:52:05 +04:00
if ( fileSummary . totalDirs > 0 && fileSummary . totalFiles > 0 ) {
2013-07-03 21:50:03 +04:00
$connector . show ( ) ;
}
}
2013-08-17 15:07:18 +04:00
} ,
2013-10-23 14:02:06 +04:00
updateEmptyContent : function ( ) {
2013-10-21 00:47:44 +04:00
var $fileList = $ ( '#fileList' ) ;
var permissions = $ ( '#permissions' ) . val ( ) ;
var isCreatable = ( permissions & OC . PERMISSION _CREATE ) !== 0 ;
2013-10-31 01:34:37 +04:00
var exists = $fileList . find ( 'tr:first' ) . exists ( ) ;
$ ( '#emptycontent' ) . toggleClass ( 'hidden' , ! isCreatable || exists ) ;
$ ( '#filestable th' ) . toggleClass ( 'hidden' , ! exists ) ;
2013-10-21 00:47:44 +04:00
} ,
2013-10-22 20:11:03 +04:00
showMask : function ( ) {
2013-08-17 15:07:18 +04:00
// in case one was shown before
var $mask = $ ( '#content .mask' ) ;
2013-10-22 20:11:03 +04:00
if ( $mask . exists ( ) ) {
2013-08-17 15:07:18 +04:00
return ;
}
$mask = $ ( '<div class="mask transparent"></div>' ) ;
$mask . css ( 'background-image' , 'url(' + OC . imagePath ( 'core' , 'loading.gif' ) + ')' ) ;
2013-08-27 12:29:28 +04:00
$mask . css ( 'background-repeat' , 'no-repeat' ) ;
2013-08-17 15:07:18 +04:00
$ ( '#content' ) . append ( $mask ) ;
// block UI, but only make visible in case loading takes longer
2013-10-22 20:11:03 +04:00
FileList . _maskTimeout = window . setTimeout ( function ( ) {
2013-08-17 15:07:18 +04:00
// reset opacity
$mask . removeClass ( 'transparent' ) ;
} , 250 ) ;
} ,
2013-10-22 20:11:03 +04:00
hideMask : function ( ) {
2013-08-17 15:07:18 +04:00
var $mask = $ ( '#content .mask' ) . remove ( ) ;
2013-10-22 20:11:03 +04:00
if ( FileList . _maskTimeout ) {
2013-08-17 15:07:18 +04:00
window . clearTimeout ( FileList . _maskTimeout ) ;
}
2013-08-01 00:24:52 +04:00
} ,
scrollTo : function ( file ) {
//scroll to and highlight preselected file
2014-01-10 18:02:26 +04:00
var $scrolltorow = FileList . findFileEl ( file ) ;
2013-10-22 20:11:03 +04:00
if ( $scrolltorow . exists ( ) ) {
$scrolltorow . addClass ( 'searchresult' ) ;
$ ( window ) . scrollTop ( $scrolltorow . position ( ) . top ) ;
2013-08-01 00:24:52 +04:00
//remove highlight when hovered over
2013-10-22 20:11:03 +04:00
$scrolltorow . one ( 'hover' , function ( ) {
$scrolltorow . removeClass ( 'searchresult' ) ;
2013-08-01 00:24:52 +04:00
} ) ;
}
} ,
2013-10-22 20:11:03 +04:00
filter : function ( query ) {
$ ( '#fileList tr:not(.summary)' ) . each ( function ( i , e ) {
2013-11-18 20:18:31 +04:00
if ( $ ( e ) . data ( 'file' ) . toString ( ) . toLowerCase ( ) . indexOf ( query . toLowerCase ( ) ) !== - 1 ) {
2013-08-01 00:24:52 +04:00
$ ( e ) . addClass ( "searchresult" ) ;
} else {
$ ( e ) . removeClass ( "searchresult" ) ;
}
} ) ;
//do not use scrollto to prevent removing searchresult css class
var first = $ ( '#fileList tr.searchresult' ) . first ( ) ;
2013-10-22 20:11:03 +04:00
if ( first . exists ( ) ) {
2013-08-01 00:24:52 +04:00
$ ( window ) . scrollTop ( first . position ( ) . top ) ;
}
} ,
2013-10-22 20:11:03 +04:00
unfilter : function ( ) {
$ ( '#fileList tr.searchresult' ) . each ( function ( i , e ) {
2013-08-01 00:24:52 +04:00
$ ( e ) . removeClass ( "searchresult" ) ;
} ) ;
2014-01-24 16:32:31 +04:00
} ,
2014-02-13 23:20:00 +04:00
/ * *
* Returns whether all files are selected
* @ return true if all files are selected , false otherwise
* /
isAllSelected : function ( ) {
return $ ( '#select_all' ) . prop ( 'checked' ) ;
} ,
2014-01-24 16:32:31 +04:00
/ * *
* Returns the download URL of the given file
* @ param filename file name of the file
* @ param dir optional directory in which the file name is , defaults to the current directory
* /
getDownloadUrl : function ( filename , dir ) {
2014-02-13 23:20:00 +04:00
var files = filename ;
if ( $ . isArray ( filename ) ) {
files = JSON . stringify ( filename ) ;
}
2014-01-24 16:32:31 +04:00
var params = {
dir : dir || FileList . getCurrentDirectory ( ) ,
2014-02-13 23:20:00 +04:00
files : files
2014-01-24 16:32:31 +04:00
} ;
return OC . filePath ( 'files' , 'ajax' , 'download.php' ) + '?' + OC . buildQueryString ( params ) ;
2011-06-04 20:44:14 +04:00
}
2012-09-23 05:16:52 +04:00
} ;
2011-08-04 02:22:44 +04:00
2013-10-22 20:11:03 +04:00
$ ( document ) . ready ( function ( ) {
2014-02-12 03:54:35 +04:00
var baseDir ,
2014-02-20 18:36:52 +04:00
isPublic = ! ! $ ( '#isPublic' ) . val ( ) ;
2013-03-13 20:26:37 +04:00
// handle upload events
2013-03-27 18:55:44 +04:00
var file _upload _start = $ ( '#file_upload_start' ) ;
2013-09-08 19:29:43 +04:00
2013-03-27 18:55:44 +04:00
file _upload _start . on ( 'fileuploaddrop' , function ( e , data ) {
2013-09-18 16:39:39 +04:00
OC . Upload . log ( 'filelist handle fileuploaddrop' , e , data ) ;
2013-09-08 19:29:43 +04:00
2013-10-15 18:14:23 +04:00
var dropTarget = $ ( e . originalEvent . target ) . closest ( 'tr, .crumb' ) ;
2013-10-22 20:11:03 +04:00
if ( dropTarget && ( dropTarget . data ( 'type' ) === 'dir' || dropTarget . hasClass ( 'crumb' ) ) ) { // drag&drop upload to folder
2013-09-08 19:29:43 +04:00
2013-09-07 00:40:10 +04:00
// remember as context
data . context = dropTarget ;
2013-09-08 19:29:43 +04:00
2013-09-06 20:51:27 +04:00
var dir = dropTarget . data ( 'file' ) ;
2013-10-15 18:14:23 +04:00
// if from file list, need to prepend parent dir
2013-10-22 20:11:03 +04:00
if ( dir ) {
2013-10-15 18:14:23 +04:00
var parentDir = $ ( '#dir' ) . val ( ) || '/' ;
2013-10-22 20:11:03 +04:00
if ( parentDir [ parentDir . length - 1 ] !== '/' ) {
2013-10-15 18:14:23 +04:00
parentDir += '/' ;
}
dir = parentDir + dir ;
}
else {
// read full path from crumb
dir = dropTarget . data ( 'dir' ) || '/' ;
}
2013-09-08 19:29:43 +04:00
2013-08-16 13:40:55 +04:00
// update folder in form
data . formData = function ( form ) {
2013-10-15 18:14:23 +04:00
return [
{ name : 'dir' , value : dir } ,
{ name : 'requesttoken' , value : oc _requesttoken }
] ;
2013-08-16 13:40:55 +04:00
} ;
2014-01-30 13:41:04 +04:00
}
2013-09-08 19:29:43 +04:00
2013-03-13 20:26:37 +04:00
} ) ;
2013-03-27 18:55:44 +04:00
file _upload _start . on ( 'fileuploadadd' , function ( e , data ) {
2013-09-18 16:39:39 +04:00
OC . Upload . log ( 'filelist handle fileuploadadd' , e , data ) ;
2013-09-08 19:29:43 +04:00
2013-09-08 12:41:20 +04:00
//finish delete if we are uploading a deleted file
2013-10-22 20:11:03 +04:00
if ( FileList . deleteFiles && FileList . deleteFiles . indexOf ( data . files [ 0 ] . name ) !== - 1 ) {
2013-08-16 13:40:55 +04:00
FileList . finishDelete ( null , true ) ; //delete file before continuing
}
2013-09-08 19:29:43 +04:00
2013-08-16 13:40:55 +04:00
// add ui visualization to existing folder
2013-10-22 20:11:03 +04:00
if ( data . context && data . context . data ( 'type' ) === 'dir' ) {
2013-08-16 13:40:55 +04:00
// add to existing folder
// update upload counter ui
var uploadtext = data . context . find ( '.uploadtext' ) ;
var currentUploads = parseInt ( uploadtext . attr ( 'currentUploads' ) ) ;
currentUploads += 1 ;
uploadtext . attr ( 'currentUploads' , currentUploads ) ;
2013-09-08 19:29:43 +04:00
2013-09-04 17:24:25 +04:00
var translatedText = n ( 'files' , 'Uploading %n file' , 'Uploading %n files' , currentUploads ) ;
2013-10-22 20:11:03 +04:00
if ( currentUploads === 1 ) {
2013-08-16 13:40:55 +04:00
var img = OC . imagePath ( 'core' , 'loading.gif' ) ;
data . context . find ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + img + ')' ) ;
2013-09-04 17:24:25 +04:00
uploadtext . text ( translatedText ) ;
2013-08-16 13:40:55 +04:00
uploadtext . show ( ) ;
} else {
2013-09-04 17:24:25 +04:00
uploadtext . text ( translatedText ) ;
2013-03-13 20:26:37 +04:00
}
2013-08-16 13:40:55 +04:00
}
2013-09-08 19:29:43 +04:00
2013-08-16 13:40:55 +04:00
} ) ;
2013-09-08 12:41:20 +04:00
/ *
* when file upload done successfully add row to filelist
* update counter when uploading to sub folder
* /
2013-08-16 13:40:55 +04:00
file _upload _start . on ( 'fileuploaddone' , function ( e , data ) {
2013-09-18 16:39:39 +04:00
OC . Upload . log ( 'filelist handle fileuploaddone' , e , data ) ;
2014-02-04 14:14:00 +04:00
2013-08-16 13:40:55 +04:00
var response ;
if ( typeof data . result === 'string' ) {
response = data . result ;
} else {
// fetch response from iframe
response = data . result [ 0 ] . body . innerText ;
}
var result = $ . parseJSON ( response ) ;
2013-03-13 20:26:37 +04:00
2013-10-22 20:11:03 +04:00
if ( typeof result [ 0 ] !== 'undefined' && result [ 0 ] . status === 'success' ) {
2013-08-16 13:40:55 +04:00
var file = result [ 0 ] ;
2013-03-13 20:26:37 +04:00
2013-08-16 13:40:55 +04:00
if ( data . context && data . context . data ( 'type' ) === 'dir' ) {
2013-03-13 20:26:37 +04:00
// update upload counter ui
var uploadtext = data . context . find ( '.uploadtext' ) ;
var currentUploads = parseInt ( uploadtext . attr ( 'currentUploads' ) ) ;
2013-08-16 13:40:55 +04:00
currentUploads -= 1 ;
2013-03-13 20:26:37 +04:00
uploadtext . attr ( 'currentUploads' , currentUploads ) ;
2013-08-14 08:29:19 +04:00
var translatedText = n ( 'files' , 'Uploading %n file' , 'Uploading %n files' , currentUploads ) ;
2013-10-22 20:11:03 +04:00
if ( currentUploads === 0 ) {
2013-08-16 13:40:55 +04:00
var img = OC . imagePath ( 'core' , 'filetypes/folder.png' ) ;
2013-03-13 20:26:37 +04:00
data . context . find ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + img + ')' ) ;
2013-08-09 22:37:18 +04:00
uploadtext . text ( translatedText ) ;
2013-09-07 00:40:10 +04:00
uploadtext . hide ( ) ;
2013-03-13 20:26:37 +04:00
} else {
2013-08-09 22:37:18 +04:00
uploadtext . text ( translatedText ) ;
2013-03-13 20:26:37 +04:00
}
2013-09-08 19:29:43 +04:00
2013-08-16 13:40:55 +04:00
// update folder size
var size = parseInt ( data . context . data ( 'size' ) ) ;
size += parseInt ( file . size ) ;
data . context . attr ( 'data-size' , size ) ;
data . context . find ( 'td.filesize' ) . text ( humanFileSize ( size ) ) ;
2013-09-08 19:29:43 +04:00
2013-03-13 20:26:37 +04:00
} else {
2014-02-07 15:53:42 +04:00
// only append new file if uploaded into the current folder
if ( file . directory !== FileList . getCurrentDirectory ( ) ) {
2013-10-15 18:14:23 +04:00
return ;
}
2013-03-13 20:26:37 +04:00
2013-08-16 13:40:55 +04:00
// add as stand-alone row to filelist
2013-09-04 17:24:25 +04:00
var size = t ( 'files' , 'Pending' ) ;
2013-10-22 20:11:03 +04:00
if ( data . files [ 0 ] . size >= 0 ) {
2013-08-16 13:40:55 +04:00
size = data . files [ 0 ] . size ;
}
var date = new Date ( ) ;
var param = { } ;
2013-10-22 20:11:03 +04:00
if ( $ ( '#publicUploadRequestToken' ) . exists ( ) ) {
2013-08-16 13:40:55 +04:00
param . download _url = document . location . href + '&download&path=/' + $ ( '#dir' ) . val ( ) + '/' + file . name ;
}
//should the file exist in the list remove it
FileList . remove ( file . name ) ;
2013-09-08 19:29:43 +04:00
2013-08-16 13:40:55 +04:00
// create new file context
data . context = FileList . addFile ( file . name , file . size , date , false , false , param ) ;
2013-03-13 20:26:37 +04:00
2013-08-16 13:40:55 +04:00
// update file data
2013-10-28 14:22:34 +04:00
data . context . attr ( 'data-mime' , file . mime ) . attr ( 'data-id' , file . id ) . attr ( 'data-etag' , file . etag ) ;
2013-03-13 20:26:37 +04:00
2013-09-19 19:00:54 +04:00
var permissions = data . context . data ( 'permissions' ) ;
2013-10-22 20:11:03 +04:00
if ( permissions !== file . permissions ) {
2013-09-19 19:00:54 +04:00
data . context . attr ( 'data-permissions' , file . permissions ) ;
data . context . data ( 'permissions' , file . permissions ) ;
}
2013-10-17 13:59:13 +04:00
FileActions . display ( data . context . find ( 'td.filename' ) , true ) ;
2013-03-13 20:26:37 +04:00
2013-09-04 17:24:25 +04:00
var path = getPathForPreview ( file . name ) ;
2013-10-28 14:22:34 +04:00
Files . lazyLoadPreview ( path , file . mime , function ( previewpath ) {
2013-09-04 17:24:25 +04:00
data . context . find ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + previewpath + ')' ) ;
2013-10-28 14:22:34 +04:00
} , null , null , file . etag ) ;
2013-03-13 20:26:37 +04:00
}
}
} ) ;
2013-08-16 13:40:55 +04:00
file _upload _start . on ( 'fileuploadstop' , function ( e , data ) {
2013-09-18 16:39:39 +04:00
OC . Upload . log ( 'filelist handle fileuploadstop' , e , data ) ;
2013-09-08 19:29:43 +04:00
2013-08-21 16:58:28 +04:00
//if user pressed cancel hide upload chrome
2013-09-07 00:40:10 +04:00
if ( data . errorThrown === 'abort' ) {
2013-08-21 16:58:28 +04:00
//cleanup uploading to a dir
var uploadtext = $ ( 'tr .uploadtext' ) ;
var img = OC . imagePath ( 'core' , 'filetypes/folder.png' ) ;
uploadtext . parents ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + img + ')' ) ;
uploadtext . fadeOut ( ) ;
uploadtext . attr ( 'currentUploads' , 0 ) ;
}
2013-08-16 13:40:55 +04:00
} ) ;
file _upload _start . on ( 'fileuploadfail' , function ( e , data ) {
2013-09-18 16:39:39 +04:00
OC . Upload . log ( 'filelist handle fileuploadfail' , e , data ) ;
2013-09-08 19:29:43 +04:00
2013-08-21 16:58:28 +04:00
//if user pressed cancel hide upload chrome
if ( data . errorThrown === 'abort' ) {
//cleanup uploading to a dir
var uploadtext = $ ( 'tr .uploadtext' ) ;
var img = OC . imagePath ( 'core' , 'filetypes/folder.png' ) ;
uploadtext . parents ( 'td.filename' ) . attr ( 'style' , 'background-image:url(' + img + ')' ) ;
uploadtext . fadeOut ( ) ;
uploadtext . attr ( 'currentUploads' , 0 ) ;
}
2013-08-16 13:40:55 +04:00
} ) ;
2013-09-08 19:29:43 +04:00
2011-08-05 09:37:08 +04:00
$ ( '#notification' ) . hide ( ) ;
2013-10-22 20:11:03 +04:00
$ ( '#notification' ) . on ( 'click' , '.undo' , function ( ) {
2012-07-30 20:21:58 +04:00
if ( FileList . deleteFiles ) {
2013-10-22 20:11:03 +04:00
$ . each ( FileList . deleteFiles , function ( index , file ) {
2014-01-10 18:02:26 +04:00
FileList . findFileEl ( file ) . show ( ) ;
2011-10-17 22:39:01 +04:00
} ) ;
FileList . deleteCanceled = true ;
FileList . deleteFiles = null ;
2012-07-30 20:21:58 +04:00
} else if ( FileList . replaceOldName && FileList . replaceNewName ) {
2012-09-05 08:12:11 +04:00
if ( FileList . replaceIsNewFile ) {
// Delete the new uploaded file
FileList . deleteCanceled = false ;
FileList . deleteFiles = [ FileList . replaceOldName ] ;
} else {
2014-01-10 18:02:26 +04:00
FileList . findFileEl ( FileList . replaceOldName ) . show ( ) ;
2012-09-05 08:12:11 +04:00
}
2013-10-22 20:11:03 +04:00
$ ( 'tr[data-replace="true"' ) . remove ( ) ;
2014-01-10 18:02:26 +04:00
FileList . findFileEl ( FileList . replaceNewName ) . show ( ) ;
2012-07-30 20:21:58 +04:00
FileList . replaceCanceled = true ;
FileList . replaceOldName = null ;
FileList . replaceNewName = null ;
2012-09-05 08:12:11 +04:00
FileList . replaceIsNewFile = null ;
2011-10-17 22:39:01 +04:00
}
2012-08-23 01:08:10 +04:00
FileList . lastAction = null ;
2013-08-01 00:24:52 +04:00
OC . Notification . hide ( ) ;
2011-08-04 02:22:44 +04:00
} ) ;
2013-02-09 20:20:08 +04:00
$ ( '#notification:first-child' ) . on ( 'click' , '.replace' , function ( ) {
2013-08-01 00:24:52 +04:00
OC . Notification . hide ( function ( ) {
FileList . replace ( $ ( '#notification > span' ) . attr ( 'data-oldName' ) , $ ( '#notification > span' ) . attr ( 'data-newName' ) , $ ( '#notification > span' ) . attr ( 'data-isNewFile' ) ) ;
} ) ;
2012-07-30 20:21:58 +04:00
} ) ;
2013-02-09 20:20:08 +04:00
$ ( '#notification:first-child' ) . on ( 'click' , '.suggest' , function ( ) {
2014-01-10 18:02:26 +04:00
var file = $ ( '#notification > span' ) . attr ( 'data-oldName' ) ;
FileList . findFileEl ( file ) . show ( ) ;
2013-08-01 00:24:52 +04:00
OC . Notification . hide ( ) ;
2012-09-05 08:12:11 +04:00
} ) ;
2013-02-09 20:20:08 +04:00
$ ( '#notification:first-child' ) . on ( 'click' , '.cancel' , function ( ) {
if ( $ ( '#notification > span' ) . attr ( 'data-isNewFile' ) ) {
2012-09-05 08:12:11 +04:00
FileList . deleteCanceled = false ;
2013-02-09 20:20:08 +04:00
FileList . deleteFiles = [ $ ( '#notification > span' ) . attr ( 'data-oldName' ) ] ;
2012-09-05 08:12:11 +04:00
}
2012-07-30 20:21:58 +04:00
} ) ;
2012-09-23 05:16:52 +04:00
FileList . useUndo = ( window . onbeforeunload ) ? true : false ;
2013-10-22 20:11:03 +04:00
$ ( window ) . bind ( 'beforeunload' , function ( ) {
2012-07-30 20:21:58 +04:00
if ( FileList . lastAction ) {
FileList . lastAction ( ) ;
}
2011-08-04 02:22:44 +04:00
} ) ;
2013-10-22 20:11:03 +04:00
$ ( window ) . unload ( function ( ) {
2012-11-05 21:42:44 +04:00
$ ( window ) . trigger ( 'beforeunload' ) ;
} ) ;
2013-07-03 21:50:03 +04:00
2013-10-22 20:11:03 +04:00
function decodeQuery ( query ) {
2013-09-20 23:52:40 +04:00
return query . replace ( /\+/g , ' ' ) ;
}
2013-10-22 20:11:03 +04:00
function parseHashQuery ( ) {
2013-08-29 23:56:14 +04:00
var hash = window . location . hash ,
pos = hash . indexOf ( '?' ) ,
query ;
2013-10-22 20:11:03 +04:00
if ( pos >= 0 ) {
2013-08-29 23:56:14 +04:00
return hash . substr ( pos + 1 ) ;
}
return '' ;
}
2013-10-22 20:11:03 +04:00
function parseCurrentDirFromUrl ( ) {
2013-08-29 23:56:14 +04:00
var query = parseHashQuery ( ) ,
params ,
dir = '/' ;
// try and parse from URL hash first
2013-10-22 20:11:03 +04:00
if ( query ) {
2013-09-20 23:52:40 +04:00
params = OC . parseQueryString ( decodeQuery ( query ) ) ;
2013-08-29 23:56:14 +04:00
}
// else read from query attributes
2013-10-22 20:11:03 +04:00
if ( ! params ) {
2013-09-20 23:52:40 +04:00
params = OC . parseQueryString ( decodeQuery ( location . search ) ) ;
2013-08-29 23:56:14 +04:00
}
return ( params && params . dir ) || '/' ;
}
2013-10-14 19:47:38 +04:00
// disable ajax/history API for public app (TODO: until it gets ported)
2013-10-22 20:11:03 +04:00
if ( ! isPublic ) {
2013-10-14 19:47:38 +04:00
// fallback to hashchange when no history support
2013-10-22 20:11:03 +04:00
if ( ! window . history . pushState ) {
$ ( window ) . on ( 'hashchange' , function ( ) {
2013-10-14 19:47:38 +04:00
FileList . changeDirectory ( parseCurrentDirFromUrl ( ) , false ) ;
} ) ;
2013-08-17 15:07:18 +04:00
}
2013-10-22 20:11:03 +04:00
window . onpopstate = function ( e ) {
2013-10-14 19:47:38 +04:00
var targetDir ;
2013-10-22 20:11:03 +04:00
if ( e . state && e . state . dir ) {
2013-10-14 19:47:38 +04:00
targetDir = e . state . dir ;
}
else {
// read from URL
targetDir = parseCurrentDirFromUrl ( ) ;
}
2013-10-22 20:11:03 +04:00
if ( targetDir ) {
2013-10-14 19:47:38 +04:00
FileList . changeDirectory ( targetDir , false ) ;
}
2013-10-22 20:11:03 +04:00
} ;
2013-08-17 15:07:18 +04:00
2013-10-22 20:11:03 +04:00
if ( parseInt ( $ ( '#ajaxLoad' ) . val ( ) , 10 ) === 1 ) {
2013-10-14 19:47:38 +04:00
// need to initially switch the dir to the one from the hash (IE8)
FileList . changeDirectory ( parseCurrentDirFromUrl ( ) , false , true ) ;
}
2013-08-29 23:56:14 +04:00
}
2014-02-20 18:16:45 +04:00
FileList . setCurrentDir ( parseCurrentDirFromUrl ( ) , false ) ;
2014-02-20 18:36:52 +04:00
2013-07-03 21:50:03 +04:00
FileList . createFileSummary ( ) ;
2011-08-04 02:22:44 +04:00
} ) ;