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