Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Cédric Le Ninivin
jio
Commits
0f5db310
Commit
0f5db310
authored
Aug 21, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
localstorage memory mode added
parent
02686913
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
25 deletions
+61
-25
src/jio.storage/localstorage.js
src/jio.storage/localstorage.js
+61
-25
No files found.
src/jio.storage/localstorage.js
View file @
0f5db310
...
...
@@ -5,8 +5,7 @@
*/
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, localStorage: true, setTimeout: true,
complex_queries, define */
/*global jIO, localStorage, setTimeout, complex_queries, define */
/**
* JIO Local Storage. Type = 'local'.
...
...
@@ -16,6 +15,9 @@
*
* {
* "type": "local",
* "mode": <string>,
* // - "localStorage" // default
* // - "memory"
* "username": <non empty string>, // to define user space
* "application_name": <string> // default 'untitled'
* }
...
...
@@ -94,11 +96,13 @@
return
true
;
}
var
ram
=
{},
memorystorage
,
localstorage
;
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* values
*/
var
localstorage
=
{
localstorage
=
{
getItem
:
function
(
item
)
{
var
value
=
localStorage
.
getItem
(
item
);
return
value
===
null
?
null
:
JSON
.
parse
(
value
);
...
...
@@ -111,6 +115,23 @@
}
};
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* values
*/
memorystorage
=
{
getItem
:
function
(
item
)
{
var
value
=
ram
[
item
];
return
value
===
undefined
?
null
:
JSON
.
parse
(
value
);
},
setItem
:
function
(
item
,
value
)
{
ram
[
item
]
=
JSON
.
stringify
(
value
);
},
removeItem
:
function
(
item
)
{
delete
ram
[
item
];
}
};
jIO
.
addStorageType
(
'
local
'
,
function
(
spec
,
my
)
{
spec
=
spec
||
{};
...
...
@@ -125,11 +146,26 @@
priv
.
localpath
=
'
jio/localstorage/
'
+
priv
.
username
+
'
/
'
+
priv
.
application_name
;
switch
(
spec
.
mode
)
{
case
"
memory
"
:
priv
.
database
=
ram
;
priv
.
storage
=
memorystorage
;
priv
.
mode
=
"
memory
"
;
break
;
default
:
priv
.
database
=
localStorage
;
priv
.
storage
=
localstorage
;
priv
.
mode
=
"
localStorage
"
;
break
;
}
// ===================== overrides ======================
that
.
specToStore
=
function
()
{
return
{
"
application_name
"
:
priv
.
application_name
,
"
username
"
:
priv
.
username
"
username
"
:
priv
.
username
,
"
mode
"
:
priv
.
mode
};
};
...
...
@@ -152,13 +188,13 @@
if
(
!
doc_id
)
{
doc_id
=
generateUuid
();
}
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
doc_id
);
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
doc_id
);
if
(
doc
===
null
)
{
// the document does not exist
doc
=
command
.
cloneDoc
();
doc
.
_id
=
doc_id
;
delete
doc
.
_attachments
;
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
doc_id
,
doc
);
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
doc_id
,
doc
);
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
doc_id
...
...
@@ -184,7 +220,7 @@
that
.
put
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
,
tmp
;
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
if
(
doc
===
null
)
{
// the document does not exist
doc
=
command
.
cloneDoc
();
...
...
@@ -196,7 +232,7 @@
doc
=
tmp
;
}
// write
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
...
...
@@ -212,7 +248,7 @@
that
.
putAttachment
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
;
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
if
(
doc
===
null
)
{
// the document does not exist
that
.
error
({
...
...
@@ -234,11 +270,11 @@
};
// upload data
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
(),
command
.
getAttachmentData
());
// write document
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
(),
...
...
@@ -254,7 +290,7 @@
*/
that
.
get
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
=
local
storage
.
getItem
(
var
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
);
if
(
doc
!==
null
)
{
...
...
@@ -278,7 +314,7 @@
*/
that
.
getAttachment
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
=
local
storage
.
getItem
(
var
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
()
);
...
...
@@ -304,7 +340,7 @@
that
.
remove
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
,
i
,
attachment_list
;
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
attachment_list
=
[];
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
)
{
if
(
typeof
doc
.
_attachments
===
"
object
"
)
{
...
...
@@ -324,10 +360,10 @@
"
reason
"
:
"
missing
"
});
}
local
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
priv
.
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
// delete all attachments
for
(
i
=
0
;
i
<
attachment_list
.
length
;
i
+=
1
)
{
local
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
priv
.
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
attachment_list
[
i
]);
}
that
.
success
({
...
...
@@ -354,7 +390,7 @@
"
reason
"
:
"
missing
"
});
};
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
// remove attachment from document
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
&&
typeof
doc
.
_attachments
===
"
object
"
)
{
...
...
@@ -364,9 +400,9 @@
if
(
objectIsEmpty
(
doc
.
_attachments
))
{
delete
doc
.
_attachments
;
}
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
local
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
priv
.
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
());
that
.
success
({
"
ok
"
:
true
,
...
...
@@ -401,15 +437,15 @@
option
.
select_list
===
undefined
&&
option
.
include_docs
===
undefined
))
{
rows
=
[];
for
(
i
in
localStorag
e
)
{
if
(
localStorag
e
.
hasOwnProperty
(
i
))
{
for
(
i
in
priv
.
databas
e
)
{
if
(
priv
.
databas
e
.
hasOwnProperty
(
i
))
{
// filter non-documents
if
(
path_re
.
test
(
i
))
{
row
=
{
value
:
{}
};
row
.
id
=
i
.
split
(
'
/
'
).
slice
(
-
1
)[
0
];
row
.
key
=
row
.
id
;
if
(
command
.
getOption
(
'
include_docs
'
))
{
row
.
doc
=
JSON
.
parse
(
localS
torage
.
getItem
(
i
));
row
.
doc
=
JSON
.
parse
(
priv
.
s
torage
.
getItem
(
i
));
}
rows
.
push
(
row
);
}
...
...
@@ -418,10 +454,10 @@
that
.
success
({
"
rows
"
:
rows
,
"
total_rows
"
:
rows
.
length
});
}
else
{
// create complex query object from returned results
for
(
i
in
localStorag
e
)
{
if
(
localStorag
e
.
hasOwnProperty
(
i
))
{
for
(
i
in
priv
.
databas
e
)
{
if
(
priv
.
databas
e
.
hasOwnProperty
(
i
))
{
if
(
path_re
.
test
(
i
))
{
document_list
.
push
(
local
storage
.
getItem
(
i
));
document_list
.
push
(
priv
.
storage
.
getItem
(
i
));
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment