added functions DOM.parseJSON and DOM.stringifyJSON

This commit is contained in:
coderaiser 2013-01-18 04:19:36 -05:00
parent 258fb11669
commit 535e13b206
5 changed files with 69 additions and 16 deletions

View file

@ -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

View file

@ -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;
}
}

View file

@ -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
*/

View file

@ -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(){
/* приватный переключатель возможности работы с кэшем */

View file

@ -96,7 +96,7 @@ Util = exports || {};
var lArg = pArg,
lConsole = Scope.console,
lDate = '[' + Util.getDate() + '] ';
if(lConsole && pArg)
lConsole.log(lDate, lArg);