You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filebrowser.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /**
  2. * ownCloud - ajax frontend
  3. *
  4. * @author Robin Appelman
  5. * @copyright 2010 Robin Appelman icewind1991@gmail.com
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. OC_FILES.browser=new Object();
  22. OC_FILES.browser.showInitial=function(){
  23. if(document.getElementById('content')){
  24. var dir=''
  25. var loc=document.location.toString();
  26. if(loc.indexOf('#')!=-1){
  27. dir=loc.substring(loc.indexOf('#')+1);
  28. }
  29. OC_FILES.getdirectorycontent(dir,OC_FILES.browser.show_callback,true);
  30. }
  31. }
  32. OC_FILES.browser.show=function(dir,forceReload){
  33. if(!dir || !dir.split){
  34. dir='';
  35. }
  36. OC_FILES.getdirectorycontent(dir,OC_FILES.browser.show_callback,forceReload);
  37. }
  38. OC_FILES.browser.breadcrumb=new Object();
  39. OC_FILES.browser.breadcrumb.node=null;
  40. OC_FILES.browser.breadcrumb.crumbs=Array();
  41. OC_FILES.browser.breadcrumb.show=function(parent,path){
  42. if((!OC_FILES.browser.breadcrumb.node==parent && parent) || OC_FILES.browser.breadcrumb.node==null){
  43. OC_FILES.browser.breadcrumb.clear();
  44. OC_FILES.browser.breadcrumb.node=parent;
  45. OC_FILES.browser.breadcrumb.add('Home','/');
  46. }
  47. var dirs=path.split('/');
  48. var currentPath='/';
  49. var paths=Array();
  50. var currentPath;
  51. if(dirs.length>0){
  52. for(var i=0;i<dirs.length;i++){
  53. dir=dirs[i];
  54. if(dir){
  55. currentPath+=dir+'/';
  56. paths[currentPath]=true;
  57. if(!OC_FILES.browser.breadcrumb.crumbs[currentPath]){
  58. OC_FILES.browser.breadcrumb.add(dir,currentPath);
  59. }
  60. }
  61. }
  62. }
  63. //remove all crumbs that are not part of our current path
  64. for(currentPath in OC_FILES.browser.breadcrumb.crumbs){
  65. if(!paths[currentPath] && currentPath!='/'){
  66. OC_FILES.browser.breadcrumb.remove(currentPath);
  67. }
  68. }
  69. }
  70. OC_FILES.browser.breadcrumb.add=function(name,path){
  71. var a=document.createElement('a');
  72. var div=document.createElement('div');
  73. OC_FILES.browser.breadcrumb.crumbs[path]=div;
  74. div.className='breadcrumb';
  75. a.setAttribute('href','#'+path);
  76. a.addEvent('onclick',OC_FILES.browser.show,path);
  77. img=document.createElement('img');
  78. img.src=WEBROOT+'/img/arrow.png';
  79. a.appendChild(document.createTextNode(' ' +name));
  80. a.appendChild(img);
  81. OC_FILES.files[path]=new OC_FILES.file('',path,'dir');
  82. div.makeDropTarget();
  83. div.file=OC_FILES.files[path];
  84. div.addEvent('ondropon',OC_FILES.browser.handleDropOn);
  85. div.appendChild(a);
  86. OC_FILES.browser.breadcrumb.node.appendChild(div);
  87. }
  88. OC_FILES.browser.breadcrumb.remove=function(path){
  89. if(OC_FILES.browser.breadcrumb.crumbs[path]){
  90. var div=OC_FILES.browser.breadcrumb.crumbs[path];
  91. div.parentNode.removeChild(div);
  92. delete OC_FILES.browser.breadcrumb.crumbs[path];
  93. }
  94. }
  95. OC_FILES.browser.breadcrumb.clear=function(){
  96. for(path in OC_FILES.browser.breadcrumb.crumbs){
  97. OC_FILES.browser.breadcrumb.remove(path);
  98. }
  99. }
  100. OC_FILES.browser.files=new Object();
  101. OC_FILES.browser.files.fileNodes=Array();
  102. OC_FILES.browser.files.node=null;
  103. OC_FILES.browser.files.tbody=null;
  104. OC_FILES.browser.files.show=function(parent,fileList){
  105. if(parent){
  106. OC_FILES.browser.files.node=parent;
  107. }
  108. var table=document.createElement('table');
  109. OC_FILES.browser.files.node.appendChild(table);
  110. var tbody=document.createElement('tbody');
  111. OC_FILES.browser.files.tbody=tbody;
  112. table.appendChild(tbody);
  113. table.setAttribute('cellpadding',6);
  114. table.setAttribute('cellspacing',0);
  115. if(fileList){
  116. var name;
  117. //remove files that no longer are in the folder
  118. for(name in OC_FILES.browser.files.fileNodes){
  119. if(!fileList[name]){
  120. OC_FILES.browser.files.remove(name);
  121. }
  122. }
  123. //add the files that arent in the list yet
  124. for(name in fileList){
  125. file=fileList[name];
  126. if(!OC_FILES.browser.files.fileNodes[file.name]){
  127. OC_FILES.browser.files.add(file.name,file.type,file.size,file.date,file.mime);
  128. }
  129. }
  130. }
  131. }
  132. OC_FILES.browser.files.add=function(name,type,size,date,mime){
  133. if(name){
  134. if(!size) size=0;
  135. if(!date) date=getTimeString();
  136. OC_FILES.files[name]=new OC_FILES.file(OC_FILES.dir,name,type,mime);
  137. tr=document.createElement('tr');
  138. OC_FILES.browser.files.fileNodes[name]=tr;
  139. OC_FILES.browser.files.tbody.appendChild(tr);
  140. tr.className='browserline';
  141. td=document.createElement('td');
  142. tr.appendChild(td);
  143. td.className='fileSelector';
  144. input=document.createElement('input');
  145. input.setAttribute('type','checkbox');
  146. input.setAttribute('name','fileSelector');
  147. input.setAttribute('value',name);
  148. td.appendChild(input);
  149. tr.appendChild(OC_FILES.browser.showicon(type));
  150. td=document.createElement('td');
  151. tr.appendChild(td);
  152. td.makeDropTarget();
  153. td.addEvent('ondropon',OC_FILES.browser.handleDropOn);
  154. td.className='nametext';
  155. td.setAttribute('name',name);
  156. td.setAttribute('id',name);
  157. var fileObject=OC_FILES.files[name];
  158. td.file=fileObject;
  159. a=document.createElement('a');
  160. td.appendChild(a);
  161. a.appendChild(document.createTextNode(name));
  162. a.addEvent('onclick',new callBack(fileObject.actions['default'],fileObject));
  163. a.makeDraggable();
  164. a.addEvent('ondrop',OC_FILES.browser.handleDrop);
  165. if(type=='dir'){
  166. td.setAttribute('colspan',2);
  167. var dirname=name;
  168. if(OC_FILES.dir[OC_FILES.dir.length-1]!='/'){
  169. dirname='/'+name;
  170. }
  171. a.setAttribute('href','#'+OC_FILES.dir+dirname);
  172. }else{
  173. a.setAttribute('href','#'+OC_FILES.dir);
  174. if(!SMALLSCREEN){
  175. sizeTd=document.createElement('td');
  176. tr.appendChild(sizeTd);
  177. sizeTd.className='sizetext';
  178. sizeTd.appendChild(document.createTextNode(sizeFormat(size)));
  179. }else{
  180. td.setAttribute('colspan',2);
  181. }
  182. }
  183. a=document.createElement('a');
  184. var img=document.createElement('img');
  185. td.appendChild(img);
  186. img.className='file_actions';
  187. img.alt=''
  188. img.title='actions';
  189. img.src=WEBROOT+'/img/arrow_down.png';
  190. img.addEvent('onclick',OC_FILES.browser.showactions,name);
  191. if(!SMALLSCREEN){
  192. td=document.createElement('td');
  193. tr.appendChild(td);
  194. td.className='sizetext';
  195. td.appendChild(document.createTextNode(date));
  196. }
  197. }
  198. }
  199. OC_FILES.browser.files.remove=function(name){
  200. if(OC_FILES.browser.files.fileNodes[name]){
  201. tr=OC_FILES.browser.files.fileNodes[name];
  202. tr.parentNode.removeChild(tr);
  203. delete OC_FILES.browser.files.fileNodes[name];
  204. }
  205. }
  206. OC_FILES.browser.files.clear=function(){
  207. for(name in OC_FILES.browser.files.fileNodes){
  208. OC_FILES.browser.files.remove(name);
  209. }
  210. }
  211. OC_FILES.browser.table=null;
  212. OC_FILES.browser.show_callback=function(content){
  213. var dir=OC_FILES.dir
  214. var tr=null;
  215. var td=null;
  216. var img=null;
  217. if(!OC_FILES.browser.table){
  218. body=document.getElementsByTagName('body').item(0);
  219. body.addEvent('onclick',OC_FILES.browser.hideallactions);
  220. //remove current content;
  221. var contentNode=document.getElementById('content');
  222. contentNode.className='center';
  223. if(contentNode.hasChildNodes()){
  224. while(contentNode.childNodes.length >=1){
  225. contentNode.removeChild(contentNode.firstChild);
  226. }
  227. }
  228. var table=document.createElement('table');
  229. OC_FILES.browser.table=table;
  230. table.className='browser';
  231. var tbody=document.createElement('tbody');
  232. var thead=document.createElement('thead');
  233. var tfoot=document.createElement('tfoot');
  234. table.appendChild(thead);
  235. table.appendChild(tbody);
  236. table.appendChild(tfoot);
  237. OC_FILES.files=Array();
  238. table.setAttribute('cellpadding',6);
  239. tr=document.createElement('tr');
  240. thead.appendChild(tr);
  241. tr.className='breadcrumb';
  242. td=document.createElement('td');
  243. tr.appendChild(td);
  244. input=document.createElement('input');
  245. input.className='fileSelector'
  246. input.setAttribute('type','checkbox');
  247. input.setAttribute('name','fileSelector');
  248. input.setAttribute('value','select_all');
  249. input.setAttribute('id','select_all');
  250. input.addEvent('onclick',OC_FILES.selectAll);
  251. td.appendChild(input);
  252. td.className='breadcrumb';
  253. OC_FILES.browser.breadcrumb.show(td,dir);
  254. // files and directories
  255. tr=document.createElement('tr');
  256. tbody.appendChild(tr);
  257. td=document.createElement('td');
  258. tr.appendChild(td);
  259. div=document.createElement('div');
  260. div.className='fileList';
  261. td.appendChild(div);
  262. OC_FILES.browser.files.show(div,content);
  263. tr=document.createElement('tr');
  264. tfoot.appendChild(tr);
  265. tr.className='utilityline';
  266. td=document.createElement('td');
  267. tr.appendChild(td);
  268. td.className='actionsSelected';
  269. dropdown=document.createElement('select');
  270. td.appendChild(dropdown);
  271. dropdown.setAttribute('id','selected_action');
  272. for(index in OC_FILES.actions_selected){
  273. if(OC_FILES.actions_selected[index].call){
  274. option=document.createElement('option');
  275. dropdown.appendChild(option);
  276. option.setAttribute('value',index);
  277. option.appendChild(document.createTextNode(capitaliseFirstLetter(index)));
  278. }
  279. }
  280. td.appendChild(document.createTextNode(' Selected '));
  281. button=document.createElement('button');
  282. td.appendChild(button);
  283. button.appendChild(document.createTextNode('Go'));
  284. button.addEvent('onclick',OC_FILES.action_selected);
  285. div=document.createElement('div');
  286. td.appendChild(div);
  287. div.className='moreActionsButton';
  288. OC_FILES.maxUpload=content['max_upload'];
  289. var p=document.createElement('p');
  290. div.appendChild(p);
  291. p.appendChild(document.createTextNode('More Actions'));
  292. div.setAttribute('id','moreActionsButton');
  293. OC_FILES.browser.moreActionsShown=false;
  294. p.addEvent('onclick',OC_FILES.browser.showMoreActions);
  295. contentNode.appendChild(table);
  296. }else{
  297. OC_FILES.browser.breadcrumb.show(null,dir);
  298. OC_FILES.browser.files.show(null,content);
  299. }
  300. }
  301. OC_FILES.browser.handleDropOn=function(event,node){
  302. var dropTargetFile=this.file;
  303. var dropFile=node.parentNode.file;
  304. if(dropTargetFile!=dropFile){
  305. if(dropTargetFile.actions.dropOn && dropTargetFile.actions.dropOn.call){
  306. dropTargetFile.actions.dropOn.call(dropTargetFile,dropFile);
  307. }
  308. return false;
  309. }
  310. }
  311. OC_FILES.browser.handleDrop=function(event,node){
  312. var dropTargetFile=node.file;
  313. var dropFile=this.parentNode.file;
  314. if(dropFile.actions.drop && dropFile.actions.drop.call){
  315. dropFile.actions.drop.call(dropFile,dropTargetFile);
  316. }
  317. return false;
  318. }
  319. OC_FILES.browser.showMoreActions=function(){
  320. if(!OC_FILES.browser.moreActionsList){
  321. var div=document.createElement('div');
  322. div.className='moreActionsList';
  323. var table=document.createElement('table');
  324. div.appendChild(table);
  325. var tbody=document.createElement('tbody');
  326. table.appendChild(tbody);
  327. var tr=document.createElement('tr');
  328. tbody.appendChild(tr);
  329. var td=document.createElement('td');
  330. tr.appendChild(td);
  331. OC_FILES.browser.showuploader(OC_FILES.dir,td,OC_FILES.maxUpload);
  332. tr=document.createElement('tr');
  333. tbody.appendChild(tr);
  334. td=document.createElement('td');
  335. tr.appendChild(td);
  336. var form=document.createElement('form');
  337. td.appendChild(form);
  338. form.appendChild(document.createTextNode('New '));
  339. var dropdown=document.createElement('select');
  340. form.appendChild(dropdown);
  341. dropdown.setAttribute('id','newFileType');
  342. var option=document.createElement('option');
  343. dropdown.appendChild(option);
  344. option.setAttribute('value','dir');
  345. option.appendChild(document.createTextNode('Folder'));
  346. option=document.createElement('option');
  347. dropdown.appendChild(option);
  348. option.setAttribute('value','file');
  349. option.appendChild(document.createTextNode('File'));
  350. form.appendChild(document.createTextNode(' '));
  351. var input=document.createElement('input');
  352. form.appendChild(input);
  353. input.setAttribute('id','newFileName');
  354. form.addEvent('onsubmit',OC_FILES.browser.newFile);
  355. var submit=document.createElement('input');
  356. submit.type='submit';
  357. form.appendChild(submit);
  358. submit.value='Create';
  359. OC_FILES.browser.moreActionsList=div;
  360. }else{
  361. var div=OC_FILES.browser.moreActionsList;
  362. }
  363. var button=document.getElementById('moreActionsButton');
  364. if(!OC_FILES.browser.moreActionsShown){
  365. button.appendChild(div);
  366. OC_FILES.browser.moreActionsShown=true;
  367. button.className='moreActionsButton moreActionsButtonClicked';
  368. }else{
  369. OC_FILES.browser.moreActionsShown=false;
  370. button.removeChild(div);
  371. button.className='moreActionsButton';
  372. }
  373. }
  374. OC_FILES.browser.newFile=function(event){
  375. if(event.preventDefault){
  376. event.preventDefault();
  377. };
  378. var typeSelect=document.getElementById('newFileType');
  379. var type=typeSelect.options[typeSelect.selectedIndex].value;
  380. var name=document.getElementById('newFileName').value;
  381. OC_FILES.newFile(type,name,OC_FILES.dir);
  382. return false;
  383. }
  384. OC_FILES.browser.showicon=function(filetype){
  385. var td=document.createElement('td');
  386. td.className='fileicon';
  387. var img=document.createElement('img');
  388. td.appendChild(img);
  389. img.setAttribute('width',16);
  390. img.setAttribute('height',16);
  391. if(filetype=='dir'){
  392. img.src=WEBROOT+'/img/icons/folder.png';
  393. }else if(filetype=='incomplete'){
  394. img.src=WEBROOT+'/img/icons/loading.gif';
  395. }else{
  396. img.src=WEBROOT+'/img/icons/other.png';
  397. }
  398. return td;
  399. }
  400. OC_FILES.uploadIFrames=Array();
  401. OC_FILES.browser.showuploader=function(dir,parent,max_upload){
  402. var iframeId=OC_FILES.uploadIFrames.length
  403. OC_FILES.uploadForm=document.createElement('form');
  404. OC_FILES.uploadForm.setAttribute('target','uploadIFrame'+iframeId);
  405. OC_FILES.uploadForm.setAttribute('action','files/upload.php?dir='+dir);
  406. OC_FILES.uploadForm.method='post';
  407. OC_FILES.uploadForm.setAttribute('enctype','multipart/form-data');
  408. OC_FILES.uploadIFrames[iframeId]=document.createElement('iframe');
  409. OC_FILES.uploadIFrames[iframeId].uploadParent=parent;
  410. OC_FILES.uploadIFrames[iframeId].className='hidden';
  411. OC_FILES.uploadIFrames[iframeId].name='uploadIFrame'+iframeId;
  412. var input=document.createElement('input');
  413. input.setAttribute('type','hidden');
  414. input.setAttribute('name','MAX_FILE_SIZE');
  415. input.setAttribute('value',max_upload);
  416. input.setAttribute('id','max_upload');
  417. OC_FILES.uploadForm.appendChild(input);
  418. var file=document.createElement('input');
  419. file.name='file';
  420. file.setAttribute('id','fileSelector');
  421. file.setAttribute('type','file');
  422. file.addEvent('onchange',OC_FILES.upload,[dir,iframeId]);
  423. OC_FILES.uploadForm.appendChild(document.createTextNode('Upload file: '));
  424. OC_FILES.uploadForm.appendChild(file);
  425. parent.appendChild(OC_FILES.uploadForm);
  426. var body=document.getElementsByTagName('body').item(0);
  427. body.appendChild(OC_FILES.uploadIFrames[iframeId]);
  428. }
  429. OC_FILES.browser.show_rename=function(dir,file){
  430. var item=document.getElementById(file);
  431. item.oldContent=Array();
  432. if(item.hasChildNodes()){
  433. while(item.childNodes.length >=1){
  434. item.oldContent[item.oldContent.length]=item.firstChild;
  435. item.removeChild(item.firstChild);
  436. }
  437. }
  438. var form=document.createElement('form');
  439. form.addEvent('onsubmit',OC_FILES.rename,[dir,file]);
  440. var input=document.createElement('input');
  441. input.setAttribute('type','text');
  442. input.setAttribute('name','newname');
  443. input.setAttribute('value',file);
  444. input.setAttribute('id',file+'_newname')
  445. input.addEvent('onblur',OC_FILES.browser.rename_cancel,[file]);
  446. form.appendChild(input);
  447. item.appendChild(form);
  448. input.focus();
  449. }
  450. OC_FILES.browser.rename_cancel=function(file){
  451. var item=document.getElementsByName(file).item(0);
  452. if(item.hasChildNodes()){
  453. while(item.childNodes.length >=1){
  454. item.removeChild(item.firstChild);
  455. }
  456. }
  457. for(index in item.oldContent){
  458. if(item.oldContent[index].nodeType){
  459. item.appendChild(item.oldContent[index]);
  460. }
  461. }
  462. }
  463. OC_FILES.browser.showactions=function(file,hide){
  464. var node=document.getElementById(file);
  465. if(node &&(node.actionsshown || hide===true)){
  466. if(node.actionsshown){
  467. node.actionsdiv.parentNode.removeChild(node.actionsdiv);
  468. }
  469. node.actionsdiv=null;
  470. node.actionsshown=false
  471. }else if(node){
  472. node.actionsshown=true
  473. div=document.createElement('div');
  474. node.actionsdiv=div;
  475. div.className='fileactionlist';
  476. table=document.createElement('table');
  477. div.appendChild(table);
  478. tbody=document.createElement('tbody');
  479. table.appendChild(tbody);
  480. var file=OC_FILES.files[file]
  481. var actions=file.actions;
  482. var name;
  483. for(name in actions){
  484. if(actions[name].call && name!='default' && name!='dropOn' && name!='drop'){
  485. tr=document.createElement('tr');
  486. tbody.appendChild(tr);
  487. td=document.createElement('td');
  488. tr.appendChild(td);
  489. a=document.createElement('a');
  490. td.appendChild(a);
  491. a.appendChild(document.createTextNode(capitaliseFirstLetter(name)));
  492. var action=actions[name];
  493. td.addEvent('onclick',new callBack(action,file));
  494. }
  495. }
  496. node.appendChild(div);
  497. OC_FILES.hideallenabled=false;
  498. setTimeout('OC_FILES.hideallenabled=true',50);
  499. }
  500. }
  501. OC_FILES.browser.hideallactions=function(){
  502. if(OC_FILES.hideallenabled){
  503. for(name in OC_FILES.files){
  504. if(OC_FILES.files[name]){
  505. if(OC_FILES.files[name].hideactions){
  506. OC_FILES.files[name].hideactions.call(OC_FILES.files[name]);
  507. }
  508. }
  509. }
  510. }
  511. }
  512. OC_FILES.hideallenabled=true; //used to prevent browsers from hiding actionslists right after they are displayed;
  513. sizeFormat=function(size){
  514. if(isNaN(size)){
  515. return false;
  516. }
  517. var orig=size;
  518. var steps=Array('B','KiB','MiB','GiB','TiB');
  519. var step=0;
  520. while(size>(1024*2)){
  521. step++;
  522. size=size/1024;
  523. }
  524. if(size.toFixed){
  525. size=size.toFixed(2);
  526. }
  527. return ''+size+' '+steps[step];
  528. }
  529. OC_FILES.browser.showImage=function(dir,file){
  530. var path=WEBROOT+'/files/open_file.php?dir='+dir+'&file='+file
  531. var div=document.createElement('div');
  532. div.setAttribute('id','imageframe');
  533. div.addEvent('onclick',OC_FILES.browser.hideImage)
  534. var img=document.createElement('img');
  535. img.setAttribute('src',path);
  536. div.appendChild(img);
  537. body=document.getElementsByTagName('body').item(0);
  538. body.appendChild(div);
  539. }
  540. OC_FILES.browser.hideImage=function(){
  541. var div=document.getElementById('imageframe');
  542. div.parentNode.removeChild(div);
  543. }
  544. function capitaliseFirstLetter(string){
  545. return string.charAt(0).toUpperCase() + string.slice(1);
  546. }