1 | (function() |
---|
2 | { |
---|
3 | var divResources = null; |
---|
4 | |
---|
5 | function addResources() |
---|
6 | { |
---|
7 | divResources = $("#resources_expresso"); |
---|
8 | |
---|
9 | divResources.html( new EJS({ url: './templates/resources.ejs'}).render({ resources : resources_expresso })); |
---|
10 | |
---|
11 | divResources.accordion( |
---|
12 | { |
---|
13 | collapsible: true, |
---|
14 | heightStyle: "content" |
---|
15 | }); |
---|
16 | |
---|
17 | for( var i in resources_expresso ) |
---|
18 | { |
---|
19 | $("#sub_"+resources_expresso[i].id).accordion( |
---|
20 | { |
---|
21 | collapsible: true, |
---|
22 | heightStyle: "content" |
---|
23 | }); |
---|
24 | } |
---|
25 | |
---|
26 | divResources.find("button").button(); |
---|
27 | } |
---|
28 | |
---|
29 | function ajax() |
---|
30 | { |
---|
31 | if( arguments.length > 0 ) |
---|
32 | { |
---|
33 | var rest = arguments[0]; |
---|
34 | |
---|
35 | //Div prams |
---|
36 | $("#param_"+resources_expresso[rest].id ).css("display","none"); |
---|
37 | $("#param_"+resources_expresso[rest].id ).html(); |
---|
38 | //Div Error |
---|
39 | $("#error_"+resources_expresso[rest].id ).css("display","none"); |
---|
40 | $("#error_"+resources_expresso[rest].id ).html(); |
---|
41 | //Div return |
---|
42 | $("#return_"+resources_expresso[rest].id ).css("display","none"); |
---|
43 | $("#return_"+resources_expresso[rest].id ).html(); |
---|
44 | |
---|
45 | if( resources_expresso[rest] ) |
---|
46 | { |
---|
47 | var server = $('#serverAPI').val() + "/" + resources_expresso[rest].rest; |
---|
48 | |
---|
49 | var obj = eval("({"+getParams(resources_expresso[rest])+"})"); |
---|
50 | |
---|
51 | var idResource = ""; |
---|
52 | |
---|
53 | if( obj.id ) |
---|
54 | { |
---|
55 | idResource = obj.id; |
---|
56 | |
---|
57 | delete obj.id; |
---|
58 | } |
---|
59 | |
---|
60 | // Login |
---|
61 | if( rest == "login" ) |
---|
62 | { |
---|
63 | delete obj.auth; |
---|
64 | } |
---|
65 | |
---|
66 | // Logout |
---|
67 | if( rest == "logout" ) |
---|
68 | { |
---|
69 | $("#key_auth_server_expresso").find("span").html("USUÁRIO NÃO AUTENTICADO"); |
---|
70 | $("#key_auth_server_expresso").find("input[type=hidden]").val(""); |
---|
71 | |
---|
72 | // Get all auth |
---|
73 | for( var i in resources_expresso ) |
---|
74 | { |
---|
75 | $("#"+i+"_auth").val(""); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | $.ajax( |
---|
80 | { |
---|
81 | type : "POST", |
---|
82 | url : "./inc/client_rest.php", |
---|
83 | data : |
---|
84 | { |
---|
85 | id : idResource, |
---|
86 | params : JSON.stringify(obj), |
---|
87 | serverUrl : server, |
---|
88 | methodType : "POST" |
---|
89 | }, |
---|
90 | beforeSend: function() |
---|
91 | { |
---|
92 | var divSend = $("#param_"+resources_expresso[rest].id ); |
---|
93 | |
---|
94 | divSend.accordion({ collapsible: true, heightStyle: "content", collapsible : true }); |
---|
95 | |
---|
96 | divSend.toggle("blind", {}, 1000 ); |
---|
97 | |
---|
98 | if( idResource != "" ) |
---|
99 | { |
---|
100 | divSend.find("div").html( "<pre class='prettyprint'>id="+JSON.stringify(idResource)+"<br>params="+JSON.stringify(obj)+"</pre>"); |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | divSend.find("div").html( "<pre class='prettyprint'>params="+JSON.stringify(obj)+"</pre>"); |
---|
105 | } |
---|
106 | |
---|
107 | divSend.find("div").find("pre").css("border","0px"); |
---|
108 | }, |
---|
109 | success: function(response) |
---|
110 | { |
---|
111 | var obj = jQuery.parseJSON(response); |
---|
112 | |
---|
113 | if( obj.error ) |
---|
114 | { |
---|
115 | var divError = $("#error_"+resources_expresso[rest].id); |
---|
116 | |
---|
117 | divError.accordion({ collapsible: true, heightStyle: "content", collapsible: true }); |
---|
118 | |
---|
119 | divError.toggle("blind", {}, 1000 ); |
---|
120 | |
---|
121 | if( obj.error.code == 5 ) |
---|
122 | { |
---|
123 | $("#key_auth_server_expresso").find("span").html(" "); |
---|
124 | $("#key_auth_server_expresso").find("input[type=hidden]").val(""); |
---|
125 | |
---|
126 | // Get all auth |
---|
127 | for( var i in resources_expresso ) |
---|
128 | { |
---|
129 | $("#"+i+"_auth").val(""); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | divError.find("div").html( "<font style='font-weight:bold'>Error code : </font> " + |
---|
134 | obj.error.code + "<br><font style='font-weight:bold'>Error msg : </font>" + obj.error.message ); |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | var divReturn = $("#return_"+resources_expresso[rest].id); |
---|
139 | |
---|
140 | divReturn.accordion({ collapsible: true, heightStyle: "content", collapsible: true }); |
---|
141 | |
---|
142 | divReturn.toggle("blind", {}, 1000 ); |
---|
143 | |
---|
144 | if( obj.result.auth ) |
---|
145 | { |
---|
146 | $("#key_auth_server_expresso").find("span").html(obj.result.auth); |
---|
147 | $("#key_auth_server_expresso").find("input[type=hidden]").val(obj.result.auth); |
---|
148 | |
---|
149 | // Get all auth |
---|
150 | for( var i in resources_expresso ) |
---|
151 | { |
---|
152 | $("#"+i+"_auth").val(obj.result.auth); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | var jsonFormatted = JSON.stringify( obj, undefined, 2); |
---|
157 | |
---|
158 | |
---|
159 | divReturn.find("div").html( "<pre class=prettyprint'>" + jsonFormatted +"</pre>" ); |
---|
160 | } |
---|
161 | }, |
---|
162 | error: function(response) |
---|
163 | { |
---|
164 | var obj = JQuery.parseJSON(response); |
---|
165 | |
---|
166 | $("#key_auth_server_expresso").find("span").html("USUÁRIO NÃO AUTENTICADO"); |
---|
167 | $("#key_auth_server_expresso").find("input[type=hidden]").val(""); |
---|
168 | |
---|
169 | var divError = $("#error_"+resources_expresso[rest].id ); |
---|
170 | |
---|
171 | divError.accordion({ collapsible: true, heightStyle: "content", collapsible: true }); |
---|
172 | |
---|
173 | divError.toggle("blind", {}, 1000 ); |
---|
174 | |
---|
175 | divError.find("div").html( "Error : <br> " + obj.code ); |
---|
176 | } |
---|
177 | }); |
---|
178 | |
---|
179 | prettyPrint(); |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | alert("RECURSO NÃO ENCONTRADO"); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | function getParams( resource ) |
---|
189 | { |
---|
190 | var params = resource.params; |
---|
191 | var result = ""; |
---|
192 | |
---|
193 | for( var i in params ) |
---|
194 | { |
---|
195 | if( params[i][3] || $.trim($('#'+resource.id + '_' + i).val()) != "" ) |
---|
196 | { |
---|
197 | result += "\""+i+"\":"+"\""+$('#'+resource.id + '_' + i).val()+"\","; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | return result.substr(0 ,(result.length - 1)); |
---|
202 | } |
---|
203 | |
---|
204 | function execute() |
---|
205 | { |
---|
206 | $(document).ready(function() |
---|
207 | { |
---|
208 | addResources(); |
---|
209 | }); |
---|
210 | } |
---|
211 | |
---|
212 | execute.prototype.ajax = ajax; |
---|
213 | |
---|
214 | window.execute = new execute(); |
---|
215 | |
---|
216 | })(); |
---|