Commit b2654ba5 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

QUnit&Sinon tests are improved!

Developping DavStorage.
parent 6cb31064
......@@ -9,6 +9,7 @@
<div id="qunit"></div>
<script type="text/javascript" src="qunit/qunit-1.5.0.js"></script>
<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="unhosted/localorcookiestorage.js"></script>
<script type="text/javascript" src="unhosted/jio.js"></script>
<script type="text/javascript" src="unhosted/base64.js"></script>
<script type="text/javascript" src="unhosted/jio.storage.js"></script>
......
var cookieOrLocal = null;
var browserStorage = function () {
};
browserStorage.prototype = {
getItem: function (name) {
return JSON.parse(localStorage.getItem(name));
},
setItem: function (name,value) {
if (name)
return localStorage.setItem(name,JSON.stringify(value));
},
getAll: function() {
return localStorage;
},
deleteItem: function (name) {
if (name)
delete localStorage[name];
}
};
var cookieStorage = function () {
};
cookieStorage.prototype = {
getItem: function (name) {
var cookies = document.cookie.split(';');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
if( x == name ) return unescape(y);
}
return null;
},
setItem: function (name,value) {
// function to store into cookies
if (value != undefined) {
document.cookie = name+'='+JSON.stringify(value)+';domain='+
window.location.hostname+
';path='+window.location.pathname;
return true;
}
return false;
},
getAll: function() {
var retObject = {};
var cookies = document.cookie.split(':');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
retObject[x] = unescape(y);
}
return retObject;
},
deleteItem: function (name) {
document.cookie = name+'=null;domain='+window.location.hostname+
';path='+window.location.pathname+
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
};
// set good localStorage
try {
if (localStorage.getItem) {
cookieOrLocal = new browserStorage();
} else {
cookieOrLocal = new cookieStorage();
}
}
catch (e) {
cookieOrLocal = new cookieStorage();
}
//// clear jio localstorage
for (var k in cookieOrLocal.getAll()) {
for (var k in LocalOrCookieStorage.getAll()) {
var splitk = k.split('/');
if ( splitk[0] === 'jio' )
cookieOrLocal.deleteItem(k);
LocalOrCookieStorage.deleteItem(k);
}
//// end clear jio localstorage
//// QUnit Tests ////
module ('Jio Global tests');
......@@ -151,11 +78,12 @@ test ('Check name availability', function () {
var mytest = function (value){
o.f = function (result) {
deepEqual(result.isAvailable,value,'checking name availabality');};
var spy = t.spy(o,'f');
t.spy(o,'f');
o.jio.checkNameAvailability(
{'userName':'MrCheckName','callback': o.f});
clock.tick(510);
ok (o.f.calledOnce, 'callback called once');
if (!o.f.calledOnce)
ok(false, 'no response / too much results');
};
// new jio
......@@ -163,11 +91,11 @@ test ('Check name availability', function () {
{"ID":'noid'});
// name must be available
cookieOrLocal.deleteItem ('jio/local/MrCheckName/jiotests/file');
LocalOrCookieStorage.deleteItem ('jio/local/MrCheckName/jiotests/file');
mytest(true);
// name must be unavailable
cookieOrLocal.setItem ('jio/local/MrCheckName/jiotests/file',{});
LocalOrCookieStorage.setItem ('jio/local/MrCheckName/jiotests/file',{});
mytest(false);
o.jio.stop();
......@@ -182,20 +110,23 @@ test ('Document save', function () {
var mytest = function (value,lm,cd){
o.f = function (result) {
deepEqual(result.isSaved,value,'saving document');};
var spy = t.spy(o,'f');
t.spy(o,'f');
o.jio.saveDocument(
{'fileName':'file','fileContent':'content','callback': o.f});
clock.tick(510);
ok (o.f.calledOnce, 'callback called once');
// check content
var tmp = cookieOrLocal.getItem ('jio/local/MrSaveName/jiotests/file');
deepEqual (tmp,{'fileName':'file','fileContent':'content',
'lastModified':lm,'creationDate':cd},'check content');
if (!o.f.calledOnce)
ok(false, 'no response / too much results');
else {
// check content
var tmp = LocalOrCookieStorage.getItem ('jio/local/MrSaveName/jiotests/file');
deepEqual (tmp,{'fileName':'file','fileContent':'content',
'lastModified':lm,'creationDate':cd},'check content');
}
};
o.jio = JIO.createNew({'type':'local','userName':'MrSaveName'},
{"ID":'jiotests'});
cookieOrLocal.deleteItem ('jio/local/MrSaveName/jiotests/file');
LocalOrCookieStorage.deleteItem ('jio/local/MrSaveName/jiotests/file');
// save and check document existence
clock.tick(200);
mytest(true,200,200); // value, lastmodified, creationdate
......@@ -215,22 +146,23 @@ test ('Document load', function () {
var mytest = function (res,value){
o.f = function (result) {
deepEqual(result[res],value,'loading document');};
var spy = t.spy(o,'f');
t.spy(o,'f');
o.jio.loadDocument(
{'fileName':'file','callback': o.f});
clock.tick(510);
ok (o.f.calledOnce, 'callback called once');
if (!o.f.calledOnce)
ok(false, 'no response / too much results');
};
o.jio = JIO.createNew({'type':'local','userName':'MrLoadName'},
{"ID":'jiotests'});
// load a non existing file
cookieOrLocal.deleteItem ('jio/local/MrLoadName/jiotests/file');
LocalOrCookieStorage.deleteItem ('jio/local/MrLoadName/jiotests/file');
mytest ('status','fail');
// re-load file after saving it manually
var doc = {'fileName':'file','fileContent':'content',
'lastModified':1234,'creationDate':1000};
cookieOrLocal.setItem ('jio/local/MrLoadName/jiotests/file',doc);
LocalOrCookieStorage.setItem ('jio/local/MrLoadName/jiotests/file',doc);
mytest ('document',doc);
o.jio.stop();
......@@ -245,21 +177,123 @@ test ('Document remove', function () {
var mytest = function (){
o.f = function (result) {
deepEqual(result.isRemoved,true,'removing document');};
var spy = t.spy(o,'f');
t.spy(o,'f');
o.jio.removeDocument(
{'fileName':'file','callback': o.f});
clock.tick(510);
ok (o.f.calledOnce, 'callback called once');
// check if the file is still there
var tmp = cookieOrLocal.getItem ('jio/local/MrRemoveName/jiotests/file');
ok (!tmp, 'check no content');
if (!o.f.calledOnce)
ok(false, 'no response / too much results');
else {
// check if the file is still there
var tmp = LocalOrCookieStorage.getItem ('jio/local/MrRemoveName/jiotests/file');
ok (!tmp, 'check no content');
}
};
o.jio = JIO.createNew({'type':'local','userName':'MrRemoveName'},
{"ID":'jiotests'});
// test removing a file
cookieOrLocal.setItem ('jio/local/MrRemoveName/jiotests/file',{});
LocalOrCookieStorage.setItem ('jio/local/MrRemoveName/jiotests/file',{});
mytest ();
o.jio.stop();
});
module ('Jio DAVStorage');
test ('Check name availability', function () {
// Test if DavStorage can check the availabality of a name.
var o = {}; var clock = this.sandbox.useFakeTimers(); var t = this;
var mytest = function (value,errno) {
var server = t.sandbox.useFakeServer();
server.respondWith ("PROPFIND",
"https://ca-davstorage:8080/dav/davcheck/",
[errno, {'Content-Type': 'text/xml' },
'']);
o.f = function (result) {
deepEqual (result.isAvailable,value,'checking name availability');};
t.spy(o,'f');
o.jio.checkNameAvailability({'userName':'davcheck','callback':o.f});
clock.tick(500);
server.respond();
if (!o.f.calledOnce)
ok(false, 'no response / too much results');
};
o.jio = JIO.createNew({'type':'dav','userName':'davcheck',
'password':'checkpwd',
'location':'https://ca-davstorage:8080'},
{'ID':'jiotests'});
// 404 error, the name does not exist, name is available.
mytest (true,404);
// 200 error, responding ok, the name already exists, name is not available.
mytest (false,200);
// 405 error, random error, name is not available by default.
mytest (false,405);
o.jio.stop();
});
test ('Document save', function () {
// Test if DavStorage can save documents.
var o = {}; var clock = this.sandbox.useFakeTimers(); var t = this;
var mytest = function (message,value,errnoput,errnoget,
lastmodified,overwrite,force) {
var server = t.sandbox.useFakeServer();
server.respondWith ("PUT",
"https://ca-davstorage:8080/dav/davsave/jiotests/file",
[errnoput, {'Content-Type':'x-www-form-urlencoded'},
'content']);
server.respondWith ("MKCOL",
"https://ca-davstorage:8080/dav/davsave/jiotests/file",
[200, {'Content-Type':'x-www-form-urlencoded'},
'content']);
server.respondWith ("GET",
"https://ca-davstorage:8080/dav/davsave/jiotests/file",
[errnoget, {}, 'content']);
o.f = function (result) {
deepEqual (result.isSaved,value,message);};
t.spy(o,'f');
o.jio.saveDocument({'fileName':'file','fileContent':'content',
'options':{'force':force,'overwrite':overwrite},
'lastModified':lastmodified,
'callback':o.f});
clock.tick(500);
server.respond();
if (!o.f.calledOnce)
ok(false, 'no response / too much results');
};
o.jio = JIO.createNew({'type':'dav','userName':'davsave',
'password':'checkpwd',
'location':'https://ca-davstorage:8080'},
{'ID':'jiotests'});
// note: http errno:
// 200 OK
// 201 Created
// 204 No Content
// 403 Forbidden
// 404 Not Found
// the path does not exist, we want to create it, and save the file.
mytest('create path if not exists, and create document',
true,403,404,999);
// the document does not exist, we want to create it
mytest('create document',
true,201,404,10000);
// the document already exists, we want to overwrite it
mytest('overwrite document',
true,204,200,10100,true);
// the document already exists, we don't want to overwrite it
mytest('do not overwrite document',
false,204,200,10200,false);
// the document is already exists, it is younger than the one we want
// to save.
mytest('younger than the one we want to save',
false,204,200,900,true,false);
// the document is already exists, it is the youngest but we want to
// force overwriting
mytest('youngest but force overwrite',
true,204,200,700,true,true);
o.jio.stop();
});
......@@ -4,7 +4,7 @@
// check dependencies
var errorDependencies=function(){console.error('Cannot find jQuery.');};
try{if(!jQuery){
try{if(!(jQuery && LocalOrCookieStorage)){
errorDependencies();return null;
}}catch(e){
errorDependencies();return null;}
......@@ -25,10 +25,6 @@
'start_event':'start_loading',
'stop_event':'stop_loading',
'retvalue':'fileContent' }, // returns the file content 'string'
'getDocument': {
'start_event':'start_gettingDocument',
'stop_event':'stop_gettingDocument',
'retvalue':'document' }, // returns the document object
'getDocumentList': {
'start_event':'start_gettingList',
'stop_event':'stop_gettingList',
......@@ -43,87 +39,13 @@
////////////////////////////////////////////////////////////////////////////
// jio globals
var jioGlobalObj = {
'localStorage': null, // where the browser stores data
'localStorage': LocalOrCookieStorage, // where the browser stores data
'queueID': 1,
'storageTypeObject': {} // ex: {'type':'local','creator': fun ...}
};
// end jio globals
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// cookies & localStorage
var browserStorage = function () {
};
browserStorage.prototype = {
getItem: function (name) {
return JSON.parse(localStorage.getItem(name));
},
setItem: function (name,value) {
if (name)
return localStorage.setItem(name,JSON.stringify(value));
},
getAll: function() {
return localStorage;
},
deleteItem: function (name) {
if (name)
delete localStorage[name];
}
};
var cookieStorage = function () {
};
cookieStorage.prototype = {
getItem: function (name) {
var cookies = document.cookie.split(';');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
if( x == name ) return unescape(y);
}
return null;
},
setItem: function (name,value) {
// function to store into cookies
if (value != undefined) {
document.cookie = name+'='+JSON.stringify(value)+';domain='+
window.location.hostname+
';path='+window.location.pathname;
return true;
}
return false;
},
getAll: function() {
var retObject = {};
var cookies = document.cookie.split(':');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
retObject[x] = unescape(y);
}
return retObject;
},
deleteItem: function (name) {
document.cookie = name+'=null;domain='+window.location.hostname+
';path='+window.location.pathname+
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
};
// set good localStorage
try {
if (localStorage.getItem) {
jioGlobalObj.localStorage = new browserStorage();
} else {
jioGlobalObj.localStorage = new cookieStorage();
}
}
catch (e) {
jioGlobalObj.localStorage = new cookieStorage();
}
// end cookies & localStorages
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Tools
var createStorageObject = function ( options ) {
......@@ -369,6 +291,9 @@
},
ended: function (job) {
// It is a callback function called just before user callback.
// It is called to manage jobObject according to the ended job.
console.log ('ended');
switch (job.status) {
case 'done':
......@@ -461,15 +386,17 @@
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Toucher
var Toucher = function () {
// The toucher is a little thread that show activity of this jio.
// ActivityUpdater
var ActivityUpdater = function () {
// The activity updater is a little thread that proves activity of this
// jio instance.
this.interval = 400;
this.id = null;
};
Toucher.prototype = {
ActivityUpdater.prototype = {
start: function (id) {
// start the toucher
// start the updater
console.log ('start touching jio/id/'+id);
if (!this.id) {
this.touch(id);
......@@ -483,7 +410,7 @@
}
},
stop: function () {
// stop the toucher
// stop the updater
console.log ('stop touching');
if (this.id) {
clearInterval (this.id);
......@@ -497,7 +424,7 @@
Date.now() );
}
};
// end Toucher
// end ActivityUpdater
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
......@@ -522,7 +449,7 @@
this['pubsub'] = new PubSub();
this['queue'] = new JobQueue(this.pubsub);
this['listener'] = new JobListener(this.queue);
this['toucher'] = new Toucher();
this['updater'] = new ActivityUpdater();
this['ready'] = false;
// check storage type
......@@ -544,7 +471,7 @@
// initializing objects
this.queue.init({'jioID':this.id});
// start touching
this.toucher.start(this.id);
this.updater.start(this.id);
// start listening
this.listener.start();
// is now ready
......@@ -557,7 +484,7 @@
this.queue.close();
this.listener.stop();
this.toucher.stop();
this.updater.stop();
this.ready = false;
this.id = 0;
return true;
......@@ -568,7 +495,7 @@
this.queue.close();
this.listener.stop();
this.toucher.stop();
this.updater.stop();
// TODO
this.ready = false;
return true;
......
......@@ -14,12 +14,6 @@
// end globals
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Tools
// end Tools
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Local Storage
var LocalStorage = function ( options ) {
......@@ -284,84 +278,95 @@
// create folders in dav storage, synchronously
// options : contains mkcol list
// options.location : the davstorage locations
// options.path : if path=/foo/bar then creates location/dav/foo/bar
// options.path: if path=/foo/bar then creates location/dav/foo/bar
// options.success: the function called if success
var settings = $.extend ({},options);
if (options.location && options.path) {
var ok = false;
$.ajax ( {
url: options.location + '/dav/' + options.path,
type: 'MKCOL',
async: false,
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
// done
console.log ('ok');
ok = true;
},
error: function (type) {
switch (type.status) {
case 405: // Method Not Allowed
ok = true; // already done
break;
}
console.log ('error?');
}
} );
return ok;
} else {
return false;
}
},
checkNameAvailability: function ( job ) {
var settings = $.extend ({'success':function(){},
'error':function(){}},options);
$.ajax ( {
url: job.storage.location + '/dav/' + job.userName,
type: "HEAD",
url: options.location + '/dav/' + options.path,
type: 'MKCOL',
async: true,
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
job.status = 'done';
job.message = job.userName + ' is not available.';
job.isAvailable = false;
$.jio('publish',{'event':'job_done',
'job':job});
// done
settings.success();
},
error: function (type) {
switch (type.status) {
case 301: // Moved Permanantly
job.status = 'done';
job.message = job.userName + ' is not available.';
job.isAvailable = false;
$.jio('publish',{'event':'job_done',
'job':job});
case 405: // Method Not Allowed
// already exists
settings.success();
break;
case 404: // Not Found
// TODO always returning 404 !! Why ??
console.warn ('always returning 404 ?');
job.status = 'done';
job.message = job.userName + ' is available.';
job.isAvailable = true;
$.jio('publish',{'event':'job_done',
'job':job});
default:
settings.error();
break;
}
}
} );
},
checkNameAvailability: function ( job, jobendcallback ) {
// checks the availability of the [job.userName].
// if the name already exists, it is not available.
// job: the job object.
// job.userName: the name we want to check.
// jobendcallback: the function called at the end of the job.
// returns {'status':string,'message':string,'isAvailable':boolean}
// in the jobendcallback arguments.
$.ajax ( {
url: job.storage.location + '/dav/' + job.storage.userName + '/',
async: true,
type: 'PROPFIND',
dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode(
job.storage.userName + ':' +
job.storage.password ), Depth: '1'},
success: function (xmlData) {
var res = {};
res.status = job.status = 'done';
res.message = job.userName + ' is not available.';
res.isAvailable = false;
jobendcallback(job);
job.callback(res);
},
error: function (type) {
var res = {};
switch(type.status){
case 404:
res.status = job.status = 'done';
res.message = job.userName + ' is available.';
res.isAvailable = true;
break;
default:
job.status = 'fail';
job.message = 'Cannot check if ' + job.userName +
res.status = job.status = 'fail';
res.message = 'Cannot check if ' + job.userName +
' is available.';
job.isAvailable = false;
job.errno = type.status;
// $.error ( type.status ': Fail while trying to check ' +
// job.userName );
$.jio('publish',{'event':'job_fail',
'job':job});
res.isAvailable = false;
break;
}
res.errno = type.status;
jobendcallback(job);
job.callback(res);
}
});
} );
},
saveDocument: function ( job ) {
saveDocument: function ( job, jobendcallback ) {
// Save a document in a DAVStorage
// job: the job object
// job.options: the save options object
// job.options.overwrite: true -> overwrite
// job.options.force: true -> save even if jobdate < existingdate
// or overwrite: false
// jobendcallback: the function called at the end of the job.
// returns {'status':string,'message':string,'isSaved':boolean}
// in the jobendcallback arguments.
var settings = $.extend ({'overwrite':true,
'force':false},job.options);
var res = {};
// TODO if path of ../dav/user/applic does not exists, it won't work !!
var saveOnDav = function () {
//// save on dav
......@@ -378,19 +383,19 @@
job.storage.userName + ':' + job.storage.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
job.status = 'done';
job.message = 'Document saved.';
job.isSaved = true;
$.jio('publish',{'event':'job_done',
'job':job});
res.status = job.status = 'done';
res.message = 'Document saved.';
res.isSaved = true;
jobendcallback(job);
job.callback(res);
},
error: function (type) {
job.status = 'fail';
job.message = 'Cannot save document.';
job.errno = type.status;
job.isSaved = false;
$.jio('publish',{'event':'job_fail',
'job':job});
res.status = job.status = 'fail';
res.message = 'Cannot save document.';
res.errno = type.status;
res.isSaved = false;
jobendcallback(job);
job.callback(res);
}
} );
//// end saving on dav
......@@ -399,6 +404,7 @@
if (settings.force) {
return saveOnDav();
}
var mkcol = this.mkcol;
//// start loading document
$.ajax ( {
url: job.storage.location + '/dav/' +
......@@ -410,43 +416,68 @@
headers: {'Authorization':'Basic '+Base64.encode(
job.storage.userName + ':' + job.storage.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) { // TODO content ?
success: function () {
// TODO merge files
// Document already exists
if (settings.overwrite) { // overwrite
// TODO check date !!!
// response headers contains
// Date: Mon, 30 Apr 2012 15:17:21 GMT
// Last-Modified: Mon, 30 Apr 2012 15:06:59 GMT
return saveOnDav();
}
// do not overwrite
job.status = 'fail';
job.message = 'Document already exists.';
job.errno = 302;
job.isSaved = false;
$.jio('publish',{'event':'job_fail',
'job':job});
return false;
res.status = job.status = 'fail';
res.message = 'Document already exists.';
res.errno = 302;
res.isSaved = false;
jobendcallback(job);
job.callback(res);
},
error: function (type) {
switch (type.status) {
case 404: // Document not found
// we can save on it
return saveOnDav();
mkcol({ // create col if not exist
location:job.storage.location,
path:'/dav/'+job.storage.userName+job.applicant.ID,
success:function(){
// and finaly save document
saveOnDav()
},
error:function(){
res.status = job.status = 'fail';
res.message = 'Cannot create document.';
res.errno = type.status;
res.isSaved = false;
jobendcallback(job);
job.callback(res);
}});
break;
default: // Unknown error
job.status = 'fail';
job.message = 'Unknown error.';
job.errno = type.status;
job.isSaved = false;
$.jio('publish',{'event':'job_fail',
'job':job});
return false;
res.status = job.status = 'fail';
res.message = 'Unknown error.';
res.errno = type.status;
res.isSaved = false;
jobendcallback(job);
job.callback(res);
break;
}
}
} );
//// end loading document
},
loadDocument: function ( job ) {
// load the document into davstorage
loadDocument: function ( job, jobendcallback ) {
// Load a document from a DAVStorage. It returns a document object
// containing all information of the document and its content.
// job: the job object
// job.fileName: the document name we want to load.
// jobendcallback: the function called at the end of the job.
// returns {'status':string,'message':string,'document':object}
// in the jobendcallback arguments.
// document object is {'fileName':string,'fileContent':string,
// 'creationDate':date,'lastModified':date} //TODO!!
// TODO check if job's features are good
$.ajax ( {
......@@ -462,27 +493,38 @@
job.storage.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) {
job.status = 'done';
job.message = 'Document loaded.';
job.fileContent = content;
$.jio('publish',{'event':'job_done',
'job':job});
res.status = job.status = 'done';
res.message = 'Document loaded.';
res.fileContent = content;
jobendcallback(job);
job.callback(res);
},
error: function (type) {
switch (type.status) {
case 404:
job.message = 'Document not found.'; break;
res.message = 'Document not found.'; break;
default:
job.message = 'Cannot load "' + job.fileName + '".'; break;
res.message = 'Cannot load "' + job.fileName + '".'; break;
}
job.status = 'fail';
job.errno = type.status;
$.jio('publish',{'event':'job_fail',
'job':job});
res.status = job.status = 'fail';
res.errno = type.status;
jobendcallback(job);
job.callback(res);
}
} );
},
getDocumentList: function ( job ) {
getDocumentList: function ( job, jobendcallback ) {
// Get a document list from a DAVStorage. It returns a document
// array containing all the user documents informations, but their
// content.
// job: the job object
// jobendcallback: the function called at the end of the job.
// returns {'status':string,'message':string,'list':array}
// in the jobendcallback arguments.
// the list is [object,object] -> object = {'fileName':string,
// 'lastModified':date,'creationDate':date}
$.ajax ( {
url: job.storage.location + '/dav/' +
job.storage.userName + '/' + job.applicant.ID + '/',
......@@ -512,22 +554,29 @@
documentArrayList.push (file);
}
});
job.status = 'done';
job.message = 'List received.';
job.list = documentArrayList;
$.jio('publish',{'event':'job_done',
'job':job});
res.status = job.status = 'done';
res.message = 'List received.';
res.list = documentArrayList;
jobendcallback(job);
job.callback(res);
},
error: function (type) {
job.status = 'fail';
job.message = 'Cannot get list.';
job.errno = type.status;
$.jio('publish',{'event':'job_fail',
'job':job});
res.status = job.status = 'fail';
res.message = 'Cannot get list.';
res.errno = type.status;
jobendcallback(job);
job.callback(res);
}
} );
},
removeDocument: function ( job ) {
removeDocument: function ( job, jobendcallback ) {
// Remove a document from a DAVStorage.
// job: the job object
// jobendcallback: the function called at the end of the job.
// returns {'status':string,'message':string,'isRemoved':boolean}
// in the jobendcallback arguments.
$.ajax ( {
url: job.storage.location + '/dav/' +
job.storage.userName + '/' +
......@@ -540,29 +589,27 @@
job.storage.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
job.status = 'done';
job.message = 'Document removed.';
job.isRemoved = true;
$.jio('publish',{'event':'job_done',
'job':job});
res.status = job.status = 'done';
res.message = 'Document removed.';
res.isRemoved = true;
jobendcallback(job);
job.callback(res);
},
error: function (type) {
switch (type.status) {
case 404:
job.status = 'done';
job.message = 'Document already removed.';
job.errno = type.status;
$.jio('publish',{'event':'job_done',
'job':job});
res.stauts = job.status = 'done';
res.message = 'Document already removed.';
res.errno = type.status;
break;
default:
job.status = 'fail';
job.message = 'Cannot remove "' + job.fileName + '".';
job.errno = type.status;
$.jio('publish',{'event':'job_fail',
'job':job});
res.status = job.status = 'fail';
res.message = 'Cannot remove "' + job.fileName + '".';
res.errno = type.status;
break;
}
jobendcallback(job);
job.callback(res);
}
} );
}
......
;var LocalOrCookieStorage =
(function () {
// localorcookiestorage.js
// Creates an object that can store persistent information in localStorage.
// If it is not supported by the browser, it will store in cookies.
// Methods :
// - LocalOrCookieStorage.setItem('name',value);
// Sets an item with its value.
// - LocalOrCookieStorage.getItem('name');
// Returns a copy of the item.
// - LocalOrCookieStorage.deleteItem('name');
// Deletes an item forever.
// - LocalOrCookieStorage.getAll();
// Returns a new object containing all items and their values.
////////////////////////////////////////////////////////////////////////////
// cookies & localStorage
var browserStorage = function () {
};
browserStorage.prototype = {
getItem: function (name) {
return JSON.parse(localStorage.getItem(name));
},
setItem: function (name,value) {
if (name)
return localStorage.setItem(name,JSON.stringify(value));
},
getAll: function() {
return localStorage;
},
deleteItem: function (name) {
if (name)
delete localStorage[name];
}
};
var cookieStorage = function () {
};
cookieStorage.prototype = {
getItem: function (name) {
var cookies = document.cookie.split(';');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
if( x == name ) return unescape(y);
}
return null;
},
setItem: function (name,value) {
// function to store into cookies
if (value != undefined) {
document.cookie = name+'='+JSON.stringify(value)+';domain='+
window.location.hostname+
';path='+window.location.pathname;
return true;
}
return false;
},
getAll: function() {
var retObject = {};
var cookies = document.cookie.split(':');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
retObject[x] = unescape(y);
}
return retObject;
},
deleteItem: function (name) {
document.cookie = name+'=null;domain='+window.location.hostname+
';path='+window.location.pathname+
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
};
// set good localStorage
try {
if (localStorage.getItem) {
return new browserStorage();
} else {
return new cookieStorage();
}
}
catch (e) {
return new cookieStorage();
}
// end cookies & localStorages
////////////////////////////////////////////////////////////////////////////
})();
\ 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