source: sandbox/webservice/restclient_new/js/execute.js @ 7818

Revision 7818, 5.7 KB checked in by alexandrecorreia, 11 years ago (diff)

Ticket #2507 - Ajustando o help e criado um exemplo para utilizar o REST.

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