fileactions.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. FileActions={
  2. actions:{},
  3. defaults:{},
  4. icons:{},
  5. currentFile:null,
  6. register:function(mime,name,icon,action){
  7. if(!FileActions.actions[mime]){
  8. FileActions.actions[mime]={};
  9. }
  10. FileActions.actions[mime][name]=action;
  11. FileActions.icons[name]=icon;
  12. },
  13. setDefault:function(mime,name){
  14. FileActions.defaults[mime]=name;
  15. },
  16. get:function(mime,type){
  17. var actions={};
  18. if(FileActions.actions.all){
  19. actions=$.extend( actions, FileActions.actions.all )
  20. }
  21. if(mime){
  22. if(FileActions.actions[mime]){
  23. actions=$.extend( actions, FileActions.actions[mime] )
  24. }
  25. var mimePart=mime.substr(0,mime.indexOf('/'));
  26. if(FileActions.actions[mimePart]){
  27. actions=$.extend( actions, FileActions.actions[mimePart] )
  28. }
  29. }
  30. if(type){//type is 'dir' or 'file'
  31. if(FileActions.actions[type]){
  32. actions=$.extend( actions, FileActions.actions[type] )
  33. }
  34. }
  35. return actions;
  36. },
  37. getDefault:function(mime,type){
  38. if(mime){
  39. var mimePart=mime.substr(0,mime.indexOf('/'));
  40. }
  41. var name=false;
  42. if(mime && FileActions.defaults[mime]){
  43. name=FileActions.defaults[mime];
  44. }else if(mime && FileActions.defaults[mimePart]){
  45. name=FileActions.defaults[mimePart];
  46. }else if(type && FileActions.defaults[type]){
  47. name=FileActions.defaults[type];
  48. }else{
  49. name=FileActions.defaults.all;
  50. }
  51. var actions=this.get(mime,type);
  52. return actions[name];
  53. },
  54. display:function(parent){
  55. FileActions.currentFile=parent;
  56. $('#fileList span.fileactions, #fileList td.date a.action').remove();
  57. var actions=FileActions.get(FileActions.getCurrentMimeType(),FileActions.getCurrentType());
  58. var file=FileActions.getCurrentFile();
  59. if($('tr').filterAttr('data-file',file).data('renaming')){
  60. return;
  61. }
  62. parent.children('a.name').append('<span class="fileactions" />');
  63. var defaultAction=FileActions.getDefault(FileActions.getCurrentMimeType(),FileActions.getCurrentType());
  64. for(name in actions){
  65. if((name=='Download' || actions[name]!=defaultAction) && name!='Delete'){
  66. var img=FileActions.icons[name];
  67. if(img.call){
  68. img=img(file);
  69. }
  70. var html='<a href="#" class="action" style="display:none">';
  71. if(img) { html+='<img src="'+img+'"/> '; }
  72. html += name+'</a>';
  73. var element=$(html);
  74. element.data('action',name);
  75. element.click(function(event){
  76. event.stopPropagation();
  77. event.preventDefault();
  78. var action=actions[$(this).data('action')];
  79. var currentFile=FileActions.getCurrentFile();
  80. FileActions.hide();
  81. action(currentFile);
  82. });
  83. element.hide();
  84. parent.find('a.name>span.fileactions').append(element);
  85. }
  86. }
  87. if(actions['Delete']){
  88. var img=FileActions.icons['Delete'];
  89. if(img.call){
  90. img=img(file);
  91. }
  92. var html='<a href="#" original-title="Delete" class="action delete" style="display:none" />';
  93. var element=$(html);
  94. if(img){
  95. element.append($('<img src="'+img+'"/>'));
  96. }
  97. element.data('action','Delete');
  98. element.click(function(event){
  99. event.stopPropagation();
  100. event.preventDefault();
  101. var action=actions[$(this).data('action')];
  102. var currentFile=FileActions.getCurrentFile();
  103. FileActions.hide();
  104. action(currentFile);
  105. });
  106. element.hide();
  107. parent.parent().children().last().append(element);
  108. }
  109. $('#fileList .action').css('-o-transition-property','none');//temporarly disable
  110. $('#fileList .action').fadeIn(200,function(){
  111. $('#fileList .action').css('-o-transition-property','opacity');
  112. });
  113. return false;
  114. },
  115. hide:function(){
  116. $('#fileList span.fileactions, #fileList td.date a.action').fadeOut(200,function(){
  117. $(this).remove();
  118. });
  119. },
  120. getCurrentFile:function(){
  121. return FileActions.currentFile.parent().attr('data-file');
  122. },
  123. getCurrentMimeType:function(){
  124. return FileActions.currentFile.parent().attr('data-mime');
  125. },
  126. getCurrentType:function(){
  127. return FileActions.currentFile.parent().attr('data-type');
  128. }
  129. }
  130. $(document).ready(function(){
  131. if($('#allowZipDownload').val() == 1){
  132. var downloadScope = 'all';
  133. } else {
  134. var downloadScope = 'file';
  135. }
  136. FileActions.register(downloadScope,'Download',function(){return OC.imagePath('core','actions/download')},function(filename){
  137. window.location=OC.filePath('files', 'ajax', 'download.php') + '?files='+encodeURIComponent(filename)+'&dir='+encodeURIComponent($('#dir').val());
  138. });
  139. });
  140. FileActions.register('all','Delete',function(){return OC.imagePath('core','actions/delete')},function(filename){
  141. if(Files.cancelUpload(filename)) {
  142. if(filename.substr){
  143. filename=[filename];
  144. }
  145. $.each(filename,function(index,file){
  146. var filename = $('tr').filterAttr('data-file',file);
  147. filename.hide();
  148. filename.find('input[type="checkbox"]').removeAttr('checked');
  149. filename.removeClass('selected');
  150. });
  151. procesSelection();
  152. }else{
  153. FileList.do_delete(filename);
  154. }
  155. });
  156. FileActions.register('all','Rename',function(){return OC.imagePath('core','actions/rename')},function(filename){
  157. FileList.rename(filename);
  158. });
  159. FileActions.register('dir','Open','',function(filename){
  160. window.location='index.php?dir='+encodeURIComponent($('#dir').val()).replace(/%2F/g, '/')+'/'+encodeURIComponent(filename);
  161. });
  162. FileActions.setDefault('dir','Open');