* Uploads file via AJAX.
*
* @author Matthias Schmidt
- * @copyright 2001-2017 WoltLab GmbH
+ * @copyright 2001-2018 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @module WoltLabSuite/Core/Upload
*/
define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Dom/Traverse'], function(AjaxRequest, Core, DomChangeListener, Language, DomUtil, DomTraverse) {
"use strict";
+ if (!COMPILER_TARGET_DEFAULT) {
+ var Fake = function() {};
+ Fake.prototype = {
+ _createButton: function() {},
+ _createFileElement: function() {},
+ _createFileElements: function() {},
+ _failure: function() {},
+ _getParameters: function() {},
+ _insertButton: function() {},
+ _progress: function() {},
+ _removeButton: function() {},
+ _success: function() {},
+ _upload: function() {},
+ _uploadFiles: function() {}
+ };
+ return Fake;
+ }
+
/**
* @constructor
*/
if (targetId === null) {
throw new Error("Element id '" + targetId + "' is unknown.");
}
- if (options.multiple && this._target.nodeName !== 'UL' && this._target.nodeName !== 'OL') {
- throw new Error("Target element has to be list when allowing upload of multiple files.");
+ if (options.multiple && this._target.nodeName !== 'UL' && this._target.nodeName !== 'OL' && this._target.nodeName !== 'TBODY') {
+ throw new Error("Target element has to be list or table body if uploading multiple files is supported.");
}
this._fileElements = [];
this._internalFileId = 0;
+ // upload ids that belong to an upload of multiple files at once
+ this._multiFileUploadIds = [];
+
this._createButton();
}
Upload.prototype = {
* Creates the document element for an uploaded file.
*
* @param {File} file uploaded file
+ * @return {HTMLElement}
*/
_createFileElement: function(file) {
var progress = elCreate('progress');
return li;
}
+ else if (this._target.nodeName === 'TBODY') {
+ return this._createFileTableRow(file);
+ }
else {
var p = elCreate('p');
p.appendChild(progress);
return null;
},
+ _createFileTableRow: function(file) {
+ throw new Error("Has to be implemented in subclass.");
+ },
+
/**
* Handles a failed file upload.
*
if (this._options.singleFileRequests) {
uploadId = [];
for (var i = 0, length = files.length; i < length; i++) {
- uploadId.push(this._uploadFiles([ files[i] ], blob));
+ var localUploadId = this._uploadFiles([ files[i] ], blob);
+
+ if (files.length !== 1) {
+ this._multiFileUploadIds.push(localUploadId)
+ }
+ uploadId.push(localUploadId);
}
}
else {
request.sendRequest();
return uploadId;
+ },
+
+ /**
+ * Returns true if there are any pending uploads handled by this
+ * upload manager.
+ *
+ * @return {boolean}
+ * @since 3.2
+ */
+ hasPendingUploads: function() {
+ for (var uploadId in this._fileElements) {
+ for (var i in this._fileElements[uploadId]) {
+ var progress = elByTag('PROGRESS', this._fileElements[uploadId][i]);
+ if (progress.length === 1) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ },
+
+ /**
+ * Uploads the given file blob.
+ *
+ * @param {Blob} blob file blob
+ * @return {int} identifier for the uploaded file
+ */
+ uploadBlob: function(blob) {
+ return this._upload(null, null, blob);
+ },
+
+ /**
+ * Uploads the given file.
+ *
+ * @param {File} file uploaded file
+ * @return {int} identifier(s) for the uploaded file
+ */
+ uploadFile: function(file) {
+ return this._upload(null, file);
}
};