From d64a58692fec99c7bb97e18b74e56a911e8d52ae Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 27 Nov 2012 05:05:04 -0500 Subject: [PATCH] Added chainable to Cache object in DOM --- ChangeLog | 2 ++ lib/client/dom.js | 66 +++++++++++++++++++++++++++++++--------- lib/client/ie.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0e64de16..67a6eb07 100644 --- a/ChangeLog +++ b/ChangeLog @@ -122,6 +122,8 @@ callback function. * Added ability to load a couple scripts after one main, in any position in anyLoadOnLoad function. +* Added chainable to Cache object in DOM. + 2012.10.01, Version 0.1.7 diff --git a/lib/client/dom.js b/lib/client/dom.js index 4ee89d94..50cb86a9 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -184,11 +184,35 @@ var CloudCommander, Util, DOM, CloudFunc; return ( CacheAllowed = Util.isObject( window.localStorage ) ); }; + + /** + * allow cache usage + */ + this.setAllowed = function(){ + var lRet = this; + CacheAllowed = true; + + return lRet; + }; + + /** + * dissalow cache usage + */ + this.UnSetAllowed = function(){ + var lRet = this; + CacheAllowed = false; + + return lRet; + }; + /** remove element */ this.remove = function(pItem){ - return CacheAllowed ? - localStorage.removeItem(pItem) : - (delete Data[pItem]); + var lRet = this; + + if(CacheAllowed) + localStorage.removeItem(pItem); + + return lRet; }; /** если доступен localStorage и @@ -196,28 +220,42 @@ var CloudCommander, Util, DOM, CloudFunc; * записываем данные в него */ this.set = function(pName, pData){ - var lRet; + var lRet = this; - if(pName && pData) - lRet = CacheAllowed ? - localStorage.setItem(pName,pData) : - Data[pName] = pData; + if(CacheAllowed && pName && pData) + localStorage.setItem(pName,pData); return lRet; }, /** Если доступен Cache принимаем из него данные*/ this.get = function(pName){ - return CacheAllowed ? - localStorage.getItem(pName) : - Data[pName]; + var lRet = this; + + if(CacheAllowed) + lRet = localStorage.getItem(pName); + + return lRet; }, + /* get all cache from local storage */ + this.getAll = function(){ + var lRet = null; + + if(CacheAllowed) + lRet = localStorage; + + return lRet; + }; + /** функция чистит весь кэш для всех каталогов*/ this.clear = function(){ - return CacheAllowed ? - localStorage.clear() : - (Data = {}); + var lRet = this; + + if(CacheAllowed) + localStorage.clear(); + + return lRet; }; }; diff --git a/lib/client/ie.js b/lib/client/ie.js index 010db487..708558fc 100644 --- a/lib/client/ie.js +++ b/lib/client/ie.js @@ -28,10 +28,17 @@ var Util, DOM, $; } /* setting function context (this) */ - DOM.bind = function(pFunction, pContext){ + Util.bind = function(pFunction, pContext){ return $.proxy(pFunction, pContext); }; + /* + * typeof callback === "function" should not be used, + * as older browsers may report objects to be a function, + * which they are not + */ + Util.isFunction = $.isFunction; + if (!document.addEventListener) /** * safe add event listener on ie @@ -141,4 +148,71 @@ var Util, DOM, $; if(!window.XMLHttpRequest) DOM.ajax = $.ajax; + + if(!window.localStorage){ + DOM.Cache = function(){ + /* приватный переключатель возможности работы с кэшем */ + var CacheAllowed, + Data = {}; + + /* функция проверяет возможно ли работать с кэшем каким-либо образом */ + this.isAllowed = function(){ + return CacheAllowed; + }; + + this.setAllowed = function(){ + CacheAllowed = true; + }; + + this.UnSetAllowed = function(){ + CacheAllowed = false; + }; + + /** remove element */ + this.remove = function(pItem){ + var lRet = this; + if(CacheAllowed) + delete Data[pItem]; + + return lRet; + }; + + /** если доступен localStorage и + * в нём есть нужная нам директория - + * записываем данные в него + */ + this.set = function(pName, pData){ + var lRet = this; + + if(CacheAllowed && pName && pData) + Data[pName] = pData; + + return lRet; + }, + + /** Если доступен Cache принимаем из него данные*/ + this.get = function(pName){ + var lRet = false; + + if(CacheAllowed) + lRet = Data[pName]; + + return lRet; + }, + + /** функция чистит весь кэш для всех каталогов*/ + this.clear = function(){ + var lRet = this; + + if(CacheAllowed) + Data = {}; + + return lRet; + }; + }; + + DOM.Cache = new DOM.Cache(); + } + + })(); \ No newline at end of file