|
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' ); |
|
|
769 |
|
|
|
770 |
YUI.add('io-form', function(Y) { |
|
|
771 |
|
|
|
772 |
/** |
|
|
773 |
* Extends the IO base class to enable HTML form data serialization, when specified |
|
|
774 |
* in the transaction's configuration object. |
|
|
775 |
* @module io |
|
|
776 |
* @submodule io-form |
|
|
777 |
*/ |
|
|
778 |
|
|
|
779 |
Y.mix(Y.io, { |
|
|
780 |
/** |
|
|
781 |
* @description Method to enumerate through an HTML form's elements collection |
|
|
782 |
* and return a string comprised of key-value pairs. |
|
|
783 |
* |
|
|
784 |
* @method _serialize |
|
|
785 |
* @private |
|
|
786 |
* @static |
|
|
787 |
* @param {object} o - YUI form node or HTML form id. |
|
|
788 |
* @return string |
|
|
789 |
*/ |
|
|
790 |
_serialize: function(o) { |
|
|
791 |
var eUC = encodeURIComponent, |
|
|
792 |
data = [], |
|
|
793 |
useDf = o.useDisabled || false, |
|
|
794 |
item = 0, |
|
|
795 |
e, f, n, v, d, i, ilen, j, jlen, o, |
|
|
796 |
id = (typeof o.id === 'string') ? o.id : o.id.getAttribute('id'); |
|
|
797 |
|
|
|
798 |
if (!id) { |
|
|
799 |
id = Y.guid('io:'); |
|
|
800 |
o.id.setAttribute('id', id); |
|
|
801 |
} |
|
|
802 |
|
|
|
803 |
f = Y.config.doc.getElementById(id); |
|
|
804 |
|
|
|
805 |
// Iterate over the form elements collection to construct the |
|
|
806 |
// label-value pairs. |
|
|
807 |
for (i = 0, ilen = f.elements.length; i < ilen; ++i) { |
|
|
808 |
e = f.elements[i]; |
|
|
809 |
d = e.disabled; |
|
|
810 |
n = e.name; |
|
|
811 |
|
|
|
812 |
if ((useDf) ? n : (n && !d)) { |
|
|
813 |
n = encodeURIComponent(n) + '='; |
|
|
814 |
v = encodeURIComponent(e.value); |
|
|
815 |
|
|
|
816 |
switch (e.type) { |
|
|
817 |
// Safari, Opera, FF all default options.value from .text if |
|
|
818 |
// value attribute not specified in markup |
|
|
819 |
case 'select-one': |
|
|
820 |
if (e.selectedIndex > -1) { |
|
|
821 |
o = e.options[e.selectedIndex]; |
|
|
822 |
data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text); |
|
|
823 |
} |
|
|
824 |
break; |
|
|
825 |
case 'select-multiple': |
|
|
826 |
if (e.selectedIndex > -1) { |
|
|
827 |
for (j = e.selectedIndex, jlen = e.options.length; j < jlen; ++j) { |
|
|
828 |
o = e.options[j]; |
|
|
829 |
if (o.selected) { |
|
|
830 |
data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text); |
|
|
831 |
} |
|
|
832 |
} |
|
|
833 |
} |
|
|
834 |
break; |
|
|
835 |
case 'radio': |
|
|
836 |
case 'checkbox': |
|
|
837 |
if(e.checked){ |
|
|
838 |
data[item++] = n + v; |
|
|
839 |
} |
|
|
840 |
break; |
|
|
841 |
case 'file': |
|
|
842 |
// stub case as XMLHttpRequest will only send the file path as a string. |
|
|
843 |
case undefined: |
|
|
844 |
// stub case for fieldset element which returns undefined. |
|
|
845 |
case 'reset': |
|
|
846 |
// stub case for input type reset button. |
|
|
847 |
case 'button': |
|
|
848 |
// stub case for input type button elements. |
|
|
849 |
break; |
|
|
850 |
case 'submit': |
|
|
851 |
default: |
|
|
852 |
data[item++] = n + v; |
|
|
853 |
} |
|
|
854 |
} |
|
|
855 |
} |
|
|
856 |
return data.join('&'); |
|
|
857 |
} |
|
|
858 |
}, true); |
|
|
859 |
|
|
|
860 |
|
|
|
861 |
|
|
|
862 |
}, '3.0.0b1' ,{requires:['io-base']}); |
|
|
863 |
|
|
|
864 |
YUI.add('io-xdr', function(Y) { |
|
|
865 |
|
|
|
866 |
/** |
|
|
867 |
* Extends the IO base class to provide an alternate, Flash transport, for making |
|
|
868 |
* cross-domain requests. |
|
|
869 |
* @module io |
|
|
870 |
* @submodule io-xdr |
|
|
871 |
*/ |
|
|
872 |
|
|
|
873 |
/** |
|
|
874 |
* @event io:xdrReady |
|
|
875 |
* @description This event is fired by YUI.io when the specified transport is |
|
|
876 |
* ready for use. |
|
|
877 |
* @type Event Custom |
|
|
878 |
*/ |
|
|
879 |
var E_XDR_READY = 'io:xdrReady'; |
|
|
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 XDR_SWF = '<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="sameDomain">' + |
|
|
897 |
'</object>'; |
|
|
898 |
Y.get('body').appendChild(Y.Node.create(XDR_SWF)); |
|
|
899 |
} |
|
|
900 |
|
|
|
901 |
Y.mix(Y.io, { |
|
|
902 |
|
|
|
903 |
/** |
|
|
904 |
* @description Map of IO transports. |
|
|
905 |
* |
|
|
906 |
* @property _transport |
|
|
907 |
* @private |
|
|
908 |
* @static |
|
|
909 |
* @type object |
|
|
910 |
*/ |
|
|
911 |
_transport: {}, |
|
|
912 |
|
|
|
913 |
/** |
|
|
914 |
* @description Object that stores callback handlers for cross-domain requests |
|
|
915 |
* when using Flash as the transport. |
|
|
916 |
* |
|
|
917 |
* @property _fn |
|
|
918 |
* @private |
|
|
919 |
* @static |
|
|
920 |
* @type object |
|
|
921 |
*/ |
|
|
922 |
_fn: {}, |
|
|
923 |
|
|
|
924 |
/** |
|
|
925 |
* @description Method for accessing the transport's interface for making a |
|
|
926 |
* cross-domain transaction. |
|
|
927 |
* |
|
|
928 |
* @method _xdr |
|
|
929 |
* @private |
|
|
930 |
* @static |
|
|
931 |
* @param {string} uri - qualified path to transaction resource. |
|
|
932 |
* @param {object} o - Transaction object generated by _create() in io-base. |
|
|
933 |
* @param {object} c - configuration object for the transaction. |
|
|
934 |
* @return object |
|
|
935 |
*/ |
|
|
936 |
_xdr: function(uri, o, c) { |
|
|
937 |
if (c.on) { |
|
|
938 |
this._fn[o.id] = c.on; |
|
|
939 |
} |
|
|
940 |
o.c.send(uri, c, o.id); |
|
|
941 |
|
|
|
942 |
return o; |
|
|
943 |
}, |
|
|
944 |
|
|
|
945 |
|
|
|
946 |
/** |
|
|
947 |
* @description Fires event "io:xdrReady" |
|
|
948 |
* |
|
|
949 |
* @method xdrReady |
|
|
950 |
* @private |
|
|
951 |
* @static |
|
|
952 |
* @param {number} id - transaction id |
|
|
953 |
* @param {object} c - configuration object for the transaction. |
|
|
954 |
* |
|
|
955 |
* @return void |
|
|
956 |
*/ |
|
|
957 |
xdrReady: function(id) { |
|
|
958 |
Y.fire(E_XDR_READY, id); |
|
|
959 |
}, |
|
|
960 |
|
|
|
961 |
/** |
|
|
962 |
* @description Method to initialize the desired transport. |
|
|
963 |
* |
|
|
964 |
* @method transport |
|
|
965 |
* @public |
|
|
966 |
* @static |
|
|
967 |
* @param {object} o - object of transport configurations. |
|
|
968 |
* @return void |
|
|
969 |
*/ |
|
|
970 |
transport: function(o) { |
|
|
971 |
switch (o.id) { |
|
|
972 |
case 'flash': |
|
|
973 |
_swf(o.src, o.yid); |
|
|
974 |
this._transport.flash = Y.config.doc.getElementById('yuiIoSwf'); |
|
|
975 |
break; |
|
|
976 |
} |
|
|
977 |
} |
|
|
978 |
}); |
|
|
979 |
|
|
|
980 |
|
|
|
981 |
|
|
|
982 |
}, '3.0.0b1' ,{requires:['io-base']}); |
|
|
983 |
|
|
|
984 |
YUI.add('io-upload-iframe', function(Y) { |
|
|
985 |
|
|
|
986 |
/** |
|
|
987 |
* Extends the IO base class to enable file uploads, with HTML forms, |
|
|
988 |
* using an iframe as the transport medium. |
|
|
989 |
* @module io |
|
|
990 |
* @submodule io-upload-iframe |
|
|
991 |
*/ |
|
|
992 |
|
|
|
993 |
var w = Y.config.win; |
|
|
994 |
/** |
|
|
995 |
* @description Parses the POST data object and creates hidden form elements |
|
|
996 |
* for each key-value, and appends them to the HTML form object. |
|
|
997 |
* @method appendData |
|
|
998 |
* @private |
|
|
999 |
* @static |
|
|
1000 |
* @param {object} d The key-value hash map. |
|
|
1001 |
* @return {array} e Array of created fields. |
|
|
1002 |
*/ |
|
|
1003 |
|
|
|
1004 |
function _addData(f, d) { |
|
|
1005 |
var e = [], |
|
|
1006 |
p, i; |
|
|
1007 |
|
|
|
1008 |
for (p in d) { |
|
|
1009 |
if (d.hasOwnProperty(d, p)) { |
|
|
1010 |
e[i] = document.createElement('input'); |
|
|
1011 |
e[i].type = 'hidden'; |
|
|
1012 |
e[i].name = p; |
|
|
1013 |
e[i].value = d[p]. |
|
|
1014 |
f.appendChild(e[i]); |
|
|
1015 |
} |
|
|
1016 |
} |
|
|
1017 |
|
|
|
1018 |
return e; |
|
|
1019 |
} |
|
|
1020 |
|
|
|
1021 |
function _removeData(f, e) { |
|
|
1022 |
var i, l; |
|
|
1023 |
if (e && e.length > 0) { |
|
|
1024 |
for(i = 0, l = e.length; i < l; i++){ |
|
|
1025 |
f.removeChild(e[i]); |
|
|
1026 |
} |
|
|
1027 |
} |
|
|
1028 |
} |
|
|
1029 |
|
|
|
1030 |
function _create(o, c) { |
|
|
1031 |
var i = Y.Node.create('<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />'), |
|
|
1032 |
cfg = { |
|
|
1033 |
position: 'absolute', |
|
|
1034 |
top: '-1000px', |
|
|
1035 |
left: '-1000px' |
|
|
1036 |
}; |
|
|
1037 |
|
|
|
1038 |
i.setStyles(cfg); |
|
|
1039 |
Y.get('body').appendChild(i); |
|
|
1040 |
// Bind the onload handler to the iframe to detect the file upload response. |
|
|
1041 |
Y.on("load", function() { _handle(o, c) }, '#ioupload' + o.id); |
|
|
1042 |
} |
|
|
1043 |
|
|
|
1044 |
// Create the upload callback handler that fires when the iframe |
|
|
1045 |
// receives the load event. Subsequently, the event handler is detached |
|
|
1046 |
// and the iframe removed from the document. |
|
|
1047 |
function _handle(o, c) { |
|
|
1048 |
var p, |
|
|
1049 |
b = Y.get('#ioupload' + o.id).get('contentWindow.document.body'); |
|
|
1050 |
|
|
|
1051 |
if (c.timeout) { |
|
|
1052 |
_clearTimeout(o.id); |
|
|
1053 |
} |
|
|
1054 |
|
|
|
1055 |
// When a response Content-Type of "text/plain" is used, Firefox and Safari |
|
|
1056 |
// will wrap the response string with <pre></pre>. |
|
|
1057 |
p = b.query('pre:first-child'); |
|
|
1058 |
o.c.responseText = (p) ? p.get('innerHTML') : b.get('innerHTML'); |
|
|
1059 |
Y.io.complete(o, c); |
|
|
1060 |
// The transaction is complete, so call _destroy to remove |
|
|
1061 |
// the event listener bound to the iframe transport, and then |
|
|
1062 |
// destroy the iframe. |
|
|
1063 |
setTimeout( function() { _destroy(o.id); }, 0); |
|
|
1064 |
} |
|
|
1065 |
|
|
|
1066 |
/** |
|
|
1067 |
* @description Starts timeout count if the configuration object |
|
|
1068 |
* has a defined timeout property. |
|
|
1069 |
* |
|
|
1070 |
* @method _startTimeout |
|
|
1071 |
* @private |
|
|
1072 |
* @static |
|
|
1073 |
* @param {object} o Transaction object generated by _create(). |
|
|
1074 |
* @param {object} c Configuration object passed to YUI.io(). |
|
|
1075 |
* @return void |
|
|
1076 |
*/ |
|
|
1077 |
function _startTimeout(o, c) { |
|
|
1078 |
Y.io._timeout[o.id] = w.setTimeout(function() { Y.io.abort(o, c); }, c.timeout); |
|
|
1079 |
} |
|
|
1080 |
|
|
|
1081 |
/** |
|
|
1082 |
* @description Clears the timeout interval started by _startTimeout(). |
|
|
1083 |
* |
|
|
1084 |
* @method _clearTimeout |
|
|
1085 |
* @private |
|
|
1086 |
* @static |
|
|
1087 |
* @param {number} id - Transaction id. |
|
|
1088 |
* @return void |
|
|
1089 |
*/ |
|
|
1090 |
function _clearTimeout(id) { |
|
|
1091 |
w.clearTimeout(Y.io._timeout[id]); |
|
|
1092 |
delete Y.io._timeout[id]; |
|
|
1093 |
} |
|
|
1094 |
|
|
|
1095 |
function _destroy(id) { |
|
|
1096 |
Y.Event.purgeElement('#ioupload' + id, false); |
|
|
1097 |
Y.get('body').removeChild(Y.get('#ioupload' + id)); |
|
|
1098 |
} |
|
|
1099 |
|
|
|
1100 |
Y.mix(Y.io, { |
|
|
1101 |
|
|
|
1102 |
/** |
|
|
1103 |
* @description Uploads HTML form, inclusive of files/attachments, using the |
|
|
1104 |
* iframe created in createFrame to facilitate the transaction. |
|
|
1105 |
* @method _upload |
|
|
1106 |
* @private |
|
|
1107 |
* @static |
|
|
1108 |
* @param {o} o The transaction object |
|
|
1109 |
* @param {object} uri Qualified path to transaction resource. |
|
|
1110 |
* @param {object} c Configuration object for the transaction. |
|
|
1111 |
* @return {void} |
|
|
1112 |
*/ |
|
|
1113 |
_upload: function(o, uri, c) { |
|
|
1114 |
var f = (typeof c.form.id === 'string') ? document.getElementById(c.form.id) : c.form.id, |
|
|
1115 |
e, fields, i, p, attr; |
|
|
1116 |
|
|
|
1117 |
_create(o, c); |
|
|
1118 |
// Track original HTML form attribute values. |
|
|
1119 |
attr = { |
|
|
1120 |
action: f.getAttribute('action'), |
|
|
1121 |
target: f.getAttribute('target') |
|
|
1122 |
}; |
|
|
1123 |
|
|
|
1124 |
// Initialize the HTML form properties in case they are |
|
|
1125 |
// not defined in the HTML form. |
|
|
1126 |
f.setAttribute('action', uri); |
|
|
1127 |
f.setAttribute('method', 'POST'); |
|
|
1128 |
f.setAttribute('target', 'ioupload' + o.id ); |
|
|
1129 |
f.setAttribute((Y.UA.ie && !document.documentMode) ? 'encoding' : 'enctype', 'multipart/form-data'); |
|
|
1130 |
|
|
|
1131 |
if (c.data) { |
|
|
1132 |
fields = _addData(f, c.data); |
|
|
1133 |
} |
|
|
1134 |
|
|
|
1135 |
// Start polling if a callback is present and the timeout |
|
|
1136 |
// property has been defined. |
|
|
1137 |
if (c.timeout) { |
|
|
1138 |
_startTimeout(o, c); |
|
|
1139 |
} |
|
|
1140 |
|
|
|
1141 |
// Start file upload. |
|
|
1142 |
f.submit(); |
|
|
1143 |
Y.io.start(o.id, c); |
|
|
1144 |
|
|
|
1145 |
if (c.data) { |
|
|
1146 |
_removeData(f, fields); |
|
|
1147 |
} |
|
|
1148 |
|
|
|
1149 |
// Restore HTML form attributes to their original |
|
|
1150 |
// values prior to file upload. |
|
|
1151 |
for (p in attr) { |
|
|
1152 |
if (attr.hasOwnProperty(attr, p)) { |
|
|
1153 |
if (attr[p]) { |
|
|
1154 |
f.setAttribute(p, f[prop]); |
|
|
1155 |
} |
|
|
1156 |
else { |
|
|
1157 |
f.removeAttribute(p); |
|
|
1158 |
} |
|
|
1159 |
} |
|
|
1160 |
} |
|
|
1161 |
} |
|
|
1162 |
}); |
|
|
1163 |
|
|
|
1164 |
|
|
|
1165 |
|
|
|
1166 |
}, '3.0.0b1' ,{requires:['io-base']}); |
|
|
1167 |
|
|
|
1168 |
YUI.add('io-queue', function(Y) { |
|
|
1169 |
|
|
|
1170 |
/** |
|
|
1171 |
* Extends the IO base class to implement Queue for synchronous |
|
|
1172 |
* transaction processing. |
|
|
1173 |
* @module io |
|
|
1174 |
* @submodule io-queue |
|
|
1175 |
*/ |
|
|
1176 |
|
|
|
1177 |
/** |
|
|
1178 |
* @description Array of transactions queued for processing |
|
|
1179 |
* |
|
|
1180 |
* @property _yQ |
|
|
1181 |
* @private |
|
|
1182 |
* @static |
|
|
1183 |
* @type Object |
|
|
1184 |
*/ |
|
|
1185 |
var _q = new Y.Queue(), |
|
|
1186 |
|
|
|
1187 |
/** |
|
|
1188 |
* @description Reference to "io:complete" event handler. |
|
|
1189 |
* |
|
|
1190 |
* @property _e |
|
|
1191 |
* @private |
|
|
1192 |
* @static |
|
|
1193 |
* @type Object |
|
|
1194 |
*/ |
|
|
1195 |
_e, |
|
|
1196 |
|
|
|
1197 |
_activeId, |
|
|
1198 |
/** |
|
|
1199 |
* @description Property to determine whether the queue is set to |
|
|
1200 |
* 1 (active) or 0 (inactive). When inactive, transactions |
|
|
1201 |
* will be stored in the queue until the queue is set to active. |
|
|
1202 |
* |
|
|
1203 |
* @property _qState |
|
|
1204 |
* @private |
|
|
1205 |
* @static |
|
|
1206 |
* @type int |
|
|
1207 |
*/ |
|
|
1208 |
_qState = 1; |
|
|
1209 |
|
|
|
1210 |
/** |
|
|
1211 |
* @description Method for requesting a transaction, and queueing the |
|
|
1212 |
* request before it is sent to the resource. |
|
|
1213 |
* |
|
|
1214 |
* @method _queue |
|
|
1215 |
* @private |
|
|
1216 |
* @static |
|
|
1217 |
* @return Object |
|
|
1218 |
*/ |
|
|
1219 |
function _queue(uri, c) { |
|
|
1220 |
var o = { uri: uri, id: Y.io._id(), cfg:c }; |
|
|
1221 |
|
|
|
1222 |
_q.add(o); |
|
|
1223 |
if (_qState === 1) { |
|
|
1224 |
_shift(); |
|
|
1225 |
} |
|
|
1226 |
|
|
|
1227 |
return o; |
|
|
1228 |
} |
|
|
1229 |
|
|
|
1230 |
/** |
|
|
1231 |
* @description Method Process the first transaction from the |
|
|
1232 |
* queue in FIFO order. |
|
|
1233 |
* |
|
|
1234 |
* @method _shift |
|
|
1235 |
* @private |
|
|
1236 |
* @static |
|
|
1237 |
* @return void |
|
|
1238 |
*/ |
|
|
1239 |
function _shift() { |
|
|
1240 |
var o = _q.next(); |
|
|
1241 |
|
|
|
1242 |
_activeId = o.id; |
|
|
1243 |
_qState = 0; |
|
|
1244 |
Y.io(o.uri, o.cfg, o.id); |
|
|
1245 |
} |
|
|
1246 |
|
|
|
1247 |
/** |
|
|
1248 |
* @description Method for promoting a transaction to the top of the queue. |
|
|
1249 |
* |
|
|
1250 |
* @method _unshift |
|
|
1251 |
* @private |
|
|
1252 |
* @static |
|
|
1253 |
* @return void |
|
|
1254 |
*/ |
|
|
1255 |
function _unshift(o) { |
|
|
1256 |
_q.promote(o); |
|
|
1257 |
} |
|
|
1258 |
|
|
|
1259 |
function _next(id) { |
|
|
1260 |
_qState = 1; |
|
|
1261 |
if (_activeId === id && _q.size() > 0) { |
|
|
1262 |
_shift(); |
|
|
1263 |
} |
|
|
1264 |
} |
|
|
1265 |
|
|
|
1266 |
/** |
|
|
1267 |
* @description Method for removing a specific, pending transaction from |
|
|
1268 |
* the queue. |
|
|
1269 |
* |
|
|
1270 |
* @method _remove |
|
|
1271 |
* @private |
|
|
1272 |
* @static |
|
|
1273 |
* @return void |
|
|
1274 |
*/ |
|
|
1275 |
function _remove(o) { |
|
|
1276 |
_q.remove(o); |
|
|
1277 |
} |
|
|
1278 |
|
|
|
1279 |
function _start() { |
|
|
1280 |
_qState = 1; |
|
|
1281 |
|
|
|
1282 |
if (_q.size() > 0) { |
|
|
1283 |
_shift(); |
|
|
1284 |
} |
|
|
1285 |
} |
|
|
1286 |
|
|
|
1287 |
/** |
|
|
1288 |
* @description Method for setting queue processing to inactive. |
|
|
1289 |
* Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
|
1290 |
* not processed until the queue is reset to "active". |
|
|
1291 |
* |
|
|
1292 |
* @method _stop |
|
|
1293 |
* @private |
|
|
1294 |
* @static |
|
|
1295 |
* @return void |
|
|
1296 |
*/ |
|
|
1297 |
function _stop() { |
|
|
1298 |
_qState = 0; |
|
|
1299 |
}; |
|
|
1300 |
|
|
|
1301 |
/** |
|
|
1302 |
* @description Method to query the current size of the queue. |
|
|
1303 |
* |
|
|
1304 |
* @method _size |
|
|
1305 |
* @private |
|
|
1306 |
* @static |
|
|
1307 |
* @return int |
|
|
1308 |
*/ |
|
|
1309 |
function _size() { |
|
|
1310 |
return _q.size(); |
|
|
1311 |
}; |
|
|
1312 |
|
|
|
1313 |
_e = Y.on('io:complete', function(id) { _next(id); }, Y.io); |
|
|
1314 |
|
|
|
1315 |
/** |
|
|
1316 |
* @description Method to query the current size of the queue, or to |
|
|
1317 |
* set a maximum queue size. This is the interface for _size(). |
|
|
1318 |
* |
|
|
1319 |
* @method size |
|
|
1320 |
* @public |
|
|
1321 |
* @static |
|
|
1322 |
* @param {number} i - Specified maximum size of queue. |
|
|
1323 |
* @return number |
|
|
1324 |
*/ |
|
|
1325 |
_queue.size = _size; |
|
|
1326 |
|
|
|
1327 |
/** |
|
|
1328 |
* @description Method for setting the queue to active. If there are |
|
|
1329 |
* transactions pending in the queue, they will be processed from the |
|
|
1330 |
* queue in FIFO order. This is the interface for _start(). |
|
|
1331 |
* |
|
|
1332 |
* @method start |
|
|
1333 |
* @public |
|
|
1334 |
* @static |
|
|
1335 |
* @return void |
|
|
1336 |
*/ |
|
|
1337 |
_queue.start = _start; |
|
|
1338 |
|
|
|
1339 |
/** |
|
|
1340 |
* @description Method for setting queue processing to inactive. |
|
|
1341 |
* Transaction requests to YUI.io.queue() will be stored in the queue, but |
|
|
1342 |
* not processed until the queue is restarted. This is the |
|
|
1343 |
* interface for _stop(). |
|
|
1344 |
* |
|
|
1345 |
* @method stop |
|
|
1346 |
* @public |
|
|
1347 |
* @static |
|
|
1348 |
* @return void |
|
|
1349 |
*/ |
|
|
1350 |
_queue.stop = _stop; |
|
|
1351 |
|
|
|
1352 |
/** |
|
|
1353 |
* @description Method for promoting a transaction to the top of the queue. |
|
|
1354 |
* This is the interface for _unshift(). |
|
|
1355 |
* |
|
|
1356 |
* @method promote |
|
|
1357 |
* @public |
|
|
1358 |
* @static |
|
|
1359 |
* @param {Object} o - Reference to queued transaction. |
|
|
1360 |
* @return void |
|
|
1361 |
*/ |
|
|
1362 |
_queue.promote = _unshift; |
|
|
1363 |
|
|
|
1364 |
/** |
|
|
1365 |
* @description Method for removing a specific, pending transaction from |
|
|
1366 |
* the queue. This is the interface for _purge(). |
|
|
1367 |
* |
|
|
1368 |
* @method purge |
|
|
1369 |
* @public |
|
|
1370 |
* @static |
|
|
1371 |
* @param {Object} o - Reference to queued transaction. |
|
|
1372 |
* @return void |
|
|
1373 |
*/ |
|
|
1374 |
_queue.remove = _remove; |
|
|
1375 |
|
|
|
1376 |
Y.mix(Y.io, { |
|
|
1377 |
queue: _queue |
|
|
1378 |
}, true); |
|
|
1379 |
|
|
|
1380 |
|
|
|
1381 |
|
|
|
1382 |
}, '3.0.0b1' ,{requires:['io-base']}); |
|
|
1383 |
|
|
|
1384 |
|
|
|
1385 |
|
|
|
1386 |
YUI.add('io', function(Y){}, '3.0.0b1' ,{use:['io-base', 'io-form', 'io-xdr', 'io-upload-iframe', 'io-queue']}); |
|
|
1387 |
|