Commit 0c0dbc06 authored by Tristan Cavelier's avatar Tristan Cavelier

Updating Dav Storage

Fix getDocumentList setTimeout bug.
parent 7996dfca
...@@ -28,6 +28,32 @@ var newDAVStorage = function ( spec, my ) { ...@@ -28,6 +28,32 @@ var newDAVStorage = function ( spec, my ) {
return 'Need at least 2 parameters: "username" and "url".'; return 'Need at least 2 parameters: "username" and "url".';
}; };
priv.newAsyncModule = function () {
var async = {};
async.call = function (obj,function_name,arglist) {
obj._wait = obj._wait || {};
if (obj._wait[function_name]) {
obj._wait[function_name]--;
return function () {};
}
// ok if undef or 0
arglist = arglist || [];
return obj[function_name].apply(obj[function_name],arglist);
};
async.neverCall = function (obj,function_name) {
obj._wait = obj._wait || {};
obj._wait[function_name] = -1;
};
async.wait = function (obj,function_name,times) {
obj._wait = obj._wait || {};
obj._wait[function_name] = times;
};
async.end = function () {
async.call = function(){};
};
return async;
};
/** /**
* Saves a document in the distant dav storage. * Saves a document in the distant dav storage.
* @method saveDocument * @method saveDocument
...@@ -49,12 +75,12 @@ var newDAVStorage = function ( spec, my ) { ...@@ -49,12 +75,12 @@ var newDAVStorage = function ( spec, my ) {
priv.username+':'+priv.password)}, priv.username+':'+priv.password)},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function () { success: function () {
that.done(); that.success();
}, },
error: function (type) { error: function (type) {
type.message = 'Cannot save "' + command.getPath() + type.message = 'Cannot save "' + command.getPath() +
'" into DAVStorage.'; '" into DAVStorage.';
that.fail(type); that.retry(type);
} }
} ); } );
//// end saving on dav //// end saving on dav
...@@ -80,19 +106,20 @@ var newDAVStorage = function ( spec, my ) { ...@@ -80,19 +106,20 @@ var newDAVStorage = function ( spec, my ) {
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) { success: function (content) {
doc.content = content; doc.content = content;
that.done(doc); that.success(doc);
}, },
error: function (type) { error: function (type) {
if (type.status === 404) { if (type.status === 404) {
type.message = 'Document "' + type.message = 'Document "' +
command.getPath() + command.getPath() +
'" not found in localStorage.'; '" not found in localStorage.';
that.error(type);
} else { } else {
type.message = type.message =
'Cannot load "' + command.getPath() + 'Cannot load "' + command.getPath() +
'" from DAVStorage.'; '" from DAVStorage.';
that.retry(type);
} }
that.fail(type);
} }
} ); } );
}; };
...@@ -124,13 +151,17 @@ var newDAVStorage = function ( spec, my ) { ...@@ -124,13 +151,17 @@ var newDAVStorage = function ( spec, my ) {
if (!command.getOption('metadata_only')) { if (!command.getOption('metadata_only')) {
getContent(); getContent();
} else { } else {
that.done(doc); that.success(doc);
} }
}, },
error: function (type) { error: function (type) {
type.message = 'Cannot load "' + command.getPath() + type.message = 'Cannot load "' + command.getPath() +
'" informations from DAVStorage.'; '" informations from DAVStorage.';
that.fail(type); if (type.status === 404) {
that.error(type);
} else {
that.retry(type);
}
} }
} ); } );
}; };
...@@ -140,21 +171,10 @@ var newDAVStorage = function ( spec, my ) { ...@@ -140,21 +171,10 @@ var newDAVStorage = function ( spec, my ) {
* @method getDocumentList * @method getDocumentList
*/ */
that.getDocumentList = function (command) { that.getDocumentList = function (command) {
var document_array = [], file = {}, path_array = []; var document_array = [], file = {}, path_array = [],
am = priv.newAsyncModule(), o = {};
$.ajax ( { o.getContent = function (file) {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/',
async: true,
type: 'PROPFIND',
dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode(
priv.username + ':' + priv.password ), Depth: '1'},
success: function (xmlData) {
var wait_for_me = 0;
var getContent = function (file) {
wait_for_me ++;
$.ajax ( { $.ajax ( {
url: priv.url + '/dav/' + url: priv.url + '/dav/' +
priv.username + '/' + priv.username + '/' +
...@@ -171,19 +191,32 @@ var newDAVStorage = function ( spec, my ) { ...@@ -171,19 +191,32 @@ var newDAVStorage = function ( spec, my ) {
// WARNING : files can be disordered because // WARNING : files can be disordered because
// of asynchronous action // of asynchronous action
document_array.push (file); document_array.push (file);
wait_for_me --; am.call(o,'success');
}, },
error: function (type) { error: function (type) {
type.message = 'Cannot get a document '+ type.message = 'Cannot get a document '+
'content from DAVStorage.'; 'content from DAVStorage.';
that.fail(type); am.call(o,'error',[type]);
wait_for_me --;
} }
}); });
}; };
$(xmlData).find( o.getDocumentList = function () {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/',
async: true,
type: 'PROPFIND',
dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode(
priv.username + ':' + priv.password ), Depth: '1'},
success: function (xmlData) {
var response = $(xmlData).find(
'D\\:response, response' 'D\\:response, response'
).each( function(i,data){ );
var len = response.length;
am.wait(o,'success',len-2);
response.each( function(i,data){
if(i>0) { // exclude parent folder if(i>0) { // exclude parent folder
file = {}; file = {};
$(data).find('D\\:href, href').each(function(){ $(data).find('D\\:href, href').each(function(){
...@@ -206,31 +239,45 @@ var newDAVStorage = function ( spec, my ) { ...@@ -206,31 +239,45 @@ var newDAVStorage = function ( spec, my ) {
file.creation_date = $(this).text(); file.creation_date = $(this).text();
}); });
if (!command.getOption ('metadata_only')) { if (!command.getOption ('metadata_only')) {
getContent(file); am.call(o,'getContent',[file]);
} else { } else {
document_array.push (file); document_array.push (file);
am.call(o,'success');
} }
} }
}); });
// wait until all getContent are ended, only if needed
var tmpfun = function () {
setTimeout(function() {
if (wait_for_me) {
tmpfun();
} else {
that.done(document_array);
}
},200);
};
tmpfun();
}, },
error: function (type) { error: function (type) {
type.message = type.message =
'Cannot get a document list from DAVStorage.'; 'Cannot get a document list from DAVStorage.';
that.fail(type); if (type.status === 404) {
am.call(o,'error',[type]);
} else {
am.call(o,'retry',[type]);
}
} }
} ); } );
}; };
o.retry = function (error) {
am.neverCall(o,'retry');
am.neverCall(o,'success');
am.neverCall(o,'error');
that.retry(error);
};
o.error = function (error) {
am.neverCall(o,'retry');
am.neverCall(o,'success');
am.neverCall(o,'error');
that.error(error);
};
o.success = function () {
am.neverCall(o,'retry');
am.neverCall(o,'success');
am.neverCall(o,'error');
that.success(document_array);
};
am.call (o,'getDocumentList');
};
/** /**
* Removes a document from a distant dav storage. * Removes a document from a distant dav storage.
...@@ -249,15 +296,15 @@ var newDAVStorage = function ( spec, my ) { ...@@ -249,15 +296,15 @@ var newDAVStorage = function ( spec, my ) {
priv.username + ':' + priv.password )}, priv.username + ':' + priv.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function () { success: function () {
that.done(); that.success();
}, },
error: function (type) { error: function (type) {
if (type.status === 404) { if (type.status === 404) {
that.done(); that.success();
} else { } else {
type.message = 'Cannot remove "' + that.getFileName() + type.message = 'Cannot remove "' + that.getFileName() +
'" from DAVStorage.'; '" from DAVStorage.';
that.fail(type); that.retry(type);
} }
} }
} ); } );
......
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