Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / components / learnboost-engine.io-client / lib / transports / polling-jsonp.js
1
2 /**
3  * Module requirements.
4  */
5
6 var Polling = require('./polling')
7   , util = require('../util');
8
9 /**
10  * Module exports.
11  */
12
13 module.exports = JSONPPolling;
14
15 /**
16  * Global reference.
17  */
18
19 var global = 'undefined' != typeof window ? window : global;
20
21 /**
22  * Cached regular expressions.
23  */
24
25 var rNewline = /\n/g;
26
27 /**
28  * Global JSONP callbacks.
29  */
30
31 var callbacks;
32
33 /**
34  * Callbacks count.
35  */
36
37 var index = 0;
38
39 /**
40  * Noop.
41  */
42
43 function empty () { }
44
45 /**
46  * JSONP Polling constructor.
47  *
48  * @param {Object} opts.
49  * @api public
50  */
51
52 function JSONPPolling (opts) {
53   Polling.call(this, opts);
54
55   // define global callbacks array if not present
56   // we do this here (lazily) to avoid unneeded global pollution
57   if (!callbacks) {
58     // we need to consider multiple engines in the same page
59     if (!global.___eio) global.___eio = [];
60     callbacks = global.___eio;
61   }
62
63   // callback identifier
64   this.index = callbacks.length;
65
66   // add callback to jsonp global
67   var self = this;
68   callbacks.push(function (msg) {
69     self.onData(msg);
70   });
71
72   // append to query string
73   this.query.j = this.index;
74 };
75
76 /**
77  * Inherits from Polling.
78  */
79
80 util.inherits(JSONPPolling, Polling);
81
82 /**
83  * Opens the socket.
84  *
85  * @api private
86  */
87
88 JSONPPolling.prototype.doOpen = function () {
89   var self = this;
90   util.defer(function () {
91     Polling.prototype.doOpen.call(self);
92   });
93 };
94
95 /**
96  * Closes the socket
97  *
98  * @api private
99  */
100
101 JSONPPolling.prototype.doClose = function () {
102   if (this.script) {
103     this.script.parentNode.removeChild(this.script);
104     this.script = null;
105   }
106
107   if (this.form) {
108     this.form.parentNode.removeChild(this.form);
109     this.form = null;
110   }
111
112   Polling.prototype.doClose.call(this);
113 };
114
115 /**
116  * Starts a poll cycle.
117  *
118  * @api private
119  */
120
121 JSONPPolling.prototype.doPoll = function () {
122   var script = document.createElement('script');
123
124   if (this.script) {
125     this.script.parentNode.removeChild(this.script);
126     this.script = null;
127   }
128
129   script.async = true;
130   script.src = this.uri();
131
132   var insertAt = document.getElementsByTagName('script')[0];
133   insertAt.parentNode.insertBefore(script, insertAt);
134   this.script = script;
135
136   if (util.ua.gecko) {
137     setTimeout(function () {
138       var iframe = document.createElement('iframe');
139       document.body.appendChild(iframe);
140       document.body.removeChild(iframe);
141     }, 100);
142   }
143 };
144
145 /**
146  * Writes with a hidden iframe.
147  *
148  * @param {String} data to send
149  * @param {Function} called upon flush.
150  * @api private
151  */
152
153 JSONPPolling.prototype.doWrite = function (data, fn) {
154   var self = this;
155
156   if (!this.form) {
157     var form = document.createElement('form')
158       , area = document.createElement('textarea')
159       , id = this.iframeId = 'eio_iframe_' + this.index
160       , iframe;
161
162     form.className = 'socketio';
163     form.style.position = 'absolute';
164     form.style.top = '-1000px';
165     form.style.left = '-1000px';
166     form.target = id;
167     form.method = 'POST';
168     form.setAttribute('accept-charset', 'utf-8');
169     area.name = 'd';
170     form.appendChild(area);
171     document.body.appendChild(form);
172
173     this.form = form;
174     this.area = area;
175   }
176
177   this.form.action = this.uri();
178
179   function complete () {
180     initIframe();
181     fn();
182   };
183
184   function initIframe () {
185     if (self.iframe) {
186       self.form.removeChild(self.iframe);
187     }
188
189     try {
190       // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
191       iframe = document.createElement('<iframe name="'+ self.iframeId +'">');
192     } catch (e) {
193       iframe = document.createElement('iframe');
194       iframe.name = self.iframeId;
195     }
196
197     iframe.id = self.iframeId;
198
199     self.form.appendChild(iframe);
200     self.iframe = iframe;
201   };
202
203   initIframe();
204
205   // escape \n to prevent it from being converted into \r\n by some UAs
206   this.area.value = data.replace(rNewline, '\\n');
207
208   try {
209     this.form.submit();
210   } catch(e) {}
211
212   if (this.iframe.attachEvent) {
213     this.iframe.onreadystatechange = function(){
214       if (self.iframe.readyState == 'complete') {
215         complete();
216       }
217     };
218   } else {
219     this.iframe.onload = complete;
220   }
221 };