main.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. //é
  2. var blockNewFolder = false;
  3. var pendingTask = false;
  4. $(function(){
  5. $(document).ajaxStop(function() {
  6. $('.preloader').fadeOut(200);
  7. });
  8. $(document).ajaxStart(function() {
  9. $('.preloader').show();
  10. });
  11. getFiles();
  12. addedFiles = Array();
  13. $('.tooltips').poshytip({
  14. className: 'tooltip',
  15. showTimeout: 0,
  16. alignTo: 'target',
  17. alignX: 'center',
  18. offsetY: 10,
  19. allowTipHover: false
  20. });
  21. imageExtension = Array();
  22. imageExtensionRoot = 'tpl/UnderBlack/img/extension/';
  23. imageExtensionMiniRoot = 'img/extension_mini/';
  24. imageExtension['sql']='sql.png';
  25. imageExtension['zip']='zip.png';
  26. imageExtension['rar']='rar.png';
  27. imageExtension['tar.gz']='archive.png';
  28. imageExtension['7z']='zip.png';
  29. imageExtension['gzip']='gzip.png';
  30. imageExtension['php']='php.png';
  31. imageExtension['py']='py.png';
  32. imageExtension['jsp']='html.png';
  33. imageExtension['html']='html.png';
  34. imageExtension['htm']='html.png';
  35. imageExtension['css']='css.png';
  36. imageExtension['java']='java.png';
  37. imageExtension['cpp']='cpp.png';
  38. imageExtension['c']='c.png';
  39. imageExtension['h']='h.png';
  40. imageExtension['hpp']='hpp.png';
  41. imageExtension['js']='js.png';
  42. imageExtension['rss']='rss.png';
  43. imageExtension['rb']='rb.png';
  44. imageExtension['vcard']='authors.png';
  45. imageExtension['exe']='exe.png';
  46. imageExtension['deb']='package.png';
  47. imageExtension['psd']='psd.png';
  48. imageExtension['nfo']='readme.png';
  49. imageExtension['csv']='calc.png';
  50. imageExtension['xls']='calc.png';
  51. imageExtension['xlsx']='calc.png';
  52. imageExtension['ppt']='pres.png';
  53. imageExtension['pptx']='pres.png';
  54. imageExtension['doc']='doc.png';
  55. imageExtension['odf']='doc.png';
  56. imageExtension['docx']='doc.png';
  57. imageExtension['otf']='doc.png';
  58. imageExtension['rtf']='rtf.png';
  59. imageExtension['txt']='txt.png';
  60. imageExtension['log']='log.png';
  61. imageExtension['src']='source.png';
  62. imageExtension['tex']='tex.png';
  63. imageExtension['bin']='bin.png';
  64. imageExtension['cd']='cd.png';
  65. imageExtension['sh']='script.png';
  66. imageExtension['bash']='script.png';
  67. imageExtension['bat']='script.png';
  68. imageExtension['vcal']='vcal.png';
  69. imageExtension['ical']='vcal.png';
  70. imageExtension['mp3']='playlist.png';
  71. imageExtension['avi']='playlist.png';
  72. imageExtension['mp4']='video.png';
  73. imageExtension['webm']='video.png';
  74. imageExtension['wmv']='video.png';
  75. imageExtension['mkv']='video.png';
  76. imageExtension['rpm']='rpm.png';
  77. imageExtension['tiff']='tiff.png';
  78. imageExtension['jpg']='jpg.png';
  79. imageExtension['jpeg']='jpg.png';
  80. imageExtension['png']='png.png';
  81. imageExtension['gif']='gif.png';
  82. imageExtension['bmp']='bmp.png';
  83. imageExtension['ico']='ico.png';
  84. imageExtension['eps']='draw.png';
  85. imageExtension['ai']='eps.png';
  86. imageExtension['pdf']='pdf.png';
  87. imageExtension['xml']='xml.png';
  88. imageExtension['fla']='makefile.png';
  89. imageExtension['swf']='makefile.png';
  90. // var dropbox = $('.message'),
  91. // message = $('.message', dropbox);
  92. $(function () {
  93. $('#uploadButton').fileupload({
  94. dataType: 'json',
  95. autoUpload: true,
  96. dropZone : '#uploadButton',
  97. maxFileSize: 5000000,
  98. sequentialUploads: true,
  99. add: function (e, data) {
  100. createImage(data.files[0]);
  101. pendingTask = true;
  102. data.submit();
  103. },
  104. done: function (e, data) {
  105. addedFiles.push({name:data.files[0].name,size:data.files[0].size,type:data.result.extension,path:data.result.filePath});
  106. },
  107. stop: function (e, data) {
  108. //enregistrement de l'evenement
  109. $.ajax({
  110. url: "php/action.php?action=addEventForUpload",
  111. data:{files:array2json(addedFiles)}
  112. });
  113. addedFiles = Array();
  114. pendingTask = false;
  115. //rafraichissement du bousin
  116. getFiles(null,'//CURRENT');
  117. },
  118. progress: function (e, data) {
  119. var progress = parseInt(data.loaded / data.total * 100, 10);
  120. $.data(data.files[0]).find('.progress').width(progress+'%');
  121. $.data(data.files[0]).find('.progressCount').html(progress+'%');
  122. }
  123. });
  124. });
  125. var template = '<div class="preview">'+
  126. '<span class="imageHolder">'+
  127. '<img />'+'<span class="uploaded"></span>'+
  128. '<span class="fileName"></span>'+
  129. '</span>'+
  130. '<div class="progressHolder">'+
  131. '<div class="progress"></div>'+
  132. '</div><span class="progressCount"></span>'+
  133. '</div>';
  134. function createImage(file){
  135. var preview = $(template),
  136. image = $('img', preview);
  137. fileName = $('.fileName', preview);
  138. image.width = 48;
  139. image.height = 48;
  140. var extension = file.name.split('.');
  141. if(extension.length==1){
  142. extension = null;
  143. }else{
  144. extension = extension[extension.length-1];
  145. }
  146. fileName.html(file.name);
  147. if(imageExtension[extension]!=null){
  148. ext = imageExtensionRoot+imageExtension[extension] ;
  149. }else{
  150. ext = imageExtensionRoot+'unknown.png';
  151. }
  152. image.attr('src',ext);
  153. preview.appendTo($("#dropbox"));
  154. $.data(file,preview);
  155. }
  156. function showMessage(msg){
  157. message.html(msg);
  158. }
  159. checkVersion();
  160. });
  161. function checkPendingTask(){
  162. if(pendingTask){
  163. alert('Certaines tâches sont encore en cours d\'execution, vous risquez de perdre des données.');
  164. }
  165. }
  166. function generateBreadCrumb(folder){
  167. returned = '';
  168. if(folder!=null){
  169. var dissolvedPath = explode('/',str_replace('../','',folder));
  170. var path = '../';
  171. for(i=0;i<dissolvedPath.length;i++){
  172. if(dissolvedPath[i]!=''){
  173. path +=dissolvedPath[i]+'/';
  174. returned +='<li alt="'+addslashes(path)+'" onclick="getFiles(null,\''+addslashes(path)+'\')">'+dissolvedPath[i]+'</li>';
  175. }
  176. }
  177. }
  178. return returned;
  179. }
  180. function getFiles(keywords,folder){
  181. if(!pendingTask){
  182. if(keywords==null){keywords='';}else{keywords = "&keywords="+keywords}
  183. if(folder==null){folderVar='';}else{folderVar = "&folder="+folder}
  184. $.ajax({
  185. url: "php/action.php?action=getFiles"+keywords+folderVar,
  186. success: function(returnedValue){
  187. response = $.parseJSON(returnedValue);
  188. if(response.succes){
  189. $('.breadcrumb').html(generateBreadCrumb(response.currentFolder));
  190. t= response.status;
  191. $('#dropbox .preview,.tooltip').remove();
  192. for(i=0;i<t.length;i++){
  193. addFile(t[i]);
  194. }
  195. $('.imageHolder,.imageHolder .addOptions li,.lien,.folderHolder').poshytip({
  196. className: 'tooltip',
  197. showTimeout: 0,
  198. alignTo: 'target',
  199. alignX: 'center',
  200. offsetY: 10,
  201. allowTipHover: false
  202. });
  203. $('.fileOption').click(function(){getFileOption(this);});
  204. $(".imageHolder").draggable({ revert: "invalid" });
  205. $( ".folderPreview" ).droppable({
  206. activeClass: "folderPreviewDroppableHover",
  207. hoverClass: "folderPreviewDroppableHover",
  208. drop: function( event, ui ) {
  209. var parent = $( ui.draggable ).parent();
  210. var fileUrl = $('.fileUrl',parent).html();
  211. var fileName = $('.fileName',parent).attr('alt');
  212. var folder = $('.fileUrl',this).html();
  213. $.ajax({
  214. url: "php/action.php?action=moveFile",
  215. data:{fileName:fileName,fileUrl:fileUrl,folder:folder},
  216. success: function(response){
  217. var response = $.parseJSON(response);
  218. parent.fadeOut(300);
  219. tell(response.status);
  220. }
  221. });
  222. }
  223. });
  224. }
  225. }
  226. });
  227. }else{
  228. alert('Des fichiers sont encore en téléchargement, veuillez patienter');
  229. }
  230. }
  231. function addFile(file){
  232. if(imageExtension[file.extension]!=null){
  233. ext = imageExtensionRoot+imageExtension[file.extension] ;
  234. }else{
  235. ext = imageExtensionRoot+'unknown.png';
  236. }
  237. if(file.type=='folder')ext = imageExtensionRoot+'folder-page.png';
  238. if(file.name=='..')ext = imageExtensionRoot+'folder-parent.png';
  239. if(file.type=='file'){
  240. var tpl = '<div class="preview filePreview" >'+
  241. '<div class="fileUrl">'+stripslashes(file.url)+'</div>'+
  242. '<span title="'+stripslashes(file.toolTipName)+'" class="imageHolder'+(file.published?' filePublished':'')+'"><div onclick="deleteFile(this)" class="deleteFile">x</div>'+
  243. '<div onclick="openFile(this)">'+
  244. '<img width="48px" height="48px" src="'+ext+'"/>'+
  245. '<ul>'+
  246. '<li>'+file.size+'</li>'+
  247. '<li>'+file.mtimeDate+' '+file.mtimeHour+'</li>'+
  248. '</ul>'+
  249. '</div>'+
  250. '<span ondblclick="renameFile(this)\" title="'+stripslashes(file.name)+'" alt="'+stripslashes(file.name)+'" class="fileName">'+stripslashes(file.shortname)+'</span>'+
  251. '</span>'+
  252. '</div>';
  253. }else if(file.type=='folder'){
  254. var tpl = '<div class="preview folderPreview">'+
  255. '<div class="fileUrl">'+stripslashes(file.url)+'</div>'+
  256. '<span title="'+stripslashes(file.name)+'" class="folderHolder ">';
  257. if(file.name!='..') tpl += '<div onclick="deleteFile(this)" class="deleteFile">x</div>';
  258. tpl += '<img width="48px" height="48px" onclick="getFiles(null,$(\'.fileUrl\',$(this).parent().parent()).html())" src="'+ext+'"/>'+
  259. '<span ';
  260. if(file.name!='..') tpl +='ondblclick="renameFile(this)\"';
  261. tpl += ' title="'+stripslashes(file.name)+'" alt="'+stripslashes(file.name)+'" class="fileName">'+stripslashes(file.shortname)+'</span>'+
  262. '</span></div>';
  263. }
  264. $('#dropbox').append(tpl);
  265. }
  266. function zipFile(element){
  267. var parent = $(element).parent().parent().parent().parent();
  268. var file =$('.fileUrl',parent).html();
  269. $.ajax({
  270. url: "php/action.php?action=zipFile",
  271. data:{file:file},
  272. success: function(response){
  273. var response = $.parseJSON(response);
  274. if(response.succes==true){
  275. window.location= './'+response.status;
  276. }else{
  277. tell(response.status);
  278. }
  279. }
  280. });
  281. }
  282. function publishFile(element){
  283. var parent = $(element).parent().parent().parent().parent();
  284. var file =$('.fileUrl',parent).html();
  285. $.ajax({
  286. url: "php/action.php?action=publishFile",
  287. data:{file:file},
  288. success: function(response){
  289. var response = $.parseJSON(response);
  290. if(response.succes==true){
  291. $(element).attr('onclick','unpublishFile(this);');
  292. $(element).parent().parent().parent().addClass('filePublished');
  293. tell(response.status);
  294. }else{
  295. tell(response.status);
  296. }
  297. }
  298. });
  299. }
  300. function unpublishFile(element){
  301. var parent = $(element).parent().parent().parent().parent();
  302. var file =$('.fileUrl',parent).html();
  303. $.ajax({
  304. url: "php/action.php?action=unpublishFile",
  305. data:{file:file},
  306. success: function(response){
  307. var response = $.parseJSON(response);
  308. if(response.succes==true){
  309. $(element).parent().parent().parent().removeClass('filePublished');
  310. $(element).attr('onclick','publishFile(this);');
  311. tell(response.status);
  312. }else{
  313. tell(response.status);
  314. }
  315. }
  316. });
  317. }
  318. function addFolder(){
  319. if(!blockNewFolder){
  320. blockNewFolder = true;
  321. $('.newFolder').hide();
  322. $.ajax({
  323. async:false,
  324. url: "php/action.php?action=addFolder",
  325. data:{name:$('input[name="folderName"]').val()},
  326. success: function(response){
  327. var response = $.parseJSON(response);
  328. if(response.succes){
  329. getFiles(null,'//CURRENT');
  330. $('.folderNameBloc').fadeOut(150);
  331. $('.folderNameBloc input').val('');
  332. }else{
  333. tell(response.status,3000);
  334. }
  335. }
  336. });
  337. $('.newFolder').fadeIn(300);
  338. blockNewFolder = false;
  339. }
  340. }
  341. function deleteUser(message,id,tmpToken){
  342. if(confirm(message))window.location="php/action.php?action=deleteUser&user="+id+"&tmpToken="+tmpToken;
  343. }
  344. function tell(message,time){
  345. var fix = false;
  346. if(time==null)time = 3000;
  347. if(time==0)fix = true;
  348. var options = {
  349. position: 'top-right',
  350. timeout: time,
  351. sticky: fix
  352. };
  353. message = '<i style="height: 20px; width: 20px; display: block; float: left; margin-right: 5px;" class="icon-ok"></i>'+message;
  354. TINYPOP.show(message,options);
  355. }
  356. function openFile(element){
  357. var parent = $(element).parent().parent();
  358. var file = $('.fileUrl',parent).html();
  359. window.location='./php/action.php?action=openFile&file='+file;
  360. }
  361. function focusFile(element){
  362. var parent = $(element).parent().parent();
  363. $('.imageHolder').css("color","#C9C9C9");
  364. $('.imageHolder').css("font-weight","normal");
  365. $('.imageHolder',parent).css("color","#ffffff");
  366. $('.imageHolder',parent).css("font-weight","bold");
  367. }
  368. function deleteFile(element){
  369. if(confirm('Etes vous sûr de vouloir supprimer cet élement?')){
  370. var parent = $(element).parent().parent();
  371. var file =$('.fileUrl',parent).html();
  372. file = file;
  373. $.ajax({
  374. url: "php/action.php?action=deleteFiles",
  375. data:{file:file},
  376. success: function(response){
  377. var response = $.parseJSON(response);
  378. tell(response.status);
  379. if(response.succes)$(element).parent().parent().fadeOut(300);
  380. }
  381. });
  382. }
  383. }
  384. function searchFiles(){
  385. var keywords = $('input[name="search"]').val();
  386. keywords = keywords.replace(' ',',');
  387. keywords = keywords.split(',');
  388. getFiles(keywords);
  389. }
  390. function renameFile(element){
  391. var parent = $(element).parent();
  392. file = $('.fileUrl',parent.parent()).html();
  393. value = $('.fileName',parent).attr("title");
  394. $('.fileName',parent).html('');
  395. $('.fileName',parent).append('<input type="text" value="'+value+'" class="fileNameArea">');
  396. pressEnter('.fileNameArea',function(){
  397. newValue = $('.fileNameArea',parent).val();
  398. if(newValue!=value){
  399. $.ajax({
  400. url: "php/action.php?action=renameFile",
  401. data:{file:file,newName:newValue},
  402. success: function(response){
  403. var response = $.parseJSON(response);
  404. if(!response.succes){
  405. tell(response.status);
  406. $('.fileName',parent).html(value);
  407. }else{
  408. $('.fileName',parent).html(newValue);
  409. $('.fileName',parent).attr("title",newValue);
  410. $('.fileName',parent).attr("alt",newValue);
  411. $('.fileUrl',parent.parent()).html($('.fileUrl',parent.parent()).html().replace(value,newValue));
  412. parent.poshytip('destroy');
  413. parent.attr("title",newValue);
  414. parent.poshytip({
  415. content:newValue,
  416. className: 'tooltip',
  417. showTimeout: 0,
  418. alignTo: 'target',
  419. alignX: 'center',
  420. offsetY: 10,
  421. allowTipHover: false
  422. });
  423. }
  424. }
  425. });
  426. }else{
  427. $('.fileName',parent).html(value);
  428. }
  429. });
  430. }
  431. function checkVersion(){
  432. if(typeof(lastVersion) != 'undefined' && typeof(lastVersionNumber) != 'undefined' && typeof(lastVersionName) != 'undefined' && typeof(lastVersionUrl) != 'undefined'){
  433. $.ajax({
  434. url: "php/action.php?action=checkVersion",
  435. success: function(response){
  436. if(response<lastVersionNumber || (typeof(specialMessage)!= 'undefined' && specialMessage!='')){
  437. var status= 'La nouvelle version '+lastVersion+' ('+lastVersionName+' - N&deg; '+lastVersionNumber+') de DropCenter est <a target="_blank" href="'+lastVersionUrl+'">disponible ici.</a>'
  438. if(typeof(specialMessage)!= 'undefined' && specialMessage!='') status = specialMessage;
  439. $('#versionBloc').html(status);
  440. $('#versionBloc').fadeIn(300);
  441. }}});
  442. }
  443. }
  444. function pressEnter(input,func){
  445. var testTextBox = $(input);
  446. var code =null;
  447. testTextBox.keypress(function(e)
  448. {
  449. code= (e.keyCode ? e.keyCode : e.which);
  450. if (code == 13) func();
  451. // e.preventDefault();
  452. });
  453. }
  454. function getFileOption(elem){
  455. $(".addOptions",$(elem).parent().parent()).slideToggle(200);
  456. }
  457. function explode(delimiter,string,limit){var emptyArray={0:''};if(arguments.length<2||typeof arguments[0]=='undefined'||typeof arguments[1]=='undefined'){return null;}
  458. if(delimiter===''||delimiter===false||delimiter===null){return false;}
  459. if(typeof delimiter=='function'||typeof delimiter=='object'||typeof string=='function'||typeof string=='object'){return emptyArray;}
  460. if(delimiter===true){delimiter='1';}
  461. if(!limit){return string.toString().split(delimiter.toString());}
  462. var splitted=string.toString().split(delimiter.toString());var partA=splitted.splice(0,limit-1);var partB=splitted.join(delimiter.toString());partA.push(partB);return partA;}
  463. function str_replace(search,replace,subject,count){var i=0,j=0,temp='',repl='',sl=0,fl=0,f=[].concat(search),r=[].concat(replace),s=subject,ra=Object.prototype.toString.call(r)==='[object Array]',sa=Object.prototype.toString.call(s)==='[object Array]';s=[].concat(s);if(count){this.window[count]=0;}
  464. for(i=0,sl=s.length;i<sl;i++){if(s[i]===''){continue;}
  465. for(j=0,fl=f.length;j<fl;j++){temp=s[i]+'';repl=ra?(r[j]!==undefined?r[j]:''):r[0];s[i]=(temp).split(f[j]).join(repl);if(count&&s[i]!==temp){this.window[count]+=(temp.length-s[i].length)/f[j].length;}}}
  466. return sa?s:s[0];}
  467. function addslashes(str){return(str+'').replace(/[\\"']/g,'\\$&').replace(/\u0000/g,'\\0');}
  468. function stripslashes(str){return(str+'').replace(/\\(.?)/g,function(s,n1){switch(n1){case'\\':return'\\';case'0':return'\u0000';case'':return'';default:return n1;}});}
  469. function array2json(arr) {
  470. var parts = [];
  471. var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
  472. for(var key in arr) {
  473. var value = arr[key];
  474. if(typeof value == "object") { //Custom handling for arrays
  475. if(is_list) parts.push(array2json(value)); /* :RECURSION: */
  476. else parts[key] = array2json(value); /* :RECURSION: */
  477. } else {
  478. var str = "";
  479. if(!is_list) str = '"' + key + '":';
  480. //Custom handling for multiple data types
  481. if(typeof value == "number") str += value; //Numbers
  482. else if(value === false) str += 'false'; //The booleans
  483. else if(value === true) str += 'true';
  484. else str += '"' + value + '"'; //All other things
  485. // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)
  486. parts.push(str);
  487. }
  488. }
  489. var json = parts.join(",");
  490. if(is_list) return '[' + json + ']';//Return numerical JSON
  491. return '{' + json + '}';//Return associative JSON
  492. }