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