(function() { var divResources = null; function addResources() { divResources = $("#resources_expresso"); divResources.html( new EJS({ url: './templates/resources.ejs'}).render({ resources : resources_expresso })); divResources.accordion( { collapsible: true, heightStyle: "content" }); for( var i in resources_expresso ) { $("#sub_"+resources_expresso[i].id).accordion( { collapsible: true, heightStyle: "content" }); } divResources.find("button").button(); } function ajax() { if( arguments.length > 0 ) { var rest = arguments[0]; //Div prams $("#param_"+resources_expresso[rest].id ).css("display","none"); $("#param_"+resources_expresso[rest].id ).html(); //Div Error $("#error_"+resources_expresso[rest].id ).css("display","none"); $("#error_"+resources_expresso[rest].id ).html(); //Div return $("#return_"+resources_expresso[rest].id ).css("display","none"); $("#return_"+resources_expresso[rest].id ).html(); if( resources_expresso[rest] ) { var server = $('#serverAPI').val() + "/" + resources_expresso[rest].rest; var obj = eval("({"+getParams(resources_expresso[rest])+"})"); var idResource = ""; if( obj.id ) { idResource = obj.id; delete obj.id; } // Login if( rest == "login" ) { delete obj.auth; } // Logout if( rest == "logout" ) { $("#key_auth_server_expresso").find("span").html("USUÁRIO NÃO AUTENTICADO"); $("#key_auth_server_expresso").find("input[type=hidden]").val(""); // Get all auth for( var i in resources_expresso ) { $("#"+i+"_auth").val(""); } } $.ajax( { type : "POST", url : "./inc/client_rest.php", data : { id : idResource, params : JSON.stringify(obj), serverUrl : server, methodType : "POST" }, beforeSend: function() { var divSend = $("#param_"+resources_expresso[rest].id ); divSend.accordion({ collapsible: true, heightStyle: "content", collapsible : true }); divSend.toggle("blind", {}, 1000 ); if( idResource != "" ) { divSend.find("div").html( "
id="+JSON.stringify(idResource)+"
params="+JSON.stringify(obj)+"
"); } else { divSend.find("div").html( "
params="+JSON.stringify(obj)+"
"); } divSend.find("div").find("pre").css("border","0px"); }, success: function(response) { var obj = jQuery.parseJSON(response); if( obj.error ) { var divError = $("#error_"+resources_expresso[rest].id); divError.accordion({ collapsible: true, heightStyle: "content", collapsible: true }); divError.toggle("blind", {}, 1000 ); if( obj.error.code == 5 ) { $("#key_auth_server_expresso").find("span").html(" "); $("#key_auth_server_expresso").find("input[type=hidden]").val(""); // Get all auth for( var i in resources_expresso ) { $("#"+i+"_auth").val(""); } } divError.find("div").html( "Error code : " + obj.error.code + "
Error msg : " + obj.error.message ); } else { var divReturn = $("#return_"+resources_expresso[rest].id); divReturn.accordion({ collapsible: true, heightStyle: "content", collapsible: true }); divReturn.toggle("blind", {}, 1000 ); if( obj.result.auth ) { $("#key_auth_server_expresso").find("span").html(obj.result.auth); $("#key_auth_server_expresso").find("input[type=hidden]").val(obj.result.auth); // Get all auth for( var i in resources_expresso ) { $("#"+i+"_auth").val(obj.result.auth); } } var jsonFormatted = JSON.stringify( obj, undefined, 2); divReturn.find("div").html( "
" + jsonFormatted +"
" ); } }, error: function(response) { var obj = JQuery.parseJSON(response); $("#key_auth_server_expresso").find("span").html("USUÁRIO NÃO AUTENTICADO"); $("#key_auth_server_expresso").find("input[type=hidden]").val(""); var divError = $("#error_"+resources_expresso[rest].id ); divError.accordion({ collapsible: true, heightStyle: "content", collapsible: true }); divError.toggle("blind", {}, 1000 ); divError.find("div").html( "Error :
" + obj.code ); } }); prettyPrint(); } else { alert("RECURSO NÃO ENCONTRADO"); } } } function getParams( resource ) { var params = resource.params; var result = ""; for( var i in params ) { if( params[i][3] || $.trim($('#'+resource.id + '_' + i).val()) != "" ) { result += "\""+i+"\":"+"\""+$('#'+resource.id + '_' + i).val()+"\","; } } return result.substr(0 ,(result.length - 1)); } function execute() { $(document).ready(function() { addResources(); }); } execute.prototype.ajax = ajax; window.execute = new execute(); })();