Commit 90ba94f2 authored by François Billioud's avatar François Billioud

Merge branch 'master' of https://git.erp5.org/repos/ung

Conflicts:
	UNGProject/js/tools.js
parents 64c7226d 13c21f67
/* /*
JIO: Javascript I/O. A library to load, edit, save documents to JIO: Javascript I/O. A library to load, edit, save documents to
multiple storage backends using a common interface multiple storage backends using a common interface
*/ */
/* /*
JIO Namespace. Everything related to JIO happens over here JIO Namespace. Everything related to JIO happens over here
*/ */
var JIO = (function () { var JIO = (function () {
/* Function returns us a JIOStorage, a basic stensil for other kind of storage*/ /* Function returns us a JIOStorage, a basic stensil for other kind of storage*/
var JIOStorage = function (store_info){ var JIOStorage = function (store_info) {
var J = {}; // dummy object. We will augment it and return it var J = {}; // dummy object. We will augment it and return it
J = { J = {
"initialize": function(server,path,username,password){ "initialize": function(server,path,username,password) {
/* /*
Basic initilization or pre-rituals Basic initilization or pre-rituals
*/ */
...@@ -18,23 +19,34 @@ var JIO = (function () { ...@@ -18,23 +19,34 @@ var JIO = (function () {
"loadDocument": function (doc_id, handler ) { "loadDocument": function (doc_id, handler ) {
/* /*
Calls handler with an object associated with requested document of the `doc_id` Calls handler with an object associated with requested document of the `doc_id`
The first argument of handler is error object or null depending on if error occured or not.(Like NodeJS)
Second argmuent is the object literal containing
.hash of the object is the MD5 hash of the data .hash of the object is the MD5 hash of the data
.data of the object is the data itself .data of the object is the data itself
Signature of Handler:
handler = function (err, dataobject) {
if(err===null) {
console.log(dataobject.hash);
console.log(dataobject.data);
}
else{
console.log(err.status);
}
};
*/ */
}, },
"saveDocument": function (doc_id, data, hash, overwrite, async) { "saveDocument": function (doc_id, data, handler, hash, conflict_func, overwrite) {
/* /*
Saves the document with a particular `docid` Saves the document with a particular `doc_id`
If `docid` is `0` then the function assigns a docid automatically
`hash` is used to check if the document was last changed by someone else, `hash` is used to check if the document was last changed by someone else,
If it was, it will raise a JIOContentChanged exception If it was, it will call the conflict_func with all other parameters passed.
If `hash` is not supplied, the document will be overwritten. If `hash` is not supplied, no validation would be performed and conflit_func won't be called and document would be saved directly.
If `overwrite` is true the content will be overwritten without any exception Setting `overwrite` to false, will prevent overwriting the document in anycase if a document with doc_id already exists
`handler` workes sames way as in loadDocument
*/ */
}, },
"getDocumentList": function (storage_id, query, order_by, metadata) { "getDocumentList": function (storage_id, query, order_by, metadata) {
/* /*
Storage implementation should provide this function which will Storage implementation should provide this function which will
...@@ -43,49 +55,155 @@ var JIO = (function () { ...@@ -43,49 +55,155 @@ var JIO = (function () {
TODO Define how `query` is specified TODO Define how `query` is specified
`ordered_by` how the results should be sorted `ordered_by` how the results should be sorted
*/ */
},
"hashdata": function(data) {
if(typeof hex_sha1 !== 'function') {
//this means that the hashing library is not load it. Load it now
$.ajax({
async:false,
type: "GET",
url: "js/sha1-min.js",
dataType: "script"
});
}
//now we can use hex_sha1(hopefully)
return hex_sha1(data);
} }
}; };
return J; return J;
}; //JIOStorage }; //JIOStorage
/* DAVStorage : Does JIO on a WebDAV server*/ /* DAVStorage : Does JIO on a WebDAV server*/
var DAVStorage = function (store_info){ var DAVStorage = function (store_info) {
if(!store_info.serverSore) if(!store_info.server)
throw Error("No server specified for DAVStorage"); throw Error("No server specified for DAVStorage");
store_info.path = store_info.path || ''; store_info.path = store_info.path || '';
var auth=false; var auth=false;
if(store_info.user){ if(store_info.user) {
if(!store_info.pass) if(store_info.pass===undefined)
throw Error("No password specified of user for DAVStorage"); throw Error("No password specified of user for DAVStorage");
auth=true; auth=true;
} }
//server,path should all be in correct format http://foo.com/ some/dir/
var complete_path = store_info.server + store_info.path;
var add_auth_headers = function (ajaxsetting) {
ajaxsetting.headers={'Authorization':"Basic "+btoa(store_info.user+":"+store_info.pass)};
ajaxsetting.fields={'withCredentials': "true"};
return ajaxsetting;
};
var J = JIOStorage(store_info); // we will return this object at the end var J = JIOStorage(store_info); // we will return this object at the end
J.loadDocument = function (doc_id,handler) { J.loadDocument = function (doc_id,handler) {
ajaxsetting={ var ajaxsetting={
/* server,path should all be in correct format http://foo.com/ some/dir/*/ 'url': complete_path+doc_id,
url:store_info.server + store_info.path + doc_id; 'type':'GET',
'cache':false,
'success': function (fetched_data, textStatus, jqXHR) {
var hashed_data=J.hashdata(fetched_data);
handler(null,{'hash':hashed_data,'data':fetched_data});
},
'error': function(jqXHR, textStatus, errorThrown) {
console.log("Error while doing ajax request for loadDocument. Return code:"+jqXHR.status+"["+errorThrown+"]");
handler(jqXHR);
}
}; };
if (auth) {
ajaxsetting = add_auth_headers(ajaxsetting);
}
$.ajax(ajaxsetting);
}; };
J.saveDocument = function(doc_id, data, handler, hash, conflict_func, overwrite){
var document_url=complete_path+doc_id;
var ajaxsetting={
'url':document_url,
'cache':false,
'type':'PUT',
'data':data,
'success' : function (fetched_data, textStatus, jqXHR) {
var hashed_data= J.hashdata(data);
handler(null,{'hash':hashed_data,'data':data});
},
'error' : function(jqXHR, textStatus, errorThrown) {
console.log("Error while doing ajax request for loadDocument. Return code:"+jqXHR.status+"["+errorThrown+"]");
handler(jqXHR);
}
};
if (auth) {
ajaxsetting = add_auth_headers(ajaxsetting);
}
//The logic:
if(hash){
//hash is provided, we will now have to get the document first.
J.loadDocument(doc_id, function(err, dataobject){
if(err!==null){
//error occured, check if it is 404
if(err.status===404){
//the document doesn't exist, just do the save
$.ajax(ajaxsetting);
}
else{
//some other strange error,just call the user's error handler
handler(err);
}
}
else{
//we got back a document, check for hash
if(dataobject.hash===hash){
//hash matched, now check for overwrite value
if(overwrite!==false){
$.ajax(ajaxsetting);
}
}
else{
//hash didn't match, raise conflict_func
conflict_func(doc_id,data,handler,hash,overwrite);
}
}
});
}
else{
//user didn't provide hash.
if(overwrite!==false){
//user doesn't care if document get's overwriten,
//save the document
$.ajax(ajaxsetting);
}
else{
//user doesn't wants to overwrite the document, now check if the document exists
J.loadDocument(doc_id,function(err,dataobject){
if(err!==null) {
//error occured while trying to fetch the doc, check if error was 404
if(err.status===404) {
//document doesn't exist, just do the save
$.ajax(ajaxsetting);
}
//now if any other error occurs, we don't care. (this is bad habbit)
}
});
}
}
}; };
})();
JIOStorage = J.getDocumentList = function(){};
}
var JIODAVStorage=Object.create(JIOStorage); //All methods are defined, now return J as the newly cooked object
JIODAVStorage.initialize = function (server,path,user,pass){ return J;
this.server=server; };//DAVStorage
this.path=path || '/';
if(user)
{
this.user=user;
this.pass=pass;
}
JIODAVStorage.loadDocument = function(doc_name){
//defined all classes, now return the interface of JIO which has all these classes
return {
'JIOStorage':JIOStorage,
'DAVStorage':DAVStorage
};
})();
...@@ -45,8 +45,11 @@ saveXHR = function(address) { ...@@ -45,8 +45,11 @@ saveXHR = function(address) {
$.ajax({ $.ajax({
url: address, url: address,
type: "PUT", type: "PUT",
username: "smik", headers: {
password: "asdf", Authorization: "Basic "+btoa("smik:asdf")},
fields: {
withCredentials: "true"
},
data: JSON.stringify(getCurrentDocument()), data: JSON.stringify(getCurrentDocument()),
success: function(){alert("saved");}, success: function(){alert("saved");},
error: function(xhr) { alert("error while saving");} error: function(xhr) { alert("error while saving");}
...@@ -59,10 +62,11 @@ loadXHR = function(address) { ...@@ -59,10 +62,11 @@ loadXHR = function(address) {
type: "GET", type: "GET",
dataType: "json", dataType: "json",
cache:false, cache:false,
username: "nom", headers: {
password: "test", Authorization: "Basic "+btoa("smik:asdf")},
/* username: "smik", No need to specify credential while GETTING fields: {
password: "asdf",*/ withCredentials: "true"
},
success: function(data){ success: function(data){
var cDoc = getCurrentDocument(); var cDoc = getCurrentDocument();
cDoc.load(data); cDoc.load(data);
......
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