From ba9c95621bccddc8c83689906643a080e1671f22 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 24 Mar 2010 16:35:24 +0100 Subject: [PATCH] ajax interface --- css/default.php | 11 +- files/delete.php | 34 +++++ files/get_files.php | 45 ++++++ files/index.php | 35 +++++ files/rename.php | 34 +++++ files/upload.php | 37 +++++ img/icons/delete.png | Bin 0 -> 641 bytes img/icons/rename.png | Bin 0 -> 245 bytes inc/lib_files.php | 67 ++++++--- inc/templates/header.php | 14 +- js/ajax.js | 28 ++++ js/lib_ajax.js | 55 +++++++ js/lib_files.js | 306 +++++++++++++++++++++++++++++++++++++++ js/lib_xmlloader.js | 150 +++++++++++++++++++ 14 files changed, 793 insertions(+), 23 deletions(-) create mode 100644 files/delete.php create mode 100644 files/get_files.php create mode 100644 files/index.php create mode 100644 files/rename.php create mode 100644 files/upload.php create mode 100644 img/icons/delete.png create mode 100644 img/icons/rename.png create mode 100644 js/ajax.js create mode 100644 js/lib_ajax.js create mode 100644 js/lib_files.js create mode 100644 js/lib_xmlloader.js diff --git a/css/default.php b/css/default.php index c781e58de7..8d10b89cab 100755 --- a/css/default.php +++ b/css/default.php @@ -127,6 +127,11 @@ a#owncloud-logo span { font-weight:bold; } - - - +.hidden{ + height:0px; + width:0px; + margin:0px; + padding:0px; + border:0px; + //do not use display:none here, it breaks iframes in some browsers +} \ No newline at end of file diff --git a/files/delete.php b/files/delete.php new file mode 100644 index 0000000000..1c660d9780 --- /dev/null +++ b/files/delete.php @@ -0,0 +1,34 @@ +. +* +*/ +require_once('../inc/lib_base.php'); + +$dir=$_GET['dir']; +$file=$_GET['file']; +if(isset($_SESSION['username']) and $_SESSION['username'] and strpos($dir,'..')===false){ + $file=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$file; + if(is_file($file)){ + unlink($file); + } +} + +?> \ No newline at end of file diff --git a/files/get_files.php b/files/get_files.php new file mode 100644 index 0000000000..f6db75b3a4 --- /dev/null +++ b/files/get_files.php @@ -0,0 +1,45 @@ +. +* +*/ +require_once('../inc/lib_base.php'); + +// header('Content-type: text/plain'); +header('Content-type: application/xml'); + +$dir=isset($_GET['dir'])?$_GET['dir']:''; +$files=OC_FILES::getdirectorycontent($CONFIG_DATADIRECTORY.'/'.$dir); +$dirname=$files[0]['directory']; +$dirname=substr($dirname,strrpos($dirname,'/')); +ob_clean(); +echo "\n"; +echo "\n"; +foreach($files as $file){ + $attributes=''; + foreach($file as $name=>$data){ + $data=str_replace("'",''',$data); + if (is_string($name)) $attributes.=" $name='$data'"; + } + $attributes.=' date=\''.date($CONFIG_DATEFORMAT,$file['mtime']).'\''; + echo "\n"; +} +echo ""; +?> \ No newline at end of file diff --git a/files/index.php b/files/index.php new file mode 100644 index 0000000000..6bd994e761 --- /dev/null +++ b/files/index.php @@ -0,0 +1,35 @@ +. +* +*/ + + +//require_once('../../config/config.php'); +require_once('../inc/lib_base.php'); + +OC_UTIL::addscript('/plugins/ajax/ajax.js'); + +OC_UTIL::showheader(); + +echo "
"; + +OC_UTIL::showfooter(); +?> diff --git a/files/rename.php b/files/rename.php new file mode 100644 index 0000000000..f0f272f018 --- /dev/null +++ b/files/rename.php @@ -0,0 +1,34 @@ +. +* +*/ +require_once('../inc/lib_base.php'); + +$dir=$_GET['dir']; +$file=$_GET['file']; +$newname=$_GET['newname']; +if($file!=$newname and $newname!='' and isset($_SESSION['username']) and $_SESSION['username'] and strpos($dir,'..')===false){ + $source=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$file; + $target=$CONFIG_DATADIRECTORY.'/'.$dir.'/'.$newname; + rename($source,$target); +} + +?> \ No newline at end of file diff --git a/files/upload.php b/files/upload.php new file mode 100644 index 0000000000..b5fed2ed5b --- /dev/null +++ b/files/upload.php @@ -0,0 +1,37 @@ +. +* +*/ +require_once('../inc/lib_base.php'); + +$fileName=$_FILES['file']['name']; +$source=$_FILES['file']['tmp_name']; +$target=$CONFIG_DATADIRECTORY.'/'.$_GET['dir'].'/'.$fileName; +if(isset($_SESSION['username']) and $_SESSION['username'] and strpos($_GET['dir'],'..')===false){ + if(move_uploaded_file($source,$target)){ + echo 'true'; + }else{ + echo 'false'; + } +}else{ + echo 'false'; +} +?> \ No newline at end of file diff --git a/img/icons/delete.png b/img/icons/delete.png new file mode 100644 index 0000000000000000000000000000000000000000..d04a554ee17853a053c7c6a795d6aa20ed32aa9d GIT binary patch literal 641 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJbFq_W2nPqp?T7vkfLzW3kH}&M z2FBeW%xLxI@gtz1WQl7;NpOBzNqJ&XDnogBxn5>oc5!lIL8@MUQTpt6Hc~*<;sHJ( zuK)l4Z(v|p!N9PVfuWs&p^JfGA_Kz=28KBd467IzAX4jDSb*GiKE6H%hCBv_Tn2_L z28Klp4AZo=>*d6nRHQFelpgl-Jed$XS6*hniPlkf`z7+iOXNkDC`v6+mRl+(v`kK5 zwTk$9b?ME9YMZs?_j%bL_HsDk<9sU2r$Je2`?nP^4n zy$lTd7#Q|j8XpwoI%I5o$k*ermDN#8qhmrm$Ax%K+S#3Uu|MtSb;ix@0t3SZA)d=& zVVA>0E~h43Ns7Cc5PLm2@wTwQZ4try!urld@myOUR3yfXZwfVuCELX$$gHBz;HAv3GxeOFfuao00U1?PcLu(fWV*& z*YDnY^6b^8FJFJbz;6)v0|bBn{{8pwYTmshp#C|YE{-7*lD!9A#hMrxSP$@P+)yej zFHd4mDu4d>{K5|<=Xn;eJT-f8`oR(f<-O;7#4aq4w-XY2{jJDX@@(tpr)jM&lV@~v z*Ho&Vn=Ex=#*GE`IlapY+3ZWUT+T69GWmBUabMW;!&!e;{7GJTTlg5GXv(e!RomBu zh3)n|!O~g#f1h~dx-S*-ITHltB~N4Bap$D7gPVoUk4G^qQ+_ZwZx+t3%CQ#%x|6}v L)z4*}Q$iB}L2Ua6 literal 0 HcmV?d00001 diff --git a/img/icons/rename.png b/img/icons/rename.png new file mode 100644 index 0000000000000000000000000000000000000000..2323757e0e94760b6aec976d52da09ee2584bb7d GIT binary patch literal 245 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFR|DNig)WhgH%*UQYyE>2D?NY%?PN}v7CMhd7%J-{c# z_5c6>9WXF)(u7^Rb{;r*;ONn#7cO3S^XARlw{PFQdk0jr(_cj#sD-&C$S;_|`i9?` zLLiU9(9^{+gyVX0f&vqVWOJiZ^RG8|9X4$4IQVMDaY==UGYPe7Y)5YNv@(V>UuQLE aH)mjws#a^|o9@yJ((UQ$=d#Wzp$PyKYFkPG literal 0 HcmV?d00001 diff --git a/inc/lib_files.php b/inc/lib_files.php index 8eb1fc9c12..dbafa52a18 100755 --- a/inc/lib_files.php +++ b/inc/lib_files.php @@ -35,7 +35,7 @@ class OC_FILES { * @param basedir $basedir * @param dir $dir */ - public static function showbrowser($basedir,$dir){ + public static function showbrowser($basedir,$dir){/* global $CONFIG_DATEFORMAT; global $WEBROOT; @@ -62,29 +62,62 @@ class OC_FILES { // files and directories echo('
'); $filesfound=false; - if (is_dir($directory)) { + $content=self::getdirectorycontent($directory); + if($content){ + foreach($content as $file){ + echo(''); + OC_UTIL::showicon($file['type']); + if($file['type']=='dir') echo(''); + if($file['type']<>'dir') echo(''); + if($file['type']<>'dir') echo(''); else echo(''); + echo(''); + echo(''); + } + } + echo('
'.$file['name'].''.$file['name'].''.$file['size'].' byte'.date($CONFIG_DATEFORMAT,$file['mtime']).'
'); + if(!$content) echo('

no files here

'); + echo('
');*/ + echo '
'; + } + + /** + * get the content of a directory + * @param dir $directory + */ + public static function getdirectorycontent($directory){ + $filesfound=true; + $content=array(); + $dirs=array(); + $file=array(); + if (is_dir($directory)) { if ($dh = opendir($directory)) { - while (($file = readdir($dh)) !== false) { - if($file<>'.' and $file<>'..'){ + while (($filename = readdir($dh)) !== false) { + if($filename<>'.' and $filename<>'..'){ + $file=array(); $filesfound=true; - $stat=stat($directory.'/'.$file); - $filetype=filetype($directory .'/'. $file); - echo(''); - OC_UTIL::showicon($filetype); - - if($filetype=='dir') echo(''.$file.''); - if($filetype<>'dir') echo(''.$file.''); - if($filetype<>'dir') echo(''.$stat['size'].' byte'); else echo(''); - echo(''.date($CONFIG_DATEFORMAT,$stat['mtime']).''); - echo(''); + $file['name']=$filename; + $file['directory']=$directory; + $stat=stat($directory.'/'.$filename); + $file=array_merge($file,$stat); + $file['type']=filetype($directory .'/'. $filename); + if($file['type']=='dir'){ + $dirs[$file['name']]=$file; + }else{ + $files[$file['name']]=$file; + } } } closedir($dh); } } - echo(''); - if(!$filesfound) echo('

no files here

'); - echo('
'); + ksort($dirs); + ksort($files); + $content=array_merge($dirs,$files); + if($filesfound){ + return $content; + }else{ + return false; + } } diff --git a/inc/templates/header.php b/inc/templates/header.php index 0399a9c654..66289a23ba 100755 --- a/inc/templates/header.php +++ b/inc/templates/header.php @@ -3,10 +3,17 @@ ownCloud - - + + + + + + + - + '); @@ -15,6 +22,7 @@ echo('

'); echo('

'.$error.'

'); diff --git a/js/ajax.js b/js/ajax.js new file mode 100644 index 0000000000..6c0185c659 --- /dev/null +++ b/js/ajax.js @@ -0,0 +1,28 @@ +/** +* ownCloud - ajax frontend +* +* @author Robin Appelman +* @copyright 2010 Robin Appelman icewind1991@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library. If not, see . +* +*/ + +var dir='' +var loc=document.location.toString(); +if(loc.indexOf('#')!=-1){ + dir=loc.substring(loc.indexOf('#')+1); +} + +OC_onload.add(new function(){OC_FILES.showbrowser(dir)}); \ No newline at end of file diff --git a/js/lib_ajax.js b/js/lib_ajax.js new file mode 100644 index 0000000000..9997942251 --- /dev/null +++ b/js/lib_ajax.js @@ -0,0 +1,55 @@ +/** +* ownCloud - ajax frontend +* +* @author Robin Appelman +* @copyright 2010 Robin Appelman icewind1991@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library. If not, see . +* +*/ + +//The callBack object provides an easy way to pass a member of an object as callback parameter and makes sure that the 'this' is always set correctly when called. +callBack=function(func,obj){ + this.func=func; + this.obj=obj; +} + +callBack.prototype=function(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){ + this.call(false,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); +} +callBack.prototype.func=false; +callBack.prototype.obj=false; +callBack.prototype.call=function(dummy,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){ + //the dummy is just to provide compatibility with the normal call function and isn't used + this.func.call(this.obj,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); +} +callBack.prototype.apply=function(dummy,arguments){ + //the dummy is just to provide compatibility with the normal call function and isn't used + this.apply(this.obj,arguments); +} + +//provide a simple way to add things to the onload +OC_onload=new Object(); + +OC_onload.items=new Array(); +OC_onload.add=function(callback){ + OC_onload.items[OC_onload.items.length]=callback; +} +OC_onload.run=function(){ + for(index in OC_onload.items){ + if(OC_onload.items[index].call){ + OC_onload.items[index].call(); + } + } +} \ No newline at end of file diff --git a/js/lib_files.js b/js/lib_files.js new file mode 100644 index 0000000000..8d1039a3b0 --- /dev/null +++ b/js/lib_files.js @@ -0,0 +1,306 @@ +/** +* ownCloud - ajax frontend +* +* @author Robin Appelman +* @copyright 2010 Robin Appelman icewind1991@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library. If not, see . +* +*/ + +OC_FILES=new Object(); +OC_FILES.xmlloader=new OCXMLLoader(); + +OC_FILES.getdirectorycontent_parse=function(req){ + var files=new Array(); + var response=req.responseXML; + if(response){ + var fileElements=response.getElementsByTagName('file'); + if(fileElements.length>0){ + for(index in fileElements){ + var file=new Array(); + var attributes=Array('size','name','type','directory','date'); + for(i in attributes){ + var name=attributes[i]; + file[name]=fileElements.item(index).getAttribute(name); + } + files[file.name]=file; + } + } + if(OC_FILES.getdirectorycontent_callback){ + OC_FILES.getdirectorycontent_callback(files); + } + } +} + +OC_FILES.getdirectorycontent=function(dir,callback){ + if(callback){ + OC_FILES.getdirectorycontent_callback=callback; + } + OC_FILES.xmlloader.setCallBack(OC_FILES.getdirectorycontent_parse); + OC_FILES.xmlloader.load('files/get_files.php?dir='+dir); +} + +OC_FILES.showicon=function(filetype){ + var td=document.createElement('td'); + var img=document.createElement('img'); + td.appendChild(img); + img.setAttribute('width',16); + img.setAttribute('height',16); + if(filetype=='dir'){ + img.src=WEBROOT+'/img/icons/folder.png'; + }else{ + img.src=WEBROOT+'/img/icons/other.png'; + } + return td; +} + +OC_FILES.dir=''; +OC_FILES.browser=null; +OC_FILES.showbrowser=function(dir){ + dir=(dir)?dir:''; + OC_FILES.dir=dir; + OC_FILES.getdirectorycontent(dir,OC_FILES.showbrowser_callback); +} + +OC_FILES.showbrowser_callback=function(content){ + var dir=OC_FILES.dir + var dirs=dir.split('/'); + var tr=null; + var td=null; + var img=null; + + //remove current content; + var contentNode=document.getElementById('content'); + if(contentNode.hasChildNodes()){ + while(contentNode.childNodes.length >=1){ + contentNode.removeChild(contentNode.firstChild); + } + } + + // breadcrumb + if(dirs.length>0) { + var breadcrumb=document.createElement('div'); + breadcrumb.className='center'; + var table=document.createElement('table'); + breadcrumb.appendChild(table); + table.setAttribute('cellpadding',2); + table.setAttribute('cellspacing',0); + var tbody=document.createElement('tbody');//some IE versions need this + table.appendChild(tbody); + tr=document.createElement('tr'); + tbody.appendChild(tr); + td=document.createElement('td'); + tr.appendChild(td); + td.className='nametext'; + var a=document.createElement('a'); + td.appendChild(a); + a.setAttribute('href','#'); + a.setAttribute('onclick','OC_FILES.showbrowser()'); + a.appendChild(document.createTextNode('Home')); + var currentdir=''; + for(index in dirs) { + d=dirs[index]; + currentdir+='/'+d; + if(d!=''){ + td=document.createElement('td'); + tr.appendChild(td); + td.className='nametext'; + a=document.createElement('a'); + td.appendChild(a); + a.setAttribute('href','#'+currentdir); + a.setAttribute('onclick','OC_FILES.showbrowser("'+currentdir+'")'); + img=document.createElement('img'); + a.appendChild(img); + img.src=WEBROOT+'/img/arrow.png'; + a.appendChild(document.createTextNode(' ' +d)); + } + } + contentNode.appendChild(breadcrumb); + } + + // files and directories + var files=document.createElement('div'); + OC_FILES.browser=files; + files.className='center'; + var table=document.createElement('table'); + files.appendChild(table); + table.setAttribute('cellpadding',6); + table.setAttribute('cellspacing',0); + table.className='browser'; + var tbody=document.createElement('tbody');//some IE versions need this + table.appendChild(tbody); + var filesfound=false; + var sizeTd=null; + if(content){ + for(index in content){ + file=content[index]; + if(file.name){ + tr=document.createElement('tr'); + tbody.appendChild(tr); + tr.className='browserline'; + tr.appendChild(OC_FILES.showicon(file['type'])); + td=document.createElement('td'); + tr.appendChild(td); + td.className='nametext'; + td.setAttribute('name',file['name']); + a=document.createElement('a'); + td.appendChild(a); + a.appendChild(document.createTextNode(file['name'])) + if(file['type']=='dir'){ + a.setAttribute('onclick','OC_FILES.showbrowser("'+dir+file['name']+'")'); + td.setAttribute('colspan',2); + a.setAttribute('href','#'+dir+file['name']); + }else{ + a.setAttribute('href',WEBROOT+'/?dir=/'+dir+'&file='+file['name']); + sizeTd=document.createElement('td'); + tr.appendChild(sizeTd); + sizeTd.className='sizetext'; + sizeTd.appendChild(document.createTextNode(file['size']+' bytes')); + } + a=document.createElement('a'); + img=document.createElement('img'); + td.appendChild(img); + img.className='rename'; + img.alt='rename' + img.title='rename'; + img.src=WEBROOT+'/img/icons/rename.png'; + img.style.height='16px' + img.style.width='16px' + img.setAttribute('onclick','OC_FILES.rename(\''+dir+'\',\''+file['name']+'\')') + td=document.createElement('td'); + tr.appendChild(td); + td.className='sizetext'; + td.appendChild(document.createTextNode(file['date'])); + if(file['type']!='dir'){ + td=document.createElement('td'); + tr.appendChild(td); + img=document.createElement('img'); + td.appendChild(img); + img.className='delete'; + img.alt='delete' + img.title='delete'; + img.src=WEBROOT+'/img/icons/delete.png'; + img.style.height='16px' + img.style.width='16px' + img.setAttribute('onclick','OC_FILES.remove(\''+dir+'\',\''+file['name']+'\')') + } + } + } + } + tr=document.createElement('tr'); + tbody.appendChild(tr); + td=document.createElement('td'); + tr.appendChild(td); + td.className='upload'; + td.setAttribute('colspan','5'); + this.showuploader(dir,td); + contentNode.appendChild(files); +} + +OC_FILES.showuploader=function(dir,parent){ + this.uploadForm=document.createElement('form'); + this.uploadForm.setAttribute('target','uploadIFrame'); + this.uploadForm.setAttribute('action','files/upload.php?dir='+dir); + this.uploadForm.method='post'; + this.uploadForm.setAttribute('enctype','multipart/form-data'); + this.uploadIFrame=document.createElement('iframe'); + this.uploadIFrame.className='hidden'; + this.uploadIFrame.name='uploadIFrame'; + parent.appendChild(this.uploadIFrame); + var file=document.createElement('input'); + file.name='file'; + file.setAttribute('type','file'); + file.setAttribute('onchange','OC_FILES.upload("'+dir+'")'); + this.uploadForm.appendChild(document.createTextNode('Upload file: ')); + this.uploadForm.appendChild(file); + parent.appendChild(this.uploadForm); +} + +OC_FILES.upload=function(dir){ + OC_FILES.uploadIFrame.setAttribute('onload',"OC_FILES.upload_callback.call(OC_FILES,'"+dir+"')"); + OC_FILES.uploadForm.submit(); +} + +OC_FILES.upload_callback=function(dir){ + this.showbrowser(dir); +} + +OC_FILES.rename=function(dir,file){ + var item=document.getElementsByName(file).item(0); + item.oldContent=new Array(); + if(item.hasChildNodes()){ + while(item.childNodes.length >=1){ + item.oldContent[item.oldContent.length]=item.firstChild; + item.removeChild(item.firstChild); + } + } + var form=document.createElement('form'); + form.setAttribute('onsubmit','return OC_FILES.do_rename(\''+dir+'\',\''+file+'\')') + var input=document.createElement('input'); + input.setAttribute('type','text'); + input.setAttribute('name','newname'); + input.setAttribute('value',file); + input.setAttribute('id',file+'_newname') + input.setAttribute('onblur','OC_FILES.rename_cancel(\''+file+'\')'); + form.appendChild(input); + item.appendChild(form); + input.focus(); +} + +OC_FILES.do_rename=function(dir,file){ + var item=document.getElementById(file+'_newname'); + var newname=item.value; + if(newname==''){ + return false; + }else if(file==newname){ + OC_FILES.showbrowser(OC_FILES.dir); + return false; + } + xmlloader=new OCXMLLoader(); + xmlloader.setCallBack(OC_FILES.rename_callback); + xmlloader.load('files/rename.php?dir='+dir+'&file='+file+'&newname='+newname); + return false; +} + +OC_FILES.rename_callback=function(req){ + OC_FILES.showbrowser(OC_FILES.dir); +} + +OC_FILES.rename_cancel=function(file){ + var item=document.getElementsByName(file).item(0); + if(item.hasChildNodes()){ + while(item.childNodes.length >=1){ + item.removeChild(item.firstChild); + } + } + for(index in item.oldContent){ + if(item.oldContent[index].nodeType){ + item.appendChild(item.oldContent[index]); + } + } +} + +OC_FILES.remove=function(dir,file){ + remove=confirm('remove file \''+file+'\'?'); + if(remove){ + xmlloader=new OCXMLLoader(); + xmlloader.setCallBack(OC_FILES.remove_callback); + xmlloader.load('files/delete.php?dir='+dir+'&file='+file); + } +} + +OC_FILES.remove_callback=function(req){ + OC_FILES.showbrowser(OC_FILES.dir); +} \ No newline at end of file diff --git a/js/lib_xmlloader.js b/js/lib_xmlloader.js new file mode 100644 index 0000000000..14fd3ee8af --- /dev/null +++ b/js/lib_xmlloader.js @@ -0,0 +1,150 @@ +/** +* ownCloud - ajax frontend +* +* @author Robin Appelman +* @copyright 2010 Robin Appelman icewind1991@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library. If not, see . +* +*/ + +var READY_STATE_UNINITIALIZED=0; +var READY_STATE_LOADING=1; +var READY_STATE_LOADED=2; +var READY_STATE_INTERACTIVE=3; +var READY_STATE_COMPLETE=4; + +/** +* Class for loaded browser independant xml loading +*/ +OCXMLLoader=function(){ + this.errorCallBack=this.defaultError; +} + +OCXMLLoader.prototype={ + contentType:'', + method:'GET', + request:'', + callBack:null, + async:true, + + /** + * Loads an XML document + * @param string url + * @param string request + * @none + */ + load:function(url,request){ + request=(request)?request:""; + method=this.method; + contentType=(!this.contentType && method=="POST")?"application/x-www-form-urlencoded":this.contentType; + if(window.XDomainRequest){ + req=new XDomainRequest(); + }else if(window.XMLHttpRequest){ + req=new XMLHttpRequest(); + }else if(window.ActiveXObject){ + req=new ActiveXObject('Microsoft.XMLHTTP') + } + if (req){ + try{ + var loader=this; + req.onreadystatechange=function(){ + loader.onReadyState.call(loader,req) + } + req.open(method,url,this.async); + if (contentType){ + req.setRequestHeader("Content-Type",contentType); + } + if(method=="POST"){ + req.setRequestHeader("Content-length", request.length); + req.setRequestHeader("Connection", "close"); + } + req.send(request); + }catch (err){ + this.errorCallBack(req); + } + } + }, + onReadyState:function(req){ + var ready=req.readyState; + if (ready==READY_STATE_COMPLETE){ + var HttpStatus=req.status; + if (HttpStatus==200 || HttpStatus==0){ + //alert("response: "+this.req.responseText); + this.callBack(req); + }else{ + this.errorCallBack(req); + } + } + }, + defaultError:function(req){ + alert("Error fetching data!" + +"\n\n

ReadyState: "+req.readyState + +"\n
Status: "+req.status + +"\n
Headers: "+req.getAllResponseHeaders() + +"\n
File: "+req.url + +"\n
Response: "+req.responseText); + }, + /** + * Sets the request method + * @param string method + * @none + */ + setMethod:function(method){ + this.method=method; + }, + /** + * Sets the content type + * @param string type + * @none + */ + setType:function(type){ + this.type=type; + }, + /** + * Sets the callback function + * @param function callBack + * @none + */ + setCallBack:function(callBack){ + this.callBack=callBack; + }, + /** + * Sets the error callback function + * @param function errorCallBack + * @none + */ + setErrorCallBack:function(errorCallBack){ + this.errorCallBack=errorCallBack; + } +} + +testClass=function(){ +} + +testClass.prototype={ + testFunc:function(){ + this.test="test"; + test=new OCXMLLoader(this); + test.setCallBack(this.callBack); + test.load(parseUri('%root%/data/sites/index.xml')); + }, + callBack:function(req){ + alert(this.test); + alert(req.responseText); + } +} +test=new testClass() +test.testFunc +// mainLoadStack.append(test.testFunc,test);