Add content selection before removing content
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Upload.js
index fe06bc063002c2651ec887ca911ecfb55c465f54..b9cff77eff21fc2556ff99949350db938b26a42b 100644 (file)
@@ -2,13 +2,31 @@
  * 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
         */
@@ -47,13 +65,16 @@ define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Do
                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 = {
@@ -87,6 +108,7 @@ define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Do
                 * Creates the document element for an uploaded file.
                 * 
                 * @param       {File}          file            uploaded file
+                * @return      {HTMLElement}
                 */
                _createFileElement: function(file) {
                        var progress = elCreate('progress');
@@ -101,6 +123,9 @@ define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Do
                                
                                return li;
                        }
+                       else if (this._target.nodeName === 'TBODY') {
+                               return this._createFileTableRow(file);
+                       }
                        else {
                                var p = elCreate('p');
                                p.appendChild(progress);
@@ -140,6 +165,10 @@ define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Do
                        return null;
                },
                
+               _createFileTableRow: function(file) {
+                       throw new Error("Has to be implemented in subclass.");
+               },
+               
                /**
                 * Handles a failed file upload.
                 * 
@@ -259,7 +288,12 @@ define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Do
                                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 {
@@ -339,6 +373,46 @@ define(['AjaxRequest', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Do
                        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);
                }
        };