From 6cfd220be062fbe4394293207a8e197dbadeecaa Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 6 Jan 2017 19:29:25 -0500 Subject: [PATCH] CRM-19829 - Add sugar methods for window.localStorage --- js/Common.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/js/Common.js b/js/Common.js index 346e926f26..57ae68ed02 100644 --- a/js/Common.js +++ b/js/Common.js @@ -1610,6 +1610,33 @@ if (!CRM.vars) CRM.vars = {}; } }; + // Sugar methods for window.localStorage, with a fallback for older browsers + var cacheItems = {}; + CRM.cache = { + get: function (name, defaultValue) { + try { + if (localStorage.getItem('CRM' + name) !== null) { + return JSON.parse(localStorage.getItem('CRM' + name)); + } + } catch(e) {} + return cacheItems[name] === undefined ? defaultValue : cacheItems[name]; + }, + set: function (name, value) { + try { + localStorage.setItem('CRM' + name, JSON.stringify(value)); + } catch(e) {} + cacheItems[name] = value; + }, + clear: function(name) { + try { + localStorage.removeItem('CRM' + name); + } catch(e) {} + delete cacheItems[name]; + } + }; + + + // Determine if a user has a given permission. // @see CRM_Core_Resources::addPermissions CRM.checkPerm = function(perm) { -- 2.25.1