|
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.0 |
|
6 build: 1549 |
|
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 * @param {number} i - transaction id, if already set by queue. |
|
182 * @return object |
|
183 */ |
|
184 function _io(uri, c, i) { |
|
185 var f, o, m; |
|
186 c = c || {}; |
|
187 o = _create(c.xdr || c.form, i); |
|
188 m = c.method ? c.method.toUpperCase() : 'GET'; |
|
189 |
|
190 if (c.form) { |
|
191 if (c.form.upload) { |
|
192 return Y.io._upload(o, uri, c); |
|
193 } |
|
194 else { |
|
195 f = Y.io._serialize(c.form, c.data); |
|
196 if (m === 'POST') { |
|
197 c.data = f; |
|
198 _setHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|
199 } |
|
200 else if (m === 'GET') { |
|
201 uri = _concat(uri, f); |
|
202 } |
|
203 } |
|
204 } |
|
205 else if (c.data && m === 'GET') { |
|
206 uri = _concat(uri, c.data); |
|
207 } |
|
208 |
|
209 if (c.xdr) { |
|
210 if (c.xdr.use === 'native' && window.XDomainRequest || c.xdr.use === 'flash') { |
|
211 return Y.io.xdr(uri, o, c); |
|
212 } |
|
213 if (c.xdr.credentials) { |
|
214 o.c.withCredentials = true; |
|
215 } |
|
216 } |
|
217 |
|
218 o.c.onreadystatechange = function() { _readyState(o, c); }; |
|
219 try { |
|
220 o.c.open(m, uri, true); |
|
221 } |
|
222 catch(e0){ |
|
223 if (c.xdr) { |
|
224 // This exception is usually thrown by browsers |
|
225 // that do not support native XDR transactions. |
|
226 return _resend(o, uri, c); |
|
227 } |
|
228 } |
|
229 |
|
230 if (c.data && m === 'POST') { |
|
231 _setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); |
|
232 } |
|
233 |
|
234 _setHeaders(o.c, c.headers || {}); |
|
235 try { |
|
236 // Using "null" will result in a POST request with |
|
237 // no Content-Length defined. |
|
238 o.c.send(c.data || ''); |
|
239 } |
|
240 catch(e1) { |
|
241 if (c.xdr) { |
|
242 // This exception is usually thrown by browsers |
|
243 // that do not support native XDR transactions. |
|
244 return _resend(o, uri, c); |
|
245 } |
|
246 } |
|
247 |
|
248 _ioStart(o.id, c); |
|
249 // If config.timeout is defined, and the request is standard XHR, |
|
250 // initialize timeout polling. |
|
251 if (c.timeout) { |
|
252 _startTimeout(o, c.timeout); |
|
253 } |
|
254 |
|
255 return { |
|
256 id: o.id, |
|
257 abort: function() { |
|
258 return o.c ? _ioCancel(o, 'abort') : false; |
|
259 }, |
|
260 isInProgress: function() { |
|
261 return o.c ? o.c.readyState !== 4 && o.c.readyState !== 0 : false; |
|
262 } |
|
263 } |
|
264 } |
|
265 |
|
266 /** |
|
267 * @description Method for creating and subscribing transaction events. |
|
268 * |
|
269 * @method _subscribe |
|
270 * @private |
|
271 * @static |
|
272 * @param {string} e - event to be published |
|
273 * @param {object} c - configuration data subset for event subscription. |
|
274 * |
|
275 * @return void |
|
276 */ |
|
277 function _subscribe(e, c){ |
|
278 var evt = new Y.EventTarget().publish('transaction:' + e); |
|
279 evt.subscribe(c.on[e], (c.context || Y), c.arguments); |
|
280 |
|
281 return evt; |
|
282 } |
|
283 |
|
284 /** |
|
285 * @description Fires event "io:start" and creates, fires a |
|
286 * transaction-specific start event, if config.on.start is |
|
287 * defined. |
|
288 * |
|
289 * @method _ioStart |
|
290 * @private |
|
291 * @static |
|
292 * @param {number} id - transaction id |
|
293 * @param {object} c - configuration object for the transaction. |
|
294 * |
|
295 * @return void |
|
296 */ |
|
297 function _ioStart(id, c) { |
|
298 var evt; |
|
299 // Set default value of argument c, property "on" to Object if |
|
300 // the property is null or undefined. |
|
301 c.on = c.on || {}; |
|
302 |
|
303 Y.fire(E_START, id); |
|
304 if (c.on.start) { |
|
305 evt = _subscribe('start', c); |
|
306 evt.fire(id); |
|
307 } |
|
308 } |
|
309 |
|
310 |
|
311 /** |
|
312 * @description Fires event "io:complete" and creates, fires a |
|
313 * transaction-specific "complete" event, if config.on.complete is |
|
314 * defined. |
|
315 * |
|
316 * @method _ioComplete |
|
317 * @private |
|
318 * @static |
|
319 * @param {object} o - transaction object. |
|
320 * @param {object} c - configuration object for the transaction. |
|
321 * |
|
322 * @return void |
|
323 */ |
|
324 function _ioComplete(o, c) { |
|
325 var evt, |
|
326 r = o.status ? { status: 0, statusText: o.status } : o.c; |
|
327 // Set default value of argument c, property "on" to Object if |
|
328 // the property is null or undefined. |
|
329 c.on = c.on || {}; |
|
330 |
|
331 Y.fire(E_COMPLETE, o.id, r); |
|
332 if (c.on.complete) { |
|
333 evt = _subscribe('complete', c); |
|
334 evt.fire(o.id, r); |
|
335 } |
|
336 } |
|
337 |
|
338 /** |
|
339 * @description Fires event "io:success" and creates, fires a |
|
340 * transaction-specific "success" event, if config.on.success is |
|
341 * defined. |
|
342 * |
|
343 * @method _ioSuccess |
|
344 * @private |
|
345 * @static |
|
346 * @param {object} o - transaction object. |
|
347 * @param {object} c - configuration object for the transaction. |
|
348 * |
|
349 * @return void |
|
350 */ |
|
351 function _ioSuccess(o, c) { |
|
352 var evt; |
|
353 // Set default value of argument c, property "on" to Object if |
|
354 // the property is null or undefined. |
|
355 c.on = c.on || {}; |
|
356 |
|
357 Y.fire(E_SUCCESS, o.id, o.c); |
|
358 if (c.on.success) { |
|
359 evt = _subscribe('success', c); |
|
360 evt.fire(o.id, o.c); |
|
361 } |
|
362 |
|
363 _ioEnd(o, c); |
|
364 } |
|
365 |
|
366 /** |
|
367 * @description Fires event "io:failure" and creates, fires a |
|
368 * transaction-specific "failure" event, if config.on.failure is |
|
369 * defined. |
|
370 * |
|
371 * @method _ioFailure |
|
372 * @private |
|
373 * @static |
|
374 * @param {object} o - transaction object. |
|
375 * @param {object} c - configuration object for the transaction. |
|
376 * |
|
377 * @return void |
|
378 */ |
|
379 function _ioFailure(o, c) { |
|
380 var evt, |
|
381 r = o.status ? { status: 0, statusText: o.status } : o.c; |
|
382 // Set default value of argument c, property "on" to Object if |
|
383 // the property is null or undefined. |
|
384 c.on = c.on || {}; |
|
385 |
|
386 Y.fire(E_FAILURE, o.id, r); |
|
387 if (c.on.failure) { |
|
388 evt = _subscribe('failure', c); |
|
389 evt.fire(o.id, r); |
|
390 } |
|
391 |
|
392 _ioEnd(o, c); |
|
393 } |
|
394 |
|
395 /** |
|
396 * @description Fires event "io:end" and creates, fires a |
|
397 * transaction-specific "end" event, if config.on.end is |
|
398 * defined. |
|
399 * |
|
400 * @method _ioEnd |
|
401 * @private |
|
402 * @static |
|
403 * @param {object} o - transaction object. |
|
404 * @param {object} c - configuration object for the transaction. |
|
405 * |
|
406 * @return void |
|
407 */ |
|
408 function _ioEnd(o, c) { |
|
409 var evt; |
|
410 // Set default value of argument c, property "on" to Object if |
|
411 // the property is null or undefined. |
|
412 c.on = c.on || {}; |
|
413 |
|
414 Y.fire(E_END, o.id); |
|
415 if (c.on.end) { |
|
416 evt = _subscribe('end', c); |
|
417 evt.fire(o.id); |
|
418 } |
|
419 |
|
420 _destroy(o, c.xdr ? true : false ); |
|
421 } |
|
422 |
|
423 /** |
|
424 * @description Terminates a transaction due to an explicit abort or |
|
425 * timeout. |
|
426 * |
|
427 * @method _ioCancel |
|
428 * @private |
|
429 * @static |
|
430 * @param {object} o - Transaction object generated by _create(). |
|
431 * @param {string} s - Identifies timed out or aborted transaction. |
|
432 * |
|
433 * @return void |
|
434 */ |
|
435 function _ioCancel(o, s) { |
|
436 if (o && o.c) { |
|
437 o.status = s; |
|
438 o.c.abort(); |
|
439 } |
|
440 } |
|
441 |
|
442 /** |
|
443 * @description Resends an XDR transaction, using the Flash tranport, |
|
444 * if the native transport fails. |
|
445 * |
|
446 * @method _resend |
|
447 * @private |
|
448 * @static |
|
449 |
|
450 * @param {object} o - Transaction object generated by _create(). |
|
451 * @param {string} uri - qualified path to transaction resource. |
|
452 * @param {object} c - configuration object for the transaction. |
|
453 * |
|
454 * @return void |
|
455 */ |
|
456 function _resend(o, uri, c) { |
|
457 var id = parseInt(o.id); |
|
458 |
|
459 _destroy(o); |
|
460 c.xdr.use = 'flash'; |
|
461 |
|
462 return Y.io(uri, c, id); |
|
463 } |
|
464 |
|
465 /** |
|
466 * @description Method that increments _transactionId for each transaction. |
|
467 * |
|
468 * @method _id |
|
469 * @private |
|
470 * @static |
|
471 * @return int |
|
472 */ |
|
473 function _id() { |
|
474 var id = transactionId; |
|
475 |
|
476 transactionId++; |
|
477 |
|
478 return id; |
|
479 } |
|
480 |
|
481 /** |
|
482 * @description Method that creates a unique transaction object for each |
|
483 * request. |
|
484 * |
|
485 * @method _create |
|
486 * @private |
|
487 * @static |
|
488 * @param {number} c - configuration object subset to determine if |
|
489 * the transaction is an XDR or file upload, |
|
490 * requiring an alternate transport. |
|
491 * @param {number} i - transaction id |
|
492 * @return object |
|
493 */ |
|
494 function _create(c, i) { |
|
495 var o = {}; |
|
496 o.id = Y.Lang.isNumber(i) ? i : _id(); |
|
497 c = c || {}; |
|
498 |
|
499 if (!c.use && !c.upload) { |
|
500 o.c = _xhr(); |
|
501 } |
|
502 else if (c.use) { |
|
503 if (c.use === 'flash') { |
|
504 o.c = Y.io._transport[c.use]; |
|
505 } |
|
506 else if (c.use === 'native' && window.XDomainRequest) { |
|
507 o.c = new XDomainRequest(); |
|
508 } |
|
509 else { |
|
510 o.c = _xhr(); |
|
511 } |
|
512 } |
|
513 else { |
|
514 o.c = {}; |
|
515 } |
|
516 |
|
517 return o; |
|
518 }; |
|
519 |
|
520 /** |
|
521 * @description Method that creates the XMLHttpRequest transport |
|
522 * |
|
523 * @method _xhr |
|
524 * @private |
|
525 * @static |
|
526 * @return object |
|
527 */ |
|
528 function _xhr() { |
|
529 return w.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); |
|
530 } |
|
531 |
|
532 /** |
|
533 * @description Method that concatenates string data for HTTP GET transactions. |
|
534 * |
|
535 * @method _concat |
|
536 * @private |
|
537 * @static |
|
538 * @param {string} s - URI or root data. |
|
539 * @param {string} d - data to be concatenated onto URI. |
|
540 * @return int |
|
541 */ |
|
542 function _concat(s, d) { |
|
543 s += ((s.indexOf('?') == -1) ? '?' : '&') + d; |
|
544 return s; |
|
545 } |
|
546 |
|
547 /** |
|
548 * @description Method that stores default client headers for all transactions. |
|
549 * If a label is passed with no value argument, the header will be deleted. |
|
550 * |
|
551 * @method _setHeader |
|
552 * @private |
|
553 * @static |
|
554 * @param {string} l - HTTP header |
|
555 * @param {string} v - HTTP header value |
|
556 * @return int |
|
557 */ |
|
558 function _setHeader(l, v) { |
|
559 if (v) { |
|
560 _headers[l] = v; |
|
561 } |
|
562 else { |
|
563 delete _headers[l]; |
|
564 } |
|
565 } |
|
566 |
|
567 /** |
|
568 * @description Method that sets all HTTP headers to be sent in a transaction. |
|
569 * |
|
570 * @method _setHeaders |
|
571 * @private |
|
572 * @static |
|
573 * @param {object} o - XHR instance for the specific transaction. |
|
574 * @param {object} h - HTTP headers for the specific transaction, as defined |
|
575 * in the configuration object passed to YUI.io(). |
|
576 * @return void |
|
577 */ |
|
578 function _setHeaders(o, h) { |
|
579 var p; |
|
580 |
|
581 for (p in _headers) { |
|
582 if (_headers.hasOwnProperty(p)) { |
|
583 if (h[p]) { |
|
584 // Configuration headers will supersede IO preset headers, |
|
585 // if headers match. |
|
586 break; |
|
587 } |
|
588 else { |
|
589 h[p] = _headers[p]; |
|
590 } |
|
591 } |
|
592 } |
|
593 |
|
594 for (p in h) { |
|
595 if (h.hasOwnProperty(p)) { |
|
596 o.setRequestHeader(p, h[p]); |
|
597 } |
|
598 } |
|
599 } |
|
600 |
|
601 /** |
|
602 * @description Starts timeout count if the configuration object |
|
603 * has a defined timeout property. |
|
604 * |
|
605 * @method _startTimeout |
|
606 * @private |
|
607 * @static |
|
608 * @param {object} o - Transaction object generated by _create(). |
|
609 * @param {object} c - Configuration object passed to YUI.io(). |
|
610 * @return void |
|
611 */ |
|
612 function _startTimeout(o, timeout) { |
|
613 _timeout[o.id] = w.setTimeout(function() { _ioCancel(o, 'timeout'); }, timeout); |
|
614 } |
|
615 |
|
616 /** |
|
617 * @description Clears the timeout interval started by _startTimeout(). |
|
618 * |
|
619 * @method _clearTimeout |
|
620 * @private |
|
621 * @static |
|
622 * @param {number} id - Transaction id. |
|
623 * @return void |
|
624 */ |
|
625 function _clearTimeout(id) { |
|
626 w.clearTimeout(_timeout[id]); |
|
627 delete _timeout[id]; |
|
628 } |
|
629 |
|
630 /** |
|
631 * @description Event handler bound to onreadystatechange. |
|
632 * |
|
633 * @method _readyState |
|
634 * @private |
|
635 * @static |
|
636 * @param {object} o - Transaction object generated by _create(). |
|
637 * @param {object} c - Configuration object passed to YUI.io(). |
|
638 * @return void |
|
639 */ |
|
640 function _readyState(o, c) { |
|
641 if (o.c.readyState === 4) { |
|
642 if (c.timeout) { |
|
643 _clearTimeout(o.id); |
|
644 } |
|
645 |
|
646 w.setTimeout( |
|
647 function() { |
|
648 _ioComplete(o, c); |
|
649 _handleResponse(o, c); |
|
650 }, 0); |
|
651 } |
|
652 } |
|
653 |
|
654 /** |
|
655 * @description Method that determines if a transaction response qualifies |
|
656 * as success or failure, based on the response HTTP status code, and |
|
657 * fires the appropriate success or failure events. |
|
658 * |
|
659 * @method _handleResponse |
|
660 * @private |
|
661 * @static |
|
662 * @param {object} o - Transaction object generated by _create(). |
|
663 * @param {object} c - Configuration object passed to io(). |
|
664 * @return void |
|
665 */ |
|
666 function _handleResponse(o, c) { |
|
667 var status; |
|
668 try{ |
|
669 if (o.c.status && o.c.status !== 0) { |
|
670 status = o.c.status; |
|
671 } |
|
672 else { |
|
673 status = 0; |
|
674 } |
|
675 } |
|
676 catch(e) { |
|
677 status = 0; |
|
678 } |
|
679 |
|
680 // IE reports HTTP 204 as HTTP 1223. |
|
681 if (status >= 200 && status < 300 || status === 1223) { |
|
682 _ioSuccess(o, c); |
|
683 } |
|
684 else { |
|
685 _ioFailure(o, c); |
|
686 } |
|
687 } |
|
688 |
|
689 function _destroy(o, transport) { |
|
690 // IE, when using XMLHttpRequest as an ActiveX Object, will throw |
|
691 // a "Type Mismatch" error if the event handler is set to "null". |
|
692 if(w.XMLHttpRequest && !transport) { |
|
693 if (o.c) { |
|
694 o.c.onreadystatechange = null; |
|
695 } |
|
696 } |
|
697 |
|
698 o.c = null; |
|
699 o = null; |
|
700 } |
|
701 |
|
702 _io.start = _ioStart; |
|
703 _io.complete = _ioComplete; |
|
704 _io.success = _ioSuccess; |
|
705 _io.failure = _ioFailure; |
|
706 _io.end = _ioEnd; |
|
707 _io._id = _id; |
|
708 _io._timeout = _timeout; |
|
709 |
|
710 //-------------------------------------- |
|
711 // Begin public interface definition |
|
712 //-------------------------------------- |
|
713 /** |
|
714 * @description Method that stores default client headers for all transactions. |
|
715 * If a label is passed with no value argument, the header will be deleted. |
|
716 * This is the interface for _setHeader(). |
|
717 * |
|
718 * @method header |
|
719 * @public |
|
720 * @static |
|
721 * @param {string} l - HTTP header |
|
722 * @param {string} v - HTTP header value |
|
723 * @return int |
|
724 */ |
|
725 _io.header = _setHeader; |
|
726 |
|
727 /** |
|
728 * @description Method for requesting a transaction. This |
|
729 * is the interface for _io(). |
|
730 * |
|
731 * @method io |
|
732 * @public |
|
733 * @static |
|
734 * @param {string} uri - qualified path to transaction resource. |
|
735 * @param {object} c - configuration object for the transaction. |
|
736 * @return object |
|
737 */ |
|
738 Y.io = _io; |
|
739 Y.io.http = _io; |
|
740 |
|
741 |
|
742 |
|
743 }, '3.0.0' ,{requires:['event-custom-base']}); |
|
744 |
|
745 YUI.add('io-form', function(Y) { |
|
746 |
|
747 /** |
|
748 * Extends the IO base class to enable HTML form data serialization, when specified |
|
749 * in the transaction's configuration object. |
|
750 * @module io |
|
751 * @submodule io-form |
|
752 */ |
|
753 |
|
754 Y.mix(Y.io, { |
|
755 /** |
|
756 * @description Method to enumerate through an HTML form's elements collection |
|
757 * and return a string comprised of key-value pairs. |
|
758 * |
|
759 * @method _serialize |
|
760 * @private |
|
761 * @static |
|
762 * @param {object} c - YUI form node or HTML form id. |
|
763 * @param {string} s - Transaction data defined in the configuration. |
|
764 * @return string |
|
765 */ |
|
766 _serialize: function(c, s) { |
|
767 var eUC = encodeURIComponent, |
|
768 data = [], |
|
769 useDf = c.useDisabled || false, |
|
770 item = 0, |
|
771 id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'), |
|
772 e, f, n, v, d, i, il, j, jl, o; |
|
773 |
|
774 if (!id) { |
|
775 id = Y.guid('io:'); |
|
776 c.id.setAttribute('id', id); |
|
777 } |
|
778 |
|
779 f = Y.config.doc.getElementById(id); |
|
780 |
|
781 // Iterate over the form elements collection to construct the |
|
782 // label-value pairs. |
|
783 for (i = 0, il = f.elements.length; i < il; ++i) { |
|
784 e = f.elements[i]; |
|
785 d = e.disabled; |
|
786 n = e.name; |
|
787 |
|
788 if ((useDf) ? n : (n && !d)) { |
|
789 n = encodeURIComponent(n) + '='; |
|
790 v = encodeURIComponent(e.value); |
|
791 |
|
792 switch (e.type) { |
|
793 // Safari, Opera, FF all default options.value from .text if |
|
794 // value attribute not specified in markup |
|
795 case 'select-one': |
|
796 if (e.selectedIndex > -1) { |
|
797 o = e.options[e.selectedIndex]; |
|
798 data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text); |
|
799 } |
|
800 break; |
|
801 case 'select-multiple': |
|
802 if (e.selectedIndex > -1) { |
|
803 for (j = e.selectedIndex, jl = e.options.length; j < jl; ++j) { |
|
804 o = e.options[j]; |
|
805 if (o.selected) { |
|
806 data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text); |
|
807 } |
|
808 } |
|
809 } |
|
810 break; |
|
811 case 'radio': |
|
812 case 'checkbox': |
|
813 if(e.checked){ |
|
814 data[item++] = n + v; |
|
815 } |
|
816 break; |
|
817 case 'file': |
|
818 // stub case as XMLHttpRequest will only send the file path as a string. |
|
819 case undefined: |
|
820 // stub case for fieldset element which returns undefined. |
|
821 case 'reset': |
|
822 // stub case for input type reset button. |
|
823 case 'button': |
|
824 // stub case for input type button elements. |
|
825 break; |
|
826 case 'submit': |
|
827 default: |
|
828 data[item++] = n + v; |
|
829 } |
|
830 } |
|
831 } |
|
832 Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io'); |
|
833 return s ? data.join('&') + "&" + s : data.join('&'); |
|
834 } |
|
835 }, true); |
|
836 |
|
837 |
|
838 |
|
839 }, '3.0.0' ,{requires:['io-base','node-base','node-style']}); |
|
840 |
|
841 YUI.add('io-xdr', function(Y) { |
|
842 |
|
843 /** |
|
844 * Extends the IO base class to provide an alternate, Flash transport, for making |
|
845 * cross-domain requests. |
|
846 * @module io |
|
847 * @submodule io-xdr |
|
848 */ |
|
849 |
|
850 /** |
|
851 * @event io:xdrReady |
|
852 * @description This event is fired by YUI.io when the specified transport is |
|
853 * ready for use. |
|
854 * @type Event Custom |
|
855 */ |
|
856 var E_XDR_READY = 'io:xdrReady', |
|
857 |
|
858 |
|
859 /** |
|
860 * @description Object that stores callback handlers for cross-domain requests |
|
861 * when using Flash as the transport. |
|
862 * |
|
863 * @property _fn |
|
864 * @private |
|
865 * @static |
|
866 * @type object |
|
867 */ |
|
868 _fn = {}, |
|
869 |
|
870 /** |
|
871 * @description Map of transaction state used when XDomainRequest is the |
|
872 * XDR transport. |
|
873 * |
|
874 * @property _rS |
|
875 * @private |
|
876 * @static |
|
877 * @type object |
|
878 */ |
|
879 _rS = {}; |
|
880 |
|
881 /** |
|
882 * @description Method that creates the Flash transport swf. |
|
883 * |
|
884 * @method _swf |
|
885 * @private |
|
886 * @static |
|
887 * @param {string} uri - location of io.swf. |
|
888 * @param {string} yid - YUI instance id. |
|
889 * @return void |
|
890 */ |
|
891 function _swf(uri, yid) { |
|
892 var o = '<object id="yuiIoSwf" type="application/x-shockwave-flash" data="' + |
|
893 uri + '" width="0" height="0">' + |
|
894 '<param name="movie" value="' + uri + '">' + |
|
895 '<param name="FlashVars" value="yid=' + yid + '">' + |
|
896 '<param name="allowScriptAccess" value="always">' + |
|
897 '</object>', |
|
898 c = document.createElement('div'); |
|
899 |
|
900 document.body.appendChild(c); |
|
901 c.innerHTML = o; |
|
902 } |
|
903 |
|
904 /** |
|
905 * @description Sets event handlers for XDomainRequest transactions. |
|
906 * |
|
907 * @method _xdr |
|
908 * @private |
|
909 * @static |
|
910 * @param {object} o - Transaction object generated by _create() in io-base. |
|
911 * @param {object} c - configuration object for the transaction. |
|
912 * @return void |
|
913 */ |
|
914 function _xdr(o, c) { |
|
915 o.c.onprogress = function() { _rS[o.id] = 3; } |
|
916 o.c.onload = function() { |
|
917 _rS[o.id] = 4; |
|
918 Y.io.xdrResponse(o, c, 'success'); |
|
919 }; |
|
920 o.c.onerror = function() { |
|
921 _rS[o.id] = 4; |
|
922 Y.io.xdrResponse(o, c, 'failure'); |
|
923 }; |
|
924 if (c.timeout) { |
|
925 o.c.ontimeout = function() { |
|
926 _rS[o.id] = 4; |
|
927 Y.io.xdrResponse(o, c, 'timeout'); |
|
928 }; |
|
929 o.c.timeout = c.timeout; |
|
930 } |
|
931 } |
|
932 |
|
933 /** |
|
934 * @description Creates a response object for XDR transactions, for success |
|
935 * and failure cases. |
|
936 * |
|
937 * @method _data |
|
938 * @private |
|
939 * @static |
|
940 * @param {object} o - Transaction object generated by _create() in io-base. |
|
941 * @param {boolean} isFlash - True if Flash was used as the transport. |
|
942 * @param {boolean} isXML - True if the response data are XML. |
|
943 * |
|
944 * @return object |
|
945 */ |
|
946 function _data(o, isFlash, isXML) { |
|
947 var text, xml; |
|
948 |
|
949 if (!o.status) { |
|
950 text = isFlash ? decodeURI(o.c.responseText) : o.c.responseText; |
|
951 xml = isXML ? Y.DataType.XML.parse(text) : null; |
|
952 |
|
953 return { id: o.id, c: { responseText: text, responseXML: xml } }; |
|
954 } |
|
955 else { |
|
956 return { id: o.id, status: o.status }; |
|
957 } |
|
958 |
|
959 } |
|
960 |
|
961 /** |
|
962 * @description Method for intiating an XDR transaction abort. |
|
963 * |
|
964 * @method _abort |
|
965 * @private |
|
966 * @static |
|
967 * @param {object} o - Transaction object generated by _create() in io-base. |
|
968 * @param {object} c - configuration object for the transaction. |
|
969 */ |
|
970 function _abort(o, c) { |
|
971 return c.xdr.use === 'flash' ? o.c.abort(o.id, c) : o.c.abort(); |
|
972 } |
|
973 |
|
974 /** |
|
975 * @description Method for determining if an XDR transaction has completed |
|
976 * and all data are received. |
|
977 * |
|
978 * @method _isInProgress. |
|
979 * @private |
|
980 * @static |
|
981 * @param {object} o - Transaction object generated by _create() in io-base. |
|
982 * @param {object} c - configuration object for the transaction. |
|
983 */ |
|
984 function _isInProgress(o, t) { |
|
985 return (t === 'flash' && o.c) ? o.c.isInProgress(o.id) : _rS[o.id] !== 4; |
|
986 } |
|
987 |
|
988 Y.mix(Y.io, { |
|
989 |
|
990 /** |
|
991 * @description Map of io transports. |
|
992 * |
|
993 * @property _transport |
|
994 * @private |
|
995 * @static |
|
996 * @type object |
|
997 */ |
|
998 _transport: {}, |
|
999 |
|
1000 /** |
|
1001 * @description Method for accessing the transport's interface for making a |
|
1002 * cross-domain transaction. |
|
1003 * |
|
1004 * @method _xdr |
|
1005 * @private |
|
1006 * @static |
|
1007 * @param {string} uri - qualified path to transaction resource. |
|
1008 * @param {object} o - Transaction object generated by _create() in io-base. |
|
1009 * @param {object} c - configuration object for the transaction. |
|
1010 */ |
|
1011 xdr: function(uri, o, c) { |
|
1012 if (c.on && c.xdr.use === 'flash') { |
|
1013 _fn[o.id] = { |
|
1014 on: c.on, |
|
1015 context: c.context, |
|
1016 arguments: c.arguments |
|
1017 }; |
|
1018 // These nodes do not need to be serialized across Flash's |
|
1019 // ExternalInterface. Doing so will result in exceptions. |
|
1020 c.context = null; |
|
1021 c.form = null; |
|
1022 |
|
1023 o.c.send(uri, c, o.id); |
|
1024 } |
|
1025 else if (window.XDomainRequest) { |
|
1026 _xdr(o, c); |
|
1027 o.c.open(c.method || 'GET', uri); |
|
1028 o.c.send(c.data); |
|
1029 } |
|
1030 |
|
1031 return { |
|
1032 id: o.id, |
|
1033 abort: function() { |
|
1034 return o.c ? _abort(o, c) : false; |
|
1035 }, |
|
1036 isInProgress: function() { |
|
1037 return o.c ? _isInProgress(o, c.xdr.use) : false; |
|
1038 } |
|
1039 } |
|
1040 }, |
|
1041 |
|
1042 /** |
|
1043 * @description Response controller for cross-domain requests when using the |
|
1044 * Flash transport or IE8's XDomainRequest object. |
|
1045 * |
|
1046 * @method xdrResponse |
|
1047 * @private |
|
1048 * @static |
|
1049 * @param {object} o - Transaction object generated by _create() in io-base. |
|
1050 * @param {object} c - configuration object for the transaction. |
|
1051 * @param {string} e - Event name |
|
1052 * @return object |
|
1053 */ |
|
1054 xdrResponse: function(o, c, e) { |
|
1055 var m, fn, |
|
1056 isFlash = c.xdr.use === 'flash' ? true : false, |
|
1057 isXML = c.xdr.dataType === 'xml' ? true : false; |
|
1058 c.on = c.on || {}; |
|
1059 |
|
1060 if (isFlash) { |
|
1061 m = _fn || {}; |
|
1062 fn = m[o.id] ? m[o.id] : null; |
|
1063 if (fn) { |
|
1064 c.on = fn.on; |
|
1065 c.context = fn.context; |
|
1066 c.arguments = fn.arguments; |
|
1067 } |
|
1068 } |
|
1069 if (e === ('abort' || 'timeout')) { |
|
1070 o.status = e; |
|
1071 } |
|
1072 |
|
1073 switch (e) { |
|
1074 case 'start': |
|
1075 Y.io.start(o.id, c); |
|
1076 break; |
|
1077 case 'success': |
|
1078 Y.io.success(_data(o, isFlash, isXML), c); |
|
1079 isFlash ? delete m[o.id] : delete _rS[o.id]; |
|
1080 break; |
|
1081 case 'timeout': |
|
1082 case 'abort': |
|
1083 case 'failure': |
|
1084 Y.io.failure(_data(o, isFlash, isXML), c); |
|
1085 isFlash ? delete m[o.id] : delete _rS[o.id]; |
|
1086 break; |
|
1087 } |
|
1088 }, |
|
1089 |
|
1090 /** |
|
1091 * @description Fires event "io:xdrReady" |
|
1092 * |
|
1093 * @method xdrReady |
|
1094 * @private |
|
1095 * @static |
|
1096 * @param {number} id - transaction id |
|
1097 * @param {object} c - configuration object for the transaction. |
|
1098 * |
|
1099 * @return void |
|
1100 */ |
|
1101 xdrReady: function(id) { |
|
1102 Y.fire(E_XDR_READY, id); |
|
1103 }, |
|
1104 |
|
1105 /** |
|
1106 * @description Method to initialize the desired transport. |
|
1107 * |
|
1108 * @method transport |
|
1109 * @public |
|
1110 * @static |
|
1111 * @param {object} o - object of transport configurations. |
|
1112 * @return void |
|
1113 */ |
|
1114 transport: function(o) { |
|
1115 var id = o.yid ? o.yid : Y.id; |
|
1116 |
|
1117 _swf(o.src, id); |
|
1118 this._transport.flash = Y.config.doc.getElementById('yuiIoSwf'); |
|
1119 } |
|
1120 }); |
|
1121 |
|
1122 |
|
1123 |
|
1124 }, '3.0.0' ,{requires:['io-base','datatype-xml']}); |
|
1125 |
|
1126 YUI.add('io-upload-iframe', function(Y) { |
|
1127 |
|
1128 /** |
|
1129 * Extends the IO base class to enable file uploads, with HTML forms, |
|
1130 * using an iframe as the transport medium. |
|
1131 * @module io |
|
1132 * @submodule io-upload-iframe |
|
1133 */ |
|
1134 |
|
1135 var w = Y.config.win; |
|
1136 /** |
|
1137 * @description Parses the POST data object and creates hidden form elements |
|
1138 * for each key-value, and appends them to the HTML form object. |
|
1139 * @method appendData |
|
1140 * @private |
|
1141 * @static |
|
1142 * @param {object} f HTML form object. |
|
1143 * @param {string} s The key-value POST data. |
|
1144 * @return {array} o Array of created fields. |
|
1145 */ |
|
1146 function _addData(f, s) { |
|
1147 var o = [], |
|
1148 m = s.split('='), |
|
1149 i, l; |
|
1150 |
|
1151 for (i = 0, l = m.length - 1; i < l; i++) { |
|
1152 o[i] = document.createElement('input'); |
|
1153 o[i].type = 'hidden'; |
|
1154 o[i].name = m[i].substring(m[i].lastIndexOf('&') + 1); |
|
1155 o[i].value = (i + 1 === l) ? m[i + 1] : m[i + 1].substring(0, (m[i + 1].lastIndexOf('&'))); |
|
1156 f.appendChild(o[i]); |
|
1157 Y.log('key: ' + o[i].name + ' and value: ' + o[i].value + ' added as form data.', 'info', 'io'); |
|
1158 } |
|
1159 |
|
1160 return o; |
|
1161 } |
|
1162 |
|
1163 /** |
|
1164 * @description Removes the custom fields created to pass additional POST |
|
1165 * data, along with the HTML form fields. |
|
1166 * @method f |
|
1167 * @private |
|
1168 * @static |
|
1169 * @param {object} f HTML form object. |
|
1170 * @param {object} o HTML form fields created from configuration.data. |
|
1171 * @return {void} |
|
1172 */ |
|
1173 function _removeData(f, o) { |
|
1174 var i, l; |
|
1175 |
|
1176 for(i = 0, l = o.length; i < l; i++){ |
|
1177 f.removeChild(o[i]); |
|
1178 } |
|
1179 } |
|
1180 |
|
1181 /** |
|
1182 * @description Sets the appropriate attributes and values to the HTML |
|
1183 * form, in preparation of a file upload transaction. |
|
1184 * @method _setAttrs |
|
1185 * @private |
|
1186 * @static |
|
1187 * @param {object} f HTML form object. |
|
1188 * @param {object} id The Transaction ID. |
|
1189 * @param {object} uri Qualified path to transaction resource. |
|
1190 * @return {void} |
|
1191 */ |
|
1192 function _setAttrs(f, id, uri) { |
|
1193 var ie8 = (document.documentMode && document.documentMode === 8) ? true : false; |
|
1194 |
|
1195 f.setAttribute('action', uri); |
|
1196 f.setAttribute('method', 'POST'); |
|
1197 f.setAttribute('target', 'ioupload' + id ); |
|
1198 f.setAttribute(Y.UA.ie && !ie8 ? 'encoding' : 'enctype', 'multipart/form-data'); |
|
1199 } |
|
1200 |
|
1201 /** |
|
1202 * @description Sets the appropriate attributes and values to the HTML |
|
1203 * form, in preparation of a file upload transaction. |
|
1204 * @method _resetAttrs |
|
1205 * @private |
|
1206 * @static |
|
1207 * @param {object} f HTML form object. |
|
1208 * @param {object} a Object of original attributes. |
|
1209 * @return {void} |
|
1210 */ |
|
1211 function _resetAttrs(f, a){ |
|
1212 var p; |
|
1213 |
|
1214 for (p in a) { |
|
1215 if (a.hasOwnProperty(a, p)) { |
|
1216 if (a[p]) { |
|
1217 f.setAttribute(p, f[p]); |
|
1218 } |
|
1219 else { |
|
1220 f.removeAttribute(p); |
|
1221 } |
|
1222 } |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 /** |
|
1227 * @description Creates the iframe transported used in file upload |
|
1228 * transactions, and binds the response event handler. |
|
1229 * |
|
1230 * @method _create |
|
1231 * @private |
|
1232 * @static |
|
1233 * @param {object} o Transaction object generated by _create(). |
|
1234 * @param {object} c Configuration object passed to YUI.io(). |
|
1235 * @return {void} |
|
1236 */ |
|
1237 function _create(o, c) { |
|
1238 var i = Y.Node.create('<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />'); |
|
1239 i._node.style.position = 'absolute'; |
|
1240 i._node.style.top = '-1000px'; |
|
1241 i._node.style.left = '-1000px'; |
|
1242 |
|
1243 Y.one('body').appendChild(i); |
|
1244 // Bind the onload handler to the iframe to detect the file upload response. |
|
1245 Y.on("load", function() { _handle(o, c) }, '#ioupload' + o.id); |
|
1246 } |
|
1247 |
|
1248 /** |
|
1249 * @description Bound to the iframe's Load event and processes |
|
1250 * the response data. |
|
1251 * @method _handle |
|
1252 * @private |
|
1253 * @static |
|
1254 * @param {o} o The transaction object |
|
1255 * @param {object} c Configuration object for the transaction. |
|
1256 * @return {void} |
|
1257 */ |
|
1258 function _handle(o, c) { |
|
1259 var d = Y.one('#ioupload' + o.id).get('contentWindow.document'), |
|
1260 b = d.one('body'), |
|
1261 xml = (d._node.nodeType === 9), |
|
1262 p; |
|
1263 |
|
1264 if (c.timeout) { |
|
1265 _clearTimeout(o.id); |
|
1266 } |
|
1267 |
|
1268 if (b) { |
|
1269 // When a response Content-Type of "text/plain" is used, Firefox and Safari |
|
1270 // will wrap the response string with <pre></pre>. |
|
1271 p = b.query('pre:first-child'); |
|
1272 o.c.responseText = p ? p.get('innerHTML') : b.get('innerHTML'); |
|
1273 Y.log('The responseText value for transaction ' + o.id + ' is: ' + o.c.responseText + '.', 'info', 'io'); |
|
1274 } |
|
1275 else if (xml) { |
|
1276 o.c.responseXML = d._node; |
|
1277 Y.log('The response for transaction ' + o.id + ' is an XML document.', 'info', 'io'); |
|
1278 } |
|
1279 |
|
1280 Y.io.complete(o, c); |
|
1281 Y.io.end(o, c); |
|
1282 // The transaction is complete, so call _destroy to remove |
|
1283 // the event listener bound to the iframe transport, and then |
|
1284 // destroy the iframe. |
|
1285 w.setTimeout( function() { _destroy(o.id); }, 0); |
|
1286 } |
|
1287 |
|
1288 /** |
|
1289 * @description Starts timeout count if the configuration object |
|
1290 * has a defined timeout property. |
|
1291 * |
|
1292 * @method _startTimeout |
|
1293 * @private |
|
1294 * @static |
|
1295 * @param {object} o Transaction object generated by _create(). |
|
1296 * @param {object} c Configuration object passed to YUI.io(). |
|
1297 * @return {void} |
|
1298 */ |
|
1299 function _startTimeout(o, c) { |
|
1300 Y.io._timeout[o.id] = w.setTimeout( |
|
1301 function() { |
|
1302 var r = { id: o.id, status: 'timeout' }; |
|
1303 |
|
1304 Y.io.complete(r, c); |
|
1305 Y.io.end(r, c); |
|
1306 Y.log('Transaction ' + o.id + ' timeout.', 'info', 'io'); |
|
1307 }, c.timeout); |
|
1308 } |
|
1309 |
|
1310 /** |
|
1311 * @description Clears the timeout interval started by _startTimeout(). |
|
1312 * @method _clearTimeout |
|
1313 * @private |
|
1314 * @static |
|
1315 * @param {number} id - Transaction ID. |
|
1316 * @return {void} |
|
1317 */ |
|
1318 function _clearTimeout(id) { |
|
1319 w.clearTimeout(Y.io._timeout[id]); |
|
1320 delete Y.io._timeout[id]; |
|
1321 } |
|
1322 |
|
1323 /** |
|
1324 * @description |
|
1325 * @method _destroy |
|
1326 * @private |
|
1327 * @static |
|
1328 * @param {o} o The transaction object |
|
1329 * @param {object} uri Qualified path to transaction resource. |
|
1330 * @param {object} c Configuration object for the transaction. |
|
1331 * @return {void} |
|
1332 */ |
|
1333 function _destroy(id) { |
|
1334 Y.Event.purgeElement('#ioupload' + id, false); |
|
1335 Y.one('body').removeChild(Y.one('#ioupload' + id)); |
|
1336 Y.log('The iframe transport for transaction ' + id + ' has been destroyed.', 'info', 'io'); |
|
1337 } |
|
1338 |
|
1339 Y.mix(Y.io, { |
|
1340 /** |
|
1341 * @description Uploads HTML form data, inclusive of files/attachments, |
|
1342 * using the iframe created in _create to facilitate the transaction. |
|
1343 * @method _upload |
|
1344 * @private |
|
1345 * @static |
|
1346 * @param {o} o The transaction object |
|
1347 * @param {object} uri Qualified path to transaction resource. |
|
1348 * @param {object} c Configuration object for the transaction. |
|
1349 * @return {void} |
|
1350 */ |
|
1351 _upload: function(o, uri, c) { |
|
1352 var f = (typeof c.form.id === 'string') ? Y.config.doc.getElementById(c.form.id) : c.form.id, |
|
1353 fields, |
|
1354 // Track original HTML form attribute values. |
|
1355 attr = { |
|
1356 action: f.getAttribute('action'), |
|
1357 target: f.getAttribute('target') |
|
1358 }; |
|
1359 |
|
1360 _create(o, c); |
|
1361 // Initialize the HTML form properties in case they are |
|
1362 // not defined in the HTML form. |
|
1363 _setAttrs(f, o.id, uri); |
|
1364 if (c.data) { |
|
1365 fields = _addData(f, c.data); |
|
1366 } |
|
1367 |
|
1368 // Start polling if a callback is present and the timeout |
|
1369 // property has been defined. |
|
1370 if (c.timeout) { |
|
1371 _startTimeout(o, c); |
|
1372 Y.log('Transaction timeout started for transaction ' + o.id + '.', 'info', 'io'); |
|
1373 } |
|
1374 |
|
1375 // Start file upload. |
|
1376 f.submit(); |
|
1377 Y.io.start(o.id, c); |
|
1378 if (c.data) { |
|
1379 _removeData(f, fields); |
|
1380 } |
|
1381 // Restore HTML form attributes to their original values. |
|
1382 _resetAttrs(f, attr); |
|
1383 |
|
1384 return { |
|
1385 id: o.id, |
|
1386 abort: function() { |
|
1387 var r = { id: o.id, status: 'abort' }; |
|
1388 |
|
1389 if (Y.one('#ioupload' + o.id)) { |
|
1390 _destroy(o.id); |
|
1391 Y.io.complete(r, c); |
|
1392 Y.io.end(r, c); |
|
1393 Y.log('Transaction ' + o.id + ' aborted.', 'info', 'io'); |
|
1394 } |
|
1395 else { |
|
1396 Y.log('Attempted to abort transaction ' + o.id + ' but transaction has completed.', 'info', 'io'); |
|
1397 return false; |
|
1398 } |
|
1399 }, |
|
1400 isInProgress: function() { |
|
1401 return Y.one('#ioupload' + o.id) ? true : false; |
|
1402 } |
|
1403 } |
|
1404 } |
|
1405 }); |
|
1406 |
|
1407 |
|
1408 |
|
1409 }, '3.0.0' ,{requires:['io-base','node-base','event-base']}); |
|
1410 |
|
1411 YUI.add('io-queue', function(Y) { |
|
1412 |
|
1413 /** |
|
1414 * Extends the IO base class to implement Queue for synchronous |
|
1415 * transaction processing. |
|
1416 * @module io |
|
1417 * @submodule io-queue |
|
1418 */ |
|
1419 |
|
1420 /** |
|
1421 * @description Array of transactions queued for processing |
|
1422 * |
|
1423 * @property _yQ |
|
1424 * @private |
|
1425 * @static |
|
1426 * @type Object |
|
1427 */ |
|
1428 var _q = new Y.Queue(), |
|
1429 |
|
1430 /** |
|
1431 * @description Reference to "io:complete" event handler. |
|
1432 * |
|
1433 * @property _e |
|
1434 * @private |
|
1435 * @static |
|
1436 * @type Object |
|
1437 */ |
|
1438 _e, |
|
1439 |
|
1440 _activeId, |
|
1441 /** |
|
1442 * @description Property to determine whether the queue is set to |
|
1443 * 1 (active) or 0 (inactive). When inactive, transactions |
|
1444 * will be stored in the queue until the queue is set to active. |
|
1445 * |
|
1446 * @property _qState |
|
1447 * @private |
|
1448 * @static |
|
1449 * @type int |
|
1450 */ |
|
1451 _qState = 1; |
|
1452 |
|
1453 /** |
|
1454 * @description Method for requesting a transaction, and queueing the |
|
1455 * request before it is sent to the resource. |
|
1456 * |
|
1457 * @method _queue |
|
1458 * @private |
|
1459 * @static |
|
1460 * @return Object |
|
1461 */ |
|
1462 function _queue(uri, c) { |
|
1463 var o = { uri: uri, id: Y.io._id(), cfg:c }; |
|
1464 |
|
1465 _q.add(o); |
|
1466 if (_qState === 1) { |
|
1467 _shift(); |
|
1468 } |
|
1469 |
|
1470 Y.log('Object queued. Transaction id is ' + o.id, 'info', 'io'); |
|
1471 return o; |
|
1472 } |
|
1473 |
|
1474 /** |
|
1475 * @description Method Process the first transaction from the |
|
1476 * queue in FIFO order. |
|
1477 * |
|
1478 * @method _shift |
|
1479 * @private |
|
1480 * @static |
|
1481 * @return void |
|
1482 */ |
|
1483 function _shift() { |
|
1484 var o = _q.next(); |
|
1485 |
|
1486 _activeId = o.id; |
|
1487 _qState = 0; |
|
1488 Y.io(o.uri, o.cfg, o.id); |
|
1489 } |
|
1490 |
|
1491 /** |
|
1492 * @description Method for promoting a transaction to the top of the queue. |
|
1493 * |
|
1494 * @method _unshift |
|
1495 * @private |
|
1496 * @static |
|
1497 * @return void |
|
1498 */ |
|
1499 function _unshift(o) { |
|
1500 _q.promote(o); |
|
1501 } |
|
1502 |
|
1503 function _next(id) { |
|
1504 _qState = 1; |
|
1505 if (_activeId === id && _q.size() > 0) { |
|
1506 _shift(); |
|
1507 } |
|
1508 } |
|
1509 |
|
1510 /** |
|
1511 * @description Method for removing a specific, pending transaction from |
|
1512 * the queue. |
|
1513 * |
|
1514 * @method _remove |
|
1515 * @private |
|
1516 * @static |
|
1517 * @return void |
|
1518 */ |
|
1519 function _remove(o) { |
|
1520 _q.remove(o); |
|
1521 } |
|
1522 |
|
1523 function _start() { |
|
1524 _qState = 1; |
|
1525 |
|
1526 if (_q.size() > 0) { |
|
1527 _shift(); |
|
1528 } |
|
1529 Y.log('Queue started.', 'info', 'io'); |
|
1530 } |
|
1531 |
|
1532 /** |
|
1533 * @description Method for setting queue processing to inactive. |
|
1534 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
1535 * not processed until the queue is reset to "active". |
|
1536 * |
|
1537 * @method _stop |
|
1538 * @private |
|
1539 * @static |
|
1540 * @return void |
|
1541 */ |
|
1542 function _stop() { |
|
1543 _qState = 0; |
|
1544 Y.log('Queue stopped.', 'info', 'io'); |
|
1545 }; |
|
1546 |
|
1547 /** |
|
1548 * @description Method to query the current size of the queue. |
|
1549 * |
|
1550 * @method _size |
|
1551 * @private |
|
1552 * @static |
|
1553 * @return int |
|
1554 */ |
|
1555 function _size() { |
|
1556 return _q.size(); |
|
1557 }; |
|
1558 |
|
1559 _e = Y.on('io:complete', function(id) { _next(id); }, Y.io); |
|
1560 |
|
1561 /** |
|
1562 * @description Method to query the current size of the queue, or to |
|
1563 * set a maximum queue size. This is the interface for _size(). |
|
1564 * |
|
1565 * @method size |
|
1566 * @public |
|
1567 * @static |
|
1568 * @param {number} i - Specified maximum size of queue. |
|
1569 * @return number |
|
1570 */ |
|
1571 _queue.size = _size; |
|
1572 |
|
1573 /** |
|
1574 * @description Method for setting the queue to active. If there are |
|
1575 * transactions pending in the queue, they will be processed from the |
|
1576 * queue in FIFO order. This is the interface for _start(). |
|
1577 * |
|
1578 * @method start |
|
1579 * @public |
|
1580 * @static |
|
1581 * @return void |
|
1582 */ |
|
1583 _queue.start = _start; |
|
1584 |
|
1585 /** |
|
1586 * @description Method for setting queue processing to inactive. |
|
1587 * Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
1588 * not processed until the queue is restarted. This is the |
|
1589 * interface for _stop(). |
|
1590 * |
|
1591 * @method stop |
|
1592 * @public |
|
1593 * @static |
|
1594 * @return void |
|
1595 */ |
|
1596 _queue.stop = _stop; |
|
1597 |
|
1598 /** |
|
1599 * @description Method for promoting a transaction to the top of the queue. |
|
1600 * This is the interface for _unshift(). |
|
1601 * |
|
1602 * @method promote |
|
1603 * @public |
|
1604 * @static |
|
1605 * @param {Object} o - Reference to queued transaction. |
|
1606 * @return void |
|
1607 */ |
|
1608 _queue.promote = _unshift; |
|
1609 |
|
1610 /** |
|
1611 * @description Method for removing a specific, pending transaction from |
|
1612 * the queue. This is the interface for _remove(). |
|
1613 * |
|
1614 * @method remove |
|
1615 * @public |
|
1616 * @static |
|
1617 * @param {Object} o - Reference to queued transaction. |
|
1618 * @return void |
|
1619 */ |
|
1620 _queue.remove = _remove; |
|
1621 |
|
1622 Y.mix(Y.io, { |
|
1623 queue: _queue |
|
1624 }, true); |
|
1625 |
|
1626 |
|
1627 |
|
1628 }, '3.0.0' ,{requires:['io-base','queue-promote']}); |
|
1629 |
|
1630 |
|
1631 |
|
1632 YUI.add('io', function(Y){}, '3.0.0' ,{use:['io-base', 'io-form', 'io-xdr', 'io-upload-iframe', 'io-queue']}); |
|
1633 |