|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('io-base', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Base IO functionality. Provides basic XHR transport support. |
|
|
12 |
* @module io |
|
|
13 |
* @submodule io-base |
|
|
14 |
*/ |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* The io class is a utility that brokers HTTP requests through a simplified |
|
|
18 |
* interface. Specifically, it allows JavaScript to make HTTP requests to |
|
|
19 |
* a resource without a page reload. The underlying transport for making |
|
|
20 |
* same-domain requests is the XMLHttpRequest object. YUI.io can also use |
|
|
21 |
* Flash, if specified as a transport, for cross-domain requests. |
|
|
22 |
* |
|
|
23 |
* @class io |
|
|
24 |
*/ |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* @event io:start |
|
|
28 |
* @description This event is fired by YUI.io when a transaction is initiated. |
|
|
29 |
* @type Event Custom |
|
|
30 |
*/ |
|
|
31 |
var E_START = 'io:start', |
|
|
32 |
|
|
|
33 |
/** |
|
|
34 |
* @event io:complete |
|
|
35 |
* @description This event is fired by YUI.io when a transaction is complete. |
|
|
36 |
* Response status and data are accessible, if available. |
|
|
37 |
* @type Event Custom |
|
|
38 |
*/ |
|
|
39 |
E_COMPLETE = 'io:complete', |
|
|
40 |
|
|
|
41 |
/** |
|
|
42 |
* @event io:success |
|
|
43 |
* @description This event is fired by YUI.io when a transaction is complete, and |
|
|
44 |
* the HTTP status resolves to HTTP2xx. |
|
|
45 |
* @type Event Custom |
|
|
46 |
*/ |
|
|
47 |
E_SUCCESS = 'io:success', |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* @event io:failure |
|
|
51 |
* @description This event is fired by YUI.io when a transaction is complete, and |
|
|
52 |
* the HTTP status resolves to HTTP4xx, 5xx and above. |
|
|
53 |
* @type Event Custom |
|
|
54 |
*/ |
|
|
55 |
E_FAILURE = 'io:failure', |
|
|
56 |
|
|
|
57 |
/** |
|
|
58 |
* @event io:end |
|
|
59 |
* @description This event signifies the end of the transaction lifecycle. The |
|
|
60 |
* transaction transport is destroyed. |
|
|
61 |
* @type Event Custom |
|
|
62 |
*/ |
|
|
63 |
E_END = 'io:end', |
|
|
64 |
|
|
|
65 |
//-------------------------------------- |
|
|
66 |
// Properties |
|
|
67 |
//-------------------------------------- |
|
|
68 |
/** |
|
|
69 |
* @description A transaction counter that increments for each transaction. |
|
|
70 |
* |
|
|
71 |
* @property transactionId |
|
|
72 |
* @private |
|
|
73 |
* @static |
|
|
74 |
* @type int |
|
|
75 |
*/ |
|
|
76 |
transactionId = 0, |
|
|
77 |
|
|
|
78 |
/** |
|
|
79 |
* @description Object of default HTTP headers to be initialized and sent |
|
|
80 |
* for all transactions. |
|
|
81 |
* |
|
|
82 |
* @property _headers |
|
|
83 |
* @private |
|
|
84 |
* @static |
|
|
85 |
* @type object |
|
|
86 |
*/ |
|
|
87 |
_headers = { |
|
|
88 |
'X-Requested-With' : 'XMLHttpRequest' |
|
|
89 |
}, |
|
|
90 |
|
|
|
91 |
/** |
|
|
92 |
* @description Object that stores timeout values for any transaction with |
|
|
93 |
* a defined "timeout" configuration property. |
|
|
94 |
* |
|
|
95 |
* @property _timeOut |
|
|
96 |
* @private |
|
|
97 |
* @static |
|
|
98 |
* @type object |
|
|
99 |
*/ |
|
|
100 |
_timeout = {}, |
|
|
101 |
|
|
|
102 |
// Window reference |
|
|
103 |
w = Y.config.win; |
|
|
104 |
|
|
|
105 |
//-------------------------------------- |
|
|
106 |
// Methods |
|
|
107 |
//-------------------------------------- |
|
|
108 |
/** |
|
|
109 |
* @description Method for requesting a transaction. _io() is implemented as |
|
|
110 |
* yui.io(). Each transaction may include a configuration object. Its |
|
|
111 |
* properties are: |
|
|
112 |
* |
|
|
113 |
* method: HTTP method verb (e.g., GET or POST). If this property is not |
|
|
114 |
* not defined, the default value will be GET. |
|
|
115 |
* |
|
|
116 |
* data: This is the name-value string that will be sent as the transaction |
|
|
117 |
* data. If the request is HTTP GET, the data become part of |
|
|
118 |
* querystring. If HTTP POST, the data are sent in the message body. |
|
|
119 |
* |
|
|
120 |
* xdr: Defines the transport to be used for cross-domain requests. By |
|
|
121 |
* setting this property, the transaction will use the specified |
|
|
122 |
* transport instead of XMLHttpRequest. Currently, the only alternate |
|
|
123 |
* transport supported is Flash (e.g., { xdr: 'flash' }). |
|
|
124 |
* |
|
|
125 |
* form: This is a defined object used to process HTML form as data. The |
|
|
126 |
* properties are: |
|
|
127 |
* { |
|
|
128 |
* id: object, //HTML form object or id of HTML form |
|
|
129 |
* useDisabled: boolean, //Allow disabled HTML form field values |
|
|
130 |
* to be sent as part of the data. |
|
|
131 |
* } |
|
|
132 |
* |
|
|
133 |
* on: This is a defined object used to create and handle specific |
|
|
134 |
* events during a transaction lifecycle. These events will fire in |
|
|
135 |
* addition to the global io events. The events are: |
|
|
136 |
* start - This event is fired when a request is sent to a resource. |
|
|
137 |
* complete - This event fires when the transaction is complete. |
|
|
138 |
* success - This event fires when the response status resolves to |
|
|
139 |
* HTTP 2xx. |
|
|
140 |
* failure - This event fires when the response status resolves to |
|
|
141 |
* HTTP 4xx, 5xx; and, for all transaction exceptions, |
|
|
142 |
* including aborted transactions and transaction timeouts. |
|
|
143 |
* end - This even is fired at the conclusion of the transaction |
|
|
144 |
* lifecycle, after a success or failure resolution. |
|
|
145 |
* |
|
|
146 |
* The properties are: |
|
|
147 |
* { |
|
|
148 |
* start: function(id, args){}, |
|
|
149 |
* complete: function(id, responseobject, args){}, |
|
|
150 |
* success: function(id, responseobject, args){}, |
|
|
151 |
* failure: function(id, responseobject, args){}, |
|
|
152 |
* end: function(id, args){} |
|
|
153 |
* } |
|
|
154 |
* Each property can reference a function or be written as an |
|
|
155 |
* inline function. |
|
|
156 |
* |
|
|
157 |
* context: Object reference for an event handler when it is implemented |
|
|
158 |
* as a method of a base object. Defining "context" will preserve |
|
|
159 |
* the proper reference of "this" used in the event handler. |
|
|
160 |
* headers: This is a defined object of client headers, as many as. |
|
|
161 |
* desired for the transaction. These headers are sentThe object |
|
|
162 |
* pattern is: |
|
|
163 |
* { |
|
|
164 |
* header: value |
|
|
165 |
* } |
|
|
166 |
* |
|
|
167 |
* timeout: This value, defined as milliseconds, is a time threshold for the |
|
|
168 |
* transaction. When this threshold is reached, and the transaction's |
|
|
169 |
* Complete event has not yet fired, the transaction will be aborted. |
|
|
170 |
* arguments: Object, array, string, or number passed to all registered |
|
|
171 |
* event handlers. This value is available as the second |
|
|
172 |
* argument in the "start" and "abort" event handlers; and, it is |
|
|
173 |
* the third argument in the "complete", "success", and "failure" |
|
|
174 |
* event handlers. |
|
|
175 |
* |
|
|
176 |
* @method _io |
|
|
177 |
* @private |
|
|
178 |
* @static |
|
|
179 |
* @param {string} uri - qualified path to transaction resource. |
|
|
180 |
* @param {object} c - configuration object for the transaction. |
|
|
181 |
* @return object |
|
|
182 |
*/ |
|
|
183 |
function _io(uri, c) { |
|
|
184 |
var u, f, |
|
|
185 |
// Set default value of argument c to Object if |
|
|
186 |
// configuration object "c" does not exist. |
|
|
187 |
c = c || {}, |
|
|
188 |
o = _create((arguments.length === 3) ? arguments[2] : null, c), |
|
|
189 |
m = (c.method) ? c.method.toUpperCase() : 'GET', |
|
|
190 |
d = (c.data) ? c.data : null; |
|
|
191 |
|
|
|
192 |
o.abort = function () { |
|
|
193 |
c.xdr ? o.c.abort(o.id, c) : _ioCancel(o, 'abort'); |
|
|
194 |
}; |
|
|
195 |
o.isInProgress = function() { |
|
|
196 |
var s = (c.xdr) ? _isInProgress(o) : (o.c.readyState !== 4 && o.c.readyState !== 0); |
|
|
197 |
return s; |
|
|
198 |
}; |
|
|
199 |
|
|
|
200 |
/* Determine configuration properties */ |
|
|
201 |
// If config.form is defined, perform data operations. |
|
|
202 |
if (c.form) { |
|
|
203 |
|
|
|
204 |
if (c.form.upload) { |
|
|
205 |
u = Y.io._upload(o, uri, c); |
|
|
206 |
return u; |
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
// Serialize the HTML form into a string of name-value pairs. |
|
|
210 |
f = Y.io._serialize(c.form); |
|
|
211 |
// If config.data is defined, concatenate the data to the form string. |
|
|
212 |
if (d) { |
|
|
213 |
f += "&" + d; |
|
|
214 |
} |
|
|
215 |
|
|
|
216 |
if (m === 'POST') { |
|
|
217 |
d = f; |
|
|
218 |
_setHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|
|
219 |
} |
|
|
220 |
else if (m === 'GET') { |
|
|
221 |
uri = _concat(uri, f); |
|
|
222 |
} |
|
|
223 |
} |
|
|
224 |
else if (d && m === 'POST') { |
|
|
225 |
_setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
if (c.xdr) { |
|
|
229 |
Y.io._xdr(uri, o, c); |
|
|
230 |
return o; |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
// If config.timeout is defined, and the request is standard XHR, |
|
|
234 |
// initialize timeout polling. |
|
|
235 |
if (c.timeout) { |
|
|
236 |
_startTimeout(o, c.timeout); |
|
|
237 |
} |
|
|
238 |
/* End Configuration Properties */ |
|
|
239 |
|
|
|
240 |
o.c.onreadystatechange = function() { _readyState(o, c); }; |
|
|
241 |
try { _open(o.c, m, uri); } catch (e0) {} |
|
|
242 |
_setHeaders(o.c, (c.headers || {})); |
|
|
243 |
|
|
|
244 |
// Do not pass null, in the absence of data, as this |
|
|
245 |
// will result in a POST request with no Content-Length |
|
|
246 |
// defined. |
|
|
247 |
_async(o, (d || ''), c); |
|
|
248 |
|
|
|
249 |
return o; |
|
|
250 |
} |
|
|
251 |
|
|
|
252 |
/** |
|
|
253 |
* @description Method for creating and subscribing transaction events. |
|
|
254 |
* |
|
|
255 |
* @method _tPubSub |
|
|
256 |
* @private |
|
|
257 |
* @static |
|
|
258 |
* @param {string} e - event to be published |
|
|
259 |
* @param {object} c - configuration data subset for event subscription. |
|
|
260 |
* |
|
|
261 |
* @return void |
|
|
262 |
*/ |
|
|
263 |
function _tPubSub(e, c){ |
|
|
264 |
var event = new Y.EventTarget().publish('transaction:' + e); |
|
|
265 |
event.subscribe(c.on[e], (c.context || this), c.arguments); |
|
|
266 |
|
|
|
267 |
return event; |
|
|
268 |
} |
|
|
269 |
|
|
|
270 |
/** |
|
|
271 |
* @description Fires event "io:start" and creates, fires a |
|
|
272 |
* transaction-specific start event, if config.on.start is |
|
|
273 |
* defined. |
|
|
274 |
* |
|
|
275 |
* @method _ioStart |
|
|
276 |
* @private |
|
|
277 |
* @static |
|
|
278 |
* @param {number} id - transaction id |
|
|
279 |
* @param {object} c - configuration object for the transaction. |
|
|
280 |
* |
|
|
281 |
* @return void |
|
|
282 |
*/ |
|
|
283 |
function _ioStart(id, c) { |
|
|
284 |
var m = Y.io._fn || {}, |
|
|
285 |
fn = (m && m[id]) ? m[id] : null, |
|
|
286 |
event; |
|
|
287 |
// Set default value of argument c, property "on" to Object if |
|
|
288 |
// the property is null or undefined. |
|
|
289 |
c.on = c.on || {}; |
|
|
290 |
|
|
|
291 |
if (fn) { |
|
|
292 |
c.on.start = fn.start; |
|
|
293 |
} |
|
|
294 |
|
|
|
295 |
Y.fire(E_START, id); |
|
|
296 |
|
|
|
297 |
if (c.on.start) { |
|
|
298 |
event = _tPubSub('start', c); |
|
|
299 |
event.fire(id); |
|
|
300 |
} |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
|
|
|
304 |
/** |
|
|
305 |
* @description Fires event "io:complete" and creates, fires a |
|
|
306 |
* transaction-specific "complete" event, if config.on.complete is |
|
|
307 |
* defined. |
|
|
308 |
* |
|
|
309 |
* @method _ioComplete |
|
|
310 |
* @private |
|
|
311 |
* @static |
|
|
312 |
* @param {object} o - transaction object. |
|
|
313 |
* @param {object} c - configuration object for the transaction. |
|
|
314 |
* |
|
|
315 |
* @return void |
|
|
316 |
*/ |
|
|
317 |
function _ioComplete(o, c) { |
|
|
318 |
var r, event; |
|
|
319 |
// Set default value of argument c, property "on" to Object if |
|
|
320 |
// the property is null or undefined. |
|
|
321 |
c.on = c.on || {}; |
|
|
322 |
|
|
|
323 |
r = (o.status) ? _response(o.status) : o.c; |
|
|
324 |
Y.fire(E_COMPLETE, o.id, r); |
|
|
325 |
|
|
|
326 |
if (c.on.complete) { |
|
|
327 |
event = _tPubSub('complete', c); |
|
|
328 |
event.fire(o.id, r); |
|
|
329 |
} |
|
|
330 |
} |
|
|
331 |
|
|
|
332 |
/** |
|
|
333 |
* @description Fires event "io:success" and creates, fires a |
|
|
334 |
* transaction-specific "success" event, if config.on.success is |
|
|
335 |
* defined. |
|
|
336 |
* |
|
|
337 |
* @method _ioSuccess |
|
|
338 |
* @private |
|
|
339 |
* @static |
|
|
340 |
* @param {object} o - transaction object. |
|
|
341 |
* @param {object} c - configuration object for the transaction. |
|
|
342 |
* |
|
|
343 |
* @return void |
|
|
344 |
*/ |
|
|
345 |
function _ioSuccess(o, c) { |
|
|
346 |
var m = Y.io._fn || {}, |
|
|
347 |
fn = (m && m[o.id]) ? m[o.id] : null, |
|
|
348 |
event; |
|
|
349 |
// Set default value of argument c, property "on" to Object if |
|
|
350 |
// the property is null or undefined. |
|
|
351 |
c.on = c.on || {}; |
|
|
352 |
|
|
|
353 |
if (fn) { |
|
|
354 |
c.on.success = fn.success; |
|
|
355 |
//Decode the response from IO.swf |
|
|
356 |
o.c.responseText = decodeURI(o.c.responseText); |
|
|
357 |
} |
|
|
358 |
|
|
|
359 |
Y.fire(E_SUCCESS, o.id, o.c); |
|
|
360 |
|
|
|
361 |
if (c.on.success) { |
|
|
362 |
event = _tPubSub('success', c); |
|
|
363 |
event.fire(o.id, o.c); |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
_ioEnd(o, c); |
|
|
367 |
} |
|
|
368 |
|
|
|
369 |
/** |
|
|
370 |
* @description Fires event "io:failure" and creates, fires a |
|
|
371 |
* transaction-specific "failure" event, if config.on.failure is |
|
|
372 |
* defined. |
|
|
373 |
* |
|
|
374 |
* @method _ioFailure |
|
|
375 |
* @private |
|
|
376 |
* @static |
|
|
377 |
* @param {object} o - transaction object. |
|
|
378 |
* @param {object} c - configuration object for the transaction. |
|
|
379 |
* |
|
|
380 |
* @return void |
|
|
381 |
*/ |
|
|
382 |
function _ioFailure(o, c) { |
|
|
383 |
var m = Y.io._fn || {}, |
|
|
384 |
fn = (m && m[o.id]) ? m[o.id] : null, |
|
|
385 |
r, event; |
|
|
386 |
// Set default value of argument c, property "on" to Object if |
|
|
387 |
// the property is null or undefined. |
|
|
388 |
c.on = c.on || {}; |
|
|
389 |
|
|
|
390 |
if (fn) { |
|
|
391 |
c.on.failure = fn.failure; |
|
|
392 |
//Decode the response from IO.swf |
|
|
393 |
o.c.responseText = decodeURI(o.c.responseText); |
|
|
394 |
} |
|
|
395 |
|
|
|
396 |
r = (o.status) ? _response(o.status) : o.c; |
|
|
397 |
Y.fire(E_FAILURE, o.id, r); |
|
|
398 |
|
|
|
399 |
if (c.on.failure) { |
|
|
400 |
event = _tPubSub('failure', c); |
|
|
401 |
event.fire(o.id, r); |
|
|
402 |
} |
|
|
403 |
|
|
|
404 |
_ioEnd(o, c); |
|
|
405 |
} |
|
|
406 |
|
|
|
407 |
/** |
|
|
408 |
* @description Fires event "io:end" and creates, fires a |
|
|
409 |
* transaction-specific "end" event, if config.on.end is |
|
|
410 |
* defined. |
|
|
411 |
* |
|
|
412 |
* @method _ioEnd |
|
|
413 |
* @private |
|
|
414 |
* @static |
|
|
415 |
* @param {object} o - transaction object. |
|
|
416 |
* @param {object} c - configuration object for the transaction. |
|
|
417 |
* |
|
|
418 |
* @return void |
|
|
419 |
*/ |
|
|
420 |
function _ioEnd(o, c) { |
|
|
421 |
var m = Y.io._fn || {}, |
|
|
422 |
fn = (m && m[o.id]) ? m[o.id] : null, |
|
|
423 |
event; |
|
|
424 |
// Set default value of argument c, property "on" to Object if |
|
|
425 |
// the property is null or undefined. |
|
|
426 |
c.on = c.on || {}; |
|
|
427 |
|
|
|
428 |
if (fn) { |
|
|
429 |
c.on.end = fn.end; |
|
|
430 |
delete m[o.id]; |
|
|
431 |
} |
|
|
432 |
|
|
|
433 |
Y.fire(E_END, o.id); |
|
|
434 |
|
|
|
435 |
if (c.on.end) { |
|
|
436 |
event = _tPubSub('end', c); |
|
|
437 |
event.fire(o.id); |
|
|
438 |
} |
|
|
439 |
|
|
|
440 |
_destroy(o, (c.xdr) ? true : false ); |
|
|
441 |
} |
|
|
442 |
|
|
|
443 |
/** |
|
|
444 |
* @description Terminates a transaction due to an explicit abort or |
|
|
445 |
* timeout. |
|
|
446 |
* |
|
|
447 |
* @method _ioCancel |
|
|
448 |
* @private |
|
|
449 |
* @static |
|
|
450 |
* @param {object} o - Transaction object generated by _create(). |
|
|
451 |
* @param {object} c - Configuration object passed to YUI.io(). |
|
|
452 |
* @param {string} s - Identifies timed out or aborted transaction. |
|
|
453 |
* |
|
|
454 |
* @return void |
|
|
455 |
*/ |
|
|
456 |
function _ioCancel(o, s) { |
|
|
457 |
if (o && o.c) { |
|
|
458 |
o.status = s; |
|
|
459 |
o.c.abort(); |
|
|
460 |
} |
|
|
461 |
} |
|
|
462 |
|
|
|
463 |
/** |
|
|
464 |
* @description Determines if a cross-domain transaction is still |
|
|
465 |
* in progress. |
|
|
466 |
* |
|
|
467 |
* @method _isInProgress |
|
|
468 |
* @private |
|
|
469 |
* @static |
|
|
470 |
* @param {object} o - Transaction object generated by _create(). |
|
|
471 |
* |
|
|
472 |
* @return boolean |
|
|
473 |
*/ |
|
|
474 |
function _isInProgress(o) { |
|
|
475 |
return o.c.isInProgress(o.id); |
|
|
476 |
} |
|
|
477 |
|
|
|
478 |
function _response(s) { |
|
|
479 |
return { status:0, statusText:s }; |
|
|
480 |
} |
|
|
481 |
|
|
|
482 |
/** |
|
|
483 |
* @description Method that increments _transactionId for each transaction. |
|
|
484 |
* |
|
|
485 |
* @method _id |
|
|
486 |
* @private |
|
|
487 |
* @static |
|
|
488 |
* @return int |
|
|
489 |
*/ |
|
|
490 |
function _id() { |
|
|
491 |
var id = transactionId; |
|
|
492 |
transactionId++; |
|
|
493 |
|
|
|
494 |
return id; |
|
|
495 |
} |
|
|
496 |
|
|
|
497 |
/** |
|
|
498 |
* @description Method that creates a unique transaction object for each |
|
|
499 |
* request. |
|
|
500 |
* |
|
|
501 |
* @method _create |
|
|
502 |
* @private |
|
|
503 |
* @static |
|
|
504 |
* @param {number} s - URI or root data. |
|
|
505 |
* @param {number} c - configuration object |
|
|
506 |
* @return object |
|
|
507 |
*/ |
|
|
508 |
function _create(i, c) { |
|
|
509 |
var o = {}; |
|
|
510 |
o.id = Y.Lang.isNumber(i) ? i : _id(); |
|
|
511 |
|
|
|
512 |
if (c.xdr) { |
|
|
513 |
o.c = Y.io._transport[c.xdr.use]; |
|
|
514 |
} |
|
|
515 |
else if (c.form && c.form.upload) { |
|
|
516 |
o.c = {}; |
|
|
517 |
} |
|
|
518 |
else { |
|
|
519 |
o.c = _xhr(); |
|
|
520 |
} |
|
|
521 |
|
|
|
522 |
return o; |
|
|
523 |
}; |
|
|
524 |
|
|
|
525 |
/** |
|
|
526 |
* @description Method that creates the XMLHttpRequest transport |
|
|
527 |
* |
|
|
528 |
* @method _xhr |
|
|
529 |
* @private |
|
|
530 |
* @static |
|
|
531 |
* @return object |
|
|
532 |
*/ |
|
|
533 |
function _xhr() { |
|
|
534 |
return (w.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); |
|
|
535 |
} |
|
|
536 |
|
|
|
537 |
/** |
|
|
538 |
* @description Method that concatenates string data for HTTP GET transactions. |
|
|
539 |
* |
|
|
540 |
* @method _concat |
|
|
541 |
* @private |
|
|
542 |
* @static |
|
|
543 |
* @param {string} s - URI or root data. |
|
|
544 |
* @param {string} d - data to be concatenated onto URI. |
|
|
545 |
* @return int |
|
|
546 |
*/ |
|
|
547 |
function _concat(s, d) { |
|
|
548 |
s += ((s.indexOf('?') == -1) ? '?' : '&') + d; |
|
|
549 |
return s; |
|
|
550 |
} |
|
|
551 |
|
|
|
552 |
/** |
|
|
553 |
* @description Method that stores default client headers for all transactions. |
|
|
554 |
* If a label is passed with no value argument, the header will be deleted. |
|
|
555 |
* |
|
|
556 |
* @method _setHeader |
|
|
557 |
* @private |
|
|
558 |
* @static |
|
|
559 |
* @param {string} l - HTTP header |
|
|
560 |
* @param {string} v - HTTP header value |
|
|
561 |
* @return int |
|
|
562 |
*/ |
|
|
563 |
function _setHeader(l, v) { |
|
|
564 |
if (v) { |
|
|
565 |
_headers[l] = v; |
|
|
566 |
} |
|
|
567 |
else { |
|
|
568 |
delete _headers[l]; |
|
|
569 |
} |
|
|
570 |
} |
|
|
571 |
|
|
|
572 |
/** |
|
|
573 |
* @description Method that sets all HTTP headers to be sent in a transaction. |
|
|
574 |
* |
|
|
575 |
* @method _setHeaders |
|
|
576 |
* @private |
|
|
577 |
* @static |
|
|
578 |
* @param {object} o - XHR instance for the specific transaction. |
|
|
579 |
* @param {object} h - HTTP headers for the specific transaction, as defined |
|
|
580 |
* in the configuration object passed to YUI.io(). |
|
|
581 |
* @return void |
|
|
582 |
*/ |
|
|
583 |
function _setHeaders(o, h) { |
|
|
584 |
var p; |
|
|
585 |
|
|
|
586 |
for (p in _headers) { |
|
|
587 |
if (_headers.hasOwnProperty(p)) { |
|
|
588 |
if (h[p]) { |
|
|
589 |
// Configuration headers will supersede IO preset headers, |
|
|
590 |
// if headers match. |
|
|
591 |
break; |
|
|
592 |
} |
|
|
593 |
else { |
|
|
594 |
h[p] = _headers[p]; |
|
|
595 |
} |
|
|
596 |
} |
|
|
597 |
} |
|
|
598 |
|
|
|
599 |
for (p in h) { |
|
|
600 |
if (h.hasOwnProperty(p)) { |
|
|
601 |
o.setRequestHeader(p, h[p]); |
|
|
602 |
} |
|
|
603 |
} |
|
|
604 |
} |
|
|
605 |
|
|
|
606 |
function _open(o, m, uri) { |
|
|
607 |
o.open(m, uri, true); |
|
|
608 |
} |
|
|
609 |
|
|
|
610 |
/** |
|
|
611 |
* @description Method that sends the transaction request. |
|
|
612 |
* |
|
|
613 |
* @method _async |
|
|
614 |
* @private |
|
|
615 |
* @static |
|
|
616 |
* @param {object} o - Transaction object generated by _create(). |
|
|
617 |
* @param {string} d - Transaction data. |
|
|
618 |
* @param {object} c - Configuration object passed to YUI.io(). |
|
|
619 |
* @return void |
|
|
620 |
*/ |
|
|
621 |
function _async(o, d, c) { |
|
|
622 |
o.c.send(d); |
|
|
623 |
_ioStart(o.id, c); |
|
|
624 |
} |
|
|
625 |
|
|
|
626 |
/** |
|
|
627 |
* @description Starts timeout count if the configuration object |
|
|
628 |
* has a defined timeout property. |
|
|
629 |
* |
|
|
630 |
* @method _startTimeout |
|
|
631 |
* @private |
|
|
632 |
* @static |
|
|
633 |
* @param {object} o - Transaction object generated by _create(). |
|
|
634 |
* @param {object} c - Configuration object passed to YUI.io(). |
|
|
635 |
* @return void |
|
|
636 |
*/ |
|
|
637 |
function _startTimeout(o, timeout) { |
|
|
638 |
_timeout[o.id] = w.setTimeout(function() { _ioCancel(o, 'timeout'); }, timeout); |
|
|
639 |
} |
|
|
640 |
|
|
|
641 |
/** |
|
|
642 |
* @description Clears the timeout interval started by _startTimeout(). |
|
|
643 |
* |
|
|
644 |
* @method _clearTimeout |
|
|
645 |
* @private |
|
|
646 |
* @static |
|
|
647 |
* @param {number} id - Transaction id. |
|
|
648 |
* @return void |
|
|
649 |
*/ |
|
|
650 |
function _clearTimeout(id) { |
|
|
651 |
w.clearTimeout(_timeout[id]); |
|
|
652 |
delete _timeout[id]; |
|
|
653 |
} |
|
|
654 |
|
|
|
655 |
/** |
|
|
656 |
* @description Event handler bound to onreadystatechange. |
|
|
657 |
* |
|
|
658 |
* @method _readyState |
|
|
659 |
* @private |
|
|
660 |
* @static |
|
|
661 |
* @param {object} o - Transaction object generated by _create(). |
|
|
662 |
* @param {object} c - Configuration object passed to YUI.io(). |
|
|
663 |
* @return void |
|
|
664 |
*/ |
|
|
665 |
function _readyState(o, c) { |
|
|
666 |
if (o.c.readyState === 4) { |
|
|
667 |
if (c.timeout) { |
|
|
668 |
_clearTimeout(o.id); |
|
|
669 |
} |
|
|
670 |
|
|
|
671 |
w.setTimeout( |
|
|
672 |
function() { |
|
|
673 |
_ioComplete(o, c); |
|
|
674 |
_handleResponse(o, c); |
|
|
675 |
}, 0); |
|
|
676 |
} |
|
|
677 |
} |
|
|
678 |
|
|
|
679 |
/** |
|
|
680 |
* @description Method that determines if a transaction response qualifies |
|
|
681 |
* as success or failure, based on the response HTTP status code, and |
|
|
682 |
* fires the appropriate success or failure events. |
|
|
683 |
* |
|
|
684 |
* @method _handleResponse |
|
|
685 |
* @private |
|
|
686 |
* @static |
|
|
687 |
* @param {object} o - Transaction object generated by _create(). |
|
|
688 |
* @param {object} c - Configuration object passed to io(). |
|
|
689 |
* @return void |
|
|
690 |
*/ |
|
|
691 |
function _handleResponse(o, c) { |
|
|
692 |
var status; |
|
|
693 |
try{ |
|
|
694 |
if (o.c.status && o.c.status !== 0) { |
|
|
695 |
status = o.c.status; |
|
|
696 |
} |
|
|
697 |
else { |
|
|
698 |
status = 0; |
|
|
699 |
} |
|
|
700 |
} |
|
|
701 |
catch(e1) { |
|
|
702 |
status = 0; |
|
|
703 |
} |
|
|
704 |
|
|
|
705 |
// IE reports HTTP 204 as HTTP 1223. |
|
|
706 |
// But, the response data are still available. |
|
|
707 |
if (status >= 200 && status < 300 || status === 1223) { |
|
|
708 |
_ioSuccess(o, c); |
|
|
709 |
} |
|
|
710 |
else { |
|
|
711 |
_ioFailure(o, c); |
|
|
712 |
} |
|
|
713 |
} |
|
|
714 |
|
|
|
715 |
function _destroy(o, isTransport) { |
|
|
716 |
// IE, when using XMLHttpRequest as an ActiveX Object, will throw |
|
|
717 |
// a "Type Mismatch" error if the event handler is set to "null". |
|
|
718 |
if(w.XMLHttpRequest && !isTransport) { |
|
|
719 |
if (o.c) { |
|
|
720 |
o.c.onreadystatechange = null; |
|
|
721 |
} |
|
|
722 |
} |
|
|
723 |
|
|
|
724 |
o.c = null; |
|
|
725 |
o = null; |
|
|
726 |
} |
|
|
727 |
|
|
|
728 |
_io.start = _ioStart; |
|
|
729 |
_io.complete = _ioComplete; |
|
|
730 |
_io.success = _ioSuccess; |
|
|
731 |
_io.failure = _ioFailure; |
|
|
732 |
_io.isInProgress = _isInProgress; |
|
|
733 |
_io._id = _id; |
|
|
734 |
|
|
|
735 |
//-------------------------------------- |
|
|
736 |
// Begin public interface definition |
|
|
737 |
//-------------------------------------- |
|
|
738 |
/** |
|
|
739 |
* @description Method that stores default client headers for all transactions. |
|
|
740 |
* If a label is passed with no value argument, the header will be deleted. |
|
|
741 |
* This is the interface for _setHeader(). |
|
|
742 |
* |
|
|
743 |
* @method header |
|
|
744 |
* @public |
|
|
745 |
* @static |
|
|
746 |
* @param {string} l - HTTP header |
|
|
747 |
* @param {string} v - HTTP header value |
|
|
748 |
* @return int |
|
|
749 |
*/ |
|
|
750 |
_io.header = _setHeader; |
|
|
751 |
|
|
|
752 |
/** |
|
|
753 |
* @description Method for requesting a transaction. This |
|
|
754 |
* is the interface for _io(). |
|
|
755 |
* |
|
|
756 |
* @method io |
|
|
757 |
* @public |
|
|
758 |
* @static |
|
|
759 |
* @param {string} uri - qualified path to transaction resource. |
|
|
760 |
* @param {object} c - configuration object for the transaction. |
|
|
761 |
* @return object |
|
|
762 |
*/ |
|
|
763 |
Y.io = _io; |
|
|
764 |
Y.io.http = _io; |
|
|
765 |
|
|
|
766 |
|
|
|
767 |
|
|
|
768 |
}, '3.0.0b1' ); |