Commit 5cbbae51 authored by Tristan Cavelier's avatar Tristan Cavelier

localstorage.js some methods moved

parent b9dfcca0
...@@ -54,18 +54,51 @@ ...@@ -54,18 +54,51 @@
}(['jio', 'complex_queries'], function (jIO, complex_queries) { }(['jio', 'complex_queries'], function (jIO, complex_queries) {
"use strict"; "use strict";
jIO.addStorageType('local', function (spec, my) { /**
* Returns 4 hexadecimal random characters.
*
* @return {String} The characters
*/
function S4() {
return ('0000' + Math.floor(
Math.random() * 0x10000 /* 65536 */
).toString(16)).slice(-4);
}
spec = spec || {}; /**
var that, priv, localstorage; * An Universal Unique ID generator
that = my.basicStorage(spec, my); *
priv = {}; * @return {String} The new UUID.
*/
function generateUuid() {
return S4() + S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + S4() + S4();
}
/**
* Checks if an object has no enumerable keys
*
* @param {Object} obj The object
* @return {Boolean} true if empty, else false
*/
function objectIsEmpty(obj) {
var k;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
return false;
}
}
return true;
}
/* /*
* Wrapper for the localStorage used to simplify instion of any kind of * Wrapper for the localStorage used to simplify instion of any kind of
* values * values
*/ */
localstorage = { var 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);
...@@ -78,6 +111,13 @@ ...@@ -78,6 +111,13 @@
} }
}; };
jIO.addStorageType('local', function (spec, my) {
spec = spec || {};
var that, priv;
that = my.basicStorage(spec, my);
priv = {};
// attributes // attributes
priv.username = spec.username || ''; priv.username = spec.username || '';
priv.application_name = spec.application_name || 'untitled'; priv.application_name = spec.application_name || 'untitled';
...@@ -85,43 +125,6 @@ ...@@ -85,43 +125,6 @@
priv.localpath = 'jio/localstorage/' + priv.username + '/' + priv.localpath = 'jio/localstorage/' + priv.username + '/' +
priv.application_name; priv.application_name;
// ==================== Tools ====================
/**
* Generate a new uuid
* @method generateUuid
* @return {string} The new uuid
*/
priv.generateUuid = function () {
var S4 = function () {
/* 65536 */
var i, string = Math.floor(
Math.random() * 0x10000
).toString(16);
for (i = string.length; i < 4; i += 1) {
string = '0' + string;
}
return string;
};
return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() +
S4() + S4();
};
/**
* Checks if an object has no enumerable keys
* @method objectIsEmpty
* @param {object} obj The object
* @return {boolean} true if empty, else false
*/
priv.objectIsEmpty = function (obj) {
var k;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
return false;
}
}
return true;
};
// ===================== overrides ====================== // ===================== overrides ======================
that.specToStore = function () { that.specToStore = function () {
return { return {
...@@ -147,7 +150,7 @@ ...@@ -147,7 +150,7 @@
setTimeout(function () { setTimeout(function () {
var doc, doc_id = command.getDocId(); var doc, doc_id = command.getDocId();
if (!doc_id) { if (!doc_id) {
doc_id = priv.generateUuid(); doc_id = generateUuid();
} }
doc = localstorage.getItem(priv.localpath + "/" + doc_id); doc = localstorage.getItem(priv.localpath + "/" + doc_id);
if (doc === null) { if (doc === null) {
...@@ -358,7 +361,7 @@ ...@@ -358,7 +361,7 @@
if (typeof doc._attachments[command.getAttachmentId()] === if (typeof doc._attachments[command.getAttachmentId()] ===
"object") { "object") {
delete doc._attachments[command.getAttachmentId()]; delete doc._attachments[command.getAttachmentId()];
if (priv.objectIsEmpty(doc._attachments)) { if (objectIsEmpty(doc._attachments)) {
delete doc._attachments; delete doc._attachments;
} }
localstorage.setItem(priv.localpath + "/" + command.getDocId(), localstorage.setItem(priv.localpath + "/" + command.getDocId(),
......
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