web-apps/apps/common/main/lib/util/LocalStorage.js

134 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-04-01 13:17:09 +00:00
/*
*
2017-01-17 14:58:08 +00:00
* (c) Copyright Ascensio System Limited 2010-2017
2016-04-01 13:17:09 +00:00
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
2016-03-11 00:48:53 +00:00
/**
* LocalStorage.js
*
* Created by Maxim Kadushkin on 31 July 2015
* Copyright (c) 2015 Ascensio System SIA. All rights reserved.
*
*/
define(['gateway'], function () {
Common.localStorage = new (function() {
var _storeName, _filter;
var _store = {};
2016-03-11 00:48:53 +00:00
var ongetstore = function(data) {
if (data.type == 'localstorage') {
_store = data.keys;
}
};
2016-03-11 00:48:53 +00:00
Common.Gateway.on('internalcommand', ongetstore);
2016-03-11 00:48:53 +00:00
var _refresh = function() {
if (!_lsAllowed)
Common.Gateway.internalMessage('localstorage', {cmd:'get', keys:_filter});
};
2016-03-11 00:48:53 +00:00
var _save = function() {
if (!_lsAllowed)
Common.Gateway.internalMessage('localstorage', {cmd:'set', keys:_store});
};
2016-03-11 00:48:53 +00:00
var _setItem = function(name, value, just) {
if (_lsAllowed) {
2017-05-25 09:47:20 +00:00
try
{
localStorage.setItem(name, value);
}
catch (error){}
} else {
_store[name] = value;
2016-03-11 00:48:53 +00:00
if (just===true) {
Common.Gateway.internalMessage('localstorage', {
cmd:'set',
keys: {
name: value
}
});
}
2016-03-11 00:48:53 +00:00
}
};
2016-03-11 00:48:53 +00:00
2017-04-20 09:27:27 +00:00
var _setItemAsBool = function(name, value, just) {
_setItem(name, value ? 1 : 0, just);
};
var _getItem = function(name) {
if (_lsAllowed)
return localStorage.getItem(name);
else
return _store[name]===undefined ? null : _store[name];
};
2016-03-11 00:48:53 +00:00
var _getItemAsBool = function (name, defValue) {
var value = _getItem(name);
defValue = defValue || false;
return (value!==null) ? (parseInt(value) != 0) : defValue;
}
2017-04-06 09:34:01 +00:00
var _getItemExists = function (name) {
var value = _getItem(name);
return value !== null;
}
try {
var _lsAllowed = !!window.localStorage;
} catch (e) {
_lsAllowed = false;
}
2016-03-11 00:48:53 +00:00
return {
getId: function() {
return _storeName;
},
setId: function(name) {
_storeName = name;
},
getItem: _getItem,
getBool: _getItemAsBool,
2017-04-20 09:27:27 +00:00
setBool: _setItemAsBool,
setItem: _setItem,
setKeysFilter: function(value) {
_filter = value;
},
2017-04-06 09:34:01 +00:00
itemExists: _getItemExists,
sync: _refresh,
save: _save
};
})();
});