Commit 6e6d67b7 authored by Sven Franck's avatar Sven Franck

jslint pass davstorage.js

parent 0f3f9fa1
jIO.addStorageType('dav', function ( spec, my ) { /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, $: true, Base64: true */
jIO.addStorageType('dav', function (spec, my) {
spec = spec || {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {}; var that = my.basicStorage(spec, my), priv = {},
super_serialized = that.serialized;
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), i; var split = string.split('/'),
i;
if (split[0] === '') { if (split[0] === '') {
split = split.slice(1); split = split.slice(1);
} }
for (i = 0; i < split.length; i+= 1) { for (i = 0; i < split.length; i += 1) {
if (split[i] === '') { return ''; } if (split[i] === '') {
return '';
}
} }
return split.join('%2F'); return split.join('%2F');
}; };
...@@ -28,8 +34,7 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -28,8 +34,7 @@ jIO.addStorageType('dav', function ( spec, my ) {
priv.url = spec.url || ''; priv.url = spec.url || '';
priv.password = spec.password || ''; // TODO : is it secured ? priv.password = spec.password || ''; // TODO : is it secured ?
var super_serialized = that.serialized; that.serialized = function () {
that.serialized = function() {
var o = super_serialized(); var o = super_serialized();
o.username = priv.username; o.username = priv.username;
o.application_name = priv.application_name; o.application_name = priv.application_name;
...@@ -43,7 +48,7 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -43,7 +48,7 @@ jIO.addStorageType('dav', function ( spec, my ) {
* @method validateState * @method validateState
* @return {string} '' -> ok, 'message' -> error * @return {string} '' -> ok, 'message' -> error
*/ */
that.validateState = function() { that.validateState = function () {
if (priv.secured_username && priv.url) { if (priv.secured_username && priv.url) {
return ''; return '';
} }
...@@ -52,48 +57,52 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -52,48 +57,52 @@ jIO.addStorageType('dav', function ( spec, my ) {
priv.newAsyncModule = function () { priv.newAsyncModule = function () {
var async = {}; var async = {};
async.call = function (obj,function_name,arglist) { async.call = function (obj, function_name, arglist) {
obj._wait = obj._wait || {}; obj._wait = obj._wait || {};
if (obj._wait[function_name]) { if (obj._wait[function_name]) {
obj._wait[function_name]--; obj._wait[function_name] -= 1;
return function () {}; return function () {};
} }
// ok if undef or 0 // ok if undef or 0
arglist = arglist || []; arglist = arglist || [];
return obj[function_name].apply(obj[function_name],arglist); return obj[function_name].apply(obj[function_name], arglist);
}; };
async.neverCall = function (obj,function_name) { async.neverCall = function (obj, function_name) {
obj._wait = obj._wait || {}; obj._wait = obj._wait || {};
obj._wait[function_name] = -1; obj._wait[function_name] = -1;
}; };
async.wait = function (obj,function_name,times) { async.wait = function (obj, function_name, times) {
obj._wait = obj._wait || {}; obj._wait = obj._wait || {};
obj._wait[function_name] = times; obj._wait[function_name] = times;
}; };
async.end = function () { async.end = function () {
async.call = function(){}; async.call = function () {};
}; };
return async; return async;
}; };
priv.putOrPost = function (command,type) { priv.putOrPost = function (command, type) {
var secured_docid = priv.secureDocId(command.getDocId()); var secured_docid = priv.secureDocId(command.getDocId());
$.ajax ( { $.ajax({
url: priv.url + '/' + url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_username + '/' + priv.secured_application_name + '/' + secured_docid + '?_=' +
priv.secured_application_name + '/' + Date.now(),
secured_docid + '?_=' + Date.now(), // to make url unique! // to make url unique and avoid chrome PUT on cache !
// and avoid chrome PUT on cache !
type: type, type: type,
data: command.getDocContent(), data: command.getDocContent(),
async: true, async: true,
dataType: 'text', // TODO is it necessary ? dataType: 'text', // TODO is it necessary ?
headers: {'Authorization':'Basic '+Base64.encode( headers: {
priv.username+':'+priv.password)}, 'Authorization': 'Basic ' + Base64.encode(priv.username + ':' +
priv.password)
},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function () { success: function () {
that.success({ok:true,id:command.getDocId()}); that.success({
ok: true,
id: command.getDocId()
});
}, },
error: function (type) { error: function (type) {
// TODO : make statusText to lower case and add '_' // TODO : make statusText to lower case and add '_'
...@@ -102,11 +111,11 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -102,11 +111,11 @@ jIO.addStorageType('dav', function ( spec, my ) {
type.message = type.reason + '.'; type.message = type.reason + '.';
that.retry(type); that.retry(type);
} }
} ); });
}; };
that.post = function (command) { that.post = function (command) {
priv.putOrPost (command,'POST'); priv.putOrPost(command, 'POST');
}; };
/** /**
...@@ -114,7 +123,7 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -114,7 +123,7 @@ jIO.addStorageType('dav', function ( spec, my ) {
* @method put * @method put
*/ */
that.put = function (command) { that.put = function (command) {
priv.putOrPost (command,'PUT'); priv.putOrPost(command, 'PUT');
}; // end put }; // end put
/** /**
...@@ -123,17 +132,19 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -123,17 +132,19 @@ jIO.addStorageType('dav', function ( spec, my ) {
*/ */
that.get = function (command) { that.get = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()), var secured_docid = priv.secureDocId(command.getDocId()),
doc = {}, getContent = function () { doc = {},
$.ajax ( { getContent = function () {
url: priv.url + '/' + $.ajax({
priv.secured_username + '/' + url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_application_name + '/' + priv.secured_application_name + '/' + secured_docid + '?_=' +
secured_docid + '?_=' + Date.now(), Date.now(),
type: "GET", type: "GET",
async: true, async: true,
dataType: 'text', // TODO is it necessary ? dataType: 'text', // TODO is it necessary ?
headers: {'Authorization':'Basic '+Base64.encode( headers: {
priv.username + ':' + priv.password )}, 'Authorization': 'Basic ' + Base64.encode(priv.username + ':' +
priv.password)
},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) { success: function (content) {
doc.content = content; doc.content = content;
...@@ -142,8 +153,7 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -142,8 +153,7 @@ jIO.addStorageType('dav', function ( spec, my ) {
error: function (type) { error: function (type) {
type.error = type.statusText; // TODO : to lower case type.error = type.statusText; // TODO : to lower case
if (type.status === 404) { if (type.status === 404) {
type.message = 'Document "' + type.message = 'Document "' + command.getDocId() +
command.getDocId() +
'" not found.'; '" not found.';
type.reason = 'missing'; type.reason = 'missing';
that.error(type); that.error(type);
...@@ -155,34 +165,33 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -155,34 +165,33 @@ jIO.addStorageType('dav', function ( spec, my ) {
that.retry(type); that.retry(type);
} }
} }
} ); });
}; };
doc._id = command.getDocId(); doc._id = command.getDocId();
// NOTE : if (command.getOption('content_only') { return getContent(); } // NOTE : if (command.getOption('content_only') { return getContent(); }
// Get properties // Get properties
$.ajax ( { $.ajax({
url: priv.url + '/' + url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_username + '/' +
priv.secured_application_name + '/' + priv.secured_application_name + '/' +
secured_docid + '?_=' + Date.now(), secured_docid + '?_=' + Date.now(),
type: "PROPFIND", type: "PROPFIND",
async: true, async: true,
dataType: 'xml', dataType: 'xml',
headers: {'Authorization':'Basic '+Base64.encode( headers: {
priv.username + ':' + priv.password )}, 'Authorization': 'Basic ' + Base64.encode(priv.username + ':' +
priv.password)
},
success: function (xmlData) { success: function (xmlData) {
$(xmlData).find( $(xmlData).find('lp1\\:getlastmodified, getlastmodified').each(
'lp1\\:getlastmodified, getlastmodified' function () {
).each( function () { doc._last_modified = new Date($(this).text()).getTime();
doc._last_modified = }
new Date($(this).text()).getTime(); );
}); $(xmlData).find('lp1\\:creationdate, creationdate').each(
$(xmlData).find( function () {
'lp1\\:creationdate, creationdate' doc._creation_date = new Date($(this).text()).getTime();
).each( function () { }
doc._creation_date = );
new Date($(this).text()).getTime();
});
if (!command.getOption('metadata_only')) { if (!command.getOption('metadata_only')) {
getContent(); getContent();
} else { } else {
...@@ -202,7 +211,7 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -202,7 +211,7 @@ jIO.addStorageType('dav', function ( spec, my ) {
that.retry(type); that.retry(type);
} }
} }
} ); });
}; };
/** /**
...@@ -211,85 +220,91 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -211,85 +220,91 @@ jIO.addStorageType('dav', function ( spec, my ) {
*/ */
that.allDocs = function (command) { that.allDocs = function (command) {
var rows = [], var rows = [],
am = priv.newAsyncModule(), o = {}; am = priv.newAsyncModule(),
o = {};
o.getContent = function (file) { o.getContent = function (file) {
$.ajax ( { $.ajax({
url: priv.url + '/' + url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_username + '/' + priv.secured_application_name + '/' + priv.secureDocId(file.id) +
priv.secured_application_name + '/' + '?_=' + Date.now(),
priv.secureDocId(file.id) + '?_=' + Date.now(),
type: "GET", type: "GET",
async: true, async: true,
dataType: 'text', // TODO : is it necessary ? dataType: 'text', // TODO : is it necessary ?
headers: {'Authorization':'Basic '+ headers: {
Base64.encode(priv.username +':'+ 'Authorization': 'Basic ' + Base64.encode(priv.username + ':' +
priv.password)}, priv.password)
},
success: function (content) { success: function (content) {
file.value.content = content; file.value.content = content;
// WARNING : files can be disordered because // WARNING : files can be disordered because
// of asynchronous action // of asynchronous action
rows.push (file); rows.push(file);
am.call(o,'success'); am.call(o, 'success');
}, },
error: function (type) { error: function (type) {
type.error = type.statusText; // TODO : to lower case type.error = type.statusText; // TODO : to lower case
type.reason = 'Cannot get a document '+ type.reason = 'Cannot get a document ' +
'content from DAVStorage'; 'content from DAVStorage';
type.message = type.message + '.'; type.message = type.message + '.';
am.call(o,'error',[type]); am.call(o, 'error', [type]);
} }
}); });
}; };
o.getDocumentList = function () { o.getDocumentList = function () {
$.ajax ( { $.ajax({
url: priv.url + '/' + url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_username + '/' +
priv.secured_application_name + '/' + '?_=' + Date.now(), priv.secured_application_name + '/' + '?_=' + Date.now(),
async: true, async: true,
type: 'PROPFIND', type: 'PROPFIND',
dataType: 'xml', dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode( headers: {
priv.username + ':' + priv.password ), Depth: '1'}, 'Authorization': 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
),
Depth: '1'
},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function (xmlData) { success: function (xmlData) {
var response = $(xmlData).find( var response = $(xmlData).find('D\\:response, response'),
'D\\:response, response' len = response.length;
);
var len = response.length;
if (len === 1) { if (len === 1) {
return am.call(o,'success'); return am.call(o, 'success');
} else {
am.wait(o,'success',len-2);
} }
response.each( function(i,data){ am.wait(o, 'success', len - 2);
if(i>0) { // exclude parent folder response.each(function (i, data) {
var file = {value:{}}; if (i > 0) { // exclude parent folder
$(data).find('D\\:href, href').each(function(){ var file = {
value: {}
};
$(data).find('D\\:href, href').each(function () {
var split = $(this).text().split('/'); var split = $(this).text().split('/');
file.id = split[split.length-1]; file.id = split[split.length - 1];
file.id = priv.restoreSlashes(file.id); file.id = priv.restoreSlashes(file.id);
file.key = file.id; file.key = file.id;
}); });
if (file.id === '.htaccess' || if (file.id === '.htaccess' || file.id === '.htpasswd') {
file.id === '.htpasswd') { return; } return;
$(data).find( }
'lp1\\:getlastmodified, getlastmodified' $(data).find('lp1\\:getlastmodified, getlastmodified').each(
).each(function () { function () {
file.value._last_modified = file.value._last_modified = new Date(
new Date($(this).text()).getTime(); $(this).text()
}); ).getTime();
$(data).find( }
'lp1\\:creationdate, creationdate' );
).each(function () { $(data).find('lp1\\:creationdate, creationdate').each(
file.value._creation_date = function () {
new Date($(this).text()).getTime(); file.value._creation_date = new Date(
}); $(this).text()
if (!command.getOption ('metadata_only')) { ).getTime();
am.call(o,'getContent',[file]); }
);
if (!command.getOption('metadata_only')) {
am.call(o, 'getContent', [file]);
} else { } else {
rows.push (file); rows.push(file);
am.call(o,'success'); am.call(o, 'success');
} }
} }
}); });
...@@ -298,37 +313,39 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -298,37 +313,39 @@ jIO.addStorageType('dav', function ( spec, my ) {
if (type.status === 404) { if (type.status === 404) {
type.error = 'not_found'; type.error = 'not_found';
type.reason = 'missing'; type.reason = 'missing';
am.call(o,'error',[type]); am.call(o, 'error', [type]);
} else { } else {
type.error = type.statusText; // TODO : to lower case type.error = type.statusText; // TODO : to lower case
type.reason = type.reason =
'Cannot get a document list from DAVStorage'; 'Cannot get a document list from DAVStorage';
type.message = type.reason + '.'; type.message = type.reason + '.';
am.call(o,'retry',[type]); am.call(o, 'retry', [type]);
} }
} }
} ); });
}; };
o.retry = function (error) { o.retry = function (error) {
am.neverCall(o,'retry'); am.neverCall(o, 'retry');
am.neverCall(o,'success'); am.neverCall(o, 'success');
am.neverCall(o,'error'); am.neverCall(o, 'error');
that.retry(error); that.retry(error);
}; };
o.error = function (error) { o.error = function (error) {
am.neverCall(o,'retry'); am.neverCall(o, 'retry');
am.neverCall(o,'success'); am.neverCall(o, 'success');
am.neverCall(o,'error'); am.neverCall(o, 'error');
that.error(error); that.error(error);
}; };
o.success = function () { o.success = function () {
am.neverCall(o,'retry'); am.neverCall(o, 'retry');
am.neverCall(o,'success'); am.neverCall(o, 'success');
am.neverCall(o,'error'); am.neverCall(o, 'error');
that.success({total_rows:rows.length, that.success({
rows:rows}); total_rows: rows.length,
rows: rows
});
}; };
am.call (o,'getDocumentList'); am.call(o, 'getDocumentList');
}; // end allDocs }; // end allDocs
/** /**
...@@ -338,20 +355,26 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -338,20 +355,26 @@ jIO.addStorageType('dav', function ( spec, my ) {
that.remove = function (command) { that.remove = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()); var secured_docid = priv.secureDocId(command.getDocId());
$.ajax ( { $.ajax({
url: priv.url + '/' + url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_username + '/' + priv.secured_application_name + '/' + secured_docid + '?_=' +
priv.secured_application_name + '/' + Date.now(),
secured_docid + '?_=' + Date.now(),
type: "DELETE", type: "DELETE",
async: true, async: true,
headers: {'Authorization':'Basic '+Base64.encode( headers: {
priv.username + ':' + priv.password )}, 'Authorization': 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function (data,state,type) { // jslint: removed params data, state, type
that.success({ok:true,id:command.getDocId()}); success: function () {
that.success({
ok: true,
id: command.getDocId()
});
}, },
error: function (type,state,statusText) { error: function (type) {
if (type.status === 404) { if (type.status === 404) {
//that.success({ok:true,id:command.getDocId()}); //that.success({ok:true,id:command.getDocId()});
type.error = 'not_found'; type.error = 'not_found';
...@@ -364,8 +387,8 @@ jIO.addStorageType('dav', function ( spec, my ) { ...@@ -364,8 +387,8 @@ jIO.addStorageType('dav', function ( spec, my ) {
that.retry(type); that.retry(type);
} }
} }
} ); });
}; };
return that; return that;
}; });
\ No newline at end of file
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