snappymail/dev/Storage/LocalDriver/LocalStorage.js

98 lines
1.6 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
var
2014-08-25 23:49:01 +08:00
window = require('window'),
JSON = require('JSON'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Consts = require('Common/Consts'),
Utils = require('Common/Utils')
;
2014-08-20 23:03:12 +08:00
/**
* @constructor
*/
function LocalStorageLocalDriver()
{
}
2014-09-02 19:34:17 +08:00
/**
* @static
* @return {boolean}
*/
LocalStorageLocalDriver.supported = function ()
2014-08-20 23:03:12 +08:00
{
return !!window.localStorage;
};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sKey
* @param {*} mData
2014-09-02 19:34:17 +08:00
* @return {boolean}
2014-08-20 23:03:12 +08:00
*/
LocalStorageLocalDriver.prototype.set = function (sKey, mData)
{
2014-08-20 23:03:12 +08:00
var
2014-09-02 19:34:17 +08:00
mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
2014-08-20 23:03:12 +08:00
bResult = false,
mResult = null
;
try
{
2014-09-02 19:34:17 +08:00
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
}
catch (oException) {}
2014-08-20 23:03:12 +08:00
2014-09-02 19:34:17 +08:00
if (!mResult)
{
mResult = {};
}
mResult[sKey] = mData;
try
{
window.localStorage[Consts.Values.ClientSideStorageIndexName] = JSON.stringify(mResult);
2014-08-20 23:03:12 +08:00
bResult = true;
}
2014-08-20 23:03:12 +08:00
catch (oException) {}
return bResult;
};
/**
* @param {string} sKey
2014-09-02 19:34:17 +08:00
* @return {*}
2014-08-20 23:03:12 +08:00
*/
LocalStorageLocalDriver.prototype.get = function (sKey)
2014-08-20 23:03:12 +08:00
{
var
2014-09-02 19:34:17 +08:00
mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
2014-08-20 23:03:12 +08:00
mResult = null
;
try
{
2014-09-02 19:34:17 +08:00
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
2014-08-20 23:03:12 +08:00
if (mResult && !Utils.isUnd(mResult[sKey]))
{
mResult = mResult[sKey];
}
else
{
mResult = null;
}
}
2014-08-20 23:03:12 +08:00
catch (oException) {}
return mResult;
};
2014-08-25 15:10:51 +08:00
module.exports = LocalStorageLocalDriver;
2014-09-05 06:49:03 +08:00
}());