Commit fc2b0295 authored by Tristan Cavelier's avatar Tristan Cavelier

localstorage.js private methods added, modified or removed

parent 7c518e1e
......@@ -5,127 +5,34 @@
var newLocalStorage = function (spec, my) {
spec = spec || {};
var that = my.basicStorage(spec, my),
priv = {},
var that, priv, localstorage;
that = my.basicStorage(spec, my);
priv = {};
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* values
*/
localstorage = {
getItem: function (item) {
return JSON.parse(localStorage.getItem(item));
},
setItem: function (item, value) {
return localStorage.setItem(item, JSON.stringify(value));
},
deleteItem: function (item) {
delete localStorage[item];
}
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* values
*/
localstorage = {
getItem: function (item) {
return JSON.parse(localStorage.getItem(item));
},
storage_user_array_name,
storage_file_array_name;
setItem: function (item, value) {
return localStorage.setItem(item, JSON.stringify(value));
},
deleteItem: function (item) {
delete localStorage[item];
}
};
// attributes
priv.username = spec.username || '';
priv.applicationname = spec.applicationname || 'untitled';
storage_user_array_name = 'jio/local_user_array';
storage_file_array_name = 'jio/local_file_name_array/' +
priv.localpath = 'jio/localstorage/' +
priv.username + '/' + priv.applicationname;
/**
* Returns a list of users.
* @method getUserArray
* @return {array} The list of users.
*/
priv.getUserArray = function () {
return localstorage.getItem(storage_user_array_name) || [];
};
/**
* Adds a user to the user list.
* @method addUser
* @param {string} user_name The user name.
*/
priv.addUser = function (user_name) {
var user_array = priv.getUserArray();
user_array.push(user_name);
localstorage.setItem(storage_user_array_name, user_array);
};
/**
* checks if a user exists in the user array.
* @method doesUserExist
* @param {string} user_name The user name
* @return {boolean} true if exist, else false
*/
priv.doesUserExist = function (user_name) {
var user_array = priv.getUserArray(), i, l;
for (i = 0, l = user_array.length; i < l; i += 1) {
if (user_array[i] === user_name) {
return true;
}
}
return false;
};
/**
* 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 localstorage.getItem(storage_file_array_name) || [];
};
/**
* Adds a file name to the local file name array.
* @method addFileName
* @param {string} file_name The new file name.
*/
priv.addFileName = function (file_name) {
var file_name_array = priv.getFileNameArray();
file_name_array.push(file_name);
localstorage.setItem(storage_file_array_name, file_name_array);
};
/**
* Removes a file name from the local file name array.
* @method removeFileName
* @param {string} file_name The file name to remove.
*/
priv.removeFileName = function (file_name) {
var i, l, array = priv.getFileNameArray(), new_array = [];
for (i = 0, l = array.length; i < l; i+= 1) {
if (array[i] !== file_name) {
new_array.push(array[i]);
}
}
localstorage.setItem(storage_file_array_name, new_array);
};
/**
* Extends [obj] adding 0 to 3 values according to [command] options.
* @method manageOptions
* @param {object} obj The obj to extend
* @param {object} command The JIO command
* @param {object} doc The document object
*/
priv.manageOptions = function (obj, command, doc) {
obj = obj || {};
if (command.getOption('revs')) {
obj.revisions = doc._revisions;
}
if (command.getOption('revs_info')) {
obj.revs_info = doc._revs_info;
}
if (command.getOption('conflicts')) {
obj.conflicts = {total_rows:0, rows:[]};
}
return obj;
};
// ==================== Tools ====================
/**
* Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting
......@@ -150,75 +57,6 @@ var newLocalStorage = function (spec, my) {
}
};
/**
* Create a new document
* @method setDocument
* @param {object} command Command object
* @param {string} trigger Put/Post
*/
priv.setDocument = function (command, trigger) {
var doc = command.getDoc(),
document_id = doc._id,
document_rev = doc._rev,
document_path = 'jio/local/'+priv.username+'/'+
priv.applicationname+'/'+document_id+
'/'+document_rev;
if (trigger === 'put'){
priv.documentObjectUpdate(doc, command.cloneDoc());
}
localstorage.setItem(document_path, doc);
if (trigger === 'post'){
if (!priv.doesUserExist(priv.username)) {
priv.addUser(priv.username);
}
priv.addFileName(document_id);
}
return priv.manageOptions(
{ok:true,id:document_id,rev:document_rev}, command, doc);
};
/**
* get a document or attachment
* @method getDocument
* @param {object} command Command object
*/
priv.getDocument = function (command) {
var doc = command.getDoc();
document_id = doc._id;
document_rev = doc._rev;
document_path = 'jio/local/'+priv.username+'/'+
priv.applicationname+'/'+document_id+
'/'+document_rev;
localStorage.getItem( document_path );
return priv.manageOptions(
{ok:true,id:document_id,rev:document_rev}, command, doc);
};
/**
* delete a document or attachment
* @method getDocument
* @param {object} command Command object
*/
priv.deleteDocument = function (command) {
var doc = command.getDoc();
document_id = doc._id;
document_rev = doc._rev;
document_path = 'jio/local/'+priv.username+'/'+
priv.applicationname+'/'+document_id+
'/'+document_rev;
localStorage.deleteItem( document_path );
return priv.manageOptions(
{ok:true,id:document_id,rev:document_rev}, command, doc);
};
// ===================== overrides ======================
that.serialized = function () {
return {
......@@ -228,7 +66,8 @@ var newLocalStorage = function (spec, my) {
};
that.validateState = function() {
if (priv.username) {
if (typeof priv.username === "string" &&
priv.username !== '') {
return '';
}
return 'Need at least one parameter: "username".';
......
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