filelist.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. FileList={
  2. useUndo:true,
  3. update:function(fileListHtml) {
  4. $('#fileList').empty().html(fileListHtml);
  5. },
  6. addFile:function(name,size,lastModified,loading){
  7. var img=(loading)?OC.imagePath('core', 'loading.gif'):OC.imagePath('core', 'filetypes/file.png');
  8. var html='<tr data-type="file" data-size="'+size+'">';
  9. if(name.indexOf('.')!=-1){
  10. var basename=name.substr(0,name.lastIndexOf('.'));
  11. var extension=name.substr(name.lastIndexOf('.'));
  12. }else{
  13. var basename=name;
  14. var extension=false;
  15. }
  16. html+='<td class="filename" style="background-image:url('+img+')"><input type="checkbox" />';
  17. html+='<a class="name" href="download.php?file='+$('#dir').val()+'/'+name+'"><span class="nametext">'+basename
  18. if(extension){
  19. html+='<span class="extension">'+extension+'</span>';
  20. }
  21. html+='</span></a></td>';
  22. if(size!='Pending'){
  23. simpleSize=simpleFileSize(size);
  24. }else{
  25. simpleSize='Pending';
  26. }
  27. sizeColor = Math.round(200-size/(1024*1024)*2);
  28. lastModifiedTime=Math.round(lastModified.getTime() / 1000);
  29. modifiedColor=Math.round((Math.round((new Date()).getTime() / 1000)-lastModifiedTime)/60/60/24*14);
  30. html+='<td class="filesize" title="'+humanFileSize(size)+'" style="color:rgb('+sizeColor+','+sizeColor+','+sizeColor+')">'+simpleSize+'</td>';
  31. html+='<td class="date"><span class="modified" title="'+formatDate(lastModified)+'" style="color:rgb('+modifiedColor+','+modifiedColor+','+modifiedColor+')">'+relative_modified_date(lastModified.getTime() / 1000)+'</span></td>';
  32. html+='</tr>';
  33. FileList.insertElement(name,'file',$(html).attr('data-file',name));
  34. if(loading){
  35. $('tr').filterAttr('data-file',name).data('loading',true);
  36. }else{
  37. $('tr').filterAttr('data-file',name).find('td.filename').draggable(dragOptions);
  38. }
  39. },
  40. addDir:function(name,size,lastModified){
  41. html = $('<tr></tr>').attr({ "data-type": "dir", "data-size": size, "data-file": name});
  42. td = $('<td></td>').attr({"class": "filename", "style": 'background-image:url('+OC.imagePath('core', 'filetypes/folder.png')+')' });
  43. td.append('<input type="checkbox" />');
  44. var link_elem = $('<a></a>').attr({ "class": "name", "href": "index.php?dir="+ encodeURIComponent($('#dir').val()+'/'+name).replace(/%2F/g, '/') });
  45. link_elem.append($('<span></span>').addClass('nametext').text(name));
  46. link_elem.append($('<span></span>').attr({'class': 'uploadtext', 'currentUploads': 0}));
  47. td.append(link_elem);
  48. html.append(td);
  49. if(size!='Pending'){
  50. simpleSize=simpleFileSize(size);
  51. }else{
  52. simpleSize='Pending';
  53. }
  54. sizeColor = Math.round(200-Math.pow((size/(1024*1024)),2));
  55. lastModifiedTime=Math.round(lastModified.getTime() / 1000);
  56. modifiedColor=Math.round((Math.round((new Date()).getTime() / 1000)-lastModifiedTime)/60/60/24*5);
  57. td = $('<td></td>').attr({ "class": "filesize", "title": humanFileSize(size), "style": 'color:rgb('+sizeColor+','+sizeColor+','+sizeColor+')'}).text(simpleSize);
  58. html.append(td);
  59. td = $('<td></td>').attr({ "class": "date" });
  60. td.append($('<span></span>').attr({ "class": "modified", "title": formatDate(lastModified), "style": 'color:rgb('+modifiedColor+','+modifiedColor+','+modifiedColor+')' }).text( relative_modified_date(lastModified.getTime() / 1000) ));
  61. html.append(td);
  62. FileList.insertElement(name,'dir',html);
  63. $('tr').filterAttr('data-file',name).find('td.filename').draggable(dragOptions);
  64. $('tr').filterAttr('data-file',name).find('td.filename').droppable(folderDropOptions);
  65. },
  66. refresh:function(data) {
  67. result = jQuery.parseJSON(data.responseText);
  68. if(typeof(result.data.breadcrumb) != 'undefined'){
  69. updateBreadcrumb(result.data.breadcrumb);
  70. }
  71. FileList.update(result.data.files);
  72. resetFileActionPanel();
  73. },
  74. remove:function(name){
  75. $('tr').filterAttr('data-file',name).find('td.filename').draggable('destroy');
  76. $('tr').filterAttr('data-file',name).remove();
  77. if($('tr[data-file]').length==0){
  78. $('#emptyfolder').show();
  79. $('.file_upload_filename').addClass('highlight');
  80. }
  81. },
  82. insertElement:function(name,type,element){
  83. //find the correct spot to insert the file or folder
  84. var fileElements=$('tr[data-file][data-type="'+type+'"]');
  85. var pos;
  86. if(name.localeCompare($(fileElements[0]).attr('data-file'))<0){
  87. pos=-1;
  88. }else if(name.localeCompare($(fileElements[fileElements.length-1]).attr('data-file'))>0){
  89. pos=fileElements.length-1;
  90. }else{
  91. for(var pos=0;pos<fileElements.length-1;pos++){
  92. if(name.localeCompare($(fileElements[pos]).attr('data-file'))>0 && name.localeCompare($(fileElements[pos+1]).attr('data-file'))<0){
  93. break;
  94. }
  95. }
  96. }
  97. if(fileElements.length){
  98. if(pos==-1){
  99. $(fileElements[0]).before(element);
  100. }else{
  101. $(fileElements[pos]).after(element);
  102. }
  103. }else if(type=='dir' && $('tr[data-file]').length>0){
  104. $('tr[data-file]').first().before(element);
  105. }else{
  106. $('#fileList').append(element);
  107. }
  108. $('#emptyfolder').hide();
  109. $('.file_upload_filename').removeClass('highlight');
  110. },
  111. loadingDone:function(name){
  112. var tr=$('tr').filterAttr('data-file',name);
  113. tr.data('loading',false);
  114. var mime=tr.data('mime');
  115. tr.attr('data-mime',mime);
  116. getMimeIcon(mime,function(path){
  117. tr.find('td.filename').attr('style','background-image:url('+path+')');
  118. });
  119. tr.find('td.filename').draggable(dragOptions);
  120. },
  121. isLoading:function(name){
  122. return $('tr').filterAttr('data-file',name).data('loading');
  123. },
  124. rename:function(name){
  125. var tr=$('tr').filterAttr('data-file',name);
  126. tr.data('renaming',true);
  127. var td=tr.children('td.filename');
  128. var input=$('<input class="filename"></input>').val(name);
  129. var form=$('<form></form>')
  130. form.append(input);
  131. td.children('a.name').text('');
  132. td.children('a.name').append(form)
  133. input.focus();
  134. form.submit(function(event){
  135. event.stopPropagation();
  136. event.preventDefault();
  137. var newname=input.val();
  138. tr.attr('data-file',newname);
  139. td.children('a.name').empty();
  140. var path = td.children('a.name').attr('href');
  141. td.children('a.name').attr('href', path.replace(encodeURIComponent(name), encodeURIComponent(newname)));
  142. if(newname.indexOf('.')>0){
  143. basename=newname.substr(0,newname.lastIndexOf('.'));
  144. }else{
  145. basename=newname;
  146. }
  147. var span=$('<span class="nametext"></span>');
  148. span.text(basename);
  149. td.children('a.name').append(span);
  150. if(newname.indexOf('.')>0){
  151. span.append($('<span class="extension">'+newname.substr(newname.lastIndexOf('.'))+'</span>'));
  152. }
  153. $.get(OC.filePath('files','ajax','rename.php'), { dir : $('#dir').val(), newname: newname, file: name },function(){
  154. tr.data('renaming',false);
  155. });
  156. return false;
  157. });
  158. input.click(function(event){
  159. event.stopPropagation();
  160. event.preventDefault();
  161. });
  162. input.blur(function(){
  163. form.trigger('submit');
  164. });
  165. },
  166. do_delete:function(files){
  167. if(FileList.deleteFiles || !FileList.useUndo){//finish any ongoing deletes first
  168. FileList.finishDelete(function(){
  169. FileList.do_delete(files);
  170. });
  171. return;
  172. }
  173. if(files.substr){
  174. files=[files];
  175. }
  176. $.each(files,function(index,file){
  177. var files = $('tr').filterAttr('data-file',file);
  178. files.hide();
  179. files.find('input[type="checkbox"]').removeAttr('checked');
  180. files.removeClass('selected');
  181. });
  182. procesSelection();
  183. FileList.deleteCanceled=false;
  184. FileList.deleteFiles=files;
  185. $('#notification').text(t('files','undo deletion'));
  186. $('#notification').data('deletefile',true);
  187. $('#notification').fadeIn();
  188. },
  189. finishDelete:function(ready,sync){
  190. if(!FileList.deleteCanceled && FileList.deleteFiles){
  191. var fileNames=FileList.deleteFiles.join(';');
  192. $.ajax({
  193. url: OC.filePath('files', 'ajax', 'delete.php'),
  194. async:!sync,
  195. data: {dir:$('#dir').val(),files:fileNames},
  196. complete: function(data){
  197. boolOperationFinished(data, function(){
  198. $('#notification').fadeOut();
  199. $.each(FileList.deleteFiles,function(index,file){
  200. FileList.remove(file);
  201. });
  202. FileList.deleteCanceled=true;
  203. FileList.deleteFiles=null;
  204. if(ready){
  205. ready();
  206. }
  207. });
  208. }
  209. });
  210. }
  211. }
  212. }
  213. $(document).ready(function(){
  214. $('#notification').hide();
  215. $('#notification').click(function(){
  216. if($('#notification').data('deletefile'))
  217. {
  218. $.each(FileList.deleteFiles,function(index,file){
  219. $('tr').filterAttr('data-file',file).show();
  220. // alert(file);
  221. });
  222. FileList.deleteCanceled=true;
  223. FileList.deleteFiles=null;
  224. }
  225. $('#notification').fadeOut();
  226. });
  227. FileList.useUndo=('onbeforeunload' in window)
  228. $(window).bind('beforeunload', function (){
  229. FileList.finishDelete(null,true);
  230. });
  231. });