Commit cf955906 authored by Sven Franck's avatar Sven Franck

jslint pass indexstorage.js

parent 6e6d67b7
jIO.addStorageType ('indexed', function (spec, my) {
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, localStorage: true, setTimeout: true */
jIO.addStorageType('indexed', function (spec, my) {
spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
var that = my.basicStorage(spec, my),
priv = {},
validatestate_sub_storage = spec.storage || false,
storage_object_name = 'jio/indexed_storage_object',
storage_file_object_name,
super_serialized = that.serialized;
var validatestate_sub_storage = spec.storage || false;
priv.sub_storage_spec = spec.storage || {type:'base'};
priv.sub_storage_string = JSON.stringify (priv.sub_storage_spec);
priv.sub_storage_spec = spec.storage || {
type: 'base'
};
priv.sub_storage_string = JSON.stringify(priv.sub_storage_spec);
var storage_object_name = 'jio/indexed_storage_object';
var storage_file_object_name = 'jio/indexed_file_object/'+
storage_file_object_name = 'jio/indexed_file_object/' +
priv.sub_storage_string;
var super_serialized = that.serialized;
that.serialized = function () {
var o = super_serialized();
o.storage = priv.sub_storage_spec;
......@@ -19,45 +25,56 @@ jIO.addStorageType ('indexed', function (spec, my) {
that.validateState = function () {
if (!validatestate_sub_storage) {
return 'Need at least one parameter: "storage" '+
return 'Need at least one parameter: "storage" ' +
'containing storage specifications.';
}
return '';
};
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');
};
priv.indexStorage = function () {
var obj = localStorage.getItem (storage_object_name) || {};
var obj = localStorage.getItem(storage_object_name) || {};
obj[priv.sub_storage_spec] = new Date().getTime();
localStorage.setItem (storage_object_name,obj);
localStorage.setItem(storage_object_name, obj);
};
priv.formatToFileObject = function (row) {
var k, obj = {_id:row.id};
var k, obj = {
_id: row.id
};
for (k in row.value) {
if (row.value.hasOwnProperty(k)) {
obj[k] = row.value[k];
}
}
return obj;
};
priv.allDocs = function (files_object) {
var k, obj = {rows:[]}, i = 0;
var k, obj = {
rows: []
}, i = 0;
for (k in files_object) {
if (files_object.hasOwnProperty(k)) {
obj.rows[i] = {};
obj.rows[i].value = files_object[k];
obj.rows[i].id = obj.rows[i].key = obj.rows[i].value._id;
delete obj.rows[i].value._id;
i ++;
i += 1;
}
}
obj.total_rows = obj.rows.length;
return obj;
......@@ -65,27 +82,27 @@ jIO.addStorageType ('indexed', function (spec, my) {
priv.setFileArray = function (file_array) {
var i, obj = {};
for (i = 0; i < file_array.length; i+= 1) {
for (i = 0; i < file_array.length; i += 1) {
obj[file_array[i].id] = priv.formatToFileObject(file_array[i]);
}
localStorage.setItem (storage_file_object_name,obj);
localStorage.setItem(storage_file_object_name, obj);
};
priv.getFileObject = function (docid) {
var obj = localStorage.getItem (storage_file_object_name) || {};
var obj = localStorage.getItem(storage_file_object_name) || {};
return obj[docid];
};
priv.addFile = function (file_obj) {
var obj = localStorage.getItem (storage_file_object_name) || {};
var obj = localStorage.getItem(storage_file_object_name) || {};
obj[file_obj._id] = file_obj;
localStorage.setItem (storage_file_object_name,obj);
localStorage.setItem(storage_file_object_name, obj);
};
priv.removeFile = function (docid) {
var obj = localStorage.getItem (storage_file_object_name) || {};
var obj = localStorage.getItem(storage_file_object_name) || {};
delete obj[docid];
localStorage.setItem (storage_file_object_name,obj);
localStorage.setItem(storage_file_object_name, obj);
};
/**
......@@ -99,8 +116,9 @@ jIO.addStorageType ('indexed', function (spec, my) {
var success = function (val) {
priv.setFileArray(val.rows);
};
that.addJob ('allDocs', priv.sub_storage_spec,null,
{max_retry:3},success,function(){});
that.addJob('allDocs', priv.sub_storage_spec, null, {
max_retry: 3
}, success, function () {});
};
that.post = function (command) {
......@@ -122,8 +140,8 @@ jIO.addStorageType ('indexed', function (spec, my) {
that.error(err);
};
priv.indexStorage();
that.addJob ('put',priv.sub_storage_spec,cloned_doc,
cloned_option,success,error);
that.addJob('put', priv.sub_storage_spec, cloned_doc,
cloned_option, success, error);
}; // end put
/**
......@@ -131,8 +149,8 @@ jIO.addStorageType ('indexed', function (spec, my) {
* @method get
*/
that.get = function (command) {
var file_array,
success = function (val) {
// jslint unused var file_array
var success = function (val) {
that.success(val);
},
error = function (err) {
......@@ -140,8 +158,8 @@ jIO.addStorageType ('indexed', function (spec, my) {
},
get = function () {
var cloned_option = command.cloneOption();
that.addJob ('get',priv.sub_storage_spec,command.cloneDoc(),
cloned_option,success,error);
that.addJob('get', priv.sub_storage_spec, command.cloneDoc(),
cloned_option, success, error);
that.end();
};
priv.indexStorage();
......@@ -149,10 +167,8 @@ jIO.addStorageType ('indexed', function (spec, my) {
if (command.getOption('metadata_only')) {
setTimeout(function () {
var file_obj = priv.getFileObject(command.getDocId());
if (file_obj &&
(file_obj._last_modified ||
file_obj._creation_date)) {
that.success (file_obj);
if (file_obj && (file_obj._last_modified || file_obj._creation_date)) {
that.success(file_obj);
} else {
get();
}
......@@ -167,22 +183,25 @@ jIO.addStorageType ('indexed', function (spec, my) {
* @method allDocs
*/
that.allDocs = function (command) {
var obj = localStorage.getItem (storage_file_object_name);
var obj = localStorage.getItem(storage_file_object_name),
success,
error;
if (obj) {
priv.update();
setTimeout(function (){
that.success (priv.allDocs(obj));
setTimeout(function () {
that.success(priv.allDocs(obj));
});
} else {
var success = function (val) {
success = function (val) {
priv.setFileArray(val.rows);
that.success(val);
},
};
error = function (err) {
that.error(err);
};
that.addJob ('allDocs', priv.sub_storage_spec,null,
command.cloneOption(),success,error);
that.addJob('allDocs', priv.sub_storage_spec, null,
command.cloneOption(), success, error);
}
}; // end allDocs
......@@ -199,9 +218,9 @@ jIO.addStorageType ('indexed', function (spec, my) {
error = function (err) {
that.error(err);
};
that.addJob ('remove',priv.sub_storage_spec,command.cloneDoc(),
command.cloneOption(),success,error);
that.addJob('remove', priv.sub_storage_spec, command.cloneDoc(),
command.cloneOption(), success, error);
}; // end remove
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