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

Revision 7808, 5.3 KB checked in by alexandrecorreia, 11 years ago (diff)

Ticket #2507 - Criado um novo cliente REST para teste/documentação no Expresso.

  • 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                                // Login
56                                if( rest == "login" )
57                                {
58                                        delete obj.auth;
59                                }
60
61                                // Logout
62                                if( rest == "logout" )
63                                {
64                                $("#key_auth_server_expresso").find("span").html("USUÁRIO NÃO AUTENTICADO");
65                                $("#key_auth_server_expresso").find("input[type=hidden]").val("");
66
67                                // Get all auth
68                                for( var i in resources_expresso )
69                                {
70                                        $("#"+i+"_auth").val("");
71                                }
72                                }
73
74                                $.ajax(
75                                {
76                                        type    : "POST",
77                                        url     : "./inc/client_rest.php",
78                                        data    :
79                                        {
80                                                params          : JSON.stringify(obj),
81                                                serverUrl       : server,
82                                                methodType      : "POST"
83                                        },
84                                        beforeSend: function()
85                                        {
86                                                var divSend = $("#param_"+resources_expresso[rest].id );
87                                               
88                                                divSend.accordion({ collapsible: true, heightStyle: "content", collapsible : true });
89
90                                                divSend.toggle("blind", {}, 1000 );
91
92                                                divSend.find("div").html( "<pre class='prettyprint'>params="+JSON.stringify(obj)+"</pre>");
93                                               
94                                                divSend.find("div").find("pre").css("border","0px");
95                                        },
96                        success: function(response)
97                        {
98                                        var obj = jQuery.parseJSON(response);
99
100                                        if( obj.error )
101                                        {
102                                                var divError = $("#error_"+resources_expresso[rest].id);
103
104                                        divError.accordion({ collapsible: true, heightStyle: "content", collapsible: true });
105
106                                        divError.toggle("blind", {}, 1000 );
107
108                                        if( obj.error.code == 5 )
109                                        {
110                                                $("#key_auth_server_expresso").find("span").html("&nbsp;");
111                                                $("#key_auth_server_expresso").find("input[type=hidden]").val("");
112
113                                                // Get all auth
114                                                for( var i in resources_expresso )
115                                                {
116                                                        $("#"+i+"_auth").val("");
117                                                }
118                                        }
119
120                                        divError.find("div").html( "<font style='font-weight:bold'>Error code : </font> " +
121                                                obj.error.code  + "<br><font style='font-weight:bold'>Error msg : </font>" + obj.error.message );
122                                        }
123                                        else
124                                        {
125                                                var divReturn = $("#return_"+resources_expresso[rest].id);
126
127                                        divReturn.accordion({ collapsible: true, heightStyle: "content", collapsible: true });
128
129                                        divReturn.toggle("blind", {}, 1000 );
130
131                                        if( obj.result.auth )
132                                        {
133                                                $("#key_auth_server_expresso").find("span").html(obj.result.auth);
134                                                $("#key_auth_server_expresso").find("input[type=hidden]").val(obj.result.auth);
135
136                                                        // Get all auth
137                                                for( var i in resources_expresso )
138                                                {
139                                                        $("#"+i+"_auth").val(obj.result.auth);
140                                                }
141                                        }
142
143                                        var jsonFormatted = JSON.stringify( obj, undefined, 2);
144
145
146                                        divReturn.find("div").html( "<pre class=prettyprint'>" + jsonFormatted +"</pre>" );
147                                        }
148                        },
149                        error: function(response)                                       
150                        {
151                                var obj = JQuery.parseJSON(response);
152
153                                var divError = $("#error_"+resources_expresso[rest].id );
154
155                                divError.accordion({ collapsible: true, heightStyle: "content", collapsible: true });
156
157                                divError.toggle("blind", {}, 1000 );
158
159                                divError.find("div").html( "Error : <br> " + obj.code );
160                        }
161                                });
162               
163                                prettyPrint();
164                        }
165                        else
166                        {
167                                alert("RECURSO NÃO ENCONTRADO");
168                        }
169                }
170        }
171
172        function getParams( resource )
173        {
174                var params = resource.params;
175                var result = "";
176
177                for( var i in params )
178                {
179                        if( params[i][3] || $.trim($('#'+resource.id + '_' + i).val()) != "" )
180                        {
181                                result += "\""+i+"\":"+"\""+$('#'+resource.id + '_' + i).val()+"\",";
182                        }
183                }
184               
185                return result.substr(0 ,(result.length - 1));
186        }
187
188        function execute()
189        {
190                $(document).ready(function()
191                {
192                        addResources();
193                });
194        }
195       
196        execute.prototype.ajax = ajax;
197       
198        window.execute = new execute();
199
200})();
Note: See TracBrowser for help on using the repository browser.