Commit 360f62d8 authored by François Billioud's avatar François Billioud

comment and debug localStorage

parent 258ef1c7
...@@ -238,8 +238,63 @@ ...@@ -238,8 +238,63 @@
}, },
/************************************************************ /**
*********************** LocalStorage ************************/ * load the list of the documents in this storage
* @param option : optional object containing
* "success" : the function to execute when the load is done
* "errorHandler" : the function to execute if an error occures
* "asynchronous" : a boolean set to false if the request must be synchronous
* @return null if the request is asynchronous, the list of documents otherwise.
* @example {"file1":{fileName:"file1",creationDate:"Tue, 23 Aug 2011 15:18:32 GMT",lastModified:"Tue, 23 Aug 2011 15:18:32 GMT"},...}
*/
getDocumentList: function(option) {
var storage = this;
var list = null;
$.ajax({
url: storage.location + "/dav/"+storage.userName+"/"+storage.applicationID+"/",
async: option.asyncronous || true,
type: "PROPFIND",
dataType: "xml",
headers: {Authorization: "Basic "+Base64.encode(this.userName+":"+this.applicationPassword), Depth: "1"},
fields: {withCredentials: "true"},
success: function(data) {list=xml2jsonFileList(data);if(option.success) option.success(list)},
error: option.errorHandler || function(type) {alert("Error "+type.status+" : fail while trying to load file list");}
});
return list;//warning : always null if asyncronous
function xml2jsonFileList(xmlData) {//transform the xml into a list
var fileList = {};
$("D\\:response",xmlData)
.each(function(i,data){
if(i>0) {//exclude the parent folder
var name = fileName(data);
fileList[name]=xml2jsonFile(data)
fileList[name].fileName = name;
}
});
//remove webDav files from the list
delete fileList[".htaccess"];
delete fileList[".htpasswd"];
return fileList;
}
function xml2jsonFile(xmlData) {//transform the xml about a file into json information
var file = {};
file.lastModified = $($("lp1\\:getlastmodified",xmlData).get(0)).text();
file.creationDate = $($("lp1\\:creationdate",xmlData).get(0)).text();
return file;
}
function fileName(xmlData) {//read the name of a file in the xml
var string = $($("D\\:href",xmlData).get(0)).text()
var T = string.split("/");
return T[T.length-1] ? T[T.length-1] : T[T.length-2]+"/";
}
}
}
/************************************************************
*********************** LocalStorage ************************/
/** /**
* Class LocalStorage * Class LocalStorage
* @class provides usual API to save/load/delete documents on the localStorage * @class provides usual API to save/load/delete documents on the localStorage
...@@ -281,7 +336,7 @@ ...@@ -281,7 +336,7 @@
} }
} else { } else {
if(option.success) { if(option.success) {
return option.success(doc); return option.success(doc.content);
} else { } else {
return doc return doc
} }
...@@ -299,17 +354,28 @@ ...@@ -299,17 +354,28 @@
* oldData : last data downloaded. Used to know if data has changed since last download and has to been merged * oldData : last data downloaded. Used to know if data has changed since last download and has to been merged
*/ */
saveDocument: function(data, fileName, option) { saveDocument: function(data, fileName, option) {
if(!this.documents[fileName] || option.overwrite) { if(!this.documents[fileName]) { //create document
this.documents[fileName] = data; this.documents[fileName] = {
fileName:fileName,
content: data,
creationDate: Date.now(),
lastModified: Date.now()
};
this.save(); this.save();
if(option.success) option.success(); if(option.success) option.success();
} else { } else {
if(option.overwrite) { //overwrite
this.documents[fileName].lastModified = Date.now();
this.documents[fileName].content = data;
this.save();
if(option.success) option.success();
} else { //repport an error
var error = {status: 403,message: "document already exists"}; var error = {status: 403,message: "document already exists"};
if(option.errorHandler) option.errorHandler(error); if(option.errorHandler) option.errorHandler(error);
} }
}
}, },
/** /**
* Delete a document or a list of documents from the storage * Delete a document or a list of documents from the storage
* @param file : fileName or array of fileNames to delete * @param file : fileName or array of fileNames to delete
...@@ -318,36 +384,17 @@ ...@@ -318,36 +384,17 @@
* "errorHandler" : the function to execute if an error occures * "errorHandler" : the function to execute if an error occures
*/ */
deleteDocument: function(file, option) { deleteDocument: function(file, option) {
var storage = this;
oneOrEach(file,deleteFile); oneOrEach(file,deleteFile);
this.save(); this.save();
if(option.success) option.success(); if(option.success) option.success();
function deleteFile(fileName) { function deleteFile(fileName) {
delete this.documents[fileName]; delete storage.documents[fileName];
} }
}, },
/**
* load the list of the documents in this storage
* @param option : optional object containing
* "success" : the function to execute when the load is done
* "errorHandler" : the function to execute if an error occures
* @return null if the request is asynchronous, the list of documents otherwise.
* @example {"file1":{fileName:"file1",creationDate:"Tue, 23 Aug 2011 15:18:32 GMT",lastModified:"Tue, 23 Aug 2011 15:18:32 GMT"},...}
*/
getDocumentList: function(option) {
var list = this.documents;
if(option.success) option.success(list);
return list;
},
save: function() {
localStorage[this.userName]=JSON.stringify(this.documents);
}
}
/*************************************************************** /***************************************************************
*********************** IndexedStorage *************************/ *********************** IndexedStorage *************************/
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment