vendor/assets/javascripts/fine_uploader/uploader.basic.js in fine_uploader-3.4.1 vs vendor/assets/javascripts/fine_uploader/uploader.basic.js in fine_uploader-3.5.0

- old
+ new

@@ -25,11 +25,11 @@ stopOnFirstInvalidFile: true }, callbacks: { onSubmit: function(id, name){}, onSubmitted: function(id, name){}, - onComplete: function(id, name, responseJSON){}, + onComplete: function(id, name, responseJSON, maybeXhr){}, onCancel: function(id, name){}, onUpload: function(id, name){}, onUploadChunk: function(id, name, chunkData){}, onResume: function(id, fileName, chunkData){}, onProgress: function(id, name, loaded, total){}, @@ -39,13 +39,11 @@ onValidateBatch: function(fileOrBlobData) {}, onValidate: function(fileOrBlobData) {}, onSubmitDelete: function(id) {}, onDelete: function(id){}, onDeleteComplete: function(id, xhr, isError){}, - onPasteReceived: function(blob) { - return new qq.Promise().success(); - } + onPasteReceived: function(blob) {} }, messages: { typeError: "{file} has an invalid extension. Valid extension(s): {extensions}.", sizeError: "{file} is too large, maximum file size is {sizeLimit}.", minSizeError: "{file} is too small, minimum file size is {minSizeLimit}.", @@ -90,10 +88,11 @@ fileOrBlobName = fileOrBlobName.slice(0, 19) + '...' + fileOrBlobName.slice(-14); } return fileOrBlobName; }, text: { + defaultResponseError: "Upload failure reason unknown", sizeSymbols: ['kB', 'MB', 'GB', 'TB', 'PB', 'EB'] }, deleteFile : { enabled: false, endpoint: '/server/upload', @@ -123,12 +122,14 @@ this._filesInProgress = []; this._storedIds = []; this._autoRetries = []; this._retryTimeouts = []; this._preventRetries = []; - this._netFilesUploadedOrQueued = 0; + this._netUploadedOrQueued = 0; + this._netUploaded = 0; + this._paramsStore = this._createParamsStore("request"); this._deleteFileParamsStore = this._createParamsStore("deleteFile"); this._endpointStore = this._createEndpointStore("request"); this._deleteFileEndpointStore = this._createEndpointStore("deleteFile"); @@ -182,13 +183,16 @@ } else { this._endpointStore.setEndpoint(endpoint, id); } }, - getInProgress: function(){ + getInProgress: function() { return this._filesInProgress.length; }, + getNetUploads: function() { + return this._netUploaded; + }, uploadStoredFiles: function(){ "use strict"; var idToUpload; while(this._storedIds.length) { @@ -200,11 +204,11 @@ clearStoredFiles: function(){ this._storedIds = []; }, retry: function(id) { if (this._onBeforeManualRetry(id)) { - this._netFilesUploadedOrQueued++; + this._netUploadedOrQueued++; this._handler.retry(id); return true; } else { return false; @@ -233,42 +237,43 @@ this._retryTimeouts = []; this._preventRetries = []; this._button.reset(); this._paramsStore.reset(); this._endpointStore.reset(); - this._netFilesUploadedOrQueued = 0; + this._netUploadedOrQueued = 0; + this._netUploaded = 0; if (this._pasteHandler) { this._pasteHandler.reset(); } }, - addFiles: function(filesBlobDataOrInputs) { + addFiles: function(filesDataOrInputs, params, endpoint) { var self = this, verifiedFilesOrInputs = [], index, fileOrInput; - if (filesBlobDataOrInputs) { - if (!window.FileList || !(filesBlobDataOrInputs instanceof FileList)) { - filesBlobDataOrInputs = [].concat(filesBlobDataOrInputs); + if (filesDataOrInputs) { + if (!window.FileList || !(filesDataOrInputs instanceof FileList)) { + filesDataOrInputs = [].concat(filesDataOrInputs); } - for (index = 0; index < filesBlobDataOrInputs.length; index+=1) { - fileOrInput = filesBlobDataOrInputs[index]; + for (index = 0; index < filesDataOrInputs.length; index+=1) { + fileOrInput = filesDataOrInputs[index]; if (qq.isFileOrInput(fileOrInput)) { verifiedFilesOrInputs.push(fileOrInput); } else { self.log(fileOrInput + ' is not a File or INPUT element! Ignoring!', 'warn'); } } this.log('Processing ' + verifiedFilesOrInputs.length + ' files or inputs...'); - this._uploadFileOrBlobDataList(verifiedFilesOrInputs); + this._uploadFileOrBlobDataList(verifiedFilesOrInputs, params, endpoint); } }, - addBlobs: function(blobDataOrArray) { + addBlobs: function(blobDataOrArray, params, endpoint) { if (blobDataOrArray) { var blobDataArray = [].concat(blobDataOrArray), verifiedBlobDataList = [], self = this; @@ -285,11 +290,11 @@ else { self.log("addBlobs: entry at index " + idx + " is not a Blob or a BlobData object", "error"); } }); - this._uploadFileOrBlobDataList(verifiedBlobDataList); + this._uploadFileOrBlobDataList(verifiedBlobDataList, params, endpoint); } else { this.log("undefined or non-array parameter passed into addBlobs", "error"); } }, @@ -318,19 +323,16 @@ } else { this._deleteFileEndpointStore.setEndpoint(endpoint, id); } }, - getPromissoryCallbackNames: function() { - return ["onPasteReceived"]; - }, _createUploadButton: function(element){ var self = this; var button = new qq.UploadButton({ element: element, - multiple: this._options.multiple && qq.isXhrUploadSupported(), + multiple: this._options.multiple && qq.supportedFeatures.ajaxUploading, acceptFiles: this._options.validation.acceptFiles, onChange: function(input){ self._onInputChange(input); }, hoverClass: this._options.classes.buttonHover, @@ -366,11 +368,11 @@ self._onProgress(id, name, loaded, total); self._options.callbacks.onProgress(id, name, loaded, total); }, onComplete: function(id, name, result, xhr){ self._onComplete(id, name, result, xhr); - self._options.callbacks.onComplete(id, name, result); + self._options.callbacks.onComplete(id, name, result, xhr); }, onCancel: function(id, name){ self._onCancel(id, name); self._options.callbacks.onCancel(id, name); }, @@ -436,22 +438,22 @@ callbacks: { log: function(str, level) { self.log(str, level); }, pasteReceived: function(blob) { - var pasteReceivedCallback = self._options.callbacks.onPasteReceived, - promise = pasteReceivedCallback(blob); + var callback = self._options.callbacks.onPasteReceived, + promise = callback(blob); - if (promise.then) { + if (promise && promise.then) { promise.then(function(successData) { self._handlePasteSuccess(blob, successData); }, function(failureData) { self.log("Ignoring pasted image per paste received callback. Reason = '" + failureData + "'"); }); } else { - self.log("Promise contract not fulfilled in pasteReceived callback handler! Ignoring pasted item.", "error"); + self._handlePasteSuccess(blob); } } } }); }, @@ -483,28 +485,31 @@ // for webkit return self._options.messages.onLeave; }); }, _onSubmit: function(id, name) { - this._netFilesUploadedOrQueued++; + this._netUploadedOrQueued++; if (this._options.autoUpload) { this._filesInProgress.push(id); } }, _onProgress: function(id, name, loaded, total){ }, _onComplete: function(id, name, result, xhr) { if (!result.success) { - this._netFilesUploadedOrQueued--; + this._netUploadedOrQueued--; } + else { + this._netUploaded++; + } this._removeFromFilesInProgress(id); this._maybeParseAndSendUploadError(id, name, result, xhr); }, _onCancel: function(id, name){ - this._netFilesUploadedOrQueued--; + this._netUploadedOrQueued--; this._removeFromFilesInProgress(id); clearTimeout(this._retryTimeouts[id]); @@ -513,14 +518,11 @@ this._storedIds.splice(storedItemIndex, 1); } }, _isDeletePossible: function() { return (this._options.deleteFile.enabled && - (!this._options.cors.expected || - (this._options.cors.expected && (qq.ie10() || !qq.ie())) - ) - ); + (!this._options.cors.expected || qq.supportedFeatures.deleteFileCors)); }, _onSubmitDelete: function(id) { if (this._isDeletePossible()) { if (this._options.callbacks.onSubmitDelete(id) !== false) { this._deleteHandler.sendDelete(id, this.getUuid(id)); @@ -539,11 +541,12 @@ if (isError) { this.log("Delete request for '" + name + "' has failed.", "error"); this._options.callbacks.onError(id, name, "Delete request failed with response code " + xhr.status, xhr); } else { - this._netFilesUploadedOrQueued--; + this._netUploadedOrQueued--; + this._netUploaded--; this.log("Delete request for '" + name + "' has succeeded."); } }, _removeFromFilesInProgress: function(id) { var index = qq.indexOf(this._filesInProgress, id); @@ -551,11 +554,11 @@ this._filesInProgress.splice(index, 1); } }, _onUpload: function(id, name){}, _onInputChange: function(input){ - if (qq.isXhrUploadSupported()){ + if (qq.supportedFeatures.ajaxUploading){ this.addFiles(input.files); } else { this.addFiles(input); } this._button.reset(); @@ -592,11 +595,11 @@ if (this._options.callbacks.onManualRetry(id, fileName) === false) { return false; } - if (itemLimit > 0 && this._netFilesUploadedOrQueued+1 > itemLimit) { + if (itemLimit > 0 && this._netUploadedOrQueued+1 > itemLimit) { this._itemError("retryFailTooManyItems", ""); return false; } this.log("Retrying upload for '" + fileName + "' (id: " + id + ")..."); @@ -613,25 +616,25 @@ if (!response.success){ if (xhr && xhr.status !== 200 && !response.error) { this._options.callbacks.onError(id, name, "XHR returned response code " + xhr.status, xhr); } else { - var errorReason = response.error ? response.error : "Upload failure reason unknown"; + var errorReason = response.error ? response.error : this._options.text.defaultResponseError; this._options.callbacks.onError(id, name, errorReason, xhr); } } }, - _uploadFileOrBlobDataList: function(fileOrBlobDataList){ + _uploadFileOrBlobDataList: function(fileOrBlobDataList, params, endpoint) { var index, validationDescriptors = this._getValidationDescriptors(fileOrBlobDataList), batchValid = this._isBatchValid(validationDescriptors); if (batchValid) { if (fileOrBlobDataList.length > 0) { for (index = 0; index < fileOrBlobDataList.length; index++){ if (this._validateFileOrBlobData(fileOrBlobDataList[index])){ - this._upload(fileOrBlobDataList[index]); + this._upload(fileOrBlobDataList[index], params, endpoint); } else { if (this._options.validation.stopOnFirstInvalidFile){ return; } } @@ -640,14 +643,22 @@ else { this._itemError("noFilesError", ""); } } }, - _upload: function(blobOrFileContainer){ + _upload: function(blobOrFileContainer, params, endpoint) { var id = this._handler.add(blobOrFileContainer); var name = this._handler.getName(id); + if (params) { + this.setParams(params, id); + } + + if (endpoint) { + this.setEndpoint(endpoint, id); + } + if (this._options.callbacks.onSubmit(id, name) !== false) { this._onSubmit(id, name); this._options.callbacks.onSubmitted(id, name); if (this._options.autoUpload) { @@ -663,11 +674,11 @@ }, _isBatchValid: function(validationDescriptors) { //first, defer the check to the callback (ask the integrator) var errorMessage, itemLimit = this._options.validation.itemLimit, - proposedNetFilesUploadedOrQueued = this._netFilesUploadedOrQueued + validationDescriptors.length, + proposedNetFilesUploadedOrQueued = this._netUploadedOrQueued + validationDescriptors.length, batchValid = this._options.callbacks.onValidateBatch(validationDescriptors) !== false; //if the callback hasn't rejected the batch, run some internal tests on the batch next if (batchValid) { if (itemLimit === 0 || proposedNetFilesUploadedOrQueued <= itemLimit) { @@ -715,14 +726,16 @@ return false; } return true; }, - _itemError: function(code, name) { + _itemError: function(code, nameOrNames) { var message = this._options.messages[code], allowedExtensions = [], - extensionsForMessage; + names = [].concat(nameOrNames), + name = names[0], + extensionsForMessage, placeholderMatch; function r(name, replacement){ message = message.replace(name, replacement); } qq.each(this._options.validation.allowedExtensions, function(idx, allowedExtension) { /** @@ -739,10 +752,17 @@ r('{file}', this._options.formatFileName(name)); r('{extensions}', extensionsForMessage); r('{sizeLimit}', this._formatSize(this._options.validation.sizeLimit)); r('{minSizeLimit}', this._formatSize(this._options.validation.minSizeLimit)); + placeholderMatch = message.match(/(\{\w+\})/g); + if (placeholderMatch !== null) { + qq.each(placeholderMatch, function(idx, placeholder) { + r(placeholder, names[idx]); + }); + } + this._options.callbacks.onError(null, name, message); return message; }, _batchError: function(message) { @@ -848,10 +868,10 @@ fileDescriptor = {}; name = this._parseFileOrBlobDataName(fileOrBlobData); size = this._parseFileOrBlobDataSize(fileOrBlobData); fileDescriptor.name = name; - if (size) { + if (size !== undefined) { fileDescriptor.size = size; } return fileDescriptor; },