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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Junming
jio
Commits
a8f62949
Commit
a8f62949
authored
Jan 10, 2013
by
Sven Franck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jslint pass localstorage.js
parent
cf955906
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
308 additions
and
310 deletions
+308
-310
src/jio.storage/localstorage.js
src/jio.storage/localstorage.js
+308
-310
No files found.
src/jio.storage/localstorage.js
View file @
a8f62949
/*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.
*/
*/
jIO
.
addStorageType
(
'
local
'
,
function
(
spec
,
my
)
{
jIO
.
addStorageType
(
'
local
'
,
function
(
spec
,
my
)
{
spec
=
spec
||
{};
spec
=
spec
||
{};
var
that
,
priv
,
localstorage
;
var
that
,
priv
,
localstorage
;
that
=
my
.
basicStorage
(
spec
,
my
);
that
=
my
.
basicStorage
(
spec
,
my
);
priv
=
{};
priv
=
{};
/*
/*
* 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
=
{
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
));
},
},
removeItem
:
function
(
item
)
{
removeItem
:
function
(
item
)
{
return
localStorage
.
removeItem
(
item
);
return
localStorage
.
removeItem
(
item
);
}
}
};
};
// attributes
// attributes
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 ====================
/**
/**
* Update [doc] the document object and remove [doc] keys
* Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting
* which are not in [new_doc]. It only changes [doc] keys not starting
* with an underscore.
* with an underscore.
* ex: doc: {key:value1,_key:value2} with
* ex: doc: {key:value1,_key:value2} with
* new_doc: {key:value3,_key:value4} updates
* new_doc: {key:value3,_key:value4} updates
* doc: {key:value3,_key:value2}.
* doc: {key:value3,_key:value2}.
* @param {object} doc The original document object.
* @param {object} doc The original document object.
* @param {object} new_doc The new document object
* @param {object} new_doc The new document object
*/
*/
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
(
k
[
0
]
!==
'
_
'
)
{
if
(
doc
.
hasOwnProperty
(
k
)
)
{
delete
doc
[
k
];
if
(
k
[
0
]
!==
'
_
'
)
{
}
delete
doc
[
k
];
}
}
for
(
k
in
new_doc
)
{
}
if
(
k
[
0
]
!==
'
_
'
)
{
}
doc
[
k
]
=
new_doc
[
k
];
for
(
k
in
new_doc
)
{
}
if
(
new_doc
.
hasOwnProperty
(
k
))
{
if
(
k
[
0
]
!==
'
_
'
)
{
doc
[
k
]
=
new_doc
[
k
];
}
}
};
}
}
};
/**
/**
* Checks if an object has no enumerable keys
* Checks if an object has no enumerable keys
* @method objectIsEmpty
* @method objectIsEmpty
* @param {object} obj The object
* @param {object} obj The object
* @return {boolean} true if empty, else false
* @return {boolean} true if empty, else false
*/
*/
priv
.
objectIsEmpty
=
function
(
obj
)
{
priv
.
objectIsEmpty
=
function
(
obj
)
{
var
k
;
var
k
;
for
(
k
in
obj
)
{
for
(
k
in
obj
)
{
return
false
;
if
(
obj
.
hasOwnProperty
(
k
))
{
}
return
false
;
return
true
;
}
};
}
return
true
;
};
// ===================== overrides ======================
// ===================== overrides ======================
that
.
specToStore
=
function
()
{
that
.
specToStore
=
function
()
{
return
{
return
{
"
application_name
"
:
priv
.
application_name
,
"
application_name
"
:
priv
.
application_name
,
"
username
"
:
priv
.
username
"
username
"
:
priv
.
username
};
};
};
};
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".
'
;
};
};
// ==================== commands ====================
// ==================== commands ====================
/**
/**
* Create a document in local storage.
* Create a document in local storage.
* @method post
* @method post
* @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
({
"
status
"
:
405
,
"
status
"
:
405
,
"
statusText
"
:
"
Method Not Allowed
"
,
"
statusText
"
:
"
Method Not Allowed
"
,
"
error
"
:
"
method_not_allowed
"
,
"
error
"
:
"
method_not_allowed
"
,
"
message
"
:
"
Cannot create document which id is undefined
"
,
"
message
"
:
"
Cannot create document which id is undefined
"
,
"
reason
"
:
"
Document id is undefined
"
"
reason
"
:
"
Document id is undefined
"
});
return
;
}
doc
=
localstorage
.
getItem
(
priv
.
localpath
+
"
/
"
+
doc
);
if
(
doc
===
null
)
{
// the document does not exist
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
command
.
cloneDoc
());
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()});
}
else
{
// the document already exists
that
.
error
({
"
status
"
:
409
,
"
statusText
"
:
"
Conflicts
"
,
"
error
"
:
"
conflicts
"
,
"
message
"
:
"
Cannot create a new document
"
,
"
reason
"
:
"
Document already exists
"
});
}
});
});
};
return
;
}
doc
=
localstorage
.
getItem
(
priv
.
localpath
+
"
/
"
+
doc
);
if
(
doc
===
null
)
{
// the document does not exist
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
command
.
cloneDoc
());
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
});
}
else
{
// the document already exists
that
.
error
({
"
status
"
:
409
,
"
statusText
"
:
"
Conflicts
"
,
"
error
"
:
"
conflicts
"
,
"
message
"
:
"
Cannot create a new document
"
,
"
reason
"
:
"
Document already exists
"
});
}
});
};
/**
/**
* Create or update a document in local storage.
* Create or update a document in local storage.
* @method put
* @method put
* @param {object} command The JIO command
* @param {object} command The JIO command
*/
*/
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
();
}
else
{
}
else
{
// the document already exists
// the document already exists
priv
.
documentObjectUpdate
(
doc
,
command
.
cloneDoc
());
priv
.
documentObjectUpdate
(
doc
,
command
.
cloneDoc
());
}
}
// write
// write
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
localstorage
.
setItem
(
that
.
success
({
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
"
ok
"
:
true
,
doc
);
"
id
"
:
command
.
getDocId
()
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()});
});
});
};
/**
* Add an attachment to a document
* @method putAttachment
* @param {object} command The JIO command
*/
that
.
putAttachment
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
;
doc
=
localstorage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
if
(
doc
===
null
)
{
// the document does not exist
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Impossible to add attachment
"
,
"
reason
"
:
"
Document not found
"
});
});
};
return
;
}
// the document already exists
doc
.
_attachments
=
doc
.
_attachments
||
{};
doc
.
_attachments
[
command
.
getAttachmentId
()]
=
{
"
content_type
"
:
command
.
getAttachmentMimeType
(),
"
digest
"
:
"
md5-
"
+
command
.
md5SumAttachmentData
(),
"
length
"
:
command
.
getAttachmentLength
()
};
// upload data
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
(),
command
.
getAttachmentData
());
// write document
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
()
});
});
};
/**
/**
* Add an attachment to a document
* Get a document or attachment
* @method putAttachment
* @method get
* @param {object} command The JIO command
* @param {object} command The JIO command
*/
*/
that
.
putAttachment
=
function
(
command
)
{
that
.
get
=
function
(
command
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
var
doc
;
var
doc
;
doc
=
localstorage
.
getItem
(
if
(
typeof
command
.
getAttachmentId
()
===
"
string
"
)
{
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
// seeking for an attachment
if
(
doc
===
null
)
{
doc
=
localstorage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
// the document does not exist
"
/
"
+
command
.
getAttachmentId
());
that
.
error
({
if
(
doc
!==
null
)
{
"
status
"
:
404
,
that
.
success
(
doc
);
"
statusText
"
:
"
Not Found
"
,
}
else
{
"
error
"
:
"
not_found
"
,
that
.
error
({
"
message
"
:
"
Impossible to add attachment
"
,
"
status
"
:
404
,
"
reason
"
:
"
Document not found
"
"
statusText
"
:
"
Not Found
"
,
});
"
error
"
:
"
not_found
"
,
return
;
"
message
"
:
"
Cannot find the attachment
"
,
}
else
{
"
reason
"
:
"
Attachment does not exist
"
// the document already exists
});
doc
[
"
_attachments
"
]
=
doc
[
"
_attachments
"
]
||
{};
}
doc
[
"
_attachments
"
][
command
.
getAttachmentId
()]
=
{
}
else
{
"
content_type
"
:
command
.
getAttachmentMimeType
(),
// seeking for a document
"
digest
"
:
"
md5-
"
+
command
.
md5SumAttachmentData
(),
doc
=
localstorage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
"
length
"
:
command
.
getAttachmentLength
()
if
(
doc
!==
null
)
{
};
that
.
success
(
doc
);
}
else
{
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Cannot find the document
"
,
"
reason
"
:
"
Document does not exist
"
});
}
}
});
};
/**
* Remove a document or attachment
* @method remove
* @param {object} command The JIO command
*/
that
.
remove
=
function
(
command
)
{
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
"
,
"
reason
"
:
"
missing
"
});
};
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
;
}
}
// upload data
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
localstorage
.
setItem
(
doc
);
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
localstorage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
command
.
getAttachmentId
(),
"
/
"
+
command
.
getAttachmentId
());
command
.
getAttachmentData
()
);
// write document
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
that
.
success
({
that
.
success
({
"
ok
"
:
true
,
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
()
"
id
"
:
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
()
});
});
});
}
else
{
};
error
(
"
Attachment
"
);
}
/**
}
else
{
* Get a document or attachment
error
(
"
Document
"
);
* @method get
}
* @param {object} command The JIO command
}
else
{
*/
// seeking for a document
that
.
get
=
function
(
command
)
{
attachment_list
=
[];
setTimeout
(
function
()
{
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
)
{
var
doc
;
if
(
typeof
doc
.
_attachments
===
"
object
"
)
{
if
(
typeof
command
.
getAttachmentId
()
===
"
string
"
)
{
// prepare list of attachments
// seeking for an attachment
for
(
i
in
doc
.
_attachments
)
{
doc
=
localstorage
.
getItem
(
if
(
doc
.
_attachments
.
hasOwnProperty
(
i
))
{
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
attachment_list
.
push
(
i
);
command
.
getAttachmentId
());
}
if
(
doc
!==
null
)
{
that
.
success
(
doc
);
}
else
{
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Cannot find the attachment
"
,
"
reason
"
:
"
Attachment does not exist
"
});
}
}
else
{
// seeking for a document
doc
=
localstorage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
if
(
doc
!==
null
)
{
that
.
success
(
doc
);
}
else
{
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Cannot find the document
"
,
"
reason
"
:
"
Document does not exist
"
});
}
}
});
};
/**
* Remove a document or attachment
* @method remove
* @param {object} command The JIO command
*/
that
.
remove
=
function
(
command
)
{
setTimeout
(
function
()
{
var
doc
,
error
;
error
=
function
(
word
)
{
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
word
+
"
not found
"
,
"
reason
"
:
"
missing
"
});
};
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
"
];
}
localstorage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
localstorage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
());
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
()
});
}
else
{
error
(
"
Attachment
"
);
}
}
else
{
error
(
"
Document
"
);
}
}
else
{
// seeking for a document
var
attachment_list
=
[],
i
;
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
)
{
if
(
typeof
doc
[
"
_attachments
"
]
===
"
object
"
)
{
// prepare list of attachments
for
(
i
in
doc
[
"
_attachments
"
])
{
attachment_list
.
push
(
i
);
}
}
}
else
{
return
error
(
"
Document
"
);
}
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
]);
}
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
});
}
}
}
}
else
{
return
error
(
"
Document
"
);
}
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
]);
}
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
});
});
};
}
});
};
/**
/**
* Get all filenames belonging to a user from the document index
* Get all filenames belonging to a user from the document index
* @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
,
"
statusText
"
:
"
Method Not Allowed
"
,
"
statusText
"
:
"
Method Not Allowed
"
,
"
error
"
:
"
method_not_allowed
"
,
"
error
"
:
"
method_not_allowed
"
,
"
message
"
:
"
Your are not allowed to use this command
"
,
"
message
"
:
"
Your are not allowed to use this command
"
,
"
reason
"
:
"
LocalStorage forbids AllDocs command executions
"
"
reason
"
:
"
LocalStorage forbids AllDocs command executions
"
});
});
});
});
};
};
return
that
;
return
that
;
});
});
\ No newline at end of file
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