Commit 10a18668 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

Improving LocalStorage + updating Qunit tests.

parent d0ac8107
/**
// Adds 3 storages to JIO * Adds 3 storages to JIO.
// type: * - LocalStorage ('local')
// - local * - DAVStorage ('dav')
// - dav * - ReplicateStorage ('replicate')
// - replicate *
* @module JIOStorages
*/
(function () { (function () {
var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) { var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Tools // Tools
var extend = function (o1,o2) {
var key;
for (key in o2) {
o1[key] = o2[key];
}
return o1;
},
// end Tools // end Tools
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Classes // Classes
newLocalStorage,newDAVStorage,newReplicateStorage; var newLocalStorage,newDAVStorage,newReplicateStorage,
newIndexedStorage,newCryptedStorage;
// end Classes // end Classes
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Local Storage // Local Storage
/**
* JIO Local Storage. Type = 'local'.
* It is a database located in the browser local storage.
*/
newLocalStorage = function ( spec, my ) { newLocalStorage = function ( spec, my ) {
// LocalStorage constructor // LocalStorage constructor
var that = Jio.newBaseStorage( spec, my ), priv = {}; var that = Jio.newBaseStorage( spec, my ), priv = {};
priv.storage_file_array_name = 'jio/localfilearray'; priv.storage_user_array_name = 'jio/localuserarray';
priv.storage_file_array_name = 'jio/localfilenamearray/' +
that.getStorageUserName() + '/' + that.getApplicantID();
/**
* Returns a list of users.
* @method getUserArray
* @return {array} The list of users.
*/
priv.getUserArray = function () {
return LocalOrCookieStorage.getItem(
priv.storage_user_array_name) || [];
};
/**
* Adds a user to the user list.
* @method addUser
* @param {string} username The user name.
*/
priv.addUser = function (username) {
var userarray = priv.getUserArray();
userarray.push(username);
LocalOrCookieStorage.setItem(priv.storage_user_array_name,
userarray);
};
priv.getPathArray = function () { /**
* Returns the file names of all existing files owned by the user.
* @method getFileNameArray
* @return {array} All the existing file paths.
*/
priv.getFileNameArray = function () {
return LocalOrCookieStorage.getItem( return LocalOrCookieStorage.getItem(
priv.storage_file_array_name) || []; priv.storage_file_array_name) || [];
}; };
priv.addPath = function (path) {
var patharray = priv.getPathArray(); /**
patharray.push(path); * Adds a file name to the local file name array.
* @method addFileName
* @param {string} filename The new file name.
*/
priv.addFileName = function (filename) {
var filenamearray = priv.getFileNameArray();
filenamearray.push(filename);
LocalOrCookieStorage.setItem(priv.storage_file_array_name, LocalOrCookieStorage.setItem(priv.storage_file_array_name,
patharray); filenamearray);
}; };
priv.removePath = function (path) {
var i, patharray = priv.getPathArray(), newpatharray = []; /**
for (i = 0; i < patharray.length; i+= 1) { * Removes a file name from the local file name array.
if (patharray[i] !== path) { * @method removeFileName
newpatharray.push(patharray[i]); * @param {string} filename The file name to remove.
*/
priv.removeFileName = function (filename) {
var i, l, array = priv.getFileNameArray(), newarray = [];
for (i = 0, l = array.length; i < l; i+= 1) {
if (array[i] !== filename) {
newarray.push(array[i]);
} }
} }
LocalOrCookieStorage.setItem(priv.storage_file_array_name, LocalOrCookieStorage.setItem(priv.storage_file_array_name,
newpatharray); newarray);
}; };
/**
* Checks the availability of a user name set in the job.
* It will check if the user is set in the local user object
* @method checkNameAvailability
*/
that.checkNameAvailability = function () { that.checkNameAvailability = function () {
// checks the availability of the [job.userName].
// if the name already exists, it is not available.
// this.job.userName: the name we want to check.
// wait a little in order to simulate asynchronous operation
setTimeout(function () { setTimeout(function () {
var allpatharray, i, split; var i, l, array = priv.getUserArray();
for (i = 0, l = array.length; i < l; i+= 1) {
allpatharray = priv.getPathArray(); if (array[i] === that.getUserName()) {
for (i = 0; i < allpatharray.length; i += 1) { that.done(false);
split = allpatharray[i].split('/'); return;
if (split[0] === 'jio' &&
split[1] === 'local' &&
split[2] === that.getUserName()) {
return that.done(false);
} }
} }
return that.done(true); that.done(true);
}, 100); }, 100);
}; // end checkNameAvailability }; // end checkNameAvailability
/**
* Saves a document in the local storage.
* It will store the file in 'jio/local/USR/APP/FILENAME'.
* @method saveDocument
*/
that.saveDocument = function () { that.saveDocument = function () {
// Save a document in the local storage
// this.job.fileName: the document name.
// this.job.fileContent: the document content.
// this.job.storage: the storage information.
// this.job.storage.userName: the user name
// this.job.applicant.ID: the applicant id.
// wait a little in order to simulate asynchronous saving // wait a little in order to simulate asynchronous saving
setTimeout (function () { setTimeout (function () {
var doc = null, path = var doc = null, path =
...@@ -102,32 +137,30 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) { ...@@ -102,32 +137,30 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
'creationDate': Date.now(), 'creationDate': Date.now(),
'lastModified': Date.now() 'lastModified': Date.now()
}; };
priv.addPath(path); priv.addFileName(that.getFileName());
} else { } else {
// overwriting // overwriting
doc.lastModified = Date.now(); doc.lastModified = Date.now();
doc.fileContent = that.getFileContent(); doc.fileContent = that.getFileContent();
} }
LocalOrCookieStorage.setItem( LocalOrCookieStorage.setItem(path, doc);
'jio/local/'+that.getStorageUserName()+'/'+
that.getApplicantID()+'/'+
that.getFileName(), doc);
return that.done(); return that.done();
}, 100); }, 100);
}; // end saveDocument }; // end saveDocument
/**
* Loads a document from the local storage.
* It will load file in 'jio/local/USR/APP/FILENAME'.
* You can add an 'options' object to the job, it can contain:
* - getContent {boolean} default true, retrieve the file content or not
* @method loadDocument
*/
that.loadDocument = function () { that.loadDocument = function () {
// Load a document from the storage. It returns a document object
// containing all information of the document and its content.
// this.job.fileName : the document name we want to load.
// this.job.options.getContent: if true, also get the file content.
// document object is {'fileName':string,'fileContent':string, // document object is {'fileName':string,'fileContent':string,
// 'creationDate':date,'lastModified':date} // 'creationDate':date,'lastModified':date}
// wait a little in order to simulate asynchronous operation
setTimeout(function () { setTimeout(function () {
var doc = null, settings = extend( var doc = null, settings = $.extend(
{'getContent':true},that.cloneOptionObject()); {'getContent':true},that.cloneOptionObject());
doc = LocalOrCookieStorage.getItem( doc = LocalOrCookieStorage.getItem(
...@@ -146,45 +179,40 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) { ...@@ -146,45 +179,40 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
}, 100); }, 100);
}; // end loadDocument }; // end loadDocument
/**
* Gets a document list from the local storage.
* It will retreive an array containing files meta data owned by
* the user.
* @method getDocumentList
*/
that.getDocumentList = function () { that.getDocumentList = function () {
// Get a document list from the storage. It returns a document
// array containing all the user documents informations.
// this.job.storage: the storage informations.
// this.job.storage.userName: the userName.
// this.job.storage.applicant.ID: the applicant ID.
// the list is [object,object] -> object = {'fileName':string, // the list is [object,object] -> object = {'fileName':string,
// 'lastModified':date,'creationDate':date} // 'lastModified':date,'creationDate':date}
setTimeout(function () { setTimeout(function () {
var list = [], patharray = [], i, k = 'key', var newarray = [], array = [], i, l, k = 'key',
splitk = ['splitedkey'], fileObject = {}; path = 'jio/local/'+that.getStorageUserName()+'/'+
that.getApplicantID(), fileObject = {};
patharray = priv.getPathArray();
for (i = 0; i < patharray.length; i += 1) { array = priv.getFileNameArray();
k = patharray[i]; for (i = 0, l = array.length; i < l; i += 1) {
splitk = k.split('/'); fileObject =
if (splitk[0] === 'jio' && LocalOrCookieStorage.getItem(path+'/'+array[i]);
splitk[1] === 'local' && newarray.push ({
splitk[2] === that.getStorageUserName() && 'fileName':fileObject.fileName,
splitk[3] === that.getApplicantID()) { 'creationDate':fileObject.creationDate,
fileObject = LocalOrCookieStorage.getItem(k); 'lastModified':fileObject.lastModified});
list.push ({
'fileName':fileObject.fileName,
'creationDate':fileObject.creationDate,
'lastModified':fileObject.lastModified});
}
} }
that.done(list); that.done(newarray);
}, 100); }, 100);
}; // end getDocumentList }; // end getDocumentList
/**
* Removes a document from the local storage.
* It will also remove the path from the local file array.
* @method removeDocument
*/
that.removeDocument = function () { that.removeDocument = function () {
// Remove a document from the storage.
// this.job.storage.userName: the userName.
// this.job.applicant.ID: the applicant ID.
// this.job.fileName: the document name.
setTimeout (function () { setTimeout (function () {
var path = 'jio/local/'+ var path = 'jio/local/'+
that.getStorageUserName()+'/'+ that.getStorageUserName()+'/'+
...@@ -192,13 +220,12 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) { ...@@ -192,13 +220,12 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
that.getFileName() that.getFileName()
// deleting // deleting
LocalOrCookieStorage.deleteItem(path); LocalOrCookieStorage.deleteItem(path);
priv.removePath(path); priv.removeFileName(that.getFileName());
return that.done(); return that.done();
}, 100); }, 100);
}; };
return that; return that;
}; };
// end Local Storage // end Local Storage
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
...@@ -218,7 +245,7 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) { ...@@ -218,7 +245,7 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
// TODO this method is not working !!! // TODO this method is not working !!!
var settings = extend ({ var settings = $.extend ({
'success':function(){},'error':function(){}},options), 'success':function(){},'error':function(){}},options),
splitpath = ['splitedpath'], tmppath = 'temp/path'; splitpath = ['splitedpath'], tmppath = 'temp/path';
...@@ -351,7 +378,7 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) { ...@@ -351,7 +378,7 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
// 'creationDate':date,'lastModified':date} // 'creationDate':date,'lastModified':date}
var doc = {}, var doc = {},
settings = extend({'getContent':true},that.cloneOptionObject()), settings = $.extend({'getContent':true},that.cloneOptionObject()),
// TODO check if job's features are good // TODO check if job's features are good
getContent = function () { getContent = function () {
......
...@@ -23,20 +23,47 @@ var getXML = function (url) { ...@@ -23,20 +23,47 @@ var getXML = function (url) {
dataType:'text',success:function(xml){tmp=xml;}}); dataType:'text',success:function(xml){tmp=xml;}});
return tmp; return tmp;
}, },
addPath = function (path) { addFile = function (user,appid,file) {
var array = LocalOrCookieStorage.getItem ('jio/localfilearray') || []; var i, l, found = false, filenamearray,
array.push(path); userarray = LocalOrCookieStorage.getItem('jio/localuserarray') || [];
LocalOrCookieStorage.setItem ('jio/localfilearray',array); for (i = 0, l = userarray.length; i < l; i+= 1) {
if (userarray[i] === user) { found = true; }
}
if (!found) {
userarray.push(user);
LocalOrCookieStorage.setItem('jio/localuserarray',userarray);
LocalOrCookieStorage.setItem('jio/localfilenamearray/'+user+'/'+appid,
[file.fileName]);
} else {
filenamearray =
LocalOrCookieStorage.getItem(
'jio/localfilenamearray/'+user+'/'+appid) || [];
filenamearray.push(file.fileName);
LocalOrCookieStorage.setItem(
'jio/localfilenamearray/'+user+'/'+appid,
filenamearray);
LocalOrCookieStorage.setItem(
'jio/local/'+user+'/'+appid+'/'+file.fileName,
file);
}
LocalOrCookieStorage.setItem(
'jio/local/'+user+'/'+appid+'/'+file.fileName,
file);
}, },
removePath = function (path) { removeFile = function (user,appid,file) {
var array = LocalOrCookieStorage.getItem ('jio/localfilearray') || [], var i, l, newarray = [],
newarray = [], i; filenamearray =
for (i = 0; i < array.length; i+= 1) { LocalOrCookieStorage.getItem(
if (array[i] !== path) { 'jio/localfilenamearray/'+user+'/'+appid) || [];
newarray.push (array[i]); for (i = 0, l = filenamearray.length; i < l; i+= 1) {
if (filenamearray[i] !== file.fileName) {
newarray.push(filenamearray[i]);
} }
} }
LocalOrCookieStorage.setItem ('jio/localfilearray',newarray); LocalOrCookieStorage.setItem('jio/localfilenamearray/'+user+'/'+appid,
newarray);
LocalOrCookieStorage.deleteItem(
'jio/local/'+user+'/'+appid+'/'+file.fileName);
}; };
//// end tools //// end tools
...@@ -281,11 +308,11 @@ test ('Check name availability', function () { ...@@ -281,11 +308,11 @@ test ('Check name availability', function () {
{"ID":'noid'}); {"ID":'noid'});
// name must be available // name must be available
removePath ('jio/local/MrCheckName/jiotests/file'); removeFile ('MrCheckName','jiotests',{fileName:'file'});
mytest(true); mytest(true);
// name must be unavailable // name must be unavailable
addPath ('jio/local/MrCheckName/jiotests/file'); addFile ('MrCheckName','jiotests',{fileName:'file'});
mytest(false); mytest(false);
o.jio.stop(); o.jio.stop();
...@@ -401,10 +428,8 @@ test ('Get document list', function () { ...@@ -401,10 +428,8 @@ test ('Get document list', function () {
'lastModified':1,'creationDate':0}; 'lastModified':1,'creationDate':0};
doc2 = {'fileName':'memo','fileContent':'test', doc2 = {'fileName':'memo','fileContent':'test',
'lastModified':5,'creationDate':2}; 'lastModified':5,'creationDate':2};
addPath ('jio/local/MrListName/jiotests/file'); addFile ('MrListName','jiotests',doc1);
addPath ('jio/local/MrListName/jiotests/memo'); addFile ('MrListName','jiotests',doc2);
LocalOrCookieStorage.setItem ('jio/local/MrListName/jiotests/file',doc1);
LocalOrCookieStorage.setItem ('jio/local/MrListName/jiotests/memo',doc2);
delete doc1.fileContent; delete doc1.fileContent;
delete doc2.fileContent; delete doc2.fileContent;
mytest ([doc1,doc2]); mytest ([doc1,doc2]);
......
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