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 ) {
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.
* @method saveDocument
......@@ -49,12 +75,12 @@ var newDAVStorage = function ( spec, my ) {
priv.username+':'+priv.password)},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
that.done();
that.success();
},
error: function (type) {
type.message = 'Cannot save "' + command.getPath() +
'" into DAVStorage.';
that.fail(type);
that.retry(type);
}
} );
//// end saving on dav
......@@ -80,19 +106,20 @@ var newDAVStorage = function ( spec, my ) {
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) {
doc.content = content;
that.done(doc);
that.success(doc);
},
error: function (type) {
if (type.status === 404) {
type.message = 'Document "' +
command.getPath() +
'" not found in localStorage.';
that.error(type);
} else {
type.message =
'Cannot load "' + command.getPath() +
'" from DAVStorage.';
that.retry(type);
}
that.fail(type);
}
} );
};
......@@ -124,13 +151,17 @@ var newDAVStorage = function ( spec, my ) {
if (!command.getOption('metadata_only')) {
getContent();
} else {
that.done(doc);
that.success(doc);
}
},
error: function (type) {
type.message = 'Cannot load "' + command.getPath() +
'" 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 ) {
* @method getDocumentList
*/
that.getDocumentList = function (command) {
var document_array = [], file = {}, path_array = [];
var document_array = [], file = {}, path_array = [],
am = priv.newAsyncModule(), o = {};
$.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 wait_for_me = 0;
var getContent = function (file) {
wait_for_me ++;
o.getContent = function (file) {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
......@@ -171,19 +191,32 @@ var newDAVStorage = function ( spec, my ) {
// WARNING : files can be disordered because
// of asynchronous action
document_array.push (file);
wait_for_me --;
am.call(o,'success');
},
error: function (type) {
type.message = 'Cannot get a document '+
'content from DAVStorage.';
that.fail(type);
wait_for_me --;
am.call(o,'error',[type]);
}
});
};
$(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'
).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
file = {};
$(data).find('D\\:href, href').each(function(){
......@@ -206,31 +239,45 @@ var newDAVStorage = function ( spec, my ) {
file.creation_date = $(this).text();
});
if (!command.getOption ('metadata_only')) {
getContent(file);
am.call(o,'getContent',[file]);
} else {
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) {
type.message =
'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.
......@@ -249,15 +296,15 @@ var newDAVStorage = function ( spec, my ) {
priv.username + ':' + priv.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
that.done();
that.success();
},
error: function (type) {
if (type.status === 404) {
that.done();
that.success();
} else {
type.message = 'Cannot remove "' + that.getFileName() +
'" 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