diff --git a/ChangeLog b/ChangeLog index 6b9bdce8..92bd8c06 100644 --- a/ChangeLog +++ b/ChangeLog @@ -91,6 +91,8 @@ clicked on menu item. * Added easy template renderrer system Util.render. +* Added functions DOM.parseJSON and DOM.stringifyJSON + 2012.12.12, Version 0.1.8 diff --git a/lib/client.js b/lib/client.js index 43af1e94..c74a3917 100644 --- a/lib/client.js +++ b/lib/client.js @@ -596,26 +596,17 @@ CloudCmd._ajaxLoad = function(pFullPath, pOptions){ /* опредиляем в какой мы панели: * правой или левой */ - var lPanel = DOM.getPanel().id, - lError; + var lPanel = DOM.getPanel().id; if(!pOptions.refresh && lPanel){ var lJSON = DOM.Cache.get(lPath); if (lJSON){ /* переводим из текста в JSON */ - if(window && !window.JSON){ - lError = Util.tryCatchLog(function(){ - lJSON = eval('('+lJSON+')'); - }); - - }else - lJSON = JSON.parse(lJSON); - + lJSON = DOM.parseJSON(lJSON); CloudCmd._createFileTable(lPanel, lJSON); CloudCmd._changeLinks(lPanel); - return; } } diff --git a/lib/client/dom.js b/lib/client/dom.js index cdfaa25e..9f62f561 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -135,13 +135,13 @@ var CloudCommander, Util, var lJqXHR = pEvent.target, lType = XMLHTTP.getResponseHeader('content-type'); - if (XMLHTTP.status === 200 /* OK */){ + if (XMLHTTP.status === 200 /* OK */){ var lData = lJqXHR.response; - /* If it's json - parse it as json */ + /* If it's json - parse it as json */ if(lType && Util.isContainStr(lType, 'application/json') ){ var lResult = Util.tryCatch(function(){ - lData = JSON.parse(lJqXHR.response); + lData = DOM.parseJSON(lJqXHR.response); }); if( Util.log(lResult) ) @@ -804,7 +804,7 @@ var CloudCommander, Util, lFunc = function(pData){ var lName = DOM.getCurrentName(); if( Util.isObject(pData) ){ - pData = JSON.stringify(pData, null, 4); + pData = DOM.stringifyJSON(pData); var lExt = '.json'; if( !Util.checkExtension(lName, lExt) ) @@ -849,6 +849,32 @@ var CloudCommander, Util, return lRefresh; }; + /** + * @pJSON + */ + DOM.parseJSON = function(pJSON){ + var lRet; + + Util.tryCatchLog(function(){ + lRet = JSON.parse(pJSON); + }); + + return lRet; + }; + + /** + * pObj + */ + DOM.stringifyJSON = function(pObj){ + var lRet; + + Util.tryCatLog(function(){ + lRet = JSON.stringify(pObj, null, 4); + }); + + return lRet; + }; + /** * unified way to set current file */ diff --git a/lib/client/ie.js b/lib/client/ie.js index fb18acee..8742f409 100644 --- a/lib/client/ie.js +++ b/lib/client/ie.js @@ -152,6 +152,40 @@ var Util, DOM, $; if(!window.XMLHttpRequest) DOM.ajax = $.ajax; + if(!window.JSON){ + DOM.parseJSON = $.parseJSON; + + /* https://gist.github.com/754454 */ + DOM.stringifyJSON = function(pObj){ + var lRet; + + if (!Util.isObject(pObj) || pObj === null) { + // simple data type + if (Util.isString(pObj)) pObj = '"' + pObj + '"'; + lRet = String(pObj); + } else { + // recurse array or object + var n, v, json = [], isArray = Util.isArray(pObj); + + for (n in pObj) { + v = pObj[n]; + + if (pObj.hasOwnProperty(n)) { + if (Util.isString(v)) + v = '"' + v + '"'; + else if (Util.isObject(v) && v !== null) + v = DOM.stringifyJSON(v); + + json.push((isArray ? "" : '"' + n + '":') + String(v)); + } + } + lRet = (isArray ? "[" : "{") + String(json) + (isArray ? "]" : "}"); + } + + return lRet; + }; + } + if(!window.localStorage){ DOM.Cache = function(){ /* приватный переключатель возможности работы с кэшем */ diff --git a/lib/util.js b/lib/util.js index f7f9f60c..b5f712e8 100644 --- a/lib/util.js +++ b/lib/util.js @@ -96,7 +96,7 @@ Util = exports || {}; var lArg = pArg, lConsole = Scope.console, lDate = '[' + Util.getDate() + '] '; - + if(lConsole && pArg) lConsole.log(lDate, lArg);