Fix local storage (Closes #303)

This commit is contained in:
RainLoop Team 2014-09-02 15:34:17 +04:00
parent 77870442fe
commit 664c1dca80
11 changed files with 147 additions and 103 deletions

View file

@ -19,7 +19,7 @@
kn = require('App:Knoin'), kn = require('App:Knoin'),
LocalStorage = require('Storage:LocalStorage'), Local = require('Storage:LocalStorage'),
Settings = require('Storage:Settings'), Settings = require('Storage:Settings'),
Data = require('Storage:RainLoop:Data'), Data = require('Storage:RainLoop:Data'),
Cache = require('Storage:RainLoop:Cache'), Cache = require('Storage:RainLoop:Cache'),
@ -1185,7 +1185,7 @@
}); });
} }
LocalStorage.set(Enums.ClientSideKeyName.FoldersLashHash, oData.Result.FoldersHash); Local.set(Enums.ClientSideKeyName.FoldersLashHash, oData.Result.FoldersHash);
} }
}; };
@ -1195,8 +1195,8 @@
*/ */
RainLoopApp.prototype.isFolderExpanded = function (sFullNameHash) RainLoopApp.prototype.isFolderExpanded = function (sFullNameHash)
{ {
var aExpandedList = LocalStorage.get(Enums.ClientSideKeyName.ExpandedFolders); var aExpandedList = Local.get(Enums.ClientSideKeyName.ExpandedFolders);
return _.isArray(aExpandedList) && -1 !== _.indexOf(aExpandedList, sFullNameHash); return Utils.isArray(aExpandedList) && -1 !== _.indexOf(aExpandedList, sFullNameHash);
}; };
/** /**
@ -1205,8 +1205,8 @@
*/ */
RainLoopApp.prototype.setExpandedFolder = function (sFullNameHash, bExpanded) RainLoopApp.prototype.setExpandedFolder = function (sFullNameHash, bExpanded)
{ {
var aExpandedList = LocalStorage.get(Enums.ClientSideKeyName.ExpandedFolders); var aExpandedList = Local.get(Enums.ClientSideKeyName.ExpandedFolders);
if (!_.isArray(aExpandedList)) if (!Utils.isArray(aExpandedList))
{ {
aExpandedList = []; aExpandedList = [];
} }
@ -1221,7 +1221,7 @@
aExpandedList = _.without(aExpandedList, sFullNameHash); aExpandedList = _.without(aExpandedList, sFullNameHash);
} }
LocalStorage.set(Enums.ClientSideKeyName.ExpandedFolders, aExpandedList); Local.set(Enums.ClientSideKeyName.ExpandedFolders, aExpandedList);
}; };
RainLoopApp.prototype.initLayoutResizer = function (sLeft, sRight, sClientSideKeyName) RainLoopApp.prototype.initLayoutResizer = function (sLeft, sRight, sClientSideKeyName)
@ -1232,7 +1232,7 @@
oLeft = $(sLeft), oLeft = $(sLeft),
oRight = $(sRight), oRight = $(sRight),
mLeftWidth = LocalStorage.get(sClientSideKeyName) || null, mLeftWidth = Local.get(sClientSideKeyName) || null,
fSetWidth = function (iWidth) { fSetWidth = function (iWidth) {
if (iWidth) if (iWidth)
@ -1256,7 +1256,7 @@
else else
{ {
oLeft.resizable('enable'); oLeft.resizable('enable');
var iWidth = Utils.pInt(LocalStorage.get(sClientSideKeyName)) || iMinWidth; var iWidth = Utils.pInt(Local.get(sClientSideKeyName)) || iMinWidth;
fSetWidth(iWidth > iMinWidth ? iWidth : iMinWidth); fSetWidth(iWidth > iMinWidth ? iWidth : iMinWidth);
} }
}, },
@ -1264,7 +1264,7 @@
fResizeFunction = function (oEvent, oObject) { fResizeFunction = function (oEvent, oObject) {
if (oObject && oObject.size && oObject.size.width) if (oObject && oObject.size && oObject.size.width)
{ {
LocalStorage.set(sClientSideKeyName, oObject.size.width); Local.set(sClientSideKeyName, oObject.size.width);
oRight.css({ oRight.css({
'left': '' + oObject.size.width + 'px' 'left': '' + oObject.size.width + 'px'

View file

@ -67,7 +67,7 @@
* @const * @const
* @type {string} * @type {string}
*/ */
Consts.Values.ClientSideCookieIndexName = 'rlcsc'; Consts.Values.ClientSideStorageIndexName = 'rlcsc';
/** /**
* @const * @const

View file

@ -10,10 +10,10 @@
Utils = require('Utils'), Utils = require('Utils'),
Settings = require('Storage:Settings'), Settings = require('Storage:Settings'),
LocalStorage = require('Storage:LocalStorage'),
Data = require('Storage:RainLoop:Data'), Data = require('Storage:RainLoop:Data'),
Cache = require('Storage:RainLoop:Cache'), Cache = require('Storage:RainLoop:Cache'),
Remote = require('Storage:RainLoop:Remote') Remote = require('Storage:RainLoop:Remote'),
LocalStorage = require('Storage:LocalStorage')
; ;
/** /**

View file

@ -11,8 +11,8 @@
{ {
var var
NextStorageDriver = require('_').find([ NextStorageDriver = require('_').find([
require('Storage:LocalStorage:Cookie'), require('Storage:LocalStorage:LocalStorage'),
require('Storage:LocalStorage:LocalStorage') require('Storage:LocalStorage:Cookie')
], function (NextStorageDriver) { ], function (NextStorageDriver) {
return NextStorageDriver && NextStorageDriver.supported(); return NextStorageDriver && NextStorageDriver.supported();
}) })
@ -26,6 +26,9 @@
} }
} }
/**
* @type {LocalStorageDriver|CookieDriver|null}
*/
LocalStorage.prototype.oDriver = null; LocalStorage.prototype.oDriver = null;
/** /**

View file

@ -17,37 +17,46 @@
*/ */
function CookieDriver() function CookieDriver()
{ {
} }
/**
* @static
* @return {boolean}
*/
CookieDriver.supported = function () CookieDriver.supported = function ()
{ {
return true; return !!(window.navigator && window.navigator.cookieEnabled);
}; };
/** /**
* @param {string} sKey * @param {string} sKey
* @param {*} mData * @param {*} mData
* @returns {boolean} * @return {boolean}
*/ */
CookieDriver.prototype.set = function (sKey, mData) CookieDriver.prototype.set = function (sKey, mData)
{ {
var var
mCokieValue = $.cookie(Consts.Values.ClientSideCookieIndexName), mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
bResult = false, bResult = false,
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (!mResult) }
{ catch (oException) {}
mResult = {};
}
mResult[sKey] = mData; if (!mResult)
$.cookie(Consts.Values.ClientSideCookieIndexName, JSON.stringify(mResult), { {
mResult = {};
}
mResult[sKey] = mData;
try
{
$.cookie(Consts.Values.ClientSideStorageIndexName, JSON.stringify(mResult), {
'expires': 30 'expires': 30
}); });
@ -60,18 +69,18 @@
/** /**
* @param {string} sKey * @param {string} sKey
* @returns {*} * @return {*}
*/ */
CookieDriver.prototype.get = function (sKey) CookieDriver.prototype.get = function (sKey)
{ {
var var
mCokieValue = $.cookie(Consts.Values.ClientSideCookieIndexName), mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (mResult && !Utils.isUnd(mResult[sKey])) if (mResult && !Utils.isUnd(mResult[sKey]))
{ {
mResult = mResult[sKey]; mResult = mResult[sKey];

View file

@ -19,6 +19,10 @@
{ {
} }
/**
* @static
* @return {boolean}
*/
LocalStorageDriver.supported = function () LocalStorageDriver.supported = function ()
{ {
return !!window.localStorage; return !!window.localStorage;
@ -27,26 +31,32 @@
/** /**
* @param {string} sKey * @param {string} sKey
* @param {*} mData * @param {*} mData
* @returns {boolean} * @return {boolean}
*/ */
LocalStorageDriver.prototype.set = function (sKey, mData) LocalStorageDriver.prototype.set = function (sKey, mData)
{ {
var var
mCookieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null, mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
bResult = false, bResult = false,
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCookieValue ? null : JSON.parse(mCookieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (!mResult) }
{ catch (oException) {}
mResult = {};
}
mResult[sKey] = mData; if (!mResult)
window.localStorage[Consts.Values.ClientSideCookieIndexName] = JSON.stringify(mResult); {
mResult = {};
}
mResult[sKey] = mData;
try
{
window.localStorage[Consts.Values.ClientSideStorageIndexName] = JSON.stringify(mResult);
bResult = true; bResult = true;
} }
@ -57,18 +67,18 @@
/** /**
* @param {string} sKey * @param {string} sKey
* @returns {*} * @return {*}
*/ */
LocalStorageDriver.prototype.get = function (sKey) LocalStorageDriver.prototype.get = function (sKey)
{ {
var var
mCokieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null, mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (mResult && !Utils.isUnd(mResult[sKey])) if (mResult && !Utils.isUnd(mResult[sKey]))
{ {
mResult = mResult[sKey]; mResult = mResult[sKey];

View file

@ -743,7 +743,7 @@
* @const * @const
* @type {string} * @type {string}
*/ */
Consts.Values.ClientSideCookieIndexName = 'rlcsc'; Consts.Values.ClientSideStorageIndexName = 'rlcsc';
/** /**
* @const * @const

File diff suppressed because one or more lines are too long

View file

@ -300,7 +300,7 @@
kn = require('App:Knoin'), kn = require('App:Knoin'),
LocalStorage = require('Storage:LocalStorage'), Local = require('Storage:LocalStorage'),
Settings = require('Storage:Settings'), Settings = require('Storage:Settings'),
Data = require('Storage:RainLoop:Data'), Data = require('Storage:RainLoop:Data'),
Cache = require('Storage:RainLoop:Cache'), Cache = require('Storage:RainLoop:Cache'),
@ -1466,7 +1466,7 @@
}); });
} }
LocalStorage.set(Enums.ClientSideKeyName.FoldersLashHash, oData.Result.FoldersHash); Local.set(Enums.ClientSideKeyName.FoldersLashHash, oData.Result.FoldersHash);
} }
}; };
@ -1476,8 +1476,8 @@
*/ */
RainLoopApp.prototype.isFolderExpanded = function (sFullNameHash) RainLoopApp.prototype.isFolderExpanded = function (sFullNameHash)
{ {
var aExpandedList = LocalStorage.get(Enums.ClientSideKeyName.ExpandedFolders); var aExpandedList = Local.get(Enums.ClientSideKeyName.ExpandedFolders);
return _.isArray(aExpandedList) && -1 !== _.indexOf(aExpandedList, sFullNameHash); return Utils.isArray(aExpandedList) && -1 !== _.indexOf(aExpandedList, sFullNameHash);
}; };
/** /**
@ -1486,8 +1486,8 @@
*/ */
RainLoopApp.prototype.setExpandedFolder = function (sFullNameHash, bExpanded) RainLoopApp.prototype.setExpandedFolder = function (sFullNameHash, bExpanded)
{ {
var aExpandedList = LocalStorage.get(Enums.ClientSideKeyName.ExpandedFolders); var aExpandedList = Local.get(Enums.ClientSideKeyName.ExpandedFolders);
if (!_.isArray(aExpandedList)) if (!Utils.isArray(aExpandedList))
{ {
aExpandedList = []; aExpandedList = [];
} }
@ -1502,7 +1502,7 @@
aExpandedList = _.without(aExpandedList, sFullNameHash); aExpandedList = _.without(aExpandedList, sFullNameHash);
} }
LocalStorage.set(Enums.ClientSideKeyName.ExpandedFolders, aExpandedList); Local.set(Enums.ClientSideKeyName.ExpandedFolders, aExpandedList);
}; };
RainLoopApp.prototype.initLayoutResizer = function (sLeft, sRight, sClientSideKeyName) RainLoopApp.prototype.initLayoutResizer = function (sLeft, sRight, sClientSideKeyName)
@ -1513,7 +1513,7 @@
oLeft = $(sLeft), oLeft = $(sLeft),
oRight = $(sRight), oRight = $(sRight),
mLeftWidth = LocalStorage.get(sClientSideKeyName) || null, mLeftWidth = Local.get(sClientSideKeyName) || null,
fSetWidth = function (iWidth) { fSetWidth = function (iWidth) {
if (iWidth) if (iWidth)
@ -1537,7 +1537,7 @@
else else
{ {
oLeft.resizable('enable'); oLeft.resizable('enable');
var iWidth = Utils.pInt(LocalStorage.get(sClientSideKeyName)) || iMinWidth; var iWidth = Utils.pInt(Local.get(sClientSideKeyName)) || iMinWidth;
fSetWidth(iWidth > iMinWidth ? iWidth : iMinWidth); fSetWidth(iWidth > iMinWidth ? iWidth : iMinWidth);
} }
}, },
@ -1545,7 +1545,7 @@
fResizeFunction = function (oEvent, oObject) { fResizeFunction = function (oEvent, oObject) {
if (oObject && oObject.size && oObject.size.width) if (oObject && oObject.size && oObject.size.width)
{ {
LocalStorage.set(sClientSideKeyName, oObject.size.width); Local.set(sClientSideKeyName, oObject.size.width);
oRight.css({ oRight.css({
'left': '' + oObject.size.width + 'px' 'left': '' + oObject.size.width + 'px'
@ -2131,7 +2131,7 @@
* @const * @const
* @type {string} * @type {string}
*/ */
Consts.Values.ClientSideCookieIndexName = 'rlcsc'; Consts.Values.ClientSideStorageIndexName = 'rlcsc';
/** /**
* @const * @const
@ -11718,10 +11718,10 @@ module.exports = window;
Utils = require('Utils'), Utils = require('Utils'),
Settings = require('Storage:Settings'), Settings = require('Storage:Settings'),
LocalStorage = require('Storage:LocalStorage'),
Data = require('Storage:RainLoop:Data'), Data = require('Storage:RainLoop:Data'),
Cache = require('Storage:RainLoop:Cache'), Cache = require('Storage:RainLoop:Cache'),
Remote = require('Storage:RainLoop:Remote') Remote = require('Storage:RainLoop:Remote'),
LocalStorage = require('Storage:LocalStorage')
; ;
/** /**
@ -14735,8 +14735,8 @@ module.exports = window;
{ {
var var
NextStorageDriver = require('_').find([ NextStorageDriver = require('_').find([
require('Storage:LocalStorage:Cookie'), require('Storage:LocalStorage:LocalStorage'),
require('Storage:LocalStorage:LocalStorage') require('Storage:LocalStorage:Cookie')
], function (NextStorageDriver) { ], function (NextStorageDriver) {
return NextStorageDriver && NextStorageDriver.supported(); return NextStorageDriver && NextStorageDriver.supported();
}) })
@ -14750,6 +14750,9 @@ module.exports = window;
} }
} }
/**
* @type {LocalStorageDriver|CookieDriver|null}
*/
LocalStorage.prototype.oDriver = null; LocalStorage.prototype.oDriver = null;
/** /**
@ -14794,37 +14797,46 @@ module.exports = window;
*/ */
function CookieDriver() function CookieDriver()
{ {
} }
/**
* @static
* @return {boolean}
*/
CookieDriver.supported = function () CookieDriver.supported = function ()
{ {
return true; return !!(window.navigator && window.navigator.cookieEnabled);
}; };
/** /**
* @param {string} sKey * @param {string} sKey
* @param {*} mData * @param {*} mData
* @returns {boolean} * @return {boolean}
*/ */
CookieDriver.prototype.set = function (sKey, mData) CookieDriver.prototype.set = function (sKey, mData)
{ {
var var
mCokieValue = $.cookie(Consts.Values.ClientSideCookieIndexName), mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
bResult = false, bResult = false,
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (!mResult) }
{ catch (oException) {}
mResult = {};
}
mResult[sKey] = mData; if (!mResult)
$.cookie(Consts.Values.ClientSideCookieIndexName, JSON.stringify(mResult), { {
mResult = {};
}
mResult[sKey] = mData;
try
{
$.cookie(Consts.Values.ClientSideStorageIndexName, JSON.stringify(mResult), {
'expires': 30 'expires': 30
}); });
@ -14837,18 +14849,18 @@ module.exports = window;
/** /**
* @param {string} sKey * @param {string} sKey
* @returns {*} * @return {*}
*/ */
CookieDriver.prototype.get = function (sKey) CookieDriver.prototype.get = function (sKey)
{ {
var var
mCokieValue = $.cookie(Consts.Values.ClientSideCookieIndexName), mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (mResult && !Utils.isUnd(mResult[sKey])) if (mResult && !Utils.isUnd(mResult[sKey]))
{ {
mResult = mResult[sKey]; mResult = mResult[sKey];
@ -14888,6 +14900,10 @@ module.exports = window;
{ {
} }
/**
* @static
* @return {boolean}
*/
LocalStorageDriver.supported = function () LocalStorageDriver.supported = function ()
{ {
return !!window.localStorage; return !!window.localStorage;
@ -14896,26 +14912,32 @@ module.exports = window;
/** /**
* @param {string} sKey * @param {string} sKey
* @param {*} mData * @param {*} mData
* @returns {boolean} * @return {boolean}
*/ */
LocalStorageDriver.prototype.set = function (sKey, mData) LocalStorageDriver.prototype.set = function (sKey, mData)
{ {
var var
mCookieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null, mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
bResult = false, bResult = false,
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCookieValue ? null : JSON.parse(mCookieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (!mResult) }
{ catch (oException) {}
mResult = {};
}
mResult[sKey] = mData; if (!mResult)
window.localStorage[Consts.Values.ClientSideCookieIndexName] = JSON.stringify(mResult); {
mResult = {};
}
mResult[sKey] = mData;
try
{
window.localStorage[Consts.Values.ClientSideStorageIndexName] = JSON.stringify(mResult);
bResult = true; bResult = true;
} }
@ -14926,18 +14948,18 @@ module.exports = window;
/** /**
* @param {string} sKey * @param {string} sKey
* @returns {*} * @return {*}
*/ */
LocalStorageDriver.prototype.get = function (sKey) LocalStorageDriver.prototype.get = function (sKey)
{ {
var var
mCokieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null, mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
mResult = null mResult = null
; ;
try try
{ {
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue); mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (mResult && !Utils.isUnd(mResult[sKey])) if (mResult && !Utils.isUnd(mResult[sKey]))
{ {
mResult = mResult[sKey]; mResult = mResult[sKey];

File diff suppressed because one or more lines are too long