Commit a8f62949 authored by Sven Franck's avatar Sven Franck

jslint pass localstorage.js

parent cf955906
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, localStorage: true, setTimeout: true */
/**
* JIO Local Storage. Type = 'local'.
* Local browser "database" storage.
......@@ -16,7 +18,7 @@ jIO.addStorageType('local', function (spec, my) {
localstorage = {
getItem: function (item) {
var value = localStorage.getItem(item);
return value === null? null: JSON.parse(value);
return value === null ? null : JSON.parse(value);
},
setItem: function (item, value) {
return localStorage.setItem(item, JSON.stringify(value));
......@@ -30,8 +32,8 @@ jIO.addStorageType('local', function (spec, my) {
priv.username = spec.username || '';
priv.application_name = spec.application_name || 'untitled';
priv.localpath = 'jio/localstorage/' +
priv.username + '/' + priv.application_name;
priv.localpath = 'jio/localstorage/' + priv.username + '/' +
priv.application_name;
// ==================== Tools ====================
/**
......@@ -47,15 +49,19 @@ jIO.addStorageType('local', function (spec, my) {
priv.documentObjectUpdate = function (doc, new_doc) {
var k;
for (k in doc) {
if (doc.hasOwnProperty(k)) {
if (k[0] !== '_') {
delete doc[k];
}
}
}
for (k in new_doc) {
if (new_doc.hasOwnProperty(k)) {
if (k[0] !== '_') {
doc[k] = new_doc[k];
}
}
}
};
/**
......@@ -67,8 +73,10 @@ jIO.addStorageType('local', function (spec, my) {
priv.objectIsEmpty = function (obj) {
var k;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
return false;
}
}
return true;
};
......@@ -80,9 +88,8 @@ jIO.addStorageType('local', function (spec, my) {
};
};
that.validateState = function() {
if (typeof priv.username === "string" &&
priv.username !== '') {
that.validateState = function () {
if (typeof priv.username === "string" && priv.username !== '') {
return '';
}
return 'Need at least one parameter: "username".';
......@@ -95,7 +102,7 @@ jIO.addStorageType('local', function (spec, my) {
* @param {object} command The JIO command
*/
that.post = function (command) {
setTimeout (function () {
setTimeout(function () {
var doc = command.getDocId();
if (!(typeof doc === "string" && doc !== "")) {
that.error({
......@@ -107,14 +114,15 @@ jIO.addStorageType('local', function (spec, my) {
});
return;
}
doc = localstorage.getItem(
priv.localpath + "/" + doc);
doc = localstorage.getItem(priv.localpath + "/" + doc);
if (doc === null) {
// the document does not exist
localstorage.setItem(
priv.localpath + "/" + command.getDocId(),
localstorage.setItem(priv.localpath + "/" + command.getDocId(),
command.cloneDoc());
that.success({"ok":true,"id":command.getDocId()});
that.success({
"ok": true,
"id": command.getDocId()
});
} else {
// the document already exists
that.error({
......@@ -136,8 +144,7 @@ jIO.addStorageType('local', function (spec, my) {
that.put = function (command) {
setTimeout(function () {
var doc;
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId());
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (doc === null) {
// the document does not exist
doc = command.cloneDoc();
......@@ -146,10 +153,11 @@ jIO.addStorageType('local', function (spec, my) {
priv.documentObjectUpdate(doc, command.cloneDoc());
}
// write
localstorage.setItem(
priv.localpath + "/" + command.getDocId(),
doc);
that.success({"ok":true,"id":command.getDocId()});
localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc);
that.success({
"ok": true,
"id": command.getDocId()
});
});
};
......@@ -161,8 +169,7 @@ jIO.addStorageType('local', function (spec, my) {
that.putAttachment = function (command) {
setTimeout(function () {
var doc;
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId());
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (doc === null) {
// the document does not exist
that.error({
......@@ -173,28 +180,25 @@ jIO.addStorageType('local', function (spec, my) {
"reason": "Document not found"
});
return;
} else {
}
// the document already exists
doc["_attachments"] = doc["_attachments"] || {};
doc["_attachments"][command.getAttachmentId()] = {
doc._attachments = doc._attachments || {};
doc._attachments[command.getAttachmentId()] = {
"content_type": command.getAttachmentMimeType(),
"digest": "md5-"+command.md5SumAttachmentData(),
"digest": "md5-" + command.md5SumAttachmentData(),
"length": command.getAttachmentLength()
};
}
// upload data
localstorage.setItem(
priv.localpath + "/" + command.getDocId() + "/" +
localstorage.setItem(priv.localpath + "/" + command.getDocId() + "/" +
command.getAttachmentId(),
command.getAttachmentData()
);
command.getAttachmentData());
// write document
localstorage.setItem(
priv.localpath + "/" + command.getDocId(),
doc);
localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc);
that.success({
"ok":true,
"id":command.getDocId()+"/"+command.getAttachmentId()
"ok": true,
"id": command.getDocId() + "/" + command.getAttachmentId()
});
});
};
......@@ -205,13 +209,12 @@ jIO.addStorageType('local', function (spec, my) {
* @param {object} command The JIO command
*/
that.get = function (command) {
setTimeout (function () {
setTimeout(function () {
var doc;
if (typeof command.getAttachmentId() === "string") {
// seeking for an attachment
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId() + "/" +
command.getAttachmentId());
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId() +
"/" + command.getAttachmentId());
if (doc !== null) {
that.success(doc);
} else {
......@@ -225,8 +228,7 @@ jIO.addStorageType('local', function (spec, my) {
}
} else {
// seeking for a document
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId());
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (doc !== null) {
that.success(doc);
} else {
......@@ -248,39 +250,35 @@ jIO.addStorageType('local', function (spec, my) {
* @param {object} command The JIO command
*/
that.remove = function (command) {
setTimeout (function () {
var doc, error;
setTimeout(function () {
var doc, error, i, attachment_list;
error = function (word) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": word+" not found",
"message": word + " not found",
"reason": "missing"
});
};
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId());
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (typeof command.getAttachmentId() === "string") {
// remove attachment from document
if (doc !== null && typeof doc === "object" &&
typeof doc["_attachments"] === "object") {
if (typeof doc["_attachments"][
command.getAttachmentId()] === "object") {
delete doc["_attachments"][command.getAttachmentId()];
if (priv.objectIsEmpty(doc["_attachments"])) {
delete doc["_attachments"];
typeof doc._attachments === "object") {
if (typeof doc._attachments[command.getAttachmentId()] ===
"object") {
delete doc._attachments[command.getAttachmentId()];
if (priv.objectIsEmpty(doc._attachments)) {
delete doc._attachments;
}
localstorage.setItem(
priv.localpath + "/" + command.getDocId(),
localstorage.setItem(priv.localpath + "/" + command.getDocId(),
doc);
localstorage.removeItem(
priv.localpath + "/" + command.getDocId() + "/" +
command.getAttachmentId());
localstorage.removeItem(priv.localpath + "/" + command.getDocId() +
"/" + command.getAttachmentId());
that.success({
"ok": true,
"id": command.getDocId()+"/"+
command.getAttachmentId()
"id": command.getDocId() + "/" + command.getAttachmentId()
});
} else {
error("Attachment");
......@@ -290,24 +288,24 @@ jIO.addStorageType('local', function (spec, my) {
}
} else {
// seeking for a document
var attachment_list = [], i;
attachment_list = [];
if (doc !== null && typeof doc === "object") {
if (typeof doc["_attachments"] === "object") {
if (typeof doc._attachments === "object") {
// prepare list of attachments
for (i in doc["_attachments"]) {
for (i in doc._attachments) {
if (doc._attachments.hasOwnProperty(i)) {
attachment_list.push(i);
}
}
}
} else {
return error("Document");
}
localstorage.removeItem(
priv.localpath + "/" + command.getDocId());
localstorage.removeItem(priv.localpath + "/" + command.getDocId());
// delete all attachments
for (i = 0; i < attachment_list.length; i += 1) {
localstorage.removeItem(
priv.localpath+"/"+command.getDocId()+"/"+
attachment_list[i]);
localstorage.removeItem(priv.localpath + "/" + command.getDocId() +
"/" + attachment_list[i]);
}
that.success({
"ok": true,
......@@ -322,7 +320,7 @@ jIO.addStorageType('local', function (spec, my) {
* @method allDocs
* @param {object} command The JIO command
*/
that.allDocs = function (command) {
that.allDocs = function () {
setTimeout(function () {
that.error({
"status": 405,
......
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