snappymail/dev/Storages/LocalStorages/LocalStorageDriver.js

87 lines
1.5 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-08-20 23:03:12 +08:00
(function (module) {
2014-08-20 23:03:12 +08:00
'use strict';
var
2014-08-21 23:08:34 +08:00
window = require('../../External/window.js'),
JSON = require('../../External/JSON.js'),
Consts = require('../../Common/Consts.js'),
Utils = require('../../Common/Utils.js')
;
2014-08-20 23:03:12 +08:00
/**
* @constructor
*/
function LocalStorageDriver()
{
}
2014-08-20 23:03:12 +08:00
LocalStorageDriver.supported = function ()
{
return !!window.localStorage;
};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sKey
* @param {*} mData
* @returns {boolean}
*/
LocalStorageDriver.prototype.set = function (sKey, mData)
{
2014-08-20 23:03:12 +08:00
var
mCookieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null,
bResult = false,
mResult = null
;
try
{
2014-08-20 23:03:12 +08:00
mResult = null === mCookieValue ? null : JSON.parse(mCookieValue);
if (!mResult)
{
mResult = {};
}
mResult[sKey] = mData;
window.localStorage[Consts.Values.ClientSideCookieIndexName] = JSON.stringify(mResult);
bResult = true;
}
2014-08-20 23:03:12 +08:00
catch (oException) {}
return bResult;
};
/**
* @param {string} sKey
* @returns {*}
*/
LocalStorageDriver.prototype.get = function (sKey)
{
var
mCokieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null,
mResult = null
;
try
{
2014-08-20 23:03:12 +08:00
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue);
if (mResult && !Utils.isUnd(mResult[sKey]))
{
mResult = mResult[sKey];
}
else
{
mResult = null;
}
}
2014-08-20 23:03:12 +08:00
catch (oException) {}
return mResult;
};
module.exports = LocalStorageDriver;
2014-08-20 23:03:12 +08:00
}(module));