filelist.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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().replace(/</, '&lt;').replace(/>/, '&gt;')+'/'+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": OC.linkTo('files', '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. if (newname != name) {
  139. if ($('tr').filterAttr('data-file', newname).length > 0) {
  140. $('#notification').html(newname+' '+t('files', 'already exists')+'<span class="replace">'+t('files', 'replace')+'</span><span class="cancel">'+t('files', 'cancel')+'</span>');
  141. $('#notification').data('oldName', name);
  142. $('#notification').data('newName', newname);
  143. $('#notification').fadeIn();
  144. newname = name;
  145. } else {
  146. $.get(OC.filePath('files','ajax','rename.php'), { dir : $('#dir').val(), newname: newname, file: name },function(result) {
  147. if (!result || result.status == 'error') {
  148. OC.dialogs.alert(result.data.message, 'Error moving file');
  149. newname = name;
  150. }
  151. });
  152. }
  153. }
  154. tr.attr('data-file', newname);
  155. var path = td.children('a.name').attr('href');
  156. td.children('a.name').attr('href', path.replace(encodeURIComponent(name), encodeURIComponent(newname)));
  157. if (newname.indexOf('.') > 0) {
  158. var basename=newname.substr(0,newname.lastIndexOf('.'));
  159. } else {
  160. var basename=newname;
  161. }
  162. td.children('a.name').empty();
  163. var span=$('<span class="nametext"></span>');
  164. span.text(basename);
  165. td.children('a.name').append(span);
  166. if (newname.indexOf('.') > 0) {
  167. span.append($('<span class="extension">'+newname.substr(newname.lastIndexOf('.'))+'</span>'));
  168. }
  169. tr.data('renaming',false);
  170. return false;
  171. });
  172. input.click(function(event){
  173. event.stopPropagation();
  174. event.preventDefault();
  175. });
  176. input.blur(function(){
  177. form.trigger('submit');
  178. });
  179. },
  180. replace:function(oldName, newName) {
  181. // Finish any existing actions
  182. if (FileList.lastAction || !FileList.useUndo) {
  183. FileList.lastAction();
  184. }
  185. var tr = $('tr').filterAttr('data-file', oldName);
  186. tr.hide();
  187. FileList.replaceCanceled = false;
  188. FileList.replaceOldName = oldName;
  189. FileList.replaceNewName = newName;
  190. FileList.lastAction = function() {
  191. FileList.finishReplace();
  192. };
  193. $('#notification').html(t('files', 'replaced')+' '+newName+' '+t('files', 'with')+' '+oldName+'<span class="undo">'+t('files', 'undo')+'</span>');
  194. $('#notification').fadeIn();
  195. },
  196. finishReplace:function() {
  197. if (!FileList.replaceCanceled && FileList.replaceOldName && FileList.replaceNewName) {
  198. // Delete the file being replaced and rename the replacement
  199. FileList.deleteCanceled = false;
  200. FileList.deleteFiles = [FileList.replaceNewName];
  201. FileList.finishDelete(function() {
  202. $.ajax({url: OC.filePath('files', 'ajax', 'rename.php'), async: false, data: { dir: $('#dir').val(), newname: FileList.replaceNewName, file: FileList.replaceOldName }, success: function(result) {
  203. if (result && result.status == 'success') {
  204. var tr = $('tr').filterAttr('data-file', FileList.replaceOldName);
  205. tr.attr('data-file', FileList.replaceNewName);
  206. var td = tr.children('td.filename');
  207. td.children('a.name .span').text(FileList.replaceNewName);
  208. var path = td.children('a.name').attr('href');
  209. td.children('a.name').attr('href', path.replace(encodeURIComponent(FileList.replaceOldName), encodeURIComponent(FileList.replaceNewName)));
  210. if (FileList.replaceNewName.indexOf('.') > 0) {
  211. var basename = FileList.replaceNewName.substr(0, FileList.replaceNewName.lastIndexOf('.'));
  212. } else {
  213. var basename = FileList.replaceNewName;
  214. }
  215. td.children('a.name').empty();
  216. var span = $('<span class="nametext"></span>');
  217. span.text(basename);
  218. td.children('a.name').append(span);
  219. if (FileList.replaceNewName.indexOf('.') > 0) {
  220. span.append($('<span class="extension">'+FileList.replaceNewName.substr(FileList.replaceNewName.lastIndexOf('.'))+'</span>'));
  221. }
  222. tr.show();
  223. } else {
  224. OC.dialogs.alert(result.data.message, 'Error moving file');
  225. }
  226. FileList.replaceCanceled = true;
  227. FileList.replaceOldName = null;
  228. FileList.replaceNewName = null;
  229. FileList.lastAction = null;
  230. }});
  231. }, true);
  232. }
  233. },
  234. do_delete:function(files){
  235. // Finish any existing actions
  236. if (FileList.lastAction || !FileList.useUndo) {
  237. FileList.lastAction();
  238. }
  239. if(files.substr){
  240. files=[files];
  241. }
  242. $.each(files,function(index,file){
  243. var files = $('tr').filterAttr('data-file',file);
  244. files.hide();
  245. files.find('input[type="checkbox"]').removeAttr('checked');
  246. files.removeClass('selected');
  247. });
  248. procesSelection();
  249. FileList.deleteCanceled=false;
  250. FileList.deleteFiles=files;
  251. FileList.lastAction = function() {
  252. FileList.finishDelete(null, true);
  253. };
  254. $('#notification').html(t('files', 'deleted')+' '+files+'<span class="undo">'+t('files', 'undo')+'</span>');
  255. $('#notification').fadeIn();
  256. },
  257. finishDelete:function(ready,sync){
  258. if(!FileList.deleteCanceled && FileList.deleteFiles){
  259. var fileNames=FileList.deleteFiles.join(';');
  260. $.ajax({
  261. url: OC.filePath('files', 'ajax', 'delete.php'),
  262. async:!sync,
  263. data: {dir:$('#dir').val(),files:fileNames},
  264. complete: function(data){
  265. boolOperationFinished(data, function(){
  266. $('#notification').fadeOut();
  267. $.each(FileList.deleteFiles,function(index,file){
  268. FileList.remove(file);
  269. });
  270. FileList.deleteCanceled=true;
  271. FileList.deleteFiles=null;
  272. FileList.lastAction = null;
  273. if(ready){
  274. ready();
  275. }
  276. });
  277. }
  278. });
  279. }
  280. }
  281. }
  282. $(document).ready(function(){
  283. $('#notification').hide();
  284. $('#notification .undo').live('click', function(){
  285. if (FileList.deleteFiles) {
  286. $.each(FileList.deleteFiles,function(index,file){
  287. $('tr').filterAttr('data-file',file).show();
  288. });
  289. FileList.deleteCanceled=true;
  290. FileList.deleteFiles=null;
  291. } else if (FileList.replaceOldName && FileList.replaceNewName) {
  292. $('tr').filterAttr('data-file', FileList.replaceOldName).show();
  293. FileList.replaceCanceled = true;
  294. FileList.replaceOldName = null;
  295. FileList.replaceNewName = null;
  296. }
  297. $('#notification').fadeOut();
  298. });
  299. $('#notification .replace').live('click', function() {
  300. $('#notification').fadeOut('400', function() {
  301. FileList.replace($('#notification').data('oldName'), $('#notification').data('newName'));
  302. });
  303. });
  304. $('#notification .cancel').live('click', function() {
  305. $('#notification').fadeOut();
  306. });
  307. FileList.useUndo=('onbeforeunload' in window)
  308. $(window).bind('beforeunload', function (){
  309. if (FileList.lastAction) {
  310. FileList.lastAction();
  311. }
  312. });
  313. });