|
15 | 15 | * @param {number} [opts.progressCallbacksInterval] |
16 | 16 | * @param {number} [opts.speedSmoothingFactor] |
17 | 17 | * @param {Object|Function} [opts.query] |
18 | | - * @param {Object} [opts.headers] |
| 18 | + * @param {Object|Function} [opts.headers] |
19 | 19 | * @param {bool} [opts.withCredentials] |
20 | 20 | * @param {Function} [opts.preprocess] |
21 | 21 | * @param {string} [opts.method] |
22 | 22 | * @param {bool} [opts.prioritizeFirstAndLastChunk] |
23 | | - * @param {string} [opts.target] |
| 23 | + * @param {string|Function} [opts.target] |
24 | 24 | * @param {number} [opts.maxChunkRetries] |
25 | 25 | * @param {number} [opts.chunkRetryInterval] |
26 | 26 | * @param {Array.<number>} [opts.permanentErrors] |
|
1174 | 1174 | * @param params |
1175 | 1175 | * @returns {string} |
1176 | 1176 | */ |
1177 | | - getTarget: function(params){ |
1178 | | - var target = this.flowObj.opts.target; |
| 1177 | + getTarget: function(target, params){ |
1179 | 1178 | if(target.indexOf('?') < 0) { |
1180 | 1179 | target += '?'; |
1181 | 1180 | } else { |
|
1194 | 1193 | this.xhr = new XMLHttpRequest(); |
1195 | 1194 | this.xhr.addEventListener("load", this.testHandler, false); |
1196 | 1195 | this.xhr.addEventListener("error", this.testHandler, false); |
1197 | | - var data = this.prepareXhrRequest('GET'); |
| 1196 | + var data = this.prepareXhrRequest('GET', true); |
1198 | 1197 | this.xhr.send(data); |
1199 | 1198 | }, |
1200 | 1199 |
|
|
1246 | 1245 | this.xhr.addEventListener("load", this.doneHandler, false); |
1247 | 1246 | this.xhr.addEventListener("error", this.doneHandler, false); |
1248 | 1247 |
|
1249 | | - var data = this.prepareXhrRequest('POST', this.flowObj.opts.method, bytes); |
1250 | | - |
| 1248 | + var data = this.prepareXhrRequest('POST', false, this.flowObj.opts.method, bytes); |
1251 | 1249 | this.xhr.send(data); |
1252 | 1250 | }, |
1253 | 1251 |
|
|
1342 | 1340 | /** |
1343 | 1341 | * Prepare Xhr request. Set query, headers and data |
1344 | 1342 | * @param {string} method GET or POST |
| 1343 | + * @param {bool} isTest is this a test request |
1345 | 1344 | * @param {string} [paramsMethod] octet or form |
1346 | 1345 | * @param {Blob} [blob] to send |
1347 | 1346 | * @returns {FormData|Blob|Null} data to send |
1348 | 1347 | */ |
1349 | | - prepareXhrRequest: function(method, paramsMethod, blob) { |
| 1348 | + prepareXhrRequest: function(method, isTest, paramsMethod, blob) { |
1350 | 1349 | // Add data from the query options |
1351 | | - var query = this.flowObj.opts.query; |
1352 | | - if (typeof query === "function") { |
1353 | | - query = query(this.fileObj, this); |
1354 | | - } |
| 1350 | + var query = evalOpts(this.flowObj.opts.query, this.fileObj, this, isTest); |
1355 | 1351 | query = extend(this.getParams(), query); |
1356 | 1352 |
|
1357 | | - var target = this.flowObj.opts.target; |
| 1353 | + var target = evalOpts(this.flowObj.opts.target, this.fileObj, this, isTest); |
1358 | 1354 | var data = null; |
1359 | 1355 | if (method === 'GET' || paramsMethod === 'octet') { |
1360 | 1356 | // Add data from the query options |
1361 | 1357 | var params = []; |
1362 | 1358 | each(query, function (v, k) { |
1363 | 1359 | params.push([encodeURIComponent(k), encodeURIComponent(v)].join('=')); |
1364 | 1360 | }); |
1365 | | - target = this.getTarget(params); |
| 1361 | + target = this.getTarget(target, params); |
1366 | 1362 | data = blob || null; |
1367 | 1363 | } else { |
1368 | 1364 | // Add data from the query options |
|
1377 | 1373 | this.xhr.withCredentials = this.flowObj.opts.withCredentials; |
1378 | 1374 |
|
1379 | 1375 | // Add data from header options |
1380 | | - each(this.flowObj.opts.headers, function (v, k) { |
| 1376 | + each(evalOpts(this.flowObj.opts.headers, this.fileObj, this, isTest), function (v, k) { |
1381 | 1377 | this.xhr.setRequestHeader(k, v); |
1382 | 1378 | }, this); |
1383 | 1379 |
|
|
1397 | 1393 | } |
1398 | 1394 | } |
1399 | 1395 |
|
| 1396 | + /** |
| 1397 | + * If option is a function, evaluate it with given params |
| 1398 | + * @param {*} data |
| 1399 | + * @param {...} args arguments of a callback |
| 1400 | + * @returns {*} |
| 1401 | + */ |
| 1402 | + function evalOpts(data, args) { |
| 1403 | + if (typeof data === "function") { |
| 1404 | + // `arguments` is an object, not array, in FF, so: |
| 1405 | + args = Array.prototype.slice.call(arguments); |
| 1406 | + data = data.apply(null, args.slice(1)); |
| 1407 | + } |
| 1408 | + return data; |
| 1409 | + } |
| 1410 | + Flow.evalOpts = evalOpts; |
| 1411 | + |
1400 | 1412 | /** |
1401 | 1413 | * Execute function asynchronously |
1402 | 1414 | * @param fn |
|
0 commit comments