CLIENT GUI Framework
[vnfsdk/refrepo.git] / openo-portal / portal-common / src / main / webapp / common / thirdparty / cometd / jquery / jquery.cometd.js
1 /*\r
2  * Copyright (c) 2010 the original author or authors.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 (function($)\r
18 {\r
19     function bind($, org_cometd)\r
20     {\r
21         // Remap cometd JSON functions to jquery JSON functions\r
22         org_cometd.JSON.toJSON = JSON.stringify;\r
23         org_cometd.JSON.fromJSON = JSON.parse;\r
24 \r
25         function _setHeaders(xhr, headers)\r
26         {\r
27             if (headers)\r
28             {\r
29                 for (var headerName in headers)\r
30                 {\r
31                     if (headerName.toLowerCase() === 'content-type')\r
32                     {\r
33                         continue;\r
34                     }\r
35                     xhr.setRequestHeader(headerName, headers[headerName]);\r
36                 }\r
37             }\r
38         }\r
39 \r
40         // Remap toolkit-specific transport calls\r
41         function LongPollingTransport()\r
42         {\r
43             var _super = new org_cometd.LongPollingTransport();\r
44             var that = org_cometd.Transport.derive(_super);\r
45 \r
46             that.xhrSend = function(packet)\r
47             {\r
48                 return $.ajax({\r
49                     url: packet.url,\r
50                     async: packet.sync !== true,\r
51                     type: 'POST',\r
52                     contentType: 'application/json;charset=UTF-8',\r
53                     data: packet.body,\r
54                     xhrFields: {\r
55                         // Has no effect if the request is not cross domain\r
56                         // but if it is, allows cookies to be sent to the server\r
57                         withCredentials: true\r
58                     },\r
59                     beforeSend: function(xhr)\r
60                     {\r
61                         _setHeaders(xhr, packet.headers);\r
62                         // Returning false will abort the XHR send\r
63                         return true;\r
64                     },\r
65                     success: packet.onSuccess,\r
66                     error: function(xhr, reason, exception)\r
67                     {\r
68                         packet.onError(reason, exception);\r
69                     }\r
70                 });\r
71             };\r
72 \r
73             return that;\r
74         }\r
75 \r
76         function CallbackPollingTransport()\r
77         {\r
78             var _super = new org_cometd.CallbackPollingTransport();\r
79             var that = org_cometd.Transport.derive(_super);\r
80 \r
81             that.jsonpSend = function(packet)\r
82             {\r
83                 $.ajax({\r
84                     url: packet.url,\r
85                     async: packet.sync !== true,\r
86                     type: 'GET',\r
87                     dataType: 'jsonp',\r
88                     jsonp: 'jsonp',\r
89                     data: {\r
90                         // In callback-polling, the content must be sent via the 'message' parameter\r
91                         message: packet.body\r
92                     },\r
93                     beforeSend: function(xhr)\r
94                     {\r
95                         _setHeaders(xhr, packet.headers);\r
96                         // Returning false will abort the XHR send\r
97                         return true;\r
98                     },\r
99                     success: packet.onSuccess,\r
100                     error: function(xhr, reason, exception)\r
101                     {\r
102                         packet.onError(reason, exception);\r
103                     }\r
104                 });\r
105             };\r
106 \r
107             return that;\r
108         }\r
109 \r
110         $.Cometd = function(name)\r
111         {\r
112             var cometd = new org_cometd.Cometd(name);\r
113 \r
114             // Registration order is important\r
115             if (org_cometd.WebSocket)\r
116             {\r
117                 cometd.registerTransport('websocket', new org_cometd.WebSocketTransport());\r
118             }\r
119             cometd.registerTransport('long-polling', new LongPollingTransport());\r
120             cometd.registerTransport('callback-polling', new CallbackPollingTransport());\r
121 \r
122             return cometd;\r
123         };\r
124 \r
125         // The default cometd instance\r
126         $.cometd = new $.Cometd();\r
127 \r
128         return $.cometd;\r
129     }\r
130 \r
131     if (typeof define === 'function' && define.amd)\r
132     {\r
133         define(['jquery', 'org/cometd'], bind);\r
134     }\r
135     else\r
136     {\r
137         bind($, org.cometd);\r
138     }\r
139 })(jQuery);\r