Commit 543a7490 authored by Romain Courteaud's avatar Romain Courteaud

Allow to use sessionStorage.

parent bb18e075
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
/*jslint nomen: true */ /*jslint nomen: true */
/*global jIO, localStorage, Blob, RSVP */ /*global jIO, sessionStorage, localStorage, Blob, RSVP */
/** /**
* JIO Local Storage. Type = 'local'. * JIO Local Storage. Type = 'local'.
...@@ -14,17 +14,22 @@ ...@@ -14,17 +14,22 @@
* Storage Description: * Storage Description:
* *
* { * {
* "type": "local" * "type": "local",
* "sessiononly": false
* } * }
* *
* @class LocalStorage * @class LocalStorage
*/ */
(function (jIO, localStorage, Blob, RSVP) { (function (jIO, sessionStorage, localStorage, Blob, RSVP) {
"use strict"; "use strict";
function LocalStorage() { function LocalStorage(spec) {
return; if (spec.sessiononly === true) {
this._storage = sessionStorage;
} else {
this._storage = localStorage;
}
} }
function restrictDocumentId(id) { function restrictDocumentId(id) {
...@@ -42,8 +47,8 @@ ...@@ -42,8 +47,8 @@
found = false, found = false,
key; key;
for (key in localStorage) { for (key in this._storage) {
if (localStorage.hasOwnProperty(key)) { if (this._storage.hasOwnProperty(key)) {
attachments[key] = {}; attachments[key] = {};
found = true; found = true;
} }
...@@ -57,7 +62,7 @@ ...@@ -57,7 +62,7 @@
LocalStorage.prototype.getAttachment = function (param) { LocalStorage.prototype.getAttachment = function (param) {
restrictDocumentId(param._id); restrictDocumentId(param._id);
var textstring = localStorage.getItem(param._attachment); var textstring = this._storage.getItem(param._attachment);
if (textstring === null) { if (textstring === null) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -69,6 +74,7 @@ ...@@ -69,6 +74,7 @@
}; };
LocalStorage.prototype.putAttachment = function (param) { LocalStorage.prototype.putAttachment = function (param) {
var context = this;
restrictDocumentId(param._id); restrictDocumentId(param._id);
// the document already exists // the document already exists
...@@ -78,13 +84,13 @@ ...@@ -78,13 +84,13 @@
return jIO.util.readBlobAsText(param._blob); return jIO.util.readBlobAsText(param._blob);
}) })
.push(function (e) { .push(function (e) {
localStorage.setItem(param._attachment, e.target.result); context._storage.setItem(param._attachment, e.target.result);
}); });
}; };
LocalStorage.prototype.removeAttachment = function (param) { LocalStorage.prototype.removeAttachment = function (param) {
restrictDocumentId(param._id); restrictDocumentId(param._id);
return localStorage.removeItem(param._attachment); return this._storage.removeItem(param._attachment);
}; };
...@@ -101,4 +107,4 @@ ...@@ -101,4 +107,4 @@
jIO.addStorage('local', LocalStorage); jIO.addStorage('local', LocalStorage);
}(jIO, localStorage, Blob, RSVP)); }(jIO, sessionStorage, localStorage, Blob, RSVP));
/*jslint nomen: true */ /*jslint nomen: true */
/*global localStorage, Blob, document*/ /*global sessionStorage, localStorage, Blob, document*/
(function (jIO, localStorage, QUnit, Blob, document) { (function (jIO, sessionStorage, localStorage, QUnit, Blob, document) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
stop = QUnit.stop, stop = QUnit.stop,
...@@ -11,6 +11,29 @@ ...@@ -11,6 +11,29 @@
equal = QUnit.equal, equal = QUnit.equal,
module = QUnit.module; module = QUnit.module;
/////////////////////////////////////////////////////////////////
// localStorage.constructor
/////////////////////////////////////////////////////////////////
module("localStorage.constructor");
test("local storage by default", function () {
var jio = jIO.createJIO({
type: "local"
});
equal(jio.__type, "local");
equal(jio.__storage._storage, localStorage);
});
test("sessiononly", function () {
var jio = jIO.createJIO({
type: "local",
sessiononly: true
});
equal(jio.__type, "local");
equal(jio.__storage._storage, sessionStorage);
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// localStorage.get // localStorage.get
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -429,4 +452,4 @@ ...@@ -429,4 +452,4 @@
}); });
}); });
}(jIO, localStorage, QUnit, Blob, document)); }(jIO, sessionStorage, localStorage, QUnit, Blob, document));
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