snappymail/dev/Storages/LocalStorages/CookieDriver.js

100 lines
1.5 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';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
$ = require('$'),
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 CookieDriver()
{
}
2014-09-02 19:34:17 +08:00
/**
* @static
* @return {boolean}
*/
2014-08-20 23:03:12 +08:00
CookieDriver.supported = function ()
{
2014-09-02 19:34:17 +08:00
return !!(window.navigator && window.navigator.cookieEnabled);
2014-08-20 23:03:12 +08:00
};
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
*/
CookieDriver.prototype.set = function (sKey, mData)
{
2014-08-20 23:03:12 +08:00
var
2014-09-02 19:34:17 +08:00
mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
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
{
$.cookie(Consts.Values.ClientSideStorageIndexName, JSON.stringify(mResult), {
2014-08-20 23:03:12 +08:00
'expires': 30
});
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
*/
CookieDriver.prototype.get = function (sKey)
{
var
2014-09-02 19:34:17 +08:00
mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
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;
};
module.exports = CookieDriver;
2014-09-05 06:49:03 +08:00
}());