Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / transports / jsonp-polling.js
1
2 /**
3  * socket.io
4  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5  * MIT Licensed
6  */
7
8 (function (exports, io, global) {
9   /**
10    * There is a way to hide the loading indicator in Firefox. If you create and
11    * remove a iframe it will stop showing the current loading indicator.
12    * Unfortunately we can't feature detect that and UA sniffing is evil.
13    *
14    * @api private
15    */
16
17   var indicator = global.document && "MozAppearance" in
18     global.document.documentElement.style;
19
20   /**
21    * Expose constructor.
22    */
23
24   exports['jsonp-polling'] = JSONPPolling;
25
26   /**
27    * The JSONP transport creates an persistent connection by dynamically
28    * inserting a script tag in the page. This script tag will receive the
29    * information of the Socket.IO server. When new information is received
30    * it creates a new script tag for the new data stream.
31    *
32    * @constructor
33    * @extends {io.Transport.xhr-polling}
34    * @api public
35    */
36
37   function JSONPPolling (socket) {
38     io.Transport['xhr-polling'].apply(this, arguments);
39
40     this.index = io.j.length;
41
42     var self = this;
43
44     io.j.push(function (msg) {
45       self._(msg);
46     });
47   };
48
49   /**
50    * Inherits from XHR polling transport.
51    */
52
53   io.util.inherit(JSONPPolling, io.Transport['xhr-polling']);
54
55   /**
56    * Transport name
57    *
58    * @api public
59    */
60
61   JSONPPolling.prototype.name = 'jsonp-polling';
62
63   /**
64    * Posts a encoded message to the Socket.IO server using an iframe.
65    * The iframe is used because script tags can create POST based requests.
66    * The iframe is positioned outside of the view so the user does not
67    * notice it's existence.
68    *
69    * @param {String} data A encoded message.
70    * @api private
71    */
72
73   JSONPPolling.prototype.post = function (data) {
74     var self = this
75       , query = io.util.query(
76              this.socket.options.query
77           , 't='+ (+new Date) + '&i=' + this.index
78         );
79
80     if (!this.form) {
81       var form = document.createElement('form')
82         , area = document.createElement('textarea')
83         , id = this.iframeId = 'socketio_iframe_' + this.index
84         , iframe;
85
86       form.className = 'socketio';
87       form.style.position = 'absolute';
88       form.style.top = '0px';
89       form.style.left = '0px';
90       form.style.display = 'none';
91       form.target = id;
92       form.method = 'POST';
93       form.setAttribute('accept-charset', 'utf-8');
94       area.name = 'd';
95       form.appendChild(area);
96       document.body.appendChild(form);
97
98       this.form = form;
99       this.area = area;
100     }
101
102     this.form.action = this.prepareUrl() + query;
103
104     function complete () {
105       initIframe();
106       self.socket.setBuffer(false);
107     };
108
109     function initIframe () {
110       if (self.iframe) {
111         self.form.removeChild(self.iframe);
112       }
113
114       try {
115         // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
116         iframe = document.createElement('<iframe name="'+ self.iframeId +'">');
117       } catch (e) {
118         iframe = document.createElement('iframe');
119         iframe.name = self.iframeId;
120       }
121
122       iframe.id = self.iframeId;
123
124       self.form.appendChild(iframe);
125       self.iframe = iframe;
126     };
127
128     initIframe();
129
130     // we temporarily stringify until we figure out how to prevent
131     // browsers from turning `\n` into `\r\n` in form inputs
132     this.area.value = io.JSON.stringify(data);
133
134     try {
135       this.form.submit();
136     } catch(e) {}
137
138     if (this.iframe.attachEvent) {
139       iframe.onreadystatechange = function () {
140         if (self.iframe.readyState == 'complete') {
141           complete();
142         }
143       };
144     } else {
145       this.iframe.onload = complete;
146     }
147
148     this.socket.setBuffer(true);
149   };
150
151   /**
152    * Creates a new JSONP poll that can be used to listen
153    * for messages from the Socket.IO server.
154    *
155    * @api private
156    */
157
158   JSONPPolling.prototype.get = function () {
159     var self = this
160       , script = document.createElement('script')
161       , query = io.util.query(
162              this.socket.options.query
163           , 't='+ (+new Date) + '&i=' + this.index
164         );
165
166     if (this.script) {
167       this.script.parentNode.removeChild(this.script);
168       this.script = null;
169     }
170
171     script.async = true;
172     script.src = this.prepareUrl() + query;
173     script.onerror = function () {
174       self.onClose();
175     };
176
177     var insertAt = document.getElementsByTagName('script')[0];
178     insertAt.parentNode.insertBefore(script, insertAt);
179     this.script = script;
180
181     if (indicator) {
182       setTimeout(function () {
183         var iframe = document.createElement('iframe');
184         document.body.appendChild(iframe);
185         document.body.removeChild(iframe);
186       }, 100);
187     }
188   };
189
190   /**
191    * Callback function for the incoming message stream from the Socket.IO server.
192    *
193    * @param {String} data The message
194    * @api private
195    */
196
197   JSONPPolling.prototype._ = function (msg) {
198     this.onData(msg);
199     if (this.isOpen) {
200       this.get();
201     }
202     return this;
203   };
204
205   /**
206    * The indicator hack only works after onload
207    *
208    * @param {Socket} socket The socket instance that needs a transport
209    * @param {Function} fn The callback
210    * @api private
211    */
212
213   JSONPPolling.prototype.ready = function (socket, fn) {
214     var self = this;
215     if (!indicator) return fn.call(this);
216
217     io.util.load(function () {
218       fn.call(self);
219     });
220   };
221
222   /**
223    * Checks if browser supports this transport.
224    *
225    * @return {Boolean}
226    * @api public
227    */
228
229   JSONPPolling.check = function () {
230     return 'document' in global;
231   };
232
233   /**
234    * Check if cross domain requests are supported
235    *
236    * @returns {Boolean}
237    * @api public
238    */
239
240   JSONPPolling.xdomainCheck = function () {
241     return true;
242   };
243
244   /**
245    * Add the transport to your public io.transports array.
246    *
247    * @api private
248    */
249
250   io.transports.push('jsonp-polling');
251
252 })(
253     'undefined' != typeof io ? io.Transport : module.exports
254   , 'undefined' != typeof io ? io : module.parent.exports
255   , this
256 );